taint_test.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  1. /*
  2. Copyright 2014 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 taint
  14. import (
  15. "io/ioutil"
  16. "net/http"
  17. "reflect"
  18. "strings"
  19. "testing"
  20. "time"
  21. corev1 "k8s.io/api/core/v1"
  22. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  23. "k8s.io/apimachinery/pkg/runtime"
  24. "k8s.io/apimachinery/pkg/util/strategicpatch"
  25. "k8s.io/cli-runtime/pkg/genericclioptions"
  26. "k8s.io/client-go/rest/fake"
  27. cmdtesting "k8s.io/kubernetes/pkg/kubectl/cmd/testing"
  28. cmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util"
  29. "k8s.io/kubernetes/pkg/kubectl/scheme"
  30. )
  31. func generateNodeAndTaintedNode(oldTaints []corev1.Taint, newTaints []corev1.Taint) (*corev1.Node, *corev1.Node) {
  32. var taintedNode *corev1.Node
  33. // Create a node.
  34. node := &corev1.Node{
  35. ObjectMeta: metav1.ObjectMeta{
  36. Name: "node-name",
  37. CreationTimestamp: metav1.Time{Time: time.Now()},
  38. },
  39. Spec: corev1.NodeSpec{
  40. Taints: oldTaints,
  41. },
  42. Status: corev1.NodeStatus{},
  43. }
  44. // A copy of the same node, but tainted.
  45. taintedNode = node.DeepCopy()
  46. taintedNode.Spec.Taints = newTaints
  47. return node, taintedNode
  48. }
  49. func equalTaints(taintsA, taintsB []corev1.Taint) bool {
  50. if len(taintsA) != len(taintsB) {
  51. return false
  52. }
  53. for _, taintA := range taintsA {
  54. found := false
  55. for _, taintB := range taintsB {
  56. if reflect.DeepEqual(taintA, taintB) {
  57. found = true
  58. break
  59. }
  60. }
  61. if !found {
  62. return false
  63. }
  64. }
  65. return true
  66. }
  67. func TestTaint(t *testing.T) {
  68. tests := []struct {
  69. description string
  70. oldTaints []corev1.Taint
  71. newTaints []corev1.Taint
  72. args []string
  73. expectFatal bool
  74. expectTaint bool
  75. }{
  76. // success cases
  77. {
  78. description: "taints a node with effect NoSchedule",
  79. newTaints: []corev1.Taint{{
  80. Key: "foo",
  81. Value: "bar",
  82. Effect: "NoSchedule",
  83. }},
  84. args: []string{"node", "node-name", "foo=bar:NoSchedule"},
  85. expectFatal: false,
  86. expectTaint: true,
  87. },
  88. {
  89. description: "taints a node with effect PreferNoSchedule",
  90. newTaints: []corev1.Taint{{
  91. Key: "foo",
  92. Value: "bar",
  93. Effect: "PreferNoSchedule",
  94. }},
  95. args: []string{"node", "node-name", "foo=bar:PreferNoSchedule"},
  96. expectFatal: false,
  97. expectTaint: true,
  98. },
  99. {
  100. description: "update an existing taint on the node, change the value from bar to barz",
  101. oldTaints: []corev1.Taint{{
  102. Key: "foo",
  103. Value: "bar",
  104. Effect: "NoSchedule",
  105. }},
  106. newTaints: []corev1.Taint{{
  107. Key: "foo",
  108. Value: "barz",
  109. Effect: "NoSchedule",
  110. }},
  111. args: []string{"node", "node-name", "foo=barz:NoSchedule", "--overwrite"},
  112. expectFatal: false,
  113. expectTaint: true,
  114. },
  115. {
  116. description: "taints a node with two taints",
  117. newTaints: []corev1.Taint{{
  118. Key: "dedicated",
  119. Value: "namespaceA",
  120. Effect: "NoSchedule",
  121. }, {
  122. Key: "foo",
  123. Value: "bar",
  124. Effect: "PreferNoSchedule",
  125. }},
  126. args: []string{"node", "node-name", "dedicated=namespaceA:NoSchedule", "foo=bar:PreferNoSchedule"},
  127. expectFatal: false,
  128. expectTaint: true,
  129. },
  130. {
  131. description: "node has two taints with the same key but different effect, remove one of them by indicating exact key and effect",
  132. oldTaints: []corev1.Taint{{
  133. Key: "dedicated",
  134. Value: "namespaceA",
  135. Effect: "NoSchedule",
  136. }, {
  137. Key: "dedicated",
  138. Value: "namespaceA",
  139. Effect: "PreferNoSchedule",
  140. }},
  141. newTaints: []corev1.Taint{{
  142. Key: "dedicated",
  143. Value: "namespaceA",
  144. Effect: "PreferNoSchedule",
  145. }},
  146. args: []string{"node", "node-name", "dedicated:NoSchedule-"},
  147. expectFatal: false,
  148. expectTaint: true,
  149. },
  150. {
  151. description: "node has two taints with the same key but different effect, remove all of them with wildcard",
  152. oldTaints: []corev1.Taint{{
  153. Key: "dedicated",
  154. Value: "namespaceA",
  155. Effect: "NoSchedule",
  156. }, {
  157. Key: "dedicated",
  158. Value: "namespaceA",
  159. Effect: "PreferNoSchedule",
  160. }},
  161. newTaints: []corev1.Taint{},
  162. args: []string{"node", "node-name", "dedicated-"},
  163. expectFatal: false,
  164. expectTaint: true,
  165. },
  166. {
  167. description: "node has two taints, update one of them and remove the other",
  168. oldTaints: []corev1.Taint{{
  169. Key: "dedicated",
  170. Value: "namespaceA",
  171. Effect: "NoSchedule",
  172. }, {
  173. Key: "foo",
  174. Value: "bar",
  175. Effect: "PreferNoSchedule",
  176. }},
  177. newTaints: []corev1.Taint{{
  178. Key: "foo",
  179. Value: "barz",
  180. Effect: "PreferNoSchedule",
  181. }},
  182. args: []string{"node", "node-name", "dedicated:NoSchedule-", "foo=barz:PreferNoSchedule", "--overwrite"},
  183. expectFatal: false,
  184. expectTaint: true,
  185. },
  186. // error cases
  187. {
  188. description: "invalid taint key",
  189. args: []string{"node", "node-name", "nospecialchars^@=banana:NoSchedule"},
  190. expectFatal: true,
  191. expectTaint: false,
  192. },
  193. {
  194. description: "invalid taint effect",
  195. args: []string{"node", "node-name", "foo=bar:NoExcute"},
  196. expectFatal: true,
  197. expectTaint: false,
  198. },
  199. {
  200. description: "duplicated taints with the same key and effect should be rejected",
  201. args: []string{"node", "node-name", "foo=bar:NoExcute", "foo=barz:NoExcute"},
  202. expectFatal: true,
  203. expectTaint: false,
  204. },
  205. {
  206. description: "can't update existing taint on the node, since 'overwrite' flag is not set",
  207. oldTaints: []corev1.Taint{{
  208. Key: "foo",
  209. Value: "bar",
  210. Effect: "NoSchedule",
  211. }},
  212. newTaints: []corev1.Taint{{
  213. Key: "foo",
  214. Value: "bar",
  215. Effect: "NoSchedule",
  216. }},
  217. args: []string{"node", "node-name", "foo=bar:NoSchedule"},
  218. expectFatal: true,
  219. expectTaint: false,
  220. },
  221. }
  222. for _, test := range tests {
  223. t.Run(test.description, func(t *testing.T) {
  224. oldNode, expectNewNode := generateNodeAndTaintedNode(test.oldTaints, test.newTaints)
  225. newNode := &corev1.Node{}
  226. tainted := false
  227. tf := cmdtesting.NewTestFactory()
  228. defer tf.Cleanup()
  229. codec := scheme.Codecs.LegacyCodec(scheme.Scheme.PrioritizedVersionsAllGroups()...)
  230. ns := scheme.Codecs
  231. tf.Client = &fake.RESTClient{
  232. NegotiatedSerializer: ns,
  233. GroupVersion: corev1.SchemeGroupVersion,
  234. Client: fake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) {
  235. m := &MyReq{req}
  236. switch {
  237. case m.isFor("GET", "/nodes"):
  238. return &http.Response{StatusCode: 200, Header: cmdtesting.DefaultHeader(), Body: cmdtesting.ObjBody(codec, oldNode)}, nil
  239. case m.isFor("GET", "/nodes/node-name"):
  240. return &http.Response{StatusCode: 200, Header: cmdtesting.DefaultHeader(), Body: cmdtesting.ObjBody(codec, oldNode)}, nil
  241. case m.isFor("PATCH", "/nodes/node-name"):
  242. tainted = true
  243. data, err := ioutil.ReadAll(req.Body)
  244. if err != nil {
  245. t.Fatalf("%s: unexpected error: %v", test.description, err)
  246. }
  247. defer req.Body.Close()
  248. // apply the patch
  249. oldJSON, err := runtime.Encode(codec, oldNode)
  250. if err != nil {
  251. t.Fatalf("%s: unexpected error: %v", test.description, err)
  252. }
  253. appliedPatch, err := strategicpatch.StrategicMergePatch(oldJSON, data, &corev1.Node{})
  254. if err != nil {
  255. t.Fatalf("%s: unexpected error: %v", test.description, err)
  256. }
  257. // decode the patch
  258. if err := runtime.DecodeInto(codec, appliedPatch, newNode); err != nil {
  259. t.Fatalf("%s: unexpected error: %v", test.description, err)
  260. }
  261. if !equalTaints(expectNewNode.Spec.Taints, newNode.Spec.Taints) {
  262. t.Fatalf("%s: expected:\n%v\nsaw:\n%v\n", test.description, expectNewNode.Spec.Taints, newNode.Spec.Taints)
  263. }
  264. return &http.Response{StatusCode: 200, Header: cmdtesting.DefaultHeader(), Body: cmdtesting.ObjBody(codec, newNode)}, nil
  265. case m.isFor("PUT", "/nodes/node-name"):
  266. tainted = true
  267. data, err := ioutil.ReadAll(req.Body)
  268. if err != nil {
  269. t.Fatalf("%s: unexpected error: %v", test.description, err)
  270. }
  271. defer req.Body.Close()
  272. if err := runtime.DecodeInto(codec, data, newNode); err != nil {
  273. t.Fatalf("%s: unexpected error: %v", test.description, err)
  274. }
  275. if !equalTaints(expectNewNode.Spec.Taints, newNode.Spec.Taints) {
  276. t.Fatalf("%s: expected:\n%v\nsaw:\n%v\n", test.description, expectNewNode.Spec.Taints, newNode.Spec.Taints)
  277. }
  278. return &http.Response{StatusCode: 200, Header: cmdtesting.DefaultHeader(), Body: cmdtesting.ObjBody(codec, newNode)}, nil
  279. default:
  280. t.Fatalf("%s: unexpected request: %v %#v\n%#v", test.description, req.Method, req.URL, req)
  281. return nil, nil
  282. }
  283. }),
  284. }
  285. tf.ClientConfigVal = cmdtesting.DefaultClientConfig()
  286. cmd := NewCmdTaint(tf, genericclioptions.NewTestIOStreamsDiscard())
  287. sawFatal := false
  288. func() {
  289. defer func() {
  290. // Recover from the panic below.
  291. if r := recover(); r != nil {
  292. t.Logf("Recovered: %v", r)
  293. }
  294. // Restore cmdutil behavior
  295. cmdutil.DefaultBehaviorOnFatal()
  296. }()
  297. cmdutil.BehaviorOnFatal(func(e string, code int) { sawFatal = true; panic(e) })
  298. cmd.SetArgs(test.args)
  299. cmd.Execute()
  300. }()
  301. if test.expectFatal {
  302. if !sawFatal {
  303. t.Fatalf("%s: unexpected non-error", test.description)
  304. }
  305. }
  306. if test.expectTaint {
  307. if !tainted {
  308. t.Fatalf("%s: node not tainted", test.description)
  309. }
  310. }
  311. if !test.expectTaint {
  312. if tainted {
  313. t.Fatalf("%s: unexpected taint", test.description)
  314. }
  315. }
  316. })
  317. }
  318. }
  319. func TestValidateFlags(t *testing.T) {
  320. tests := []struct {
  321. taintOpts TaintOptions
  322. description string
  323. expectFatal bool
  324. }{
  325. {
  326. taintOpts: TaintOptions{selector: "myLabel=X", all: false},
  327. description: "With Selector and without All flag",
  328. expectFatal: false,
  329. },
  330. {
  331. taintOpts: TaintOptions{selector: "", all: true},
  332. description: "Without selector and All flag",
  333. expectFatal: false,
  334. },
  335. {
  336. taintOpts: TaintOptions{selector: "myLabel=X", all: true},
  337. description: "With Selector and with All flag",
  338. expectFatal: true,
  339. },
  340. {
  341. taintOpts: TaintOptions{selector: "", all: false, resources: []string{"node"}},
  342. description: "Without Selector and All flags and if node name is not provided",
  343. expectFatal: true,
  344. },
  345. {
  346. taintOpts: TaintOptions{selector: "", all: false, resources: []string{"node", "node-name"}},
  347. description: "Without Selector and ALL flags and if node name is provided",
  348. expectFatal: false,
  349. },
  350. }
  351. for _, test := range tests {
  352. sawFatal := false
  353. err := test.taintOpts.validateFlags()
  354. if err != nil {
  355. sawFatal = true
  356. }
  357. if test.expectFatal {
  358. if !sawFatal {
  359. t.Fatalf("%s expected not to fail", test.description)
  360. }
  361. }
  362. }
  363. }
  364. type MyReq struct {
  365. Request *http.Request
  366. }
  367. func (m *MyReq) isFor(method string, path string) bool {
  368. req := m.Request
  369. return method == req.Method && (req.URL.Path == path ||
  370. req.URL.Path == strings.Join([]string{"/api/v1", path}, "") ||
  371. req.URL.Path == strings.Join([]string{"/apis/extensions/v1beta1", path}, "") ||
  372. req.URL.Path == strings.Join([]string{"/apis/batch/v1", path}, ""))
  373. }