Skip to content

Commit

Permalink
Add CleanupNFDObjects func
Browse files Browse the repository at this point in the history
Signed-off-by: Carlos Eduardo Arango Gutierrez <[email protected]>
  • Loading branch information
ArangoGutierrez committed May 9, 2024
1 parent 7dc8301 commit 2781b55
Showing 1 changed file with 37 additions and 12 deletions.
49 changes: 37 additions & 12 deletions pkg/kubernetes/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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{})
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
}
gomega.Expect(err).NotTo(gomega.HaveOccurred())

if len(nfrs.Items) != 0 {
ginkgo.By("Deleting NodeFeatureRule objects from the cluster")
for _, nfr := range nfrs.Items {
err = cli.NfdV1alpha1().NodeFeatureRules().Delete(ctx, nfr.Name, metav1.DeleteOptions{})
if len(nfs.Items) != 0 {
ginkgo.By("[Cleanup]\tDeleting 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
}
gomega.Expect(err).NotTo(gomega.HaveOccurred())
}
}
}

nfs, err := cli.NfdV1alpha1().NodeFeatures(namespace).List(ctx, metav1.ListOptions{})
// 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
}
gomega.Expect(err).NotTo(gomega.HaveOccurred())

if len(nfs.Items) != 0 {
ginkgo.By("Deleting NodeFeature objects from namespace " + namespace)
for _, nf := range nfs.Items {
err = cli.NfdV1alpha1().NodeFeatures(namespace).Delete(ctx, nf.Name, metav1.DeleteOptions{})
if len(nfrs.Items) != 0 {
ginkgo.By("[Cleanup]\tDeleting 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
}
gomega.Expect(err).NotTo(gomega.HaveOccurred())
}
}
Expand Down

0 comments on commit 2781b55

Please sign in to comment.