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

kubeadm: Let the --node-name flag flow down to --hostname-override for the kubelet #64706

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
3 changes: 3 additions & 0 deletions cmd/kubeadm/app/phases/kubelet/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ go_library(
"//pkg/apis/rbac/v1:go_default_library",
"//pkg/kubelet/apis/kubeletconfig/scheme:go_default_library",
"//pkg/kubelet/apis/kubeletconfig/v1beta1:go_default_library",
"//pkg/util/node:go_default_library",
"//pkg/util/procfs:go_default_library",
"//pkg/util/version:go_default_library",
"//vendor/github.com/golang/glog:go_default_library",
Expand All @@ -37,12 +38,14 @@ go_test(
srcs = [
"config_test.go",
"dynamic_test.go",
"flags_test.go",
],
embed = [":go_default_library"],
deps = [
"//cmd/kubeadm/app/apis/kubeadm:go_default_library",
"//pkg/kubelet/apis:go_default_library",
"//pkg/kubelet/apis/kubeletconfig/v1beta1:go_default_library",
"//pkg/util/node:go_default_library",
"//pkg/util/version:go_default_library",
"//vendor/k8s.io/api/core/v1:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/apis/meta/v1:go_default_library",
Expand Down
8 changes: 7 additions & 1 deletion cmd/kubeadm/app/phases/kubelet/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
kubeadmapiv1alpha2 "k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm/v1alpha2"
"k8s.io/kubernetes/cmd/kubeadm/app/constants"
kubeadmutil "k8s.io/kubernetes/cmd/kubeadm/app/util"
nodeutil "k8s.io/kubernetes/pkg/util/node"
"k8s.io/kubernetes/pkg/util/procfs"
utilsexec "k8s.io/utils/exec"
)
Expand Down Expand Up @@ -78,7 +79,12 @@ func buildKubeletArgMap(nodeRegOpts *kubeadmapi.NodeRegistrationOptions, registe
kubeletFlags["resolv-conf"] = "/run/systemd/resolve/resolv.conf"
}

// TODO: Pass through --hostname-override if a custom name is used?
// Make sure the node name we're passed will work with Kubelet
if nodeRegOpts.Name != "" && nodeRegOpts.Name != nodeutil.GetHostname("") {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

given that the most common thing folks do is use fully qualified domain names I'm almost wondering if we should force tolower on the names similar to your other patch.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just tested this, and it appears this already happens automatically:

ubuntu@ip-172-31-84-104:~$ sudo /tmp/kubeadm init -v 1 --pod-network-cidr=192.168.0.0/16 --node-name ALLCAPS.horse

ubuntu@ip-172-31-84-104:~$ kubectl get nodes
NAME            STATUS    ROLES     AGE       VERSION
allcaps.horse   Ready     master    44s       v1.11.0-beta.0

I don't particularly know how, but I think this is a nonissue either way.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The nodeutil.GetHostname() func lowercases automatically:

// GetHostname returns OS's hostname if 'hostnameOverride' is empty; otherwise, return 'hostnameOverride'.
func GetHostname(hostnameOverride string) string {
	hostname := hostnameOverride
	if hostname == "" {
		nodename, err := os.Hostname()
		if err != nil {
			glog.Fatalf("Couldn't determine hostname: %v", err)
		}
		hostname = nodename
	}
	return strings.ToLower(strings.TrimSpace(hostname))
}

glog.V(1).Info("setting kubelet hostname-override to %q", nodeRegOpts.Name)
kubeletFlags["hostname-override"] = nodeRegOpts.Name
}

// TODO: Conditionally set `--cgroup-driver` to either `systemd` or `cgroupfs` for CRI other than Docker

return kubeletFlags
Expand Down
62 changes: 62 additions & 0 deletions cmd/kubeadm/app/phases/kubelet/flags_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
Copyright 2018 The Kubernetes Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package kubelet

import (
"testing"

kubeadmapi "k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm"
nodeutil "k8s.io/kubernetes/pkg/util/node"
)

func TestBuildKubeletArgMap(t *testing.T) {

tests := []struct {
name string
hostname string
expectedHostname string
}{
{
name: "manually set to current hostname",
hostname: nodeutil.GetHostname(""),
expectedHostname: "",
},
{
name: "unset hostname",
hostname: "",
expectedHostname: "",
},
{
name: "override hostname",
hostname: "my-node",
expectedHostname: "my-node",
},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
opts := &kubeadmapi.NodeRegistrationOptions{
Name: test.hostname,
}

m := buildKubeletArgMap(opts, false)
if m["hostname-override"] != test.expectedHostname {
t.Errorf("expected hostname %q, got %q", test.expectedHostname, m["hostname-override"])
}
})
}
}