admission_test.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. "reflect"
  16. "testing"
  17. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  18. "k8s.io/apimachinery/pkg/runtime"
  19. "k8s.io/apiserver/pkg/admission"
  20. "k8s.io/apiserver/pkg/authentication/user"
  21. "k8s.io/component-base/featuregate"
  22. api "k8s.io/kubernetes/pkg/apis/core"
  23. "k8s.io/kubernetes/pkg/features"
  24. )
  25. var (
  26. enableTaintNodesByCondition = featuregate.NewFeatureGate()
  27. disableTaintNodesByCondition = featuregate.NewFeatureGate()
  28. )
  29. func init() {
  30. if err := enableTaintNodesByCondition.Add(map[featuregate.Feature]featuregate.FeatureSpec{features.TaintNodesByCondition: {Default: true}}); err != nil {
  31. panic(err)
  32. }
  33. if err := disableTaintNodesByCondition.Add(map[featuregate.Feature]featuregate.FeatureSpec{features.TaintNodesByCondition: {Default: false}}); err != nil {
  34. panic(err)
  35. }
  36. }
  37. func Test_nodeTaints(t *testing.T) {
  38. var (
  39. mynode = &user.DefaultInfo{Name: "system:node:mynode", Groups: []string{"system:nodes"}}
  40. resource = api.Resource("nodes").WithVersion("v1")
  41. notReadyTaint = api.Taint{Key: TaintNodeNotReady, Effect: api.TaintEffectNoSchedule}
  42. notReadyCondition = api.NodeCondition{Type: api.NodeReady, Status: api.ConditionFalse}
  43. myNodeObjMeta = metav1.ObjectMeta{Name: "mynode"}
  44. myNodeObj = api.Node{ObjectMeta: myNodeObjMeta}
  45. myTaintedNodeObj = api.Node{ObjectMeta: myNodeObjMeta,
  46. Spec: api.NodeSpec{Taints: []api.Taint{notReadyTaint}}}
  47. myUnreadyNodeObj = api.Node{ObjectMeta: myNodeObjMeta,
  48. Status: api.NodeStatus{Conditions: []api.NodeCondition{notReadyCondition}}}
  49. nodeKind = api.Kind("Node").WithVersion("v1")
  50. )
  51. tests := []struct {
  52. name string
  53. node api.Node
  54. oldNode api.Node
  55. features featuregate.FeatureGate
  56. operation admission.Operation
  57. options runtime.Object
  58. expectedTaints []api.Taint
  59. }{
  60. {
  61. name: "notReady taint is added on creation",
  62. node: myNodeObj,
  63. features: enableTaintNodesByCondition,
  64. operation: admission.Create,
  65. options: &metav1.CreateOptions{},
  66. expectedTaints: []api.Taint{notReadyTaint},
  67. },
  68. {
  69. name: "NotReady taint is not added when TaintNodesByCondition is disabled",
  70. node: myNodeObj,
  71. features: disableTaintNodesByCondition,
  72. operation: admission.Create,
  73. options: &metav1.CreateOptions{},
  74. expectedTaints: nil,
  75. },
  76. {
  77. name: "already tainted node is not tainted again",
  78. node: myTaintedNodeObj,
  79. features: enableTaintNodesByCondition,
  80. operation: admission.Create,
  81. options: &metav1.CreateOptions{},
  82. expectedTaints: []api.Taint{notReadyTaint},
  83. },
  84. {
  85. name: "NotReady taint is added to an unready node as well",
  86. node: myUnreadyNodeObj,
  87. features: enableTaintNodesByCondition,
  88. operation: admission.Create,
  89. options: &metav1.CreateOptions{},
  90. expectedTaints: []api.Taint{notReadyTaint},
  91. },
  92. }
  93. for _, tt := range tests {
  94. t.Run(tt.name, func(t *testing.T) {
  95. attributes := admission.NewAttributesRecord(&tt.node, &tt.oldNode, nodeKind, myNodeObj.Namespace, myNodeObj.Name, resource, "", tt.operation, tt.options, false, mynode)
  96. c := NewPlugin()
  97. if tt.features != nil {
  98. c.features = tt.features
  99. }
  100. err := c.Admit(attributes, nil)
  101. if err != nil {
  102. t.Errorf("nodePlugin.Admit() error = %v", err)
  103. }
  104. node, _ := attributes.GetObject().(*api.Node)
  105. if !reflect.DeepEqual(node.Spec.Taints, tt.expectedTaints) {
  106. t.Errorf("Unexpected Node taints. Got %v\nExpected: %v", node.Spec.Taints, tt.expectedTaints)
  107. }
  108. })
  109. }
  110. }