2016-03-31 03:42:57 +00:00
|
|
|
/*
|
2016-06-03 00:25:58 +00:00
|
|
|
Copyright 2014 The Kubernetes Authors.
|
2016-03-31 03:42:57 +00:00
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2017-06-22 18:24:23 +00:00
|
|
|
"k8s.io/api/core/v1"
|
2017-01-11 14:09:48 +00:00
|
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
|
|
|
"k8s.io/apimachinery/pkg/runtime"
|
2017-01-24 14:35:22 +00:00
|
|
|
"k8s.io/apimachinery/pkg/util/strategicpatch"
|
2017-01-24 15:00:24 +00:00
|
|
|
"k8s.io/client-go/rest/fake"
|
2016-03-31 03:42:57 +00:00
|
|
|
"k8s.io/kubernetes/pkg/api"
|
2016-10-18 22:53:26 +00:00
|
|
|
cmdtesting "k8s.io/kubernetes/pkg/kubectl/cmd/testing"
|
2016-03-31 03:42:57 +00:00
|
|
|
cmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util"
|
|
|
|
)
|
|
|
|
|
2017-02-07 14:08:45 +00:00
|
|
|
func generateNodeAndTaintedNode(oldTaints []v1.Taint, newTaints []v1.Taint) (*v1.Node, *v1.Node) {
|
|
|
|
var taintedNode *v1.Node
|
2016-03-31 03:42:57 +00:00
|
|
|
|
|
|
|
// Create a node.
|
2017-02-07 14:08:45 +00:00
|
|
|
node := &v1.Node{
|
2017-01-17 03:38:19 +00:00
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
2016-03-31 03:42:57 +00:00
|
|
|
Name: "node-name",
|
2016-12-03 18:57:26 +00:00
|
|
|
CreationTimestamp: metav1.Time{Time: time.Now()},
|
2016-03-31 03:42:57 +00:00
|
|
|
},
|
2017-02-07 14:08:45 +00:00
|
|
|
Spec: v1.NodeSpec{
|
2016-03-31 03:42:57 +00:00
|
|
|
ExternalID: "node-name",
|
2017-02-07 14:08:45 +00:00
|
|
|
Taints: oldTaints,
|
2016-03-31 03:42:57 +00:00
|
|
|
},
|
2017-02-07 14:08:45 +00:00
|
|
|
Status: v1.NodeStatus{},
|
2016-03-31 03:42:57 +00:00
|
|
|
}
|
2016-12-04 20:51:44 +00:00
|
|
|
clone, _ := api.Scheme.DeepCopy(node)
|
2016-03-31 03:42:57 +00:00
|
|
|
|
|
|
|
// A copy of the same node, but tainted.
|
2017-02-07 14:08:45 +00:00
|
|
|
taintedNode = clone.(*v1.Node)
|
|
|
|
taintedNode.Spec.Taints = newTaints
|
2016-03-31 03:42:57 +00:00
|
|
|
|
|
|
|
return node, taintedNode
|
|
|
|
}
|
|
|
|
|
2017-02-07 14:08:45 +00:00
|
|
|
func equalTaints(taintsA, taintsB []v1.Taint) bool {
|
2016-03-31 03:42:57 +00:00
|
|
|
if len(taintsA) != len(taintsB) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, taintA := range taintsA {
|
|
|
|
found := false
|
|
|
|
for _, taintB := range taintsB {
|
|
|
|
if reflect.DeepEqual(taintA, taintB) {
|
|
|
|
found = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !found {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestTaint(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
description string
|
2017-01-13 04:18:34 +00:00
|
|
|
oldTaints []v1.Taint
|
|
|
|
newTaints []v1.Taint
|
2016-03-31 03:42:57 +00:00
|
|
|
args []string
|
|
|
|
expectFatal bool
|
|
|
|
expectTaint bool
|
2017-04-20 21:27:09 +00:00
|
|
|
selector bool
|
2016-03-31 03:42:57 +00:00
|
|
|
}{
|
|
|
|
// success cases
|
|
|
|
{
|
|
|
|
description: "taints a node with effect NoSchedule",
|
2017-01-13 04:18:34 +00:00
|
|
|
newTaints: []v1.Taint{{
|
2016-03-31 03:42:57 +00:00
|
|
|
Key: "foo",
|
|
|
|
Value: "bar",
|
|
|
|
Effect: "NoSchedule",
|
|
|
|
}},
|
|
|
|
args: []string{"node", "node-name", "foo=bar:NoSchedule"},
|
|
|
|
expectFatal: false,
|
|
|
|
expectTaint: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
description: "taints a node with effect PreferNoSchedule",
|
2017-01-13 04:18:34 +00:00
|
|
|
newTaints: []v1.Taint{{
|
2016-03-31 03:42:57 +00:00
|
|
|
Key: "foo",
|
|
|
|
Value: "bar",
|
|
|
|
Effect: "PreferNoSchedule",
|
|
|
|
}},
|
|
|
|
args: []string{"node", "node-name", "foo=bar:PreferNoSchedule"},
|
|
|
|
expectFatal: false,
|
|
|
|
expectTaint: true,
|
|
|
|
},
|
|
|
|
{
|
2016-08-12 08:46:40 +00:00
|
|
|
description: "update an existing taint on the node, change the value from bar to barz",
|
2017-01-13 04:18:34 +00:00
|
|
|
oldTaints: []v1.Taint{{
|
2016-03-31 03:42:57 +00:00
|
|
|
Key: "foo",
|
|
|
|
Value: "bar",
|
|
|
|
Effect: "NoSchedule",
|
|
|
|
}},
|
2017-01-13 04:18:34 +00:00
|
|
|
newTaints: []v1.Taint{{
|
2016-03-31 03:42:57 +00:00
|
|
|
Key: "foo",
|
2016-08-12 08:46:40 +00:00
|
|
|
Value: "barz",
|
|
|
|
Effect: "NoSchedule",
|
2016-03-31 03:42:57 +00:00
|
|
|
}},
|
2016-08-12 08:46:40 +00:00
|
|
|
args: []string{"node", "node-name", "foo=barz:NoSchedule", "--overwrite"},
|
2016-03-31 03:42:57 +00:00
|
|
|
expectFatal: false,
|
|
|
|
expectTaint: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
description: "taints a node with two taints",
|
2017-01-13 04:18:34 +00:00
|
|
|
newTaints: []v1.Taint{{
|
2016-03-31 03:42:57 +00:00
|
|
|
Key: "dedicated",
|
|
|
|
Value: "namespaceA",
|
|
|
|
Effect: "NoSchedule",
|
|
|
|
}, {
|
|
|
|
Key: "foo",
|
|
|
|
Value: "bar",
|
|
|
|
Effect: "PreferNoSchedule",
|
|
|
|
}},
|
|
|
|
args: []string{"node", "node-name", "dedicated=namespaceA:NoSchedule", "foo=bar:PreferNoSchedule"},
|
|
|
|
expectFatal: false,
|
|
|
|
expectTaint: true,
|
|
|
|
},
|
|
|
|
{
|
2016-08-12 08:46:40 +00:00
|
|
|
description: "node has two taints with the same key but different effect, remove one of them by indicating exact key and effect",
|
2017-01-13 04:18:34 +00:00
|
|
|
oldTaints: []v1.Taint{{
|
2016-03-31 03:42:57 +00:00
|
|
|
Key: "dedicated",
|
|
|
|
Value: "namespaceA",
|
|
|
|
Effect: "NoSchedule",
|
|
|
|
}, {
|
2016-08-12 08:46:40 +00:00
|
|
|
Key: "dedicated",
|
|
|
|
Value: "namespaceA",
|
2016-03-31 03:42:57 +00:00
|
|
|
Effect: "PreferNoSchedule",
|
|
|
|
}},
|
2017-01-13 04:18:34 +00:00
|
|
|
newTaints: []v1.Taint{{
|
2016-08-12 08:46:40 +00:00
|
|
|
Key: "dedicated",
|
|
|
|
Value: "namespaceA",
|
2016-03-31 03:42:57 +00:00
|
|
|
Effect: "PreferNoSchedule",
|
|
|
|
}},
|
2016-08-12 08:46:40 +00:00
|
|
|
args: []string{"node", "node-name", "dedicated:NoSchedule-"},
|
|
|
|
expectFatal: false,
|
|
|
|
expectTaint: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
description: "node has two taints with the same key but different effect, remove all of them with wildcard",
|
2017-01-13 04:18:34 +00:00
|
|
|
oldTaints: []v1.Taint{{
|
2016-08-12 08:46:40 +00:00
|
|
|
Key: "dedicated",
|
|
|
|
Value: "namespaceA",
|
|
|
|
Effect: "NoSchedule",
|
|
|
|
}, {
|
|
|
|
Key: "dedicated",
|
|
|
|
Value: "namespaceA",
|
|
|
|
Effect: "PreferNoSchedule",
|
|
|
|
}},
|
2017-01-13 04:18:34 +00:00
|
|
|
newTaints: []v1.Taint{},
|
2016-03-31 03:42:57 +00:00
|
|
|
args: []string{"node", "node-name", "dedicated-"},
|
|
|
|
expectFatal: false,
|
|
|
|
expectTaint: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
description: "node has two taints, update one of them and remove the other",
|
2017-01-13 04:18:34 +00:00
|
|
|
oldTaints: []v1.Taint{{
|
2016-03-31 03:42:57 +00:00
|
|
|
Key: "dedicated",
|
|
|
|
Value: "namespaceA",
|
|
|
|
Effect: "NoSchedule",
|
|
|
|
}, {
|
|
|
|
Key: "foo",
|
|
|
|
Value: "bar",
|
|
|
|
Effect: "PreferNoSchedule",
|
|
|
|
}},
|
2017-01-13 04:18:34 +00:00
|
|
|
newTaints: []v1.Taint{{
|
2016-03-31 03:42:57 +00:00
|
|
|
Key: "foo",
|
2016-08-12 08:46:40 +00:00
|
|
|
Value: "barz",
|
|
|
|
Effect: "PreferNoSchedule",
|
2016-03-31 03:42:57 +00:00
|
|
|
}},
|
2016-08-12 08:46:40 +00:00
|
|
|
args: []string{"node", "node-name", "dedicated:NoSchedule-", "foo=barz:PreferNoSchedule", "--overwrite"},
|
2016-03-31 03:42:57 +00:00
|
|
|
expectFatal: false,
|
|
|
|
expectTaint: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
// error cases
|
|
|
|
{
|
|
|
|
description: "invalid taint key",
|
|
|
|
args: []string{"node", "node-name", "nospecialchars^@=banana:NoSchedule"},
|
|
|
|
expectFatal: true,
|
|
|
|
expectTaint: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
description: "invalid taint effect",
|
|
|
|
args: []string{"node", "node-name", "foo=bar:NoExcute"},
|
|
|
|
expectFatal: true,
|
|
|
|
expectTaint: false,
|
|
|
|
},
|
2016-08-12 08:46:40 +00:00
|
|
|
{
|
|
|
|
description: "duplicated taints with the same key and effect should be rejected",
|
|
|
|
args: []string{"node", "node-name", "foo=bar:NoExcute", "foo=barz:NoExcute"},
|
|
|
|
expectFatal: true,
|
|
|
|
expectTaint: false,
|
|
|
|
},
|
2016-03-31 03:42:57 +00:00
|
|
|
{
|
|
|
|
description: "can't update existing taint on the node, since 'overwrite' flag is not set",
|
2017-01-13 04:18:34 +00:00
|
|
|
oldTaints: []v1.Taint{{
|
2016-03-31 03:42:57 +00:00
|
|
|
Key: "foo",
|
|
|
|
Value: "bar",
|
|
|
|
Effect: "NoSchedule",
|
|
|
|
}},
|
2017-01-13 04:18:34 +00:00
|
|
|
newTaints: []v1.Taint{{
|
2016-03-31 03:42:57 +00:00
|
|
|
Key: "foo",
|
|
|
|
Value: "bar",
|
|
|
|
Effect: "NoSchedule",
|
|
|
|
}},
|
2016-08-12 08:46:40 +00:00
|
|
|
args: []string{"node", "node-name", "foo=bar:NoSchedule"},
|
2016-03-31 03:42:57 +00:00
|
|
|
expectFatal: true,
|
|
|
|
expectTaint: false,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
|
|
|
oldNode, expectNewNode := generateNodeAndTaintedNode(test.oldTaints, test.newTaints)
|
2017-02-07 14:08:45 +00:00
|
|
|
new_node := &v1.Node{}
|
2016-03-31 03:42:57 +00:00
|
|
|
tainted := false
|
2016-10-18 22:53:26 +00:00
|
|
|
f, tf, codec, ns := cmdtesting.NewAPIFactory()
|
2016-03-31 03:42:57 +00:00
|
|
|
|
|
|
|
tf.Client = &fake.RESTClient{
|
2017-01-24 15:00:24 +00:00
|
|
|
APIRegistry: api.Registry,
|
2016-05-04 10:24:16 +00:00
|
|
|
NegotiatedSerializer: ns,
|
2016-03-31 03:42:57 +00:00
|
|
|
Client: fake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) {
|
|
|
|
m := &MyReq{req}
|
|
|
|
switch {
|
2017-04-20 21:27:09 +00:00
|
|
|
case m.isFor("GET", "/nodes"):
|
|
|
|
return &http.Response{StatusCode: 200, Header: defaultHeader(), Body: objBody(codec, oldNode)}, nil
|
2016-03-31 03:42:57 +00:00
|
|
|
case m.isFor("GET", "/nodes/node-name"):
|
|
|
|
return &http.Response{StatusCode: 200, Header: defaultHeader(), Body: objBody(codec, oldNode)}, nil
|
2016-12-09 02:43:36 +00:00
|
|
|
case m.isFor("PATCH", "/nodes/node-name"):
|
|
|
|
tainted = true
|
|
|
|
data, err := ioutil.ReadAll(req.Body)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("%s: unexpected error: %v", test.description, err)
|
|
|
|
}
|
|
|
|
defer req.Body.Close()
|
|
|
|
|
|
|
|
// apply the patch
|
|
|
|
oldJSON, err := runtime.Encode(codec, oldNode)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("%s: unexpected error: %v", test.description, err)
|
|
|
|
}
|
|
|
|
appliedPatch, err := strategicpatch.StrategicMergePatch(oldJSON, data, &v1.Node{})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("%s: unexpected error: %v", test.description, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// decode the patch
|
|
|
|
if err := runtime.DecodeInto(codec, appliedPatch, new_node); err != nil {
|
|
|
|
t.Fatalf("%s: unexpected error: %v", test.description, err)
|
|
|
|
}
|
2017-02-07 14:08:45 +00:00
|
|
|
if !equalTaints(expectNewNode.Spec.Taints, new_node.Spec.Taints) {
|
|
|
|
t.Fatalf("%s: expected:\n%v\nsaw:\n%v\n", test.description, expectNewNode.Spec.Taints, new_node.Spec.Taints)
|
2016-12-09 02:43:36 +00:00
|
|
|
}
|
|
|
|
return &http.Response{StatusCode: 200, Header: defaultHeader(), Body: objBody(codec, new_node)}, nil
|
|
|
|
case m.isFor("PUT", "/nodes/node-name"):
|
2016-03-31 03:42:57 +00:00
|
|
|
tainted = true
|
|
|
|
data, err := ioutil.ReadAll(req.Body)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("%s: unexpected error: %v", test.description, err)
|
|
|
|
}
|
|
|
|
defer req.Body.Close()
|
|
|
|
if err := runtime.DecodeInto(codec, data, new_node); err != nil {
|
|
|
|
t.Fatalf("%s: unexpected error: %v", test.description, err)
|
|
|
|
}
|
2017-02-07 14:08:45 +00:00
|
|
|
if !equalTaints(expectNewNode.Spec.Taints, new_node.Spec.Taints) {
|
|
|
|
t.Fatalf("%s: expected:\n%v\nsaw:\n%v\n", test.description, expectNewNode.Spec.Taints, new_node.Spec.Taints)
|
2016-03-31 03:42:57 +00:00
|
|
|
}
|
|
|
|
return &http.Response{StatusCode: 200, Header: defaultHeader(), Body: objBody(codec, new_node)}, nil
|
|
|
|
default:
|
|
|
|
t.Fatalf("%s: unexpected request: %v %#v\n%#v", test.description, req.Method, req.URL, req)
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
}
|
|
|
|
tf.ClientConfig = defaultClientConfig()
|
|
|
|
|
|
|
|
buf := bytes.NewBuffer([]byte{})
|
|
|
|
cmd := NewCmdTaint(f, buf)
|
|
|
|
|
|
|
|
saw_fatal := false
|
|
|
|
func() {
|
|
|
|
defer func() {
|
|
|
|
// Recover from the panic below.
|
|
|
|
_ = recover()
|
|
|
|
// Restore cmdutil behavior
|
|
|
|
cmdutil.DefaultBehaviorOnFatal()
|
|
|
|
}()
|
2016-08-08 18:24:01 +00:00
|
|
|
cmdutil.BehaviorOnFatal(func(e string, code int) { saw_fatal = true; panic(e) })
|
2016-03-31 03:42:57 +00:00
|
|
|
cmd.SetArgs(test.args)
|
|
|
|
cmd.Execute()
|
|
|
|
}()
|
|
|
|
|
|
|
|
if test.expectFatal {
|
|
|
|
if !saw_fatal {
|
|
|
|
t.Fatalf("%s: unexpected non-error", test.description)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if test.expectTaint {
|
|
|
|
if !tainted {
|
|
|
|
t.Fatalf("%s: node not tainted", test.description)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !test.expectTaint {
|
|
|
|
if tainted {
|
|
|
|
t.Fatalf("%s: unexpected taint", test.description)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-04-20 21:27:09 +00:00
|
|
|
|
|
|
|
func TestValidateFlags(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
taintOpts TaintOptions
|
|
|
|
description string
|
|
|
|
expectFatal bool
|
|
|
|
}{
|
|
|
|
|
|
|
|
{
|
|
|
|
taintOpts: TaintOptions{selector: "myLabel=X", all: false},
|
|
|
|
description: "With Selector and without All flag",
|
|
|
|
expectFatal: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
taintOpts: TaintOptions{selector: "", all: true},
|
|
|
|
description: "Without selector and All flag",
|
|
|
|
expectFatal: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
taintOpts: TaintOptions{selector: "myLabel=X", all: true},
|
|
|
|
description: "With Selector and with All flag",
|
|
|
|
expectFatal: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
taintOpts: TaintOptions{selector: "", all: false, resources: []string{"node"}},
|
|
|
|
description: "Without Selector and All flags and if node name is not provided",
|
|
|
|
expectFatal: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
taintOpts: TaintOptions{selector: "", all: false, resources: []string{"node", "node-name"}},
|
|
|
|
description: "Without Selector and ALL flags and if node name is provided",
|
|
|
|
expectFatal: false,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, test := range tests {
|
|
|
|
sawFatal := false
|
|
|
|
err := test.taintOpts.validateFlags()
|
|
|
|
if err != nil {
|
|
|
|
sawFatal = true
|
|
|
|
}
|
|
|
|
if test.expectFatal {
|
|
|
|
if !sawFatal {
|
|
|
|
t.Fatalf("%s expected not to fail", test.description)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|