Skip to content

Commit 8871513

Browse files
committed
review fixes
1 parent e51b55b commit 8871513

File tree

1 file changed

+16
-8
lines changed

1 file changed

+16
-8
lines changed

cmd/kubeadm/app/phases/upgrade/postupgrade.go

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -290,15 +290,16 @@ func GetKubeletDir(dryRun bool) (string, error) {
290290
return kubeadmconstants.KubeletRunDirectory, nil
291291
}
292292

293+
// UpdateKubeletLocalMode changes the Server URL in the kubelets kubeconfig to the local API endpoint if it is currently
294+
// set to the ControlPlaneEndpoint.
295+
// TODO: remove this function once kubeletKubeConfigFilePath goes GA and is hardcoded to enabled by default:
296+
// https://github.com/kubernetes/kubeadm/issues/2271
293297
func UpdateKubeletLocalMode(cfg *kubeadmapi.InitConfiguration, dryRun bool) error {
294-
// TODO(chrischdi): how to get the correct dir? kubeadm init has a flag to change the location
295-
dir := kubeadmconstants.KubernetesDir
296-
297-
kubeletKubeConfigFilePath := filepath.Join(dir, kubeadmconstants.KubeletKubeConfigFileName)
298+
kubeletKubeConfigFilePath := filepath.Join(kubeadmconstants.KubernetesDir, kubeadmconstants.KubeletKubeConfigFileName)
298299

299300
if _, err := os.Stat(kubeletKubeConfigFilePath); err != nil {
300301
if os.IsNotExist(err) {
301-
// TODO(chrischdi): should we print a warning or even return the error?
302+
klog.V(2).Infof("Could not mutate the Server URL in %s: %v", kubeletKubeConfigFilePath, err)
302303
return nil
303304
}
304305
return err
@@ -319,16 +320,23 @@ func UpdateKubeletLocalMode(cfg *kubeadmapi.InitConfiguration, dryRun bool) erro
319320
return err
320321
}
321322

322-
// Skip changing kubeconfig file if LocalAPIEndpoint is already set.
323-
if config.Clusters[configContext.Cluster].Server == localAPIEndpoint {
323+
controlPlaneAPIEndpoint, err := kubeadmutil.GetControlPlaneEndpoint(cfg.ControlPlaneEndpoint, &cfg.LocalAPIEndpoint)
324+
if err != nil {
325+
return err
326+
}
327+
328+
// Skip changing kubeconfig file if Server does not match the ControlPlaneEndoint.
329+
if config.Clusters[configContext.Cluster].Server != controlPlaneAPIEndpoint || controlPlaneAPIEndpoint == localAPIEndpoint {
330+
klog.V(2).Infof("Skipping update of the Server URL in %s, because it's already not equal to %q or already matches the localAPIEndpoint", kubeletKubeConfigFilePath, cfg.ControlPlaneEndpoint)
324331
return nil
325332
}
326333

327334
if dryRun {
328-
fmt.Printf("[dryrun] Would change the server url from %q to %q in %s and try to restart kubelet\n", localAPIEndpoint, config.Clusters[configContext.Cluster].Server, kubeletKubeConfigFilePath)
335+
fmt.Printf("[dryrun] Would change the Server URL from %q to %q in %s and try to restart kubelet\n", config.Clusters[configContext.Cluster].Server, localAPIEndpoint, kubeletKubeConfigFilePath)
329336
return nil
330337
}
331338

339+
klog.V(1).Infof("Changing the Server URL from %q to %q in %s", config.Clusters[configContext.Cluster].Server, localAPIEndpoint, kubeletKubeConfigFilePath)
332340
config.Clusters[configContext.Cluster].Server = localAPIEndpoint
333341

334342
if err := clientcmd.WriteToFile(*config, kubeletKubeConfigFilePath); err != nil {

0 commit comments

Comments
 (0)