k8s.io/kubernetes@v1.31.0-alpha.0.0.20240520171757-56147500dadc/test/e2e/upgrades/apps/daemonsets.go (about) 1 /* 2 Copyright 2017 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 apps 18 19 import ( 20 "context" 21 22 "github.com/onsi/ginkgo/v2" 23 24 appsv1 "k8s.io/api/apps/v1" 25 v1 "k8s.io/api/core/v1" 26 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 27 "k8s.io/apimachinery/pkg/util/wait" 28 "k8s.io/kubernetes/test/e2e/framework" 29 e2edaemonset "k8s.io/kubernetes/test/e2e/framework/daemonset" 30 "k8s.io/kubernetes/test/e2e/upgrades" 31 imageutils "k8s.io/kubernetes/test/utils/image" 32 ) 33 34 // DaemonSetUpgradeTest tests that a DaemonSet is running before and after 35 // a cluster upgrade. 36 type DaemonSetUpgradeTest struct { 37 daemonSet *appsv1.DaemonSet 38 } 39 40 // Name returns the tracking name of the test. 41 func (DaemonSetUpgradeTest) Name() string { return "[sig-apps] daemonset-upgrade" } 42 43 // Setup creates a DaemonSet and verifies that it's running 44 func (t *DaemonSetUpgradeTest) Setup(ctx context.Context, f *framework.Framework) { 45 daemonSetName := "ds1" 46 labelSet := map[string]string{"ds-name": daemonSetName} 47 image := imageutils.GetE2EImage(imageutils.Agnhost) 48 49 ns := f.Namespace 50 51 t.daemonSet = e2edaemonset.NewDaemonSet(daemonSetName, image, labelSet, nil, nil, []v1.ContainerPort{{ContainerPort: 9376}}, "serve-hostname") 52 t.daemonSet.Spec.Template.Spec.Tolerations = []v1.Toleration{ 53 {Operator: v1.TolerationOpExists}, 54 } 55 56 ginkgo.By("Creating a DaemonSet") 57 var err error 58 if t.daemonSet, err = f.ClientSet.AppsV1().DaemonSets(ns.Name).Create(ctx, t.daemonSet, metav1.CreateOptions{}); err != nil { 59 framework.Failf("unable to create test DaemonSet %s: %v", t.daemonSet.Name, err) 60 } 61 62 ginkgo.By("Waiting for DaemonSet pods to become ready") 63 err = wait.PollWithContext(ctx, framework.Poll, framework.PodStartTimeout, func(ctx context.Context) (bool, error) { 64 return e2edaemonset.CheckRunningOnAllNodes(ctx, f, t.daemonSet) 65 }) 66 framework.ExpectNoError(err) 67 68 ginkgo.By("Validating the DaemonSet after creation") 69 t.validateRunningDaemonSet(ctx, f) 70 } 71 72 // Test waits until the upgrade has completed and then verifies that the DaemonSet 73 // is still running 74 func (t *DaemonSetUpgradeTest) Test(ctx context.Context, f *framework.Framework, done <-chan struct{}, upgrade upgrades.UpgradeType) { 75 ginkgo.By("Waiting for upgrade to complete before re-validating DaemonSet") 76 <-done 77 78 ginkgo.By("validating the DaemonSet is still running after upgrade") 79 t.validateRunningDaemonSet(ctx, f) 80 } 81 82 // Teardown cleans up any remaining resources. 83 func (t *DaemonSetUpgradeTest) Teardown(ctx context.Context, f *framework.Framework) { 84 // rely on the namespace deletion to clean up everything 85 } 86 87 func (t *DaemonSetUpgradeTest) validateRunningDaemonSet(ctx context.Context, f *framework.Framework) { 88 ginkgo.By("confirming the DaemonSet pods are running on all expected nodes") 89 res, err := e2edaemonset.CheckRunningOnAllNodes(ctx, f, t.daemonSet) 90 framework.ExpectNoError(err) 91 if !res { 92 framework.Failf("expected DaemonSet pod to be running on all nodes, it was not") 93 } 94 95 // DaemonSet resource itself should be good 96 ginkgo.By("confirming the DaemonSet resource is in a good state") 97 err = e2edaemonset.CheckDaemonStatus(ctx, f, t.daemonSet.Name) 98 framework.ExpectNoError(err) 99 }