Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move kubelet config file into run directory #5305

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 24 additions & 13 deletions pkg/component/worker/kubelet.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package worker
import (
"bufio"
"context"
"errors"
"fmt"
"net"
"os"
Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}

Expand All @@ -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()
Expand Down Expand Up @@ -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)
}
Expand Down