From c02e05245ba817d885f2fe086f384f77b602faaf Mon Sep 17 00:00:00 2001 From: Markus Lehtonen Date: Fri, 8 Dec 2023 15:12:54 +0200 Subject: [PATCH] test/e2e: test NodeFeature owner reference Add tests for verifying that the automatic garbage-colletion of NodeFeature objects created by nfd-worker works as expected. --- test/e2e/node_feature_discovery_test.go | 26 ++++++++++++++++++++++++- test/e2e/utils/pod/pod.go | 16 +++++++++++++++ 2 files changed, 41 insertions(+), 1 deletion(-) diff --git a/test/e2e/node_feature_discovery_test.go b/test/e2e/node_feature_discovery_test.go index 5493acd75..4347d6765 100644 --- a/test/e2e/node_feature_discovery_test.go +++ b/test/e2e/node_feature_discovery_test.go @@ -30,6 +30,7 @@ import ( corev1 "k8s.io/api/core/v1" apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1" extclient "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset" + apierrors "k8s.io/apimachinery/pkg/api/errors" resourcev1 "k8s.io/apimachinery/pkg/api/resource" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/types" @@ -171,7 +172,12 @@ func cleanupCRs(ctx context.Context, cli *nfdclient.Clientset, namespace string) By("Deleting NodeFeature objects from namespace " + namespace) for _, nf := range nfs.Items { err = cli.NfdV1alpha1().NodeFeatures(namespace).Delete(ctx, nf.Name, metav1.DeleteOptions{}) - Expect(err).NotTo(HaveOccurred()) + Expect(func() error { + if apierrors.IsNotFound(err) { + return nil + } + return err + }()).NotTo(HaveOccurred()) } } } @@ -308,6 +314,12 @@ var _ = SIGDescribe("NFD master and worker", func() { By("Deleting the node-feature-discovery worker pod") err = f.ClientSet.CoreV1().Pods(f.Namespace.Name).Delete(ctx, workerPod.Name, metav1.DeleteOptions{}) Expect(err).NotTo(HaveOccurred()) + + if useNodeFeatureApi { + By("Verify that labels from nfd-worker are garbage-collected") + delete(expectedLabels, workerPod.Spec.NodeName) + eventuallyNonControlPlaneNodes(ctx, f.ClientSet).WithTimeout(1 * time.Minute).Should(MatchLabels(expectedLabels, nodes)) + } }) }) @@ -490,6 +502,12 @@ var _ = SIGDescribe("NFD master and worker", func() { By("Deleting nfd-worker daemonset") err = f.ClientSet.AppsV1().DaemonSets(f.Namespace.Name).Delete(ctx, workerDS.Name, metav1.DeleteOptions{}) Expect(err).NotTo(HaveOccurred()) + + if useNodeFeatureApi { + By("Verify that labels from nfd-worker are garbage-collected") + delete(expectedLabels, targetNodeName) + eventuallyNonControlPlaneNodes(ctx, f.ClientSet).WithTimeout(1 * time.Minute).Should(MatchLabels(expectedLabels, nodes)) + } }) }) @@ -836,6 +854,12 @@ core: By("Deleting nfd-worker daemonset") err = f.ClientSet.AppsV1().DaemonSets(f.Namespace.Name).Delete(ctx, workerDS.Name, metav1.DeleteOptions{}) Expect(err).NotTo(HaveOccurred()) + + By("Verify that labels from nfd-worker are garbage-collected") + expectedLabels = map[string]k8sLabels{ + "*": {}, + } + eventuallyNonControlPlaneNodes(ctx, f.ClientSet).WithTimeout(1 * time.Minute).Should(MatchLabels(expectedLabels, nodes)) }) }) diff --git a/test/e2e/utils/pod/pod.go b/test/e2e/utils/pod/pod.go index 93b851095..a6a7108b8 100644 --- a/test/e2e/utils/pod/pod.go +++ b/test/e2e/utils/pod/pod.go @@ -270,6 +270,22 @@ func nfdWorkerSpec(opts ...SpecOption) *corev1.PodSpec { }, }, }, + { + Name: "POD_NAME", + ValueFrom: &corev1.EnvVarSource{ + FieldRef: &corev1.ObjectFieldSelector{ + FieldPath: "metadata.name", + }, + }, + }, + { + Name: "POD_UID", + ValueFrom: &corev1.EnvVarSource{ + FieldRef: &corev1.ObjectFieldSelector{ + FieldPath: "metadata.uid", + }, + }, + }, }, SecurityContext: &corev1.SecurityContext{ Capabilities: &corev1.Capabilities{