Skip to content

Commit 20fbdea

Browse files
committed
kubeadm: fix upgrade to be able to rollback ControlPlaneLocalMode
1 parent bb36212 commit 20fbdea

File tree

3 files changed

+27
-22
lines changed

3 files changed

+27
-22
lines changed

cmd/kubeadm/app/cmd/phases/upgrade/apply/kubeconfig.go

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ import (
2424

2525
"k8s.io/kubernetes/cmd/kubeadm/app/cmd/options"
2626
"k8s.io/kubernetes/cmd/kubeadm/app/cmd/phases/workflow"
27-
"k8s.io/kubernetes/cmd/kubeadm/app/features"
2827
"k8s.io/kubernetes/cmd/kubeadm/app/phases/upgrade"
2928
)
3029

@@ -53,13 +52,11 @@ func runKubeconfig() func(c workflow.RunData) error {
5352

5453
cfg := data.InitCfg()
5554

56-
if features.Enabled(cfg.FeatureGates, features.ControlPlaneKubeletLocalMode) {
57-
if err := upgrade.UpdateKubeletLocalMode(cfg, data.DryRun()); err != nil {
58-
return errors.Wrap(err, "failed to update kubelet local mode")
59-
}
55+
if err := upgrade.UpdateKubeletKubeconfigServer(cfg, data.DryRun()); err != nil {
56+
return errors.Wrap(err, "failed to update kubelet local mode")
6057
}
6158

62-
fmt.Println("[upgrad/kubeconfig] The kubeconfig files for this node were successfully upgraded!")
59+
fmt.Println("[upgrade/kubeconfig] The kubeconfig files for this node were successfully upgraded!")
6360

6461
return nil
6562
}

cmd/kubeadm/app/cmd/phases/upgrade/node/kubeconfig.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ import (
2424

2525
"k8s.io/kubernetes/cmd/kubeadm/app/cmd/options"
2626
"k8s.io/kubernetes/cmd/kubeadm/app/cmd/phases/workflow"
27-
"k8s.io/kubernetes/cmd/kubeadm/app/features"
2827
"k8s.io/kubernetes/cmd/kubeadm/app/phases/upgrade"
2928
)
3029

@@ -60,10 +59,8 @@ func runKubeconfig() func(c workflow.RunData) error {
6059
// Otherwise, retrieve all the info required for kubeconfig upgrade.
6160
cfg := data.InitCfg()
6261

63-
if features.Enabled(cfg.FeatureGates, features.ControlPlaneKubeletLocalMode) {
64-
if err := upgrade.UpdateKubeletLocalMode(cfg, data.DryRun()); err != nil {
65-
return errors.Wrap(err, "failed to update kubelet local mode")
66-
}
62+
if err := upgrade.UpdateKubeletKubeconfigServer(cfg, data.DryRun()); err != nil {
63+
return errors.Wrap(err, "failed to update kubelet local mode")
6764
}
6865

6966
fmt.Println("[upgrade/kubeconfig] The kubeconfig files for this node were successfully upgraded!")

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

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -193,11 +193,11 @@ func WriteKubeletConfigFiles(cfg *kubeadmapi.InitConfiguration, kubeletConfigDir
193193
return nil
194194
}
195195

196-
// UpdateKubeletLocalMode changes the Server URL in the kubelets kubeconfig to the local API endpoint if it is currently
197-
// set to the ControlPlaneEndpoint.
198-
// TODO: remove this function once kubeletKubeConfigFilePath goes GA and is hardcoded to enabled by default:
196+
// UpdateKubeletKubeconfigServer changes the Server URL in the kubelets kubeconfig if necessary depending on the
197+
// ControlPlaneKubeletLocalMode feature gate.
198+
// TODO: remove this function once ControlPlaneKubeletLocalMode goes GA and is hardcoded to be enabled by default:
199199
// https://github.com/kubernetes/kubeadm/issues/2271
200-
func UpdateKubeletLocalMode(cfg *kubeadmapi.InitConfiguration, dryRun bool) error {
200+
func UpdateKubeletKubeconfigServer(cfg *kubeadmapi.InitConfiguration, dryRun bool) error {
201201
kubeletKubeConfigFilePath := filepath.Join(kubeadmconstants.KubernetesDir, kubeadmconstants.KubeletKubeConfigFileName)
202202

203203
if _, err := os.Stat(kubeletKubeConfigFilePath); err != nil {
@@ -228,19 +228,30 @@ func UpdateKubeletLocalMode(cfg *kubeadmapi.InitConfiguration, dryRun bool) erro
228228
return err
229229
}
230230

231-
// Skip changing kubeconfig file if Server does not match the ControlPlaneEndpoint.
232-
if config.Clusters[configContext.Cluster].Server != controlPlaneAPIEndpoint || controlPlaneAPIEndpoint == localAPIEndpoint {
233-
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)
234-
return nil
231+
var targetServer string
232+
if features.Enabled(cfg.FeatureGates, features.ControlPlaneKubeletLocalMode) {
233+
// Skip changing kubeconfig file if Server does not match the ControlPlaneEndpoint.
234+
if config.Clusters[configContext.Cluster].Server != controlPlaneAPIEndpoint || controlPlaneAPIEndpoint == localAPIEndpoint {
235+
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, controlPlaneAPIEndpoint)
236+
return nil
237+
}
238+
targetServer = localAPIEndpoint
239+
} else {
240+
// Skip changing kubeconfig file if Server does not match the localAPIEndpoint.
241+
if config.Clusters[configContext.Cluster].Server != localAPIEndpoint {
242+
klog.V(2).Infof("Skipping update of the Server URL in %s, because it already matches the controlPlaneAPIEndpoint", kubeletKubeConfigFilePath)
243+
return nil
244+
}
245+
targetServer = controlPlaneAPIEndpoint
235246
}
236247

237248
if dryRun {
238-
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)
249+
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, targetServer, kubeletKubeConfigFilePath)
239250
return nil
240251
}
241252

242-
klog.V(1).Infof("Changing the Server URL from %q to %q in %s", config.Clusters[configContext.Cluster].Server, localAPIEndpoint, kubeletKubeConfigFilePath)
243-
config.Clusters[configContext.Cluster].Server = localAPIEndpoint
253+
klog.V(1).Infof("Changing the Server URL from %q to %q in %s", config.Clusters[configContext.Cluster].Server, targetServer, kubeletKubeConfigFilePath)
254+
config.Clusters[configContext.Cluster].Server = targetServer
244255

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

0 commit comments

Comments
 (0)