runtimeclass.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /*
  2. Copyright 2019 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package node
  14. import (
  15. "context"
  16. "fmt"
  17. v1 "k8s.io/api/core/v1"
  18. nodev1beta1 "k8s.io/api/node/v1beta1"
  19. apierrors "k8s.io/apimachinery/pkg/api/errors"
  20. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  21. runtimeclasstest "k8s.io/kubernetes/pkg/kubelet/runtimeclass/testing"
  22. "k8s.io/kubernetes/test/e2e/framework"
  23. e2enode "k8s.io/kubernetes/test/e2e/framework/node"
  24. e2epod "k8s.io/kubernetes/test/e2e/framework/pod"
  25. "k8s.io/kubernetes/test/e2e/scheduling"
  26. "github.com/onsi/ginkgo"
  27. "github.com/onsi/gomega"
  28. )
  29. var _ = ginkgo.Describe("[sig-node] RuntimeClass", func() {
  30. f := framework.NewDefaultFramework("runtimeclass")
  31. ginkgo.It("should reject a Pod requesting a RuntimeClass with conflicting node selector", func() {
  32. scheduling := &nodev1beta1.Scheduling{
  33. NodeSelector: map[string]string{
  34. "foo": "conflict",
  35. },
  36. }
  37. runtimeClass := newRuntimeClass(f.Namespace.Name, "conflict-runtimeclass", framework.TestContext.ContainerRuntime)
  38. runtimeClass.Scheduling = scheduling
  39. rc, err := f.ClientSet.NodeV1beta1().RuntimeClasses().Create(context.TODO(), runtimeClass, metav1.CreateOptions{})
  40. framework.ExpectNoError(err, "failed to create RuntimeClass resource")
  41. pod := e2enode.NewRuntimeClassPod(rc.GetName())
  42. pod.Spec.NodeSelector = map[string]string{
  43. "foo": "bar",
  44. }
  45. _, err = f.ClientSet.CoreV1().Pods(f.Namespace.Name).Create(context.TODO(), pod, metav1.CreateOptions{})
  46. framework.ExpectError(err, "should be forbidden")
  47. framework.ExpectEqual(apierrors.IsForbidden(err), true, "should be forbidden error")
  48. })
  49. ginkgo.It("should run a Pod requesting a RuntimeClass with scheduling [NodeFeature:RuntimeHandler] [Disruptive] ", func() {
  50. nodeName := scheduling.GetNodeThatCanRunPod(f)
  51. nodeSelector := map[string]string{
  52. "foo": "bar",
  53. "fizz": "buzz",
  54. }
  55. tolerations := []v1.Toleration{
  56. {
  57. Key: "foo",
  58. Operator: v1.TolerationOpEqual,
  59. Value: "bar",
  60. Effect: v1.TaintEffectNoSchedule,
  61. },
  62. }
  63. scheduling := &nodev1beta1.Scheduling{
  64. NodeSelector: nodeSelector,
  65. Tolerations: tolerations,
  66. }
  67. ginkgo.By("Trying to apply a label on the found node.")
  68. for key, value := range nodeSelector {
  69. framework.AddOrUpdateLabelOnNode(f.ClientSet, nodeName, key, value)
  70. framework.ExpectNodeHasLabel(f.ClientSet, nodeName, key, value)
  71. defer framework.RemoveLabelOffNode(f.ClientSet, nodeName, key)
  72. }
  73. ginkgo.By("Trying to apply taint on the found node.")
  74. taint := v1.Taint{
  75. Key: "foo",
  76. Value: "bar",
  77. Effect: v1.TaintEffectNoSchedule,
  78. }
  79. framework.AddOrUpdateTaintOnNode(f.ClientSet, nodeName, taint)
  80. framework.ExpectNodeHasTaint(f.ClientSet, nodeName, &taint)
  81. defer framework.RemoveTaintOffNode(f.ClientSet, nodeName, taint)
  82. ginkgo.By("Trying to create runtimeclass and pod")
  83. runtimeClass := newRuntimeClass(f.Namespace.Name, "non-conflict-runtimeclass", framework.TestContext.ContainerRuntime)
  84. runtimeClass.Scheduling = scheduling
  85. rc, err := f.ClientSet.NodeV1beta1().RuntimeClasses().Create(context.TODO(), runtimeClass, metav1.CreateOptions{})
  86. framework.ExpectNoError(err, "failed to create RuntimeClass resource")
  87. pod := e2enode.NewRuntimeClassPod(rc.GetName())
  88. pod.Spec.NodeSelector = map[string]string{
  89. "foo": "bar",
  90. }
  91. pod = f.PodClient().Create(pod)
  92. framework.ExpectNoError(e2epod.WaitForPodNotPending(f.ClientSet, f.Namespace.Name, pod.Name))
  93. // check that pod got scheduled on specified node.
  94. scheduledPod, err := f.ClientSet.CoreV1().Pods(f.Namespace.Name).Get(context.TODO(), pod.Name, metav1.GetOptions{})
  95. framework.ExpectNoError(err)
  96. framework.ExpectEqual(nodeName, scheduledPod.Spec.NodeName)
  97. framework.ExpectEqual(nodeSelector, pod.Spec.NodeSelector)
  98. gomega.Expect(pod.Spec.Tolerations).To(gomega.ContainElement(tolerations[0]))
  99. })
  100. })
  101. // newRuntimeClass returns a test runtime class.
  102. func newRuntimeClass(namespace, name, handler string) *nodev1beta1.RuntimeClass {
  103. uniqueName := fmt.Sprintf("%s-%s", namespace, name)
  104. return runtimeclasstest.NewRuntimeClass(uniqueName, e2enode.PreconfiguredRuntimeClassHandler(handler))
  105. }