|
| 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 implements phases of 'kubeadm upgrade node'. |
| 18 | +package node |
| 19 | + |
| 20 | +import ( |
| 21 | + "fmt" |
| 22 | + "io" |
| 23 | + |
| 24 | + "github.com/pkg/errors" |
| 25 | + |
| 26 | + clientset "k8s.io/client-go/kubernetes" |
| 27 | + |
| 28 | + kubeadmapi "k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm" |
| 29 | + "k8s.io/kubernetes/cmd/kubeadm/app/cmd/options" |
| 30 | + "k8s.io/kubernetes/cmd/kubeadm/app/cmd/phases/workflow" |
| 31 | + cmdutil "k8s.io/kubernetes/cmd/kubeadm/app/cmd/util" |
| 32 | + dnsaddon "k8s.io/kubernetes/cmd/kubeadm/app/phases/addons/dns" |
| 33 | + proxyaddon "k8s.io/kubernetes/cmd/kubeadm/app/phases/addons/proxy" |
| 34 | + "k8s.io/kubernetes/cmd/kubeadm/app/phases/upgrade" |
| 35 | +) |
| 36 | + |
| 37 | +// NewAddonPhase returns a new addon phase. |
| 38 | +func NewAddonPhase() workflow.Phase { |
| 39 | + return workflow.Phase{ |
| 40 | + Name: "addon", |
| 41 | + Short: "Upgrade the default kubeadm addons", |
| 42 | + Long: cmdutil.MacroCommandLongDescription, |
| 43 | + Phases: []workflow.Phase{ |
| 44 | + { |
| 45 | + Name: "all", |
| 46 | + Short: "Upgrade all the addons", |
| 47 | + InheritFlags: getAddonPhaseFlags("all"), |
| 48 | + RunAllSiblings: true, |
| 49 | + }, |
| 50 | + { |
| 51 | + Name: "coredns", |
| 52 | + Short: "Upgrade the CoreDNS addon", |
| 53 | + InheritFlags: getAddonPhaseFlags("coredns"), |
| 54 | + Run: runCoreDNSAddon, |
| 55 | + }, |
| 56 | + { |
| 57 | + Name: "kube-proxy", |
| 58 | + Short: "Upgrade the kube-proxy addon", |
| 59 | + InheritFlags: getAddonPhaseFlags("kube-proxy"), |
| 60 | + Run: runKubeProxyAddon, |
| 61 | + }, |
| 62 | + }, |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +func shouldUpgradeAddons(client clientset.Interface, cfg *kubeadmapi.InitConfiguration, out io.Writer) (bool, error) { |
| 67 | + unupgradedControlPlanes, err := upgrade.UnupgradedControlPlaneInstances(client, cfg.NodeRegistration.Name) |
| 68 | + if err != nil { |
| 69 | + return false, errors.Wrapf(err, "failed to determine whether all the control plane instances have been upgraded") |
| 70 | + } |
| 71 | + if len(unupgradedControlPlanes) > 0 { |
| 72 | + fmt.Fprintf(out, "[upgrade/addon] Skipping upgrade of addons because control plane instances %v have not been upgraded\n", unupgradedControlPlanes) |
| 73 | + return false, nil |
| 74 | + } |
| 75 | + return true, nil |
| 76 | +} |
| 77 | + |
| 78 | +func getInitData(c workflow.RunData) (*kubeadmapi.InitConfiguration, clientset.Interface, string, io.Writer, bool, error) { |
| 79 | + data, ok := c.(Data) |
| 80 | + if !ok { |
| 81 | + return nil, nil, "", nil, false, errors.New("addon phase invoked with an invalid data struct") |
| 82 | + } |
| 83 | + return data.InitCfg(), data.Client(), data.PatchesDir(), data.OutputWriter(), data.DryRun(), nil |
| 84 | +} |
| 85 | + |
| 86 | +// runCoreDNSAddon upgrades the CoreDNS addon. |
| 87 | +func runCoreDNSAddon(c workflow.RunData) error { |
| 88 | + cfg, client, patchesDir, out, dryRun, err := getInitData(c) |
| 89 | + if err != nil { |
| 90 | + return err |
| 91 | + } |
| 92 | + |
| 93 | + shouldUpgradeAddons, err := shouldUpgradeAddons(client, cfg, out) |
| 94 | + if err != nil { |
| 95 | + return err |
| 96 | + } |
| 97 | + if !shouldUpgradeAddons { |
| 98 | + return nil |
| 99 | + } |
| 100 | + |
| 101 | + // Upgrade CoreDNS |
| 102 | + if err := dnsaddon.EnsureDNSAddon(&cfg.ClusterConfiguration, client, patchesDir, out, dryRun); err != nil { |
| 103 | + return err |
| 104 | + } |
| 105 | + |
| 106 | + return nil |
| 107 | +} |
| 108 | + |
| 109 | +// runKubeProxyAddon upgrades the kube-proxy addon. |
| 110 | +func runKubeProxyAddon(c workflow.RunData) error { |
| 111 | + cfg, client, _, out, dryRun, err := getInitData(c) |
| 112 | + if err != nil { |
| 113 | + return err |
| 114 | + } |
| 115 | + |
| 116 | + shouldUpgradeAddons, err := shouldUpgradeAddons(client, cfg, out) |
| 117 | + if err != nil { |
| 118 | + return err |
| 119 | + } |
| 120 | + if !shouldUpgradeAddons { |
| 121 | + return nil |
| 122 | + } |
| 123 | + |
| 124 | + // Upgrade kube-proxy |
| 125 | + if err := proxyaddon.EnsureProxyAddon(&cfg.ClusterConfiguration, &cfg.LocalAPIEndpoint, client, out, dryRun); err != nil { |
| 126 | + return err |
| 127 | + } |
| 128 | + |
| 129 | + return nil |
| 130 | +} |
| 131 | + |
| 132 | +func getAddonPhaseFlags(name string) []string { |
| 133 | + flags := []string{ |
| 134 | + options.CfgPath, |
| 135 | + options.KubeconfigPath, |
| 136 | + options.DryRun, |
| 137 | + } |
| 138 | + if name == "all" || name == "coredns" { |
| 139 | + flags = append(flags, |
| 140 | + options.Patches, |
| 141 | + ) |
| 142 | + } |
| 143 | + return flags |
| 144 | +} |
0 commit comments