From c0871b4433783a30c97e204b2011cf17d0457a62 Mon Sep 17 00:00:00 2001 From: "Lubomir I. Ivanov" Date: Thu, 13 Jan 2022 16:10:49 +0200 Subject: [PATCH] kubeadm: delete the old "master" label during upgrade - Rename the function in postupgrade.go to better reflect what is being done. - During "upgrade apply" find all nodes with the old label and remove it by calling PatchNode. - Update health check for CP nodes to not track "master" labeled nodes. At this point all CP nodes should have "control-plane" and we can use that selector only. --- cmd/kubeadm/app/cmd/join.go | 2 +- cmd/kubeadm/app/cmd/upgrade/apply.go | 7 ++++--- cmd/kubeadm/app/phases/upgrade/health.go | 21 ++----------------- cmd/kubeadm/app/phases/upgrade/postupgrade.go | 10 +++------ 4 files changed, 10 insertions(+), 30 deletions(-) diff --git a/cmd/kubeadm/app/cmd/join.go b/cmd/kubeadm/app/cmd/join.go index 934a0951ff3..fa0b07e3631 100644 --- a/cmd/kubeadm/app/cmd/join.go +++ b/cmd/kubeadm/app/cmd/join.go @@ -64,7 +64,7 @@ var ( * Certificate signing request was sent to apiserver and approval was received. * The Kubelet was informed of the new secure connection details. - * Control plane (master) label and taint were applied to the new node. + * Control plane label and taint were applied to the new node. * The Kubernetes control plane instances scaled up. {{.etcdMessage}} diff --git a/cmd/kubeadm/app/cmd/upgrade/apply.go b/cmd/kubeadm/app/cmd/upgrade/apply.go index 1705dfd175f..6f7f04b8fa9 100644 --- a/cmd/kubeadm/app/cmd/upgrade/apply.go +++ b/cmd/kubeadm/app/cmd/upgrade/apply.go @@ -157,9 +157,10 @@ func runApply(flags *applyFlags, args []string) error { } // TODO: https://github.com/kubernetes/kubeadm/issues/2200 - fmt.Printf("[upgrade/postupgrade] Applying label %s='' to Nodes with label %s='' (deprecated)\n", - kubeadmconstants.LabelNodeRoleControlPlane, kubeadmconstants.LabelNodeRoleOldControlPlane) - if err := upgrade.LabelOldControlPlaneNodes(client); err != nil { + fmt.Printf("[upgrade/postupgrade] Removing the deprecated label %s='' from all control plane Nodes. "+ + "After this step only the label %s='' will be present on control plane Nodes.\n", + kubeadmconstants.LabelNodeRoleOldControlPlane, kubeadmconstants.LabelNodeRoleControlPlane) + if err := upgrade.RemoveOldControlPlaneLabel(client); err != nil { return err } diff --git a/cmd/kubeadm/app/phases/upgrade/health.go b/cmd/kubeadm/app/phases/upgrade/health.go index 7ac2fa317cc..5a901ffe43f 100644 --- a/cmd/kubeadm/app/phases/upgrade/health.go +++ b/cmd/kubeadm/app/phases/upgrade/health.go @@ -212,34 +212,17 @@ func deleteHealthCheckJob(client clientset.Interface, ns, jobName string) error // controlPlaneNodesReady checks whether all control-plane Nodes in the cluster are in the Running state func controlPlaneNodesReady(client clientset.Interface, _ *kubeadmapi.ClusterConfiguration) error { - // list nodes labeled with a "master" node-role - selectorOldControlPlane := labels.SelectorFromSet(labels.Set(map[string]string{ - constants.LabelNodeRoleOldControlPlane: "", - })) - nodesWithOldLabel, err := client.CoreV1().Nodes().List(context.TODO(), metav1.ListOptions{ - LabelSelector: selectorOldControlPlane.String(), - }) - if err != nil { - return errors.Wrapf(err, "could not list nodes labeled with %q", constants.LabelNodeRoleOldControlPlane) - } - - // list nodes labeled with a "control-plane" node-role selectorControlPlane := labels.SelectorFromSet(labels.Set(map[string]string{ constants.LabelNodeRoleControlPlane: "", })) - nodesControlPlane, err := client.CoreV1().Nodes().List(context.TODO(), metav1.ListOptions{ + nodes, err := client.CoreV1().Nodes().List(context.TODO(), metav1.ListOptions{ LabelSelector: selectorControlPlane.String(), }) if err != nil { return errors.Wrapf(err, "could not list nodes labeled with %q", constants.LabelNodeRoleControlPlane) } - nodes := append(nodesWithOldLabel.Items, nodesControlPlane.Items...) - if len(nodes) == 0 { - return errors.New("failed to find any nodes with a control-plane role") - } - - notReadyControlPlanes := getNotReadyNodes(nodes) + notReadyControlPlanes := getNotReadyNodes(nodes.Items) if len(notReadyControlPlanes) != 0 { return errors.Errorf("there are NotReady control-planes in the cluster: %v", notReadyControlPlanes) } diff --git a/cmd/kubeadm/app/phases/upgrade/postupgrade.go b/cmd/kubeadm/app/phases/upgrade/postupgrade.go index c3e169489de..c590900568c 100644 --- a/cmd/kubeadm/app/phases/upgrade/postupgrade.go +++ b/cmd/kubeadm/app/phases/upgrade/postupgrade.go @@ -215,10 +215,9 @@ func rollbackFiles(files map[string]string, originalErr error) error { return errors.Errorf("couldn't move these files: %v. Got errors: %v", files, errorsutil.NewAggregate(errs)) } -// LabelOldControlPlaneNodes finds all nodes with the legacy node-role label and also applies -// the "control-plane" node-role label to them. +// RemoveOldControlPlaneLabel finds all nodes with the legacy node-role label and removes it // TODO: https://github.com/kubernetes/kubeadm/issues/2200 -func LabelOldControlPlaneNodes(client clientset.Interface) error { +func RemoveOldControlPlaneLabel(client clientset.Interface) error { selectorOldControlPlane := labels.SelectorFromSet(labels.Set(map[string]string{ kubeadmconstants.LabelNodeRoleOldControlPlane: "", })) @@ -230,11 +229,8 @@ func LabelOldControlPlaneNodes(client clientset.Interface) error { } for _, n := range nodesWithOldLabel.Items { - if _, hasNewLabel := n.ObjectMeta.Labels[kubeadmconstants.LabelNodeRoleControlPlane]; hasNewLabel { - continue - } err = apiclient.PatchNode(client, n.Name, func(n *v1.Node) { - n.ObjectMeta.Labels[kubeadmconstants.LabelNodeRoleControlPlane] = "" + delete(n.ObjectMeta.Labels, kubeadmconstants.LabelNodeRoleOldControlPlane) }) if err != nil { return err