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