Skip to content

Commit

Permalink
Do not set --hostname-override on kubelet and kube-proxy if hostname …
Browse files Browse the repository at this point in the history
…matches (#499)

* Only add --hostname-override flag if hostname does not match

* unit tests for kubelet and kube-proxy hostname override
  • Loading branch information
neoaggelos committed Jun 17, 2024
1 parent 200e4bc commit 94c7bdc
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 4 deletions.
4 changes: 3 additions & 1 deletion src/k8s/pkg/k8sd/setup/kube_proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
16 changes: 16 additions & 0 deletions src/k8s/pkg/k8sd/setup/kube_proxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"os"
"path"
"path/filepath"
"testing"

"github.com/canonical/k8s/pkg/k8sd/setup"
Expand Down Expand Up @@ -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())
})
}
5 changes: 4 additions & 1 deletion src/k8s/pkg/k8sd/setup/kubelet.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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
}
Expand Down
15 changes: 15 additions & 0 deletions src/k8s/pkg/k8sd/setup/kubelet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
})
}
5 changes: 3 additions & 2 deletions src/k8s/pkg/snap/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions src/k8s/pkg/snap/mock/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ type Mock struct {
OnLXDErr error
UID int
GID int
Hostname string
KubernetesConfigDir string
KubernetesPKIDir string
EtcdPKIDir string
Expand Down Expand Up @@ -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
}
Expand Down
8 changes: 8 additions & 0 deletions src/k8s/pkg/snap/snap.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand Down

0 comments on commit 94c7bdc

Please sign in to comment.