markcontrolplane.go 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. /*
  2. Copyright 2017 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package markcontrolplane
  14. import (
  15. "fmt"
  16. "k8s.io/api/core/v1"
  17. clientset "k8s.io/client-go/kubernetes"
  18. "k8s.io/kubernetes/cmd/kubeadm/app/constants"
  19. "k8s.io/kubernetes/cmd/kubeadm/app/util/apiclient"
  20. )
  21. // MarkControlPlane taints the control-plane and sets the control-plane label
  22. func MarkControlPlane(client clientset.Interface, controlPlaneName string, taints []v1.Taint) error {
  23. fmt.Printf("[mark-control-plane] Marking the node %s as control-plane by adding the label \"%s=''\"\n", controlPlaneName, constants.LabelNodeRoleMaster)
  24. if len(taints) > 0 {
  25. taintStrs := []string{}
  26. for _, taint := range taints {
  27. taintStrs = append(taintStrs, taint.ToString())
  28. }
  29. fmt.Printf("[mark-control-plane] Marking the node %s as control-plane by adding the taints %v\n", controlPlaneName, taintStrs)
  30. }
  31. return apiclient.PatchNode(client, controlPlaneName, func(n *v1.Node) {
  32. markControlPlaneNode(n, taints)
  33. })
  34. }
  35. func taintExists(taint v1.Taint, taints []v1.Taint) bool {
  36. for _, t := range taints {
  37. if t == taint {
  38. return true
  39. }
  40. }
  41. return false
  42. }
  43. func markControlPlaneNode(n *v1.Node, taints []v1.Taint) {
  44. n.ObjectMeta.Labels[constants.LabelNodeRoleMaster] = ""
  45. for _, nt := range n.Spec.Taints {
  46. if !taintExists(nt, taints) {
  47. taints = append(taints, nt)
  48. }
  49. }
  50. n.Spec.Taints = taints
  51. }