@@ -31,10 +31,12 @@ import (
31
31
errorsutil "k8s.io/apimachinery/pkg/util/errors"
32
32
"k8s.io/apimachinery/pkg/util/sets"
33
33
clientset "k8s.io/client-go/kubernetes"
34
+ "k8s.io/client-go/tools/clientcmd"
34
35
"k8s.io/klog/v2"
35
36
36
37
kubeadmapi "k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm"
37
38
kubeadmconstants "k8s.io/kubernetes/cmd/kubeadm/app/constants"
39
+ "k8s.io/kubernetes/cmd/kubeadm/app/features"
38
40
"k8s.io/kubernetes/cmd/kubeadm/app/phases/addons/dns"
39
41
"k8s.io/kubernetes/cmd/kubeadm/app/phases/addons/proxy"
40
42
"k8s.io/kubernetes/cmd/kubeadm/app/phases/bootstraptoken/clusterinfo"
@@ -109,6 +111,12 @@ func PerformPostUpgradeTasks(client clientset.Interface, cfg *kubeadmapi.InitCon
109
111
errs = append (errs , err )
110
112
}
111
113
114
+ if features .Enabled (cfg .FeatureGates , features .ControlPlaneKubeletLocalMode ) {
115
+ if err := UpdateKubeletLocalMode (cfg , dryRun ); err != nil {
116
+ return errors .Wrap (err , "failed to update kubelet local mode" )
117
+ }
118
+ }
119
+
112
120
return errorsutil .NewAggregate (errs )
113
121
}
114
122
@@ -281,3 +289,53 @@ func GetKubeletDir(dryRun bool) (string, error) {
281
289
}
282
290
return kubeadmconstants .KubeletRunDirectory , nil
283
291
}
292
+
293
+ 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
+
299
+ if _ , err := os .Stat (kubeletKubeConfigFilePath ); err != nil {
300
+ if os .IsNotExist (err ) {
301
+ // TODO(chrischdi): should we print a warning or even return the error?
302
+ return nil
303
+ }
304
+ return err
305
+ }
306
+
307
+ config , err := clientcmd .LoadFromFile (kubeletKubeConfigFilePath )
308
+ if err != nil {
309
+ return err
310
+ }
311
+
312
+ configContext , ok := config .Contexts [config .CurrentContext ]
313
+ if ! ok {
314
+ return errors .Errorf ("cannot find cluster for active context in kubeconfig %q" , kubeletKubeConfigFilePath )
315
+ }
316
+
317
+ localAPIEndpoint , err := kubeadmutil .GetLocalAPIEndpoint (& cfg .LocalAPIEndpoint )
318
+ if err != nil {
319
+ return err
320
+ }
321
+
322
+ // Skip changing kubeconfig file if LocalAPIEndpoint is already set.
323
+ if config .Clusters [configContext .Cluster ].Server == localAPIEndpoint {
324
+ return nil
325
+ }
326
+
327
+ 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 )
329
+ return nil
330
+ }
331
+
332
+ config .Clusters [configContext .Cluster ].Server = localAPIEndpoint
333
+
334
+ if err := clientcmd .WriteToFile (* config , kubeletKubeConfigFilePath ); err != nil {
335
+ return err
336
+ }
337
+
338
+ kubeletphase .TryRestartKubelet ()
339
+
340
+ return nil
341
+ }
0 commit comments