runtimeclass.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /*
  2. Copyright 2018 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 common
  14. import (
  15. "context"
  16. "fmt"
  17. "time"
  18. v1 "k8s.io/api/core/v1"
  19. apierrors "k8s.io/apimachinery/pkg/api/errors"
  20. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  21. "k8s.io/apimachinery/pkg/fields"
  22. "k8s.io/apimachinery/pkg/util/wait"
  23. "k8s.io/kubernetes/pkg/kubelet/events"
  24. runtimeclasstest "k8s.io/kubernetes/pkg/kubelet/runtimeclass/testing"
  25. "k8s.io/kubernetes/test/e2e/framework"
  26. e2eevents "k8s.io/kubernetes/test/e2e/framework/events"
  27. e2enode "k8s.io/kubernetes/test/e2e/framework/node"
  28. e2epod "k8s.io/kubernetes/test/e2e/framework/pod"
  29. "github.com/onsi/ginkgo"
  30. )
  31. var _ = ginkgo.Describe("[sig-node] RuntimeClass", func() {
  32. f := framework.NewDefaultFramework("runtimeclass")
  33. ginkgo.It("should reject a Pod requesting a non-existent RuntimeClass", func() {
  34. rcName := f.Namespace.Name + "-nonexistent"
  35. expectPodRejection(f, e2enode.NewRuntimeClassPod(rcName))
  36. })
  37. ginkgo.It("should reject a Pod requesting a RuntimeClass with an unconfigured handler", func() {
  38. handler := f.Namespace.Name + "-handler"
  39. rcName := createRuntimeClass(f, "unconfigured-handler", handler)
  40. pod := f.PodClient().Create(e2enode.NewRuntimeClassPod(rcName))
  41. expectSandboxFailureEvent(f, pod, handler)
  42. })
  43. // This test requires that the PreconfiguredRuntimeHandler has already been set up on nodes.
  44. ginkgo.It("should run a Pod requesting a RuntimeClass with a configured handler [NodeFeature:RuntimeHandler]", func() {
  45. // The built-in docker runtime does not support configuring runtime handlers.
  46. handler := e2enode.PreconfiguredRuntimeClassHandler(framework.TestContext.ContainerRuntime)
  47. rcName := createRuntimeClass(f, "preconfigured-handler", handler)
  48. pod := f.PodClient().Create(e2enode.NewRuntimeClassPod(rcName))
  49. expectPodSuccess(f, pod)
  50. })
  51. ginkgo.It("should reject a Pod requesting a deleted RuntimeClass", func() {
  52. rcName := createRuntimeClass(f, "delete-me", "runc")
  53. rcClient := f.ClientSet.NodeV1beta1().RuntimeClasses()
  54. ginkgo.By("Deleting RuntimeClass "+rcName, func() {
  55. err := rcClient.Delete(context.TODO(), rcName, nil)
  56. framework.ExpectNoError(err, "failed to delete RuntimeClass %s", rcName)
  57. ginkgo.By("Waiting for the RuntimeClass to disappear")
  58. framework.ExpectNoError(wait.PollImmediate(framework.Poll, time.Minute, func() (bool, error) {
  59. _, err := rcClient.Get(context.TODO(), rcName, metav1.GetOptions{})
  60. if apierrors.IsNotFound(err) {
  61. return true, nil // done
  62. }
  63. if err != nil {
  64. return true, err // stop wait with error
  65. }
  66. return false, nil
  67. }))
  68. })
  69. expectPodRejection(f, e2enode.NewRuntimeClassPod(rcName))
  70. })
  71. })
  72. // createRuntimeClass generates a RuntimeClass with the desired handler and a "namespaced" name,
  73. // synchronously creates it, and returns the generated name.
  74. func createRuntimeClass(f *framework.Framework, name, handler string) string {
  75. uniqueName := fmt.Sprintf("%s-%s", f.Namespace.Name, name)
  76. rc := runtimeclasstest.NewRuntimeClass(uniqueName, handler)
  77. rc, err := f.ClientSet.NodeV1beta1().RuntimeClasses().Create(context.TODO(), rc, metav1.CreateOptions{})
  78. framework.ExpectNoError(err, "failed to create RuntimeClass resource")
  79. return rc.GetName()
  80. }
  81. func expectPodRejection(f *framework.Framework, pod *v1.Pod) {
  82. // The Node E2E doesn't run the RuntimeClass admission controller, so we expect the rejection to
  83. // happen by the Kubelet.
  84. if framework.TestContext.NodeE2E {
  85. pod = f.PodClient().Create(pod)
  86. expectSandboxFailureEvent(f, pod, fmt.Sprintf("\"%s\" not found", *pod.Spec.RuntimeClassName))
  87. } else {
  88. _, err := f.ClientSet.CoreV1().Pods(f.Namespace.Name).Create(context.TODO(), pod, metav1.CreateOptions{})
  89. framework.ExpectError(err, "should be forbidden")
  90. framework.ExpectEqual(apierrors.IsForbidden(err), true, "should be forbidden error")
  91. }
  92. }
  93. // expectPodSuccess waits for the given pod to terminate successfully.
  94. func expectPodSuccess(f *framework.Framework, pod *v1.Pod) {
  95. framework.ExpectNoError(e2epod.WaitForPodSuccessInNamespace(
  96. f.ClientSet, pod.Name, f.Namespace.Name))
  97. }
  98. // expectSandboxFailureEvent polls for an event with reason "FailedCreatePodSandBox" containing the
  99. // expected message string.
  100. func expectSandboxFailureEvent(f *framework.Framework, pod *v1.Pod, msg string) {
  101. eventSelector := fields.Set{
  102. "involvedObject.kind": "Pod",
  103. "involvedObject.name": pod.Name,
  104. "involvedObject.namespace": f.Namespace.Name,
  105. "reason": events.FailedCreatePodSandBox,
  106. }.AsSelector().String()
  107. framework.ExpectNoError(e2eevents.WaitTimeoutForEvent(
  108. f.ClientSet, f.Namespace.Name, eventSelector, msg, framework.PodEventTimeout))
  109. }