Skip to content
Open
Show file tree
Hide file tree
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
2 changes: 2 additions & 0 deletions pkg/agent/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -601,6 +601,7 @@ func get(ctx context.Context, envInfo *cmds.Agent, proxy proxy.Proxy) (*config.N
return nil, err
}

kubeletConfig := filepath.Join(envInfo.DataDir, "agent", "etc", "kubelet.conf")
// Ensure kubelet config dir exists
kubeletConfigDir := filepath.Join(envInfo.DataDir, "agent", "etc", "kubelet.conf.d")
if err := os.MkdirAll(kubeletConfigDir, 0700); err != nil {
Expand Down Expand Up @@ -635,6 +636,7 @@ func get(ctx context.Context, envInfo *cmds.Agent, proxy proxy.Proxy) (*config.N
nodeConfig.AgentConfig.ClusterDomain = controlConfig.ClusterDomain
nodeConfig.AgentConfig.ResolvConf = locateOrGenerateResolvConf(envInfo)
nodeConfig.AgentConfig.ClientCA = clientCAFile
nodeConfig.AgentConfig.KubeletConfig = kubeletConfig
nodeConfig.AgentConfig.KubeletConfigDir = kubeletConfigDir
nodeConfig.AgentConfig.KubeConfigKubelet = kubeconfigKubelet
nodeConfig.AgentConfig.KubeConfigKubeProxy = kubeconfigKubeproxy
Expand Down
20 changes: 12 additions & 8 deletions pkg/daemons/agent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,16 +64,20 @@ func startKubeProxy(ctx context.Context, cfg *daemonconfig.Agent) error {
func startKubelet(ctx context.Context, cfg *daemonconfig.Agent) error {
argsMap, defaultConfig, err := kubeletArgsAndConfig(cfg)
if err != nil {
return pkgerrors.WithMessage(err, "prepare default configuration drop-in")
return pkgerrors.WithMessage(err, "prepare default kubelet configuration")
}

extraArgs, err := extractConfigArgs(cfg.KubeletConfigDir, cfg.ExtraKubeletArgs, defaultConfig)
if err != nil {
return pkgerrors.WithMessage(err, "prepare user configuration drop-ins")
if err := writeKubeletConfig(cfg.KubeletConfig, defaultConfig); err != nil {
return pkgerrors.WithMessage(err, "generate default kubelet configuration")
Comment on lines +70 to +71
Copy link
Member

@brandond brandond May 9, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we remove the old drop-in at "00-"+version.Program+"-defaults.conf" if it exists? If we're going to generate our defaults in a new path, we should clean up the file generated by previous releases to avoid leaving unwanted drop-ins behind that will override what is currently expected. This could cause unexpected behavior when upgrading.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure if the whole agent directory will be regenerated or not upon upgrade. If not, I agree we should remove "00-"+version.Program+"-defaults.conf"

Copy link
Member

@brandond brandond May 9, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no, things do not get cleaned out of there when upgrading. We also intentionally leave the kubelet config dir alone so that users can put their own stuff in it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated the PR. I did not check the err returned by Remove. I guess it should be fine?

}

if err := writeKubeletConfig(cfg.KubeletConfigDir, defaultConfig); err != nil {
return pkgerrors.WithMessage(err, "generate default kubelet configuration drop-in")
// Remove default kubelet configuration generated in drop-in directory
// TODO: Clean this up some time after k3s with version <=1.33 is EOL.
os.Remove(filepath.Join(cfg.KubeletConfigDir, "00-"+version.Program+"-defaults.conf"))

extraArgs, err := extractConfigArgs(cfg.KubeletConfigDir, cfg.ExtraKubeletArgs)
if err != nil {
return pkgerrors.WithMessage(err, "prepare user kubelet configuration drop-ins")
}

args := util.GetArgs(argsMap, extraArgs)
Expand All @@ -99,7 +103,7 @@ func ImageCredProvAvailable(cfg *daemonconfig.Agent) bool {
// extractConfigArgs strips out any --config or --config-dir flags from the
// provided args list, and if set, copies the content of the file or dir into
// the target drop-in directory.
func extractConfigArgs(path string, extraArgs []string, config *kubeletconfig.KubeletConfiguration) ([]string, error) {
func extractConfigArgs(path string, extraArgs []string) ([]string, error) {
args := make([]string, 0, len(extraArgs))
strippedArgs := map[string]string{}
var skipVal bool
Expand Down Expand Up @@ -158,7 +162,7 @@ func writeKubeletConfig(path string, config *kubeletconfig.KubeletConfiguration)
if err != nil {
return err
}
return os.WriteFile(filepath.Join(path, "00-"+version.Program+"-defaults.conf"), b, 0600)
return os.WriteFile(path, b, 0600)
}

func defaultKubeletConfig(cfg *daemonconfig.Agent) (*kubeletconfig.KubeletConfiguration, error) {
Expand Down
4 changes: 1 addition & 3 deletions pkg/daemons/agent/agent_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,9 @@ func kubeletArgsAndConfig(cfg *config.Agent) (map[string]string, *kubeletconfig.
return nil, nil, err
}
argsMap := map[string]string{
"config": cfg.KubeletConfig,
"config-dir": cfg.KubeletConfigDir,
"kubeconfig": cfg.KubeConfigKubelet,
// note: KubeletConfiguration will omit this field when marshalling if it is set to 0, so we set it via CLI
// https://github.com/k3s-io/k3s/issues/12164
"read-only-port": "0",
}

if cfg.RootDir != "" {
Expand Down
4 changes: 1 addition & 3 deletions pkg/daemons/agent/agent_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,9 @@ func kubeletArgsAndConfig(cfg *config.Agent) (map[string]string, *kubeletconfig.
return nil, nil, err
}
argsMap := map[string]string{
"config": cfg.KubeletConfig,
"config-dir": cfg.KubeletConfigDir,
"kubeconfig": cfg.KubeConfigKubelet,
// note: KubeletConfiguration will omit this field when marshalling if it is set to 0, so we set it via CLI
// https://github.com/k3s-io/k3s/issues/12164
"read-only-port": "0",
}
if cfg.RootDir != "" {
argsMap["root-dir"] = cfg.RootDir
Expand Down
1 change: 1 addition & 0 deletions pkg/daemons/config/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ type Agent struct {
ClusterDomain string
ResolvConf string
RootDir string
KubeletConfig string
KubeletConfigDir string
KubeConfigKubelet string
KubeConfigKubeProxy string
Expand Down
Loading