simplify code and add unit test for NotReady taint

This commit is contained in:
ceshihao 2018-05-14 06:55:42 +00:00
parent 842ae0bc22
commit 4eb72d7bcd
2 changed files with 48 additions and 18 deletions

View File

@ -81,32 +81,34 @@ var (
} }
nodeConditionToTaintKeyStatusMap = map[v1.NodeConditionType]struct { nodeConditionToTaintKeyStatusMap = map[v1.NodeConditionType]struct {
TaintKey string taintKey string
Status v1.ConditionStatus // noScheduleStatus is the condition under which the node should be tainted as not schedulable for this
// NodeConditionType
noScheduleStatus v1.ConditionStatus
}{ }{
v1.NodeReady: { v1.NodeReady: {
TaintKey: algorithm.TaintNodeNotReady, taintKey: algorithm.TaintNodeNotReady,
Status: v1.ConditionFalse, noScheduleStatus: v1.ConditionFalse,
}, },
v1.NodeMemoryPressure: { v1.NodeMemoryPressure: {
TaintKey: algorithm.TaintNodeMemoryPressure, taintKey: algorithm.TaintNodeMemoryPressure,
Status: v1.ConditionTrue, noScheduleStatus: v1.ConditionTrue,
}, },
v1.NodeOutOfDisk: { v1.NodeOutOfDisk: {
TaintKey: algorithm.TaintNodeOutOfDisk, taintKey: algorithm.TaintNodeOutOfDisk,
Status: v1.ConditionTrue, noScheduleStatus: v1.ConditionTrue,
}, },
v1.NodeDiskPressure: { v1.NodeDiskPressure: {
TaintKey: algorithm.TaintNodeDiskPressure, taintKey: algorithm.TaintNodeDiskPressure,
Status: v1.ConditionTrue, noScheduleStatus: v1.ConditionTrue,
}, },
v1.NodeNetworkUnavailable: { v1.NodeNetworkUnavailable: {
TaintKey: algorithm.TaintNodeNetworkUnavailable, taintKey: algorithm.TaintNodeNetworkUnavailable,
Status: v1.ConditionTrue, noScheduleStatus: v1.ConditionTrue,
}, },
v1.NodePIDPressure: { v1.NodePIDPressure: {
TaintKey: algorithm.TaintNodePIDPressure, taintKey: algorithm.TaintNodePIDPressure,
Status: v1.ConditionTrue, noScheduleStatus: v1.ConditionTrue,
}, },
} }
@ -453,12 +455,12 @@ func (nc *Controller) doFixDeprecatedTaintKeyPass(node *v1.Node) error {
func (nc *Controller) doNoScheduleTaintingPass(node *v1.Node) error { func (nc *Controller) doNoScheduleTaintingPass(node *v1.Node) error {
// Map node's condition to Taints. // Map node's condition to Taints.
taints := []v1.Taint{} var taints []v1.Taint
for _, condition := range node.Status.Conditions { for _, condition := range node.Status.Conditions {
if taintKeyStatus, found := nodeConditionToTaintKeyStatusMap[condition.Type]; found { if taint, found := nodeConditionToTaintKeyStatusMap[condition.Type]; found {
if condition.Status == taintKeyStatus.Status { if condition.Status == taint.noScheduleStatus {
taints = append(taints, v1.Taint{ taints = append(taints, v1.Taint{
Key: taintKeyStatus.TaintKey, Key: taint.taintKey,
Effect: v1.TaintEffectNoSchedule, Effect: v1.TaintEffectNoSchedule,
}) })
} }

View File

@ -2163,6 +2163,10 @@ func TestTaintsNodeByCondition(t *testing.T) {
Key: algorithm.TaintNodeNetworkUnavailable, Key: algorithm.TaintNodeNetworkUnavailable,
Effect: v1.TaintEffectNoSchedule, Effect: v1.TaintEffectNoSchedule,
} }
notReadyTaint := &v1.Taint{
Key: algorithm.TaintNodeNotReady,
Effect: v1.TaintEffectNoSchedule,
}
tests := []struct { tests := []struct {
Name string Name string
@ -2271,6 +2275,30 @@ func TestTaintsNodeByCondition(t *testing.T) {
}, },
ExpectedTaints: []*v1.Taint{networkUnavailableTaint}, ExpectedTaints: []*v1.Taint{networkUnavailableTaint},
}, },
{
Name: "Ready is false",
Node: &v1.Node{
ObjectMeta: metav1.ObjectMeta{
Name: "node0",
CreationTimestamp: metav1.Date(2012, 1, 1, 0, 0, 0, 0, time.UTC),
Labels: map[string]string{
kubeletapis.LabelZoneRegion: "region1",
kubeletapis.LabelZoneFailureDomain: "zone1",
},
},
Status: v1.NodeStatus{
Conditions: []v1.NodeCondition{
{
Type: v1.NodeReady,
Status: v1.ConditionFalse,
LastHeartbeatTime: metav1.Date(2015, 1, 1, 12, 0, 0, 0, time.UTC),
LastTransitionTime: metav1.Date(2015, 1, 1, 12, 0, 0, 0, time.UTC),
},
},
},
},
ExpectedTaints: []*v1.Taint{notReadyTaint},
},
} }
for _, test := range tests { for _, test := range tests {