|
| 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 node |
| 18 | + |
| 19 | +import ( |
| 20 | + "fmt" |
| 21 | + |
| 22 | + v1 "k8s.io/api/core/v1" |
| 23 | + "k8s.io/api/node/v1beta1" |
| 24 | + apierrs "k8s.io/apimachinery/pkg/api/errors" |
| 25 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 26 | + runtimeclasstest "k8s.io/kubernetes/pkg/kubelet/runtimeclass/testing" |
| 27 | + "k8s.io/kubernetes/test/e2e/framework" |
| 28 | + e2epod "k8s.io/kubernetes/test/e2e/framework/pod" |
| 29 | + "k8s.io/kubernetes/test/e2e/scheduling" |
| 30 | + imageutils "k8s.io/kubernetes/test/utils/image" |
| 31 | + utilpointer "k8s.io/utils/pointer" |
| 32 | + |
| 33 | + "github.com/onsi/ginkgo" |
| 34 | + "github.com/onsi/gomega" |
| 35 | +) |
| 36 | + |
| 37 | +var _ = ginkgo.Describe("[sig-node] RuntimeClass", func() { |
| 38 | + f := framework.NewDefaultFramework("runtimeclass") |
| 39 | + |
| 40 | + ginkgo.It("should reject a Pod requesting a RuntimeClass with conflicting node selector", func() { |
| 41 | + scheduling := &v1beta1.Scheduling{ |
| 42 | + NodeSelector: map[string]string{ |
| 43 | + "foo": "conflict", |
| 44 | + }, |
| 45 | + } |
| 46 | + |
| 47 | + runtimeClass := newRuntimeClass(f.Namespace.Name, "conflict-runtimeclass") |
| 48 | + runtimeClass.Scheduling = scheduling |
| 49 | + rc, err := f.ClientSet.NodeV1beta1().RuntimeClasses().Create(runtimeClass) |
| 50 | + framework.ExpectNoError(err, "failed to create RuntimeClass resource") |
| 51 | + |
| 52 | + pod := newRuntimeClassPod(rc.GetName()) |
| 53 | + pod.Spec.NodeSelector = map[string]string{ |
| 54 | + "foo": "bar", |
| 55 | + } |
| 56 | + _, err = f.ClientSet.CoreV1().Pods(f.Namespace.Name).Create(pod) |
| 57 | + framework.ExpectError(err, "should be forbidden") |
| 58 | + gomega.Expect(apierrs.IsForbidden(err)).To(gomega.BeTrue(), "should be forbidden error") |
| 59 | + }) |
| 60 | + |
| 61 | + ginkgo.It("should run a Pod requesting a RuntimeClass with scheduling [NodeFeature:RuntimeHandler] ", func() { |
| 62 | + nodeName := scheduling.GetNodeThatCanRunPod(f) |
| 63 | + nodeSelector := map[string]string{ |
| 64 | + "foo": "bar", |
| 65 | + "fizz": "buzz", |
| 66 | + } |
| 67 | + tolerations := []v1.Toleration{ |
| 68 | + { |
| 69 | + Key: "foo", |
| 70 | + Operator: v1.TolerationOpEqual, |
| 71 | + Value: "bar", |
| 72 | + Effect: v1.TaintEffectNoSchedule, |
| 73 | + }, |
| 74 | + } |
| 75 | + scheduling := &v1beta1.Scheduling{ |
| 76 | + NodeSelector: nodeSelector, |
| 77 | + Tolerations: tolerations, |
| 78 | + } |
| 79 | + |
| 80 | + ginkgo.By("Trying to apply a label on the found node.") |
| 81 | + for key, value := range nodeSelector { |
| 82 | + framework.AddOrUpdateLabelOnNode(f.ClientSet, nodeName, key, value) |
| 83 | + framework.ExpectNodeHasLabel(f.ClientSet, nodeName, key, value) |
| 84 | + defer framework.RemoveLabelOffNode(f.ClientSet, nodeName, key) |
| 85 | + } |
| 86 | + |
| 87 | + ginkgo.By("Trying to apply taint on the found node.") |
| 88 | + taint := v1.Taint{ |
| 89 | + Key: "foo", |
| 90 | + Value: "bar", |
| 91 | + Effect: v1.TaintEffectNoSchedule, |
| 92 | + } |
| 93 | + framework.AddOrUpdateTaintOnNode(f.ClientSet, nodeName, taint) |
| 94 | + framework.ExpectNodeHasTaint(f.ClientSet, nodeName, &taint) |
| 95 | + defer framework.RemoveTaintOffNode(f.ClientSet, nodeName, taint) |
| 96 | + |
| 97 | + ginkgo.By("Trying to create runtimeclass and pod") |
| 98 | + runtimeClass := newRuntimeClass(f.Namespace.Name, "non-conflict-runtimeclass") |
| 99 | + runtimeClass.Scheduling = scheduling |
| 100 | + rc, err := f.ClientSet.NodeV1beta1().RuntimeClasses().Create(runtimeClass) |
| 101 | + framework.ExpectNoError(err, "failed to create RuntimeClass resource") |
| 102 | + |
| 103 | + pod := newRuntimeClassPod(rc.GetName()) |
| 104 | + pod.Spec.NodeSelector = map[string]string{ |
| 105 | + "foo": "bar", |
| 106 | + } |
| 107 | + pod = f.PodClient().Create(pod) |
| 108 | + |
| 109 | + framework.ExpectNoError(e2epod.WaitForPodNotPending(f.ClientSet, f.Namespace.Name, pod.Name)) |
| 110 | + |
| 111 | + // check that pod got scheduled on specified node. |
| 112 | + scheduledPod, err := f.ClientSet.CoreV1().Pods(f.Namespace.Name).Get(pod.Name, metav1.GetOptions{}) |
| 113 | + framework.ExpectNoError(err) |
| 114 | + framework.ExpectEqual(nodeName, scheduledPod.Spec.NodeName) |
| 115 | + framework.ExpectEqual(nodeSelector, pod.Spec.NodeSelector) |
| 116 | + gomega.Expect(pod.Spec.Tolerations).To(gomega.ContainElement(tolerations[0])) |
| 117 | + }) |
| 118 | +}) |
| 119 | + |
| 120 | +// newRuntimeClass returns a test runtime class. |
| 121 | +func newRuntimeClass(namespace, name string) *v1beta1.RuntimeClass { |
| 122 | + uniqueName := fmt.Sprintf("%s-%s", namespace, name) |
| 123 | + return runtimeclasstest.NewRuntimeClass(uniqueName, framework.PreconfiguredRuntimeClassHandler()) |
| 124 | +} |
| 125 | + |
| 126 | +// newRuntimeClassPod returns a test pod with the given runtimeClassName. |
| 127 | +func newRuntimeClassPod(runtimeClassName string) *v1.Pod { |
| 128 | + return &v1.Pod{ |
| 129 | + ObjectMeta: metav1.ObjectMeta{ |
| 130 | + GenerateName: fmt.Sprintf("test-runtimeclass-%s-", runtimeClassName), |
| 131 | + }, |
| 132 | + Spec: v1.PodSpec{ |
| 133 | + RuntimeClassName: &runtimeClassName, |
| 134 | + Containers: []v1.Container{{ |
| 135 | + Name: "test", |
| 136 | + Image: imageutils.GetE2EImage(imageutils.BusyBox), |
| 137 | + Command: []string{"true"}, |
| 138 | + }}, |
| 139 | + RestartPolicy: v1.RestartPolicyNever, |
| 140 | + AutomountServiceAccountToken: utilpointer.BoolPtr(false), |
| 141 | + }, |
| 142 | + } |
| 143 | +} |
0 commit comments