|
| 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 apply implements phases of 'kubeadm upgrade apply'. |
| 18 | +package apply |
| 19 | + |
| 20 | +import ( |
| 21 | + "context" |
| 22 | + "fmt" |
| 23 | + "io" |
| 24 | + |
| 25 | + "github.com/pkg/errors" |
| 26 | + |
| 27 | + apierrors "k8s.io/apimachinery/pkg/api/errors" |
| 28 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 29 | + clientset "k8s.io/client-go/kubernetes" |
| 30 | + "k8s.io/klog/v2" |
| 31 | + |
| 32 | + kubeadmapi "k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm" |
| 33 | + "k8s.io/kubernetes/cmd/kubeadm/app/cmd/options" |
| 34 | + "k8s.io/kubernetes/cmd/kubeadm/app/cmd/phases/workflow" |
| 35 | + cmdutil "k8s.io/kubernetes/cmd/kubeadm/app/cmd/util" |
| 36 | + kubeadmconstants "k8s.io/kubernetes/cmd/kubeadm/app/constants" |
| 37 | + dnsaddon "k8s.io/kubernetes/cmd/kubeadm/app/phases/addons/dns" |
| 38 | + proxyaddon "k8s.io/kubernetes/cmd/kubeadm/app/phases/addons/proxy" |
| 39 | + "k8s.io/kubernetes/cmd/kubeadm/app/phases/upgrade" |
| 40 | +) |
| 41 | + |
| 42 | +// NewAddonPhase returns the addon Cobra command |
| 43 | +func NewAddonPhase() workflow.Phase { |
| 44 | + return workflow.Phase{ |
| 45 | + Name: "addon", |
| 46 | + Short: "Install required addons for passing conformance tests", |
| 47 | + Long: cmdutil.MacroCommandLongDescription, |
| 48 | + Phases: []workflow.Phase{ |
| 49 | + { |
| 50 | + Name: "all", |
| 51 | + Short: "Install all the addons", |
| 52 | + InheritFlags: getAddonPhaseFlags("all"), |
| 53 | + RunAllSiblings: true, |
| 54 | + }, |
| 55 | + { |
| 56 | + Name: "coredns", |
| 57 | + Short: "Install the CoreDNS addon to a Kubernetes cluster", |
| 58 | + InheritFlags: getAddonPhaseFlags("coredns"), |
| 59 | + Run: runCoreDNSAddon, |
| 60 | + }, |
| 61 | + { |
| 62 | + Name: "kube-proxy", |
| 63 | + Short: "Install the kube-proxy addon to a Kubernetes cluster", |
| 64 | + InheritFlags: getAddonPhaseFlags("kube-proxy"), |
| 65 | + Run: runKubeProxyAddon, |
| 66 | + }, |
| 67 | + }, |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +func shouldUpgradeAddons(client clientset.Interface, cfg *kubeadmapi.InitConfiguration, out io.Writer) (bool, error) { |
| 72 | + unupgradedControlPlanes, err := upgrade.UnupgradedControlPlaneInstances(client, cfg.NodeRegistration.Name) |
| 73 | + if err != nil { |
| 74 | + return false, errors.Wrapf(err, "failed to determine whether all the control plane instances have been upgraded") |
| 75 | + } |
| 76 | + if len(unupgradedControlPlanes) > 0 { |
| 77 | + fmt.Fprintf(out, "[upgrade/addons] skip upgrade addons because control plane instances %v have not been upgraded\n", unupgradedControlPlanes) |
| 78 | + return false, nil |
| 79 | + } |
| 80 | + |
| 81 | + return true, nil |
| 82 | +} |
| 83 | + |
| 84 | +func getInitData(c workflow.RunData) (*kubeadmapi.InitConfiguration, clientset.Interface, string, io.Writer, bool, error) { |
| 85 | + data, ok := c.(Data) |
| 86 | + if !ok { |
| 87 | + return nil, nil, "", nil, false, errors.New("addon phase invoked with an invalid data struct") |
| 88 | + } |
| 89 | + return data.InitCfg(), data.Client(), data.PatchesDir(), data.OutputWriter(), data.DryRun(), nil |
| 90 | +} |
| 91 | + |
| 92 | +// runCoreDNSAddon installs CoreDNS addon to a Kubernetes cluster |
| 93 | +func runCoreDNSAddon(c workflow.RunData) error { |
| 94 | + cfg, client, patchesDir, out, dryRun, err := getInitData(c) |
| 95 | + if err != nil { |
| 96 | + return err |
| 97 | + } |
| 98 | + |
| 99 | + shouldUpgradeAddons, err := shouldUpgradeAddons(client, cfg, out) |
| 100 | + if err != nil { |
| 101 | + return err |
| 102 | + } |
| 103 | + if !shouldUpgradeAddons { |
| 104 | + return nil |
| 105 | + } |
| 106 | + |
| 107 | + // If the coredns ConfigMap is missing, show a warning and assume that the |
| 108 | + // DNS addon was skipped during "kubeadm init", and that its redeployment on upgrade is not desired. |
| 109 | + // |
| 110 | + // TODO: remove this once "kubeadm upgrade apply" phases are supported: |
| 111 | + // https://github.com/kubernetes/kubeadm/issues/1318 |
| 112 | + if _, err := client.CoreV1().ConfigMaps(metav1.NamespaceSystem).Get( |
| 113 | + context.TODO(), |
| 114 | + kubeadmconstants.CoreDNSConfigMap, |
| 115 | + metav1.GetOptions{}, |
| 116 | + ); err != nil && apierrors.IsNotFound(err) { |
| 117 | + klog.Warningf("the ConfigMaps %q in the namespace %q were not found. "+ |
| 118 | + "Assuming that a DNS server was not deployed for this cluster. "+ |
| 119 | + "Note that once 'kubeadm upgrade apply' supports phases you "+ |
| 120 | + "will have to skip the DNS upgrade manually", |
| 121 | + kubeadmconstants.CoreDNSConfigMap, |
| 122 | + metav1.NamespaceSystem) |
| 123 | + return nil |
| 124 | + } |
| 125 | + |
| 126 | + // Upgrade CoreDNS |
| 127 | + if err := dnsaddon.EnsureDNSAddon(&cfg.ClusterConfiguration, client, patchesDir, out, dryRun); err != nil { |
| 128 | + return err |
| 129 | + } |
| 130 | + |
| 131 | + return nil |
| 132 | +} |
| 133 | + |
| 134 | +// runKubeProxyAddon installs KubeProxy addon to a Kubernetes cluster |
| 135 | +func runKubeProxyAddon(c workflow.RunData) error { |
| 136 | + cfg, client, _, out, dryRun, err := getInitData(c) |
| 137 | + if err != nil { |
| 138 | + return err |
| 139 | + } |
| 140 | + |
| 141 | + shouldUpgradeAddons, err := shouldUpgradeAddons(client, cfg, out) |
| 142 | + if err != nil { |
| 143 | + return err |
| 144 | + } |
| 145 | + if !shouldUpgradeAddons { |
| 146 | + return nil |
| 147 | + } |
| 148 | + |
| 149 | + // If the kube-proxy ConfigMap is missing, show a warning and assume that kube-proxy |
| 150 | + // was skipped during "kubeadm init", and that its redeployment on upgrade is not desired. |
| 151 | + // |
| 152 | + // TODO: remove this once "kubeadm upgrade apply" phases are supported: |
| 153 | + // https://github.com/kubernetes/kubeadm/issues/1318 |
| 154 | + if _, err := client.CoreV1().ConfigMaps(metav1.NamespaceSystem).Get( |
| 155 | + context.TODO(), |
| 156 | + kubeadmconstants.KubeProxyConfigMap, |
| 157 | + metav1.GetOptions{}, |
| 158 | + ); err != nil && apierrors.IsNotFound(err) { |
| 159 | + klog.Warningf("the ConfigMap %q in the namespace %q was not found. "+ |
| 160 | + "Assuming that kube-proxy was not deployed for this cluster. "+ |
| 161 | + "Note that once 'kubeadm upgrade apply' supports phases you "+ |
| 162 | + "will have to skip the kube-proxy upgrade manually", |
| 163 | + kubeadmconstants.KubeProxyConfigMap, |
| 164 | + metav1.NamespaceSystem) |
| 165 | + return nil |
| 166 | + } |
| 167 | + |
| 168 | + // Upgrade kube-proxy |
| 169 | + if err := proxyaddon.EnsureProxyAddon(&cfg.ClusterConfiguration, &cfg.LocalAPIEndpoint, client, out, dryRun); err != nil { |
| 170 | + return err |
| 171 | + } |
| 172 | + |
| 173 | + return nil |
| 174 | +} |
| 175 | + |
| 176 | +func getAddonPhaseFlags(name string) []string { |
| 177 | + flags := []string{ |
| 178 | + options.CfgPath, |
| 179 | + options.KubeconfigPath, |
| 180 | + options.DryRun, |
| 181 | + } |
| 182 | + if name == "all" || name == "coredns" { |
| 183 | + flags = append(flags, |
| 184 | + options.Patches, |
| 185 | + ) |
| 186 | + } |
| 187 | + return flags |
| 188 | +} |
0 commit comments