...
1
16
17 package markcontrolplane
18
19 import (
20 "fmt"
21
22 v1 "k8s.io/api/core/v1"
23 clientset "k8s.io/client-go/kubernetes"
24
25 "k8s.io/kubernetes/cmd/kubeadm/app/constants"
26 "k8s.io/kubernetes/cmd/kubeadm/app/util/apiclient"
27 )
28
29
30 var labelsToAdd = []string{
31 constants.LabelNodeRoleControlPlane,
32 constants.LabelExcludeFromExternalLB,
33 }
34
35
36 func MarkControlPlane(client clientset.Interface, controlPlaneName string, taints []v1.Taint) error {
37 fmt.Printf("[mark-control-plane] Marking the node %s as control-plane by adding the labels: %v\n",
38 controlPlaneName, labelsToAdd)
39
40 if len(taints) > 0 {
41 taintStrs := []string{}
42 for _, taint := range taints {
43 taintStrs = append(taintStrs, taint.ToString())
44 }
45 fmt.Printf("[mark-control-plane] Marking the node %s as control-plane by adding the taints %v\n", controlPlaneName, taintStrs)
46 }
47
48 return apiclient.PatchNode(client, controlPlaneName, func(n *v1.Node) {
49 markControlPlaneNode(n, taints)
50 })
51 }
52
53 func taintExists(taint v1.Taint, taints []v1.Taint) bool {
54 for _, t := range taints {
55 if t == taint {
56 return true
57 }
58 }
59
60 return false
61 }
62
63 func markControlPlaneNode(n *v1.Node, taints []v1.Taint) {
64 for _, label := range labelsToAdd {
65 n.ObjectMeta.Labels[label] = ""
66 }
67
68 for _, nt := range n.Spec.Taints {
69 if !taintExists(nt, taints) {
70 taints = append(taints, nt)
71 }
72 }
73
74 n.Spec.Taints = taints
75 }
76
View as plain text