|
| 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 | +} |
0 commit comments