Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add preflight check for hostnames #3091

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
30 changes: 30 additions & 0 deletions pkg/tasks/preflight_checks.go
Expand Up @@ -32,6 +32,7 @@ import (
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/util/validation"
dynclient "sigs.k8s.io/controller-runtime/pkg/client"
)

Expand Down Expand Up @@ -70,6 +71,15 @@ func runPreflightChecks(s *state.State) error {
}
}

s.Logger.Infoln("Verifying that hostnames are valid Kubernetes node names...")
allHostnames := make([]string, len(s.Cluster.ControlPlane.Hosts)+len(s.Cluster.StaticWorkers.Hosts))
for i, host := range append(s.Cluster.ControlPlane.Hosts, s.Cluster.StaticWorkers.Hosts...) {
allHostnames[i] = host.Hostname
}
if err := checkHostnames(allHostnames); err != nil {
return err
}

return nil
}

Expand Down Expand Up @@ -210,3 +220,23 @@ func checkVersionSkew(reqVer, currVer *semver.Version, diff uint64) error {

return nil
}

func checkHostnames(hostnames []string) error {
// According to k8s spec, the node hostname has to be a valid RFC 1123 DNS Subdomain (https://kubernetes.io/docs/concepts/overview/working-with-objects/names/#dns-subdomain-names)
allErrors := []string{}

for _, hostname := range hostnames {
err := validation.IsDNS1123Subdomain(hostname)
if len(err) > 0 {
allErrors = append(allErrors, fmt.Sprintf("- hostname %q cannot be used as Kubernetes node: %s", hostname, err))
}
}

if len(allErrors) > 0 {
allErrors = append(allErrors, "Please rename host(s)")

return fail.Runtime(fmt.Errorf(strings.Join(allErrors, "\n")), "validating node hostnames")
}

return nil
}
50 changes: 50 additions & 0 deletions pkg/tasks/preflight_checks_test.go
Expand Up @@ -267,3 +267,53 @@ func TestCheckVersionSkewInvalid(t *testing.T) {
})
}
}

func TestCheckHostname(t *testing.T) {
t.Parallel()

testcases := []struct {
name string
hostnames []string
expError error
}{
{
name: "valid hostname",
hostnames: []string{"valid-hostname"},
expError: nil,
},
{
name: "invalid hostname",
hostnames: []string{"ALL-CAPS"},
expError: fmt.Errorf(`runtime: validating node hostnames
- hostname "ALL-CAPS" cannot be used as Kubernetes node: [a lowercase RFC 1123 subdomain must consist of lower case alphanumeric characters, '-' or '.', and must start and end with an alphanumeric character (e.g. 'example.com', regex used for validation is '[a-z0-9]([-a-z0-9]*[a-z0-9])?(\.[a-z0-9]([-a-z0-9]*[a-z0-9])?)*')]
Please rename host(s)`),
},
{
name: "multiple errors",
hostnames: []string{"ALL-CAPS", "inv@lid-symbol"},
expError: fmt.Errorf(`runtime: validating node hostnames
- hostname "ALL-CAPS" cannot be used as Kubernetes node: [a lowercase RFC 1123 subdomain must consist of lower case alphanumeric characters, '-' or '.', and must start and end with an alphanumeric character (e.g. 'example.com', regex used for validation is '[a-z0-9]([-a-z0-9]*[a-z0-9])?(\.[a-z0-9]([-a-z0-9]*[a-z0-9])?)*')]
- hostname "inv@lid-symbol" cannot be used as Kubernetes node: [a lowercase RFC 1123 subdomain must consist of lower case alphanumeric characters, '-' or '.', and must start and end with an alphanumeric character (e.g. 'example.com', regex used for validation is '[a-z0-9]([-a-z0-9]*[a-z0-9])?(\.[a-z0-9]([-a-z0-9]*[a-z0-9])?)*')]
Please rename host(s)`),
},
}

for _, tc := range testcases {
tc := tc
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
err := checkHostnames(tc.hostnames)

exp, act := "", ""
if tc.expError != nil {
exp = tc.expError.Error()
}
if err != nil {
act = err.Error()
}
if exp != act {
t.Fatalf("want error\n%q\ngot\n%q\n", tc.expError, err)
}
})
}
}