diff --git a/cmd/kubeadm/app/cmd/phases/upgrade/node/controlplane.go b/cmd/kubeadm/app/cmd/phases/upgrade/node/controlplane.go index 330d89b9823..cbdca3ec1c6 100644 --- a/cmd/kubeadm/app/cmd/phases/upgrade/node/controlplane.go +++ b/cmd/kubeadm/app/cmd/phases/upgrade/node/controlplane.go @@ -24,7 +24,6 @@ import ( "k8s.io/kubernetes/cmd/kubeadm/app/cmd/options" "k8s.io/kubernetes/cmd/kubeadm/app/cmd/phases/workflow" - "k8s.io/kubernetes/cmd/kubeadm/app/features" "k8s.io/kubernetes/cmd/kubeadm/app/phases/upgrade" "k8s.io/kubernetes/cmd/kubeadm/app/util/apiclient" ) @@ -83,12 +82,6 @@ func runControlPlane() func(c workflow.RunData) error { return errors.Wrap(err, "failed to perform addons upgrade") } - if features.Enabled(cfg.FeatureGates, features.ControlPlaneKubeletLocalMode) { - if err := upgrade.UpdateKubeletLocalMode(cfg, dryRun); err != nil { - return errors.Wrap(err, "failed to update kubelet local mode") - } - } - fmt.Println("[upgrade] The control plane instance for this node was successfully updated!") return nil diff --git a/cmd/kubeadm/app/cmd/phases/upgrade/node/kubeconfig.go b/cmd/kubeadm/app/cmd/phases/upgrade/node/kubeconfig.go new file mode 100644 index 00000000000..305ca987e37 --- /dev/null +++ b/cmd/kubeadm/app/cmd/phases/upgrade/node/kubeconfig.go @@ -0,0 +1,73 @@ +/* +Copyright 2024 The Kubernetes Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +// Package node holds phases of "kubeadm upgrade node". +package node + +import ( + "fmt" + + "github.com/pkg/errors" + + "k8s.io/kubernetes/cmd/kubeadm/app/cmd/options" + "k8s.io/kubernetes/cmd/kubeadm/app/cmd/phases/workflow" + "k8s.io/kubernetes/cmd/kubeadm/app/features" + "k8s.io/kubernetes/cmd/kubeadm/app/phases/upgrade" +) + +// NewKubeconfigPhase creates a kubeadm workflow phase that implements handling of kubeconfig upgrade. +func NewKubeconfigPhase() workflow.Phase { + phase := workflow.Phase{ + Name: "kubeconfig", + Short: "Upgrade kubeconfig files for this node", + Run: runKubeconfig(), + Hidden: true, + InheritFlags: []string{ + options.DryRun, + options.KubeconfigPath, + }, + } + return phase +} + +func runKubeconfig() func(c workflow.RunData) error { + return func(c workflow.RunData) error { + data, ok := c.(Data) + if !ok { + return errors.New("kubeconfig phase invoked with an invalid data struct") + } + + // if this is not a control-plane node, this phase should not be executed + if !data.IsControlPlaneNode() { + fmt.Println("[upgrade] Skipping phase. Not a control plane node.") + return nil + } + + // otherwise, retrieve all the info required for kubeconfig upgrade + cfg := data.InitCfg() + dryRun := data.DryRun() + + if features.Enabled(cfg.FeatureGates, features.ControlPlaneKubeletLocalMode) { + if err := upgrade.UpdateKubeletLocalMode(cfg, dryRun); err != nil { + return errors.Wrap(err, "failed to update kubelet local mode") + } + } + + fmt.Println("[upgrade] The kubeconfig for this node was successfully updated!") + + return nil + } +} diff --git a/cmd/kubeadm/app/cmd/upgrade/node.go b/cmd/kubeadm/app/cmd/upgrade/node.go index eaafd186be1..90eea2875f1 100644 --- a/cmd/kubeadm/app/cmd/upgrade/node.go +++ b/cmd/kubeadm/app/cmd/upgrade/node.go @@ -97,6 +97,7 @@ func newCmdNode(out io.Writer) *cobra.Command { // initialize the workflow runner with the list of phases nodeRunner.AppendPhase(phases.NewPreflightPhase()) nodeRunner.AppendPhase(phases.NewControlPlane()) + nodeRunner.AppendPhase(phases.NewKubeconfigPhase()) nodeRunner.AppendPhase(phases.NewKubeletConfigPhase()) // sets the data builder function, that will be used by the runner