|
| 1 | +/* |
| 2 | +Copyright 2019 The Kubernetes Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package nodetaint |
| 18 | + |
| 19 | +import ( |
| 20 | + "reflect" |
| 21 | + "testing" |
| 22 | + |
| 23 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 24 | + "k8s.io/apiserver/pkg/admission" |
| 25 | + "k8s.io/apiserver/pkg/authentication/user" |
| 26 | + utilfeature "k8s.io/apiserver/pkg/util/feature" |
| 27 | + api "k8s.io/kubernetes/pkg/apis/core" |
| 28 | + "k8s.io/kubernetes/pkg/features" |
| 29 | +) |
| 30 | + |
| 31 | +var ( |
| 32 | + enableTaintNodesByCondition = utilfeature.NewFeatureGate() |
| 33 | + disableTaintNodesByCondition = utilfeature.NewFeatureGate() |
| 34 | +) |
| 35 | + |
| 36 | +func init() { |
| 37 | + if err := enableTaintNodesByCondition.Add(map[utilfeature.Feature]utilfeature.FeatureSpec{features.TaintNodesByCondition: {Default: true}}); err != nil { |
| 38 | + panic(err) |
| 39 | + } |
| 40 | + if err := disableTaintNodesByCondition.Add(map[utilfeature.Feature]utilfeature.FeatureSpec{features.TaintNodesByCondition: {Default: false}}); err != nil { |
| 41 | + panic(err) |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +func Test_nodeTaints(t *testing.T) { |
| 46 | + var ( |
| 47 | + mynode = &user.DefaultInfo{Name: "system:node:mynode", Groups: []string{"system:nodes"}} |
| 48 | + resource = api.Resource("nodes").WithVersion("v1") |
| 49 | + notReadyTaint = api.Taint{Key: TaintNodeNotReady, Effect: api.TaintEffectNoSchedule} |
| 50 | + notReadyCondition = api.NodeCondition{Type: api.NodeReady, Status: api.ConditionFalse} |
| 51 | + myNodeObjMeta = metav1.ObjectMeta{Name: "mynode"} |
| 52 | + myNodeObj = api.Node{ObjectMeta: myNodeObjMeta} |
| 53 | + myTaintedNodeObj = api.Node{ObjectMeta: myNodeObjMeta, |
| 54 | + Spec: api.NodeSpec{Taints: []api.Taint{notReadyTaint}}} |
| 55 | + myUnreadyNodeObj = api.Node{ObjectMeta: myNodeObjMeta, |
| 56 | + Status: api.NodeStatus{Conditions: []api.NodeCondition{notReadyCondition}}} |
| 57 | + nodeKind = api.Kind("Node").WithVersion("v1") |
| 58 | + ) |
| 59 | + tests := []struct { |
| 60 | + name string |
| 61 | + node api.Node |
| 62 | + oldNode api.Node |
| 63 | + features utilfeature.FeatureGate |
| 64 | + operation admission.Operation |
| 65 | + expectedTaints []api.Taint |
| 66 | + }{ |
| 67 | + { |
| 68 | + name: "notReady taint is added on creation", |
| 69 | + node: myNodeObj, |
| 70 | + features: enableTaintNodesByCondition, |
| 71 | + operation: admission.Create, |
| 72 | + expectedTaints: []api.Taint{notReadyTaint}, |
| 73 | + }, |
| 74 | + { |
| 75 | + name: "NotReady taint is not added when TaintNodesByCondition is disabled", |
| 76 | + node: myNodeObj, |
| 77 | + features: disableTaintNodesByCondition, |
| 78 | + operation: admission.Create, |
| 79 | + expectedTaints: nil, |
| 80 | + }, |
| 81 | + { |
| 82 | + name: "already tainted node is not tainted again", |
| 83 | + node: myTaintedNodeObj, |
| 84 | + features: enableTaintNodesByCondition, |
| 85 | + operation: admission.Create, |
| 86 | + expectedTaints: []api.Taint{notReadyTaint}, |
| 87 | + }, |
| 88 | + { |
| 89 | + name: "NotReady taint is added to an unready node as well", |
| 90 | + node: myUnreadyNodeObj, |
| 91 | + features: enableTaintNodesByCondition, |
| 92 | + operation: admission.Create, |
| 93 | + expectedTaints: []api.Taint{notReadyTaint}, |
| 94 | + }, |
| 95 | + } |
| 96 | + for _, tt := range tests { |
| 97 | + t.Run(tt.name, func(t *testing.T) { |
| 98 | + attributes := admission.NewAttributesRecord(&tt.node, &tt.oldNode, nodeKind, myNodeObj.Namespace, myNodeObj.Name, resource, "", tt.operation, false, mynode) |
| 99 | + c := NewPlugin() |
| 100 | + if tt.features != nil { |
| 101 | + c.features = tt.features |
| 102 | + } |
| 103 | + err := c.Admit(attributes) |
| 104 | + if err != nil { |
| 105 | + t.Errorf("nodePlugin.Admit() error = %v", err) |
| 106 | + } |
| 107 | + node, _ := attributes.GetObject().(*api.Node) |
| 108 | + if !reflect.DeepEqual(node.Spec.Taints, tt.expectedTaints) { |
| 109 | + t.Errorf("Unexpected Node taints. Got %v\nExpected: %v", node.Spec.Taints, tt.expectedTaints) |
| 110 | + } |
| 111 | + }) |
| 112 | + } |
| 113 | +} |
0 commit comments