admission_test.go 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /*
  2. Copyright 2019 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 nodetaint
  14. import (
  15. "context"
  16. "reflect"
  17. "testing"
  18. v1 "k8s.io/api/core/v1"
  19. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  20. "k8s.io/apimachinery/pkg/runtime"
  21. "k8s.io/apiserver/pkg/admission"
  22. "k8s.io/apiserver/pkg/authentication/user"
  23. api "k8s.io/kubernetes/pkg/apis/core"
  24. )
  25. func Test_nodeTaints(t *testing.T) {
  26. var (
  27. mynode = &user.DefaultInfo{Name: "system:node:mynode", Groups: []string{"system:nodes"}}
  28. resource = api.Resource("nodes").WithVersion("v1")
  29. notReadyTaint = api.Taint{Key: v1.TaintNodeNotReady, Effect: api.TaintEffectNoSchedule}
  30. notReadyCondition = api.NodeCondition{Type: api.NodeReady, Status: api.ConditionFalse}
  31. myNodeObjMeta = metav1.ObjectMeta{Name: "mynode"}
  32. myNodeObj = api.Node{ObjectMeta: myNodeObjMeta}
  33. myTaintedNodeObj = api.Node{ObjectMeta: myNodeObjMeta,
  34. Spec: api.NodeSpec{Taints: []api.Taint{notReadyTaint}}}
  35. myUnreadyNodeObj = api.Node{ObjectMeta: myNodeObjMeta,
  36. Status: api.NodeStatus{Conditions: []api.NodeCondition{notReadyCondition}}}
  37. nodeKind = api.Kind("Node").WithVersion("v1")
  38. )
  39. tests := []struct {
  40. name string
  41. node api.Node
  42. oldNode api.Node
  43. operation admission.Operation
  44. options runtime.Object
  45. expectedTaints []api.Taint
  46. }{
  47. {
  48. name: "notReady taint is added on creation",
  49. node: myNodeObj,
  50. operation: admission.Create,
  51. options: &metav1.CreateOptions{},
  52. expectedTaints: []api.Taint{notReadyTaint},
  53. },
  54. {
  55. name: "already tainted node is not tainted again",
  56. node: myTaintedNodeObj,
  57. operation: admission.Create,
  58. options: &metav1.CreateOptions{},
  59. expectedTaints: []api.Taint{notReadyTaint},
  60. },
  61. {
  62. name: "NotReady taint is added to an unready node as well",
  63. node: myUnreadyNodeObj,
  64. operation: admission.Create,
  65. options: &metav1.CreateOptions{},
  66. expectedTaints: []api.Taint{notReadyTaint},
  67. },
  68. }
  69. for _, tt := range tests {
  70. t.Run(tt.name, func(t *testing.T) {
  71. attributes := admission.NewAttributesRecord(&tt.node, &tt.oldNode, nodeKind, myNodeObj.Namespace, myNodeObj.Name, resource, "", tt.operation, tt.options, false, mynode)
  72. c := NewPlugin()
  73. err := c.Admit(context.TODO(), attributes, nil)
  74. if err != nil {
  75. t.Errorf("nodePlugin.Admit() error = %v", err)
  76. }
  77. node, _ := attributes.GetObject().(*api.Node)
  78. if !reflect.DeepEqual(node.Spec.Taints, tt.expectedTaints) {
  79. t.Errorf("Unexpected Node taints. Got %v\nExpected: %v", node.Spec.Taints, tt.expectedTaints)
  80. }
  81. })
  82. }
  83. }