From 6196001bbf72c85caff3f862a780139a7b4d1f12 Mon Sep 17 00:00:00 2001 From: Tom Wieczorek Date: Fri, 29 Nov 2024 14:46:14 +0100 Subject: [PATCH 1/2] Group private kubelet fields together To have a better overview which fields are used by the external configuration versus the internal state. Also, rename dataDir to rootDir to reflect that it's the kubelet's root directory and not k0s's data directory. Signed-off-by: Tom Wieczorek --- pkg/component/worker/kubelet.go | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/pkg/component/worker/kubelet.go b/pkg/component/worker/kubelet.go index a5e852c6755c..103ce0d56f9f 100644 --- a/pkg/component/worker/kubelet.go +++ b/pkg/component/worker/kubelet.go @@ -56,13 +56,14 @@ type Kubelet struct { Configuration kubeletv1beta1.KubeletConfiguration StaticPods StaticPods LogLevel string - dataDir string - supervisor supervisor.Supervisor ClusterDNS string Labels []string Taints []string ExtraArgs string DualStackEnabled bool + + rootDir string + supervisor supervisor.Supervisor } var _ manager.Component = (*Kubelet)(nil) @@ -81,10 +82,10 @@ func (k *Kubelet) Init(_ context.Context) error { } } - k.dataDir = filepath.Join(k.K0sVars.DataDir, "kubelet") - err := dir.Init(k.dataDir, constant.DataDirMode) + k.rootDir = filepath.Join(k.K0sVars.DataDir, "kubelet") + err := dir.Init(k.rootDir, constant.DataDirMode) if err != nil { - return fmt.Errorf("failed to create %s: %w", k.dataDir, err) + return fmt.Errorf("failed to create %s: %w", k.rootDir, err) } return nil @@ -122,12 +123,12 @@ func (k *Kubelet) Start(ctx context.Context) error { kubeletConfigPath := filepath.Join(k.K0sVars.DataDir, "kubelet-config.yaml") args := stringmap.StringMap{ - "--root-dir": k.dataDir, + "--root-dir": k.rootDir, "--config": kubeletConfigPath, "--kubeconfig": k.Kubeconfig, "--v": k.LogLevel, "--runtime-cgroups": "/system.slice/containerd.service", - "--cert-dir": filepath.Join(k.dataDir, "pki"), + "--cert-dir": filepath.Join(k.rootDir, "pki"), } if len(k.Labels) > 0 { From 213969007b8ea5f80684d4e369c6e3a48ae0120c Mon Sep 17 00:00:00 2001 From: Tom Wieczorek Date: Fri, 29 Nov 2024 14:49:37 +0100 Subject: [PATCH 2/2] Move kubelet config file into run directory This is generated at runtime and doesn't need to be part of the persistent data directory. Signed-off-by: Tom Wieczorek --- pkg/component/worker/kubelet.go | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/pkg/component/worker/kubelet.go b/pkg/component/worker/kubelet.go index 103ce0d56f9f..8a0465808674 100644 --- a/pkg/component/worker/kubelet.go +++ b/pkg/component/worker/kubelet.go @@ -19,6 +19,7 @@ package worker import ( "bufio" "context" + "errors" "fmt" "net" "os" @@ -63,6 +64,7 @@ type Kubelet struct { DualStackEnabled bool rootDir string + configPath string supervisor supervisor.Supervisor } @@ -88,6 +90,16 @@ func (k *Kubelet) Init(_ context.Context) error { return fmt.Errorf("failed to create %s: %w", k.rootDir, err) } + runDir := filepath.Join(k.K0sVars.RunDir, "kubelet") + if err := dir.Init(runDir, constant.RunDirMode); err != nil { + return fmt.Errorf("failed to create %s: %w", runDir, err) + } + k.configPath = filepath.Join(runDir, "config.yaml") + // Delete legacy config file (removed in 1.32) + if err := os.Remove(filepath.Join(k.K0sVars.DataDir, "kubelet-config.yaml")); err != nil && !errors.Is(err, os.ErrNotExist) { + logrus.WithError(err).Warn("Failed to remove legacy kubelet config file") + } + return nil } @@ -120,11 +132,9 @@ func (k *Kubelet) Start(ctx context.Context) error { } logrus.Info("Starting kubelet") - kubeletConfigPath := filepath.Join(k.K0sVars.DataDir, "kubelet-config.yaml") - args := stringmap.StringMap{ "--root-dir": k.rootDir, - "--config": kubeletConfigPath, + "--config": k.configPath, "--kubeconfig": k.Kubeconfig, "--v": k.LogLevel, "--runtime-cgroups": "/system.slice/containerd.service", @@ -186,7 +196,7 @@ func (k *Kubelet) Start(ctx context.Context) error { Args: args.ToArgs(), } - if err := k.writeKubeletConfig(kubeletConfigPath); err != nil { + if err := k.writeKubeletConfig(); err != nil { return err } @@ -199,7 +209,7 @@ func (k *Kubelet) Stop() error { return nil } -func (k *Kubelet) writeKubeletConfig(path string) error { +func (k *Kubelet) writeKubeletConfig() error { var staticPodURL string if k.StaticPods != nil { url, err := k.StaticPods.ManifestURL() @@ -245,7 +255,7 @@ func (k *Kubelet) writeKubeletConfig(path string) error { return fmt.Errorf("can't marshal kubelet config: %w", err) } - err = file.WriteContentAtomically(path, configBytes, 0644) + err = file.WriteContentAtomically(k.configPath, configBytes, 0644) if err != nil { return fmt.Errorf("failed to write kubelet config: %w", err) }