Skip to content

Commit 405fd11

Browse files
committed
kubeadm: use separate phase for changing the kubelet's kubeconfig on upgrade for ControlPlaneKubeletLocalMode
1 parent 2ad04a0 commit 405fd11

File tree

3 files changed

+74
-7
lines changed

3 files changed

+74
-7
lines changed

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

Lines changed: 0 additions & 7 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
"k8s.io/kubernetes/cmd/kubeadm/app/util/apiclient"
3029
)
@@ -83,12 +82,6 @@ func runControlPlane() func(c workflow.RunData) error {
8382
return errors.Wrap(err, "failed to perform addons upgrade")
8483
}
8584

86-
if features.Enabled(cfg.FeatureGates, features.ControlPlaneKubeletLocalMode) {
87-
if err := upgrade.UpdateKubeletLocalMode(cfg, dryRun); err != nil {
88-
return errors.Wrap(err, "failed to update kubelet local mode")
89-
}
90-
}
91-
9285
fmt.Println("[upgrade] The control plane instance for this node was successfully updated!")
9386

9487
return nil
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/*
2+
Copyright 2024 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
// Package node holds phases of "kubeadm upgrade node".
18+
package node
19+
20+
import (
21+
"fmt"
22+
23+
"github.com/pkg/errors"
24+
25+
"k8s.io/kubernetes/cmd/kubeadm/app/cmd/options"
26+
"k8s.io/kubernetes/cmd/kubeadm/app/cmd/phases/workflow"
27+
"k8s.io/kubernetes/cmd/kubeadm/app/features"
28+
"k8s.io/kubernetes/cmd/kubeadm/app/phases/upgrade"
29+
)
30+
31+
// NewKubeconfigPhase creates a kubeadm workflow phase that implements handling of kubeconfig upgrade.
32+
func NewKubeconfigPhase() workflow.Phase {
33+
phase := workflow.Phase{
34+
Name: "kubeconfig",
35+
Short: "Upgrade kubeconfig files for this node",
36+
Run: runKubeconfig(),
37+
Hidden: true,
38+
InheritFlags: []string{
39+
options.DryRun,
40+
options.KubeconfigPath,
41+
},
42+
}
43+
return phase
44+
}
45+
46+
func runKubeconfig() func(c workflow.RunData) error {
47+
return func(c workflow.RunData) error {
48+
data, ok := c.(Data)
49+
if !ok {
50+
return errors.New("kubeconfig phase invoked with an invalid data struct")
51+
}
52+
53+
// if this is not a control-plane node, this phase should not be executed
54+
if !data.IsControlPlaneNode() {
55+
fmt.Println("[upgrade] Skipping phase. Not a control plane node.")
56+
return nil
57+
}
58+
59+
// otherwise, retrieve all the info required for kubeconfig upgrade
60+
cfg := data.InitCfg()
61+
dryRun := data.DryRun()
62+
63+
if features.Enabled(cfg.FeatureGates, features.ControlPlaneKubeletLocalMode) {
64+
if err := upgrade.UpdateKubeletLocalMode(cfg, dryRun); err != nil {
65+
return errors.Wrap(err, "failed to update kubelet local mode")
66+
}
67+
}
68+
69+
fmt.Println("[upgrade] The kubeconfig for this node was successfully updated!")
70+
71+
return nil
72+
}
73+
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ func newCmdNode(out io.Writer) *cobra.Command {
9797
// initialize the workflow runner with the list of phases
9898
nodeRunner.AppendPhase(phases.NewPreflightPhase())
9999
nodeRunner.AppendPhase(phases.NewControlPlane())
100+
nodeRunner.AppendPhase(phases.NewKubeconfigPhase())
100101
nodeRunner.AppendPhase(phases.NewKubeletConfigPhase())
101102

102103
// sets the data builder function, that will be used by the runner

0 commit comments

Comments
 (0)