k8s.io/kubernetes@v1.31.0-alpha.0.0.20240520171757-56147500dadc/test/e2e/node/events.go (about) 1 /* 2 Copyright 2014 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 "strconv" 22 "time" 23 24 v1 "k8s.io/api/core/v1" 25 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 26 "k8s.io/apimachinery/pkg/fields" 27 "k8s.io/apimachinery/pkg/labels" 28 "k8s.io/apimachinery/pkg/util/uuid" 29 "k8s.io/apimachinery/pkg/util/wait" 30 "k8s.io/kubernetes/test/e2e/framework" 31 e2epod "k8s.io/kubernetes/test/e2e/framework/pod" 32 imageutils "k8s.io/kubernetes/test/utils/image" 33 admissionapi "k8s.io/pod-security-admission/api" 34 35 "github.com/onsi/ginkgo/v2" 36 "github.com/onsi/gomega" 37 ) 38 39 var _ = SIGDescribe("Events", func() { 40 f := framework.NewDefaultFramework("events") 41 f.NamespacePodSecurityLevel = admissionapi.LevelBaseline 42 43 ginkgo.It("should be sent by kubelets and the scheduler about pods scheduling and running ", func(ctx context.Context) { 44 45 podClient := f.ClientSet.CoreV1().Pods(f.Namespace.Name) 46 47 ginkgo.By("creating the pod") 48 name := "send-events-" + string(uuid.NewUUID()) 49 value := strconv.Itoa(time.Now().Nanosecond()) 50 pod := &v1.Pod{ 51 ObjectMeta: metav1.ObjectMeta{ 52 Name: name, 53 Labels: map[string]string{ 54 "name": "foo", 55 "time": value, 56 }, 57 }, 58 Spec: v1.PodSpec{ 59 Containers: []v1.Container{ 60 { 61 Name: "p", 62 Image: imageutils.GetE2EImage(imageutils.Agnhost), 63 Args: []string{"serve-hostname"}, 64 Ports: []v1.ContainerPort{{ContainerPort: 80}}, 65 }, 66 }, 67 }, 68 } 69 70 ginkgo.By("submitting the pod to kubernetes") 71 ginkgo.DeferCleanup(func(ctx context.Context) error { 72 ginkgo.By("deleting the pod") 73 return podClient.Delete(ctx, pod.Name, metav1.DeleteOptions{}) 74 }) 75 if _, err := podClient.Create(ctx, pod, metav1.CreateOptions{}); err != nil { 76 framework.Failf("Failed to create pod: %v", err) 77 } 78 79 framework.ExpectNoError(e2epod.WaitForPodNameRunningInNamespace(ctx, f.ClientSet, pod.Name, f.Namespace.Name)) 80 81 ginkgo.By("verifying the pod is in kubernetes") 82 selector := labels.SelectorFromSet(labels.Set(map[string]string{"time": value})) 83 options := metav1.ListOptions{LabelSelector: selector.String()} 84 pods, err := podClient.List(ctx, options) 85 framework.ExpectNoError(err) 86 gomega.Expect(pods.Items).To(gomega.HaveLen(1)) 87 88 ginkgo.By("retrieving the pod") 89 podWithUID, err := podClient.Get(ctx, pod.Name, metav1.GetOptions{}) 90 if err != nil { 91 framework.Failf("Failed to get pod: %v", err) 92 } 93 framework.Logf("%+v\n", podWithUID) 94 var events *v1.EventList 95 // Check for scheduler event about the pod. 96 ginkgo.By("checking for scheduler event about the pod") 97 framework.ExpectNoError(wait.Poll(framework.Poll, 5*time.Minute, func() (bool, error) { 98 selector := fields.Set{ 99 "involvedObject.kind": "Pod", 100 "involvedObject.uid": string(podWithUID.UID), 101 "involvedObject.namespace": f.Namespace.Name, 102 "source": v1.DefaultSchedulerName, 103 }.AsSelector().String() 104 options := metav1.ListOptions{FieldSelector: selector} 105 events, err := f.ClientSet.CoreV1().Events(f.Namespace.Name).List(ctx, options) 106 if err != nil { 107 return false, err 108 } 109 if len(events.Items) > 0 { 110 framework.Logf("Saw scheduler event for our pod.") 111 return true, nil 112 } 113 return false, nil 114 })) 115 // Check for kubelet event about the pod. 116 ginkgo.By("checking for kubelet event about the pod") 117 framework.ExpectNoError(wait.Poll(framework.Poll, 5*time.Minute, func() (bool, error) { 118 selector := fields.Set{ 119 "involvedObject.uid": string(podWithUID.UID), 120 "involvedObject.kind": "Pod", 121 "involvedObject.namespace": f.Namespace.Name, 122 "source": "kubelet", 123 }.AsSelector().String() 124 options := metav1.ListOptions{FieldSelector: selector} 125 events, err = f.ClientSet.CoreV1().Events(f.Namespace.Name).List(ctx, options) 126 if err != nil { 127 return false, err 128 } 129 if len(events.Items) > 0 { 130 framework.Logf("Saw kubelet event for our pod.") 131 return true, nil 132 } 133 return false, nil 134 })) 135 }) 136 })