markcontrolplane_test.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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. "bytes"
  16. "encoding/json"
  17. "io"
  18. "net/http"
  19. "net/http/httptest"
  20. "testing"
  21. "k8s.io/api/core/v1"
  22. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  23. clientset "k8s.io/client-go/kubernetes"
  24. restclient "k8s.io/client-go/rest"
  25. kubeadmconstants "k8s.io/kubernetes/cmd/kubeadm/app/constants"
  26. kubeadmutil "k8s.io/kubernetes/cmd/kubeadm/app/util"
  27. )
  28. func TestMarkControlPlane(t *testing.T) {
  29. // Note: this test takes advantage of the deterministic marshalling of
  30. // JSON provided by strategicpatch so that "expectedPatch" can use a
  31. // string equality test instead of a logical JSON equality test. That
  32. // will need to change if strategicpatch's behavior changes in the
  33. // future.
  34. tests := []struct {
  35. name string
  36. existingLabel string
  37. existingTaints []v1.Taint
  38. newTaints []v1.Taint
  39. expectedPatch string
  40. }{
  41. {
  42. "control-plane label and taint missing",
  43. "",
  44. nil,
  45. []v1.Taint{kubeadmconstants.ControlPlaneTaint},
  46. "{\"metadata\":{\"labels\":{\"node-role.kubernetes.io/master\":\"\"}},\"spec\":{\"taints\":[{\"effect\":\"NoSchedule\",\"key\":\"node-role.kubernetes.io/master\"}]}}",
  47. },
  48. {
  49. "control-plane label and taint missing but taint not wanted",
  50. "",
  51. nil,
  52. nil,
  53. "{\"metadata\":{\"labels\":{\"node-role.kubernetes.io/master\":\"\"}}}",
  54. },
  55. {
  56. "control-plane label missing",
  57. "",
  58. []v1.Taint{kubeadmconstants.ControlPlaneTaint},
  59. []v1.Taint{kubeadmconstants.ControlPlaneTaint},
  60. "{\"metadata\":{\"labels\":{\"node-role.kubernetes.io/master\":\"\"}}}",
  61. },
  62. {
  63. "control-plane taint missing",
  64. kubeadmconstants.LabelNodeRoleMaster,
  65. nil,
  66. []v1.Taint{kubeadmconstants.ControlPlaneTaint},
  67. "{\"spec\":{\"taints\":[{\"effect\":\"NoSchedule\",\"key\":\"node-role.kubernetes.io/master\"}]}}",
  68. },
  69. {
  70. "nothing missing",
  71. kubeadmconstants.LabelNodeRoleMaster,
  72. []v1.Taint{kubeadmconstants.ControlPlaneTaint},
  73. []v1.Taint{kubeadmconstants.ControlPlaneTaint},
  74. "{}",
  75. },
  76. {
  77. "has taint and no new taints wanted",
  78. kubeadmconstants.LabelNodeRoleMaster,
  79. []v1.Taint{
  80. {
  81. Key: "node.cloudprovider.kubernetes.io/uninitialized",
  82. Effect: v1.TaintEffectNoSchedule,
  83. },
  84. },
  85. nil,
  86. "{}",
  87. },
  88. {
  89. "has taint and should merge with wanted taint",
  90. kubeadmconstants.LabelNodeRoleMaster,
  91. []v1.Taint{
  92. {
  93. Key: "node.cloudprovider.kubernetes.io/uninitialized",
  94. Effect: v1.TaintEffectNoSchedule,
  95. },
  96. },
  97. []v1.Taint{kubeadmconstants.ControlPlaneTaint},
  98. "{\"spec\":{\"taints\":[{\"effect\":\"NoSchedule\",\"key\":\"node-role.kubernetes.io/master\"},{\"effect\":\"NoSchedule\",\"key\":\"node.cloudprovider.kubernetes.io/uninitialized\"}]}}",
  99. },
  100. }
  101. for _, tc := range tests {
  102. t.Run(tc.name, func(t *testing.T) {
  103. hostname, err := kubeadmutil.GetHostname("")
  104. if err != nil {
  105. t.Fatalf("MarkControlPlane(%s): unexpected error: %v", tc.name, err)
  106. }
  107. controlPlaneNode := &v1.Node{
  108. ObjectMeta: metav1.ObjectMeta{
  109. Name: hostname,
  110. Labels: map[string]string{
  111. v1.LabelHostname: hostname,
  112. },
  113. },
  114. }
  115. if tc.existingLabel != "" {
  116. controlPlaneNode.ObjectMeta.Labels[tc.existingLabel] = ""
  117. }
  118. if tc.existingTaints != nil {
  119. controlPlaneNode.Spec.Taints = tc.existingTaints
  120. }
  121. jsonNode, err := json.Marshal(controlPlaneNode)
  122. if err != nil {
  123. t.Fatalf("MarkControlPlane(%s): unexpected encoding error: %v", tc.name, err)
  124. }
  125. var patchRequest string
  126. s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
  127. w.Header().Set("Content-Type", "application/json")
  128. if req.URL.Path != "/api/v1/nodes/"+hostname {
  129. t.Errorf("MarkControlPlane(%s): request for unexpected HTTP resource: %v", tc.name, req.URL.Path)
  130. http.Error(w, "", http.StatusNotFound)
  131. return
  132. }
  133. switch req.Method {
  134. case "GET":
  135. case "PATCH":
  136. patchRequest = toString(req.Body)
  137. default:
  138. t.Errorf("MarkControlPlane(%s): request for unexpected HTTP verb: %v", tc.name, req.Method)
  139. http.Error(w, "", http.StatusNotFound)
  140. return
  141. }
  142. w.WriteHeader(http.StatusOK)
  143. w.Write(jsonNode)
  144. }))
  145. defer s.Close()
  146. cs, err := clientset.NewForConfig(&restclient.Config{Host: s.URL})
  147. if err != nil {
  148. t.Fatalf("MarkControlPlane(%s): unexpected error building clientset: %v", tc.name, err)
  149. }
  150. if err := MarkControlPlane(cs, hostname, tc.newTaints); err != nil {
  151. t.Errorf("MarkControlPlane(%s) returned unexpected error: %v", tc.name, err)
  152. }
  153. if tc.expectedPatch != patchRequest {
  154. t.Errorf("MarkControlPlane(%s) wanted patch %v, got %v", tc.name, tc.expectedPatch, patchRequest)
  155. }
  156. })
  157. }
  158. }
  159. func toString(r io.Reader) string {
  160. buf := new(bytes.Buffer)
  161. buf.ReadFrom(r)
  162. return buf.String()
  163. }