Commit fff9940a authored by Xing Zhou's avatar Xing Zhou

AddOrUpdateTaint should ignore duplicate Taint.

The parameter of AddOrUpdateTaint is Taint pointer, so should use Taint object itself to compare with the node's taint list to ignore duplicate taint.
parent a881405b
...@@ -753,7 +753,7 @@ func TestAddOrUpdateTaintOnNode(t *testing.T) { ...@@ -753,7 +753,7 @@ func TestAddOrUpdateTaintOnNode(t *testing.T) {
{Key: "key1", Value: "value1", Effect: "NoSchedule"}, {Key: "key1", Value: "value1", Effect: "NoSchedule"},
{Key: "key2", Value: "value2", Effect: "NoExecute"}, {Key: "key2", Value: "value2", Effect: "NoExecute"},
}, },
requestCount: 3, requestCount: 2,
}, },
{ {
name: "add taint to node without taints", name: "add taint to node without taints",
......
...@@ -30,6 +30,7 @@ go_test( ...@@ -30,6 +30,7 @@ go_test(
deps = [ deps = [
"//pkg/api:go_default_library", "//pkg/api:go_default_library",
"//vendor/github.com/spf13/pflag:go_default_library", "//vendor/github.com/spf13/pflag:go_default_library",
"//vendor/k8s.io/api/core/v1:go_default_library",
], ],
) )
......
...@@ -271,7 +271,7 @@ func AddOrUpdateTaint(node *v1.Node, taint *v1.Taint) (*v1.Node, bool, error) { ...@@ -271,7 +271,7 @@ func AddOrUpdateTaint(node *v1.Node, taint *v1.Taint) (*v1.Node, bool, error) {
updated := false updated := false
for i := range nodeTaints { for i := range nodeTaints {
if taint.MatchTaint(&nodeTaints[i]) { if taint.MatchTaint(&nodeTaints[i]) {
if helper.Semantic.DeepEqual(taint, nodeTaints[i]) { if helper.Semantic.DeepEqual(*taint, nodeTaints[i]) {
return newNode, false, nil return newNode, false, nil
} }
newTaints = append(newTaints, *taint) newTaints = append(newTaints, *taint)
......
...@@ -21,6 +21,7 @@ import ( ...@@ -21,6 +21,7 @@ import (
"strings" "strings"
"testing" "testing"
"k8s.io/api/core/v1"
"k8s.io/kubernetes/pkg/api" "k8s.io/kubernetes/pkg/api"
"github.com/spf13/pflag" "github.com/spf13/pflag"
...@@ -74,3 +75,39 @@ func TestTaintsVar(t *testing.T) { ...@@ -74,3 +75,39 @@ func TestTaintsVar(t *testing.T) {
} }
} }
func TestAddOrUpdateTaint(t *testing.T) {
node := &v1.Node{}
taint := &v1.Taint{
Key: "foo",
Value: "bar",
Effect: v1.TaintEffectNoSchedule,
}
checkResult := func(testCaseName string, newNode *v1.Node, expectedTaint *v1.Taint, result, expectedResult bool, err error) {
if err != nil {
t.Errorf("[%s] should not raise error but got %v", testCaseName, err)
}
if result != expectedResult {
t.Errorf("[%s] should return %t, but got: %t", testCaseName, expectedResult, result)
}
if len(newNode.Spec.Taints) != 1 || !reflect.DeepEqual(newNode.Spec.Taints[0], *expectedTaint) {
t.Errorf("[%s] node should only have one taint: %v, but got: %v", testCaseName, *expectedTaint, newNode.Spec.Taints)
}
}
// Add a new Taint.
newNode, result, err := AddOrUpdateTaint(node, taint)
checkResult("Add New Taint", newNode, taint, result, true, err)
// Update a Taint.
taint.Value = "bar_1"
newNode, result, err = AddOrUpdateTaint(node, taint)
checkResult("Update Taint", newNode, taint, result, true, err)
// Add a duplicate Taint.
node = newNode
newNode, result, err = AddOrUpdateTaint(node, taint)
checkResult("Add Duplicate Taint", newNode, taint, result, false, err)
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment