diff --git a/pkg/component/worker/kubelet.go b/pkg/component/worker/kubelet.go index a5e852c6755c..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" @@ -56,13 +57,15 @@ 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 + configPath string + supervisor supervisor.Supervisor } var _ manager.Component = (*Kubelet)(nil) @@ -81,10 +84,20 @@ 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) + } + + 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 @@ -119,15 +132,13 @@ 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.dataDir, - "--config": kubeletConfigPath, + "--root-dir": k.rootDir, + "--config": k.configPath, "--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 { @@ -185,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 } @@ -198,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() @@ -244,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) }