diff --git a/tests/e2e/common/kubernetes.go b/tests/e2e/common/kubernetes.go index 84d3e8aae..2631042d1 100644 --- a/tests/e2e/common/kubernetes.go +++ b/tests/e2e/common/kubernetes.go @@ -26,6 +26,7 @@ import ( . "github.com/onsi/gomega" corev1 "k8s.io/api/core/v1" + "k8s.io/apimachinery/pkg/api/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" clientset "k8s.io/client-go/kubernetes" nfdv1alpha1 "sigs.k8s.io/node-feature-discovery/pkg/apis/nfd/v1alpha1" @@ -164,26 +165,50 @@ func CleanupNode(ctx context.Context, cs clientset.Interface) { } } -func CleanupNodeFeatureRules(ctx context.Context, cli *nfdclient.Clientset, namespace string) { - // Drop NodeFeatureRule objects - nfrs, err := cli.NfdV1alpha1().NodeFeatureRules().List(ctx, metav1.ListOptions{}) - Expect(err).NotTo(HaveOccurred()) - - if len(nfrs.Items) != 0 { - By("Deleting NodeFeatureRule objects from the cluster") - for _, nfr := range nfrs.Items { - err = cli.NfdV1alpha1().NodeFeatureRules().Delete(ctx, nfr.Name, metav1.DeleteOptions{}) - Expect(err).NotTo(HaveOccurred()) - } - } +func CleanupNFDObjects(ctx context.Context, cli *nfdclient.Clientset, namespace string) { + cleanupNodeFeatureRules(ctx, cli) + cleanupNodeFeatures(ctx, cli, namespace) +} +// cleanupNodeFeatures deletes all NodeFeature objects in the given namespace +func cleanupNodeFeatures(ctx context.Context, cli *nfdclient.Clientset, namespace string) { nfs, err := cli.NfdV1alpha1().NodeFeatures(namespace).List(ctx, metav1.ListOptions{}) + if errors.IsNotFound(err) { + // Omitted error, nothing to do. + return + } Expect(err).NotTo(HaveOccurred()) if len(nfs.Items) != 0 { By("Deleting NodeFeature objects from namespace " + namespace) for _, nf := range nfs.Items { err = cli.NfdV1alpha1().NodeFeatures(namespace).Delete(ctx, nf.Name, metav1.DeleteOptions{}) + if errors.IsNotFound(err) { + // Omitted error + continue + } + Expect(err).NotTo(HaveOccurred()) + } + } +} + +// cleanupNodeFeatureRules deletes all NodeFeatureRule objects +func cleanupNodeFeatureRules(ctx context.Context, cli *nfdclient.Clientset) { + nfrs, err := cli.NfdV1alpha1().NodeFeatureRules().List(ctx, metav1.ListOptions{}) + if errors.IsNotFound(err) { + // Omitted error, nothing to do. + return + } + Expect(err).NotTo(HaveOccurred()) + + if len(nfrs.Items) != 0 { + By("Deleting NodeFeatureRule objects from the cluster") + for _, nfr := range nfrs.Items { + err = cli.NfdV1alpha1().NodeFeatureRules().Delete(ctx, nfr.Name, metav1.DeleteOptions{}) + if errors.IsNotFound(err) { + // Omitted error + continue + } Expect(err).NotTo(HaveOccurred()) } } diff --git a/tests/e2e/gpu-feature-discovery_test.go b/tests/e2e/gpu-feature-discovery_test.go index 549913c93..c17d14dc6 100644 --- a/tests/e2e/gpu-feature-discovery_test.go +++ b/tests/e2e/gpu-feature-discovery_test.go @@ -143,9 +143,9 @@ var _ = NVDescribe("GPU Feature Discovery", func() { // Delete Helm release err := helmClient.UninstallReleaseByName(helmReleaseName) Expect(err).NotTo(HaveOccurred()) - // cleanup node + // Cleanup node common.CleanupNode(ctx, f.ClientSet) - common.CleanupNodeFeatureRules(ctx, nfdClient, f.Namespace.Name) + common.CleanupNFDObjects(ctx, nfdClient, f.Namespace.Name) }) AfterAll(func(ctx context.Context) {