diff --git a/src/k8s/pkg/k8sd/setup/kube_proxy.go b/src/k8s/pkg/k8sd/setup/kube_proxy.go index 64da185d6..e2307d861 100644 --- a/src/k8s/pkg/k8sd/setup/kube_proxy.go +++ b/src/k8s/pkg/k8sd/setup/kube_proxy.go @@ -16,11 +16,13 @@ func KubeProxy(ctx context.Context, snap snap.Snap, hostname string, podCIDR str serviceArgs := map[string]string{ "--cluster-cidr": podCIDR, "--healthz-bind-address": "127.0.0.1", - "--hostname-override": hostname, "--kubeconfig": path.Join(snap.KubernetesConfigDir(), "proxy.conf"), "--profiling": "false", } + if hostname != snap.Hostname() { + serviceArgs["--hostname-override"] = hostname + } onLXD, err := snap.OnLXD(ctx) if err != nil { log.Printf("failed to check if on lxd: %v", err) diff --git a/src/k8s/pkg/k8sd/setup/kube_proxy_test.go b/src/k8s/pkg/k8sd/setup/kube_proxy_test.go index 69c71d921..7aaba3c3d 100644 --- a/src/k8s/pkg/k8sd/setup/kube_proxy_test.go +++ b/src/k8s/pkg/k8sd/setup/kube_proxy_test.go @@ -4,6 +4,7 @@ import ( "context" "os" "path" + "path/filepath" "testing" "github.com/canonical/k8s/pkg/k8sd/setup" @@ -94,4 +95,19 @@ func TestKubeProxy(t *testing.T) { } }) + t.Run("HostnameOverride", func(t *testing.T) { + g := NewWithT(t) + + // FIXME(neoaggelos): kube-proxy tests should not reuse the same snap instance, as it leads + // to implicit state like this shared between the tests + s.Mock.Hostname = "dev" + s.Mock.ServiceArgumentsDir = filepath.Join(dir, "k8s") + + g.Expect(setup.EnsureAllDirectories(s)).To(BeNil()) + g.Expect(setup.KubeProxy(context.Background(), s, "dev", "10.1.0.0/16", nil)).To(BeNil()) + + val, err := snaputil.GetServiceArgument(s, "kube-proxy", "--hostname-override") + g.Expect(err).To(BeNil()) + g.Expect(val).To(BeEmpty()) + }) } diff --git a/src/k8s/pkg/k8sd/setup/kubelet.go b/src/k8s/pkg/k8sd/setup/kubelet.go index 45028a324..cc033ca28 100644 --- a/src/k8s/pkg/k8sd/setup/kubelet.go +++ b/src/k8s/pkg/k8sd/setup/kubelet.go @@ -51,7 +51,6 @@ func kubelet(snap snap.Snap, hostname string, nodeIP net.IP, clusterDNS string, "--containerd": path.Join(snap.ContainerdSocketDir(), "containerd.sock"), "--eviction-hard": "'memory.available<100Mi,nodefs.available<1Gi,imagefs.available<1Gi'", "--fail-swap-on": "false", - "--hostname-override": hostname, "--kubeconfig": path.Join(snap.KubernetesConfigDir(), "kubelet.conf"), "--node-labels": strings.Join(labels, ","), "--read-only-port": "0", @@ -62,6 +61,10 @@ func kubelet(snap snap.Snap, hostname string, nodeIP net.IP, clusterDNS string, "--tls-cert-file": path.Join(snap.KubernetesPKIDir(), "kubelet.crt"), "--tls-private-key-file": path.Join(snap.KubernetesPKIDir(), "kubelet.key"), } + + if hostname != snap.Hostname() { + args["--hostname-override"] = hostname + } if cloudProvider != "" { args["--cloud-provider"] = cloudProvider } diff --git a/src/k8s/pkg/k8sd/setup/kubelet_test.go b/src/k8s/pkg/k8sd/setup/kubelet_test.go index 3414c796e..67387d117 100644 --- a/src/k8s/pkg/k8sd/setup/kubelet_test.go +++ b/src/k8s/pkg/k8sd/setup/kubelet_test.go @@ -379,4 +379,19 @@ func TestKubelet(t *testing.T) { g.Expect(setup.KubeletWorker(s, "dev", net.ParseIP("192.168.0.1"), "10.152.1.1", "test-cluster.local", "provider", nil)).ToNot(Succeed()) }) + + t.Run("HostnameOverride", func(t *testing.T) { + g := NewWithT(t) + + // Create a mock snap + s := mustSetupSnapAndDirectories(t, setKubeletMock) + s.Mock.Hostname = "dev" + + // Call the kubelet control plane setup function + g.Expect(setup.KubeletControlPlane(s, "dev", net.ParseIP("192.168.0.1"), "10.152.1.1", "test-cluster.local", "provider", nil, nil)).To(Succeed()) + + val, err := snaputil.GetServiceArgument(s, "kubelet", "--hostname-override") + g.Expect(err).To(BeNil()) + g.Expect(val).To(BeEmpty()) + }) } diff --git a/src/k8s/pkg/snap/interface.go b/src/k8s/pkg/snap/interface.go index c5580a26a..7ff10723f 100644 --- a/src/k8s/pkg/snap/interface.go +++ b/src/k8s/pkg/snap/interface.go @@ -14,8 +14,9 @@ type Snap interface { Strict() bool // Strict returns true if the snap is installed with strict confinement. OnLXD(context.Context) (bool, error) // OnLXD returns true if the host runs on LXD. - UID() int // UID is the user ID to set on config files. - GID() int // GID is the group ID to set on config files. + UID() int // UID is the user ID to set on config files. + GID() int // GID is the group ID to set on config files. + Hostname() string // Hostname is the name of the node. StartService(ctx context.Context, serviceName string) error // snapctl start $service StopService(ctx context.Context, serviceName string) error // snapctl stop $service diff --git a/src/k8s/pkg/snap/mock/mock.go b/src/k8s/pkg/snap/mock/mock.go index f42efb395..f84d9a4b6 100644 --- a/src/k8s/pkg/snap/mock/mock.go +++ b/src/k8s/pkg/snap/mock/mock.go @@ -17,6 +17,7 @@ type Mock struct { OnLXDErr error UID int GID int + Hostname string KubernetesConfigDir string KubernetesPKIDir string EtcdPKIDir string @@ -100,6 +101,9 @@ func (s *Snap) UID() int { func (s *Snap) GID() int { return s.Mock.GID } +func (s *Snap) Hostname() string { + return s.Mock.Hostname +} func (s *Snap) ContainerdConfigDir() string { return s.Mock.ContainerdConfigDir } diff --git a/src/k8s/pkg/snap/snap.go b/src/k8s/pkg/snap/snap.go index 653469989..7d71d63d6 100644 --- a/src/k8s/pkg/snap/snap.go +++ b/src/k8s/pkg/snap/snap.go @@ -95,6 +95,14 @@ func (s *snap) GID() int { return 0 } +func (s *snap) Hostname() string { + hostname, err := os.Hostname() + if err != nil { + return "dev" + } + return hostname +} + func (s *snap) ContainerdConfigDir() string { return path.Join(s.snapCommonDir, "etc", "containerd") }