Skip to content

Commit

Permalink
Kubeadm-initialised kubelet uses provided hostname if present
Browse files Browse the repository at this point in the history
If --node-name is passed in to `kubeadm init`, `--hostname-override` will be
passed to kubelet.
  • Loading branch information
liztio committed Jun 4, 2018
1 parent 3da1228 commit b42564a
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 1 deletion.
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("") {
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"])
}
})
}
}

0 comments on commit b42564a

Please sign in to comment.