|
| 1 | +/* |
| 2 | +Copyright 2024 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 | + "context" |
| 21 | + |
| 22 | + "github.com/onsi/ginkgo/v2" |
| 23 | + "github.com/onsi/gomega" |
| 24 | + v1 "k8s.io/api/core/v1" |
| 25 | + "k8s.io/apimachinery/pkg/api/resource" |
| 26 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 27 | + "k8s.io/kubernetes/test/e2e/framework" |
| 28 | + e2enode "k8s.io/kubernetes/test/e2e/framework/node" |
| 29 | + e2epod "k8s.io/kubernetes/test/e2e/framework/pod" |
| 30 | + imageutils "k8s.io/kubernetes/test/utils/image" |
| 31 | + admissionapi "k8s.io/pod-security-admission/api" |
| 32 | +) |
| 33 | + |
| 34 | +var _ = SIGDescribe("PodRejectionStatus", func() { |
| 35 | + f := framework.NewDefaultFramework("pod-rejection-status") |
| 36 | + f.NamespacePodSecurityLevel = admissionapi.LevelBaseline |
| 37 | + ginkgo.Context("Kubelet", func() { |
| 38 | + ginkgo.It("should reject pod when the node didn't have enough resource", func(ctx context.Context) { |
| 39 | + node, err := e2enode.GetRandomReadySchedulableNode(ctx, f.ClientSet) |
| 40 | + framework.ExpectNoError(err, "Failed to get a ready schedulable node") |
| 41 | + |
| 42 | + // Create a pod that requests more CPU than the node has |
| 43 | + pod := &v1.Pod{ |
| 44 | + ObjectMeta: metav1.ObjectMeta{ |
| 45 | + Name: "pod-out-of-cpu", |
| 46 | + Namespace: f.Namespace.Name, |
| 47 | + }, |
| 48 | + Spec: v1.PodSpec{ |
| 49 | + Containers: []v1.Container{ |
| 50 | + { |
| 51 | + Name: "pod-out-of-cpu", |
| 52 | + Image: imageutils.GetPauseImageName(), |
| 53 | + Resources: v1.ResourceRequirements{ |
| 54 | + Requests: v1.ResourceList{ |
| 55 | + v1.ResourceCPU: resource.MustParse("1000000000000"), // requests more CPU than any node has |
| 56 | + }, |
| 57 | + }, |
| 58 | + }, |
| 59 | + }, |
| 60 | + }, |
| 61 | + } |
| 62 | + |
| 63 | + pod, err = f.ClientSet.CoreV1().Pods(f.Namespace.Name).Create(ctx, pod, metav1.CreateOptions{}) |
| 64 | + framework.ExpectNoError(err) |
| 65 | + |
| 66 | + // Wait for the scheduler to update the pod status |
| 67 | + err = e2epod.WaitForPodNameUnschedulableInNamespace(ctx, f.ClientSet, pod.Name, pod.Namespace) |
| 68 | + framework.ExpectNoError(err) |
| 69 | + |
| 70 | + // Fetch the pod to get the latest status which should be last one observed by the scheduler |
| 71 | + // before it rejected the pod |
| 72 | + pod, err = f.ClientSet.CoreV1().Pods(pod.Namespace).Get(ctx, pod.Name, metav1.GetOptions{}) |
| 73 | + framework.ExpectNoError(err) |
| 74 | + |
| 75 | + // force assign the Pod to a node in order to get rejection status later |
| 76 | + binding := &v1.Binding{ |
| 77 | + ObjectMeta: metav1.ObjectMeta{ |
| 78 | + Name: pod.Name, |
| 79 | + Namespace: pod.Namespace, |
| 80 | + UID: pod.UID, |
| 81 | + }, |
| 82 | + Target: v1.ObjectReference{ |
| 83 | + Kind: "Node", |
| 84 | + Name: node.Name, |
| 85 | + }, |
| 86 | + } |
| 87 | + err = f.ClientSet.CoreV1().Pods(pod.Namespace).Bind(ctx, binding, metav1.CreateOptions{}) |
| 88 | + framework.ExpectNoError(err) |
| 89 | + |
| 90 | + // kubelet has rejected the pod |
| 91 | + err = e2epod.WaitForPodFailedReason(ctx, f.ClientSet, pod, "OutOfcpu", f.Timeouts.PodStartShort) |
| 92 | + framework.ExpectNoError(err) |
| 93 | + |
| 94 | + // fetch the reject Pod and compare the status |
| 95 | + gotPod, err := f.ClientSet.CoreV1().Pods(pod.Namespace).Get(ctx, pod.Name, metav1.GetOptions{}) |
| 96 | + framework.ExpectNoError(err) |
| 97 | + |
| 98 | + // This detects if there are any new fields in Status that were dropped by the pod rejection. |
| 99 | + // These new fields either should be kept by kubelet's admission or added explicitly in the list of fields that are having a different value or must be cleared. |
| 100 | + expectedStatus := pod.Status.DeepCopy() |
| 101 | + expectedStatus.Phase = gotPod.Status.Phase |
| 102 | + expectedStatus.Conditions = nil |
| 103 | + expectedStatus.Message = gotPod.Status.Message |
| 104 | + expectedStatus.Reason = gotPod.Status.Reason |
| 105 | + expectedStatus.StartTime = gotPod.Status.StartTime |
| 106 | + // expectedStatus.QOSClass keep it as is |
| 107 | + gomega.Expect(gotPod.Status).To(gomega.Equal(*expectedStatus)) |
| 108 | + }) |
| 109 | + }) |
| 110 | +}) |
0 commit comments