From 12615ed6d64eb9ac536bb9fde0ebea0240e7441a Mon Sep 17 00:00:00 2001 From: Annaraya Narasagond Date: Wed, 14 Aug 2024 15:38:16 +0530 Subject: [PATCH 1/7] Adding cleanup steps with little refactoring Signed-off-by: Annaraya Narasagond Signed-off-by: Annaraya Narasagond Annaraya Narasagond --- e2e/deployers/applicationset.go | 2 +- e2e/deployers/crud.go | 39 +----------------- e2e/deployers/subscription.go | 2 +- e2e/exhaustive_suite_test.go | 5 +++ e2e/main_test.go | 16 +++++--- e2e/util/context.go | 1 + e2e/util/crud.go | 71 +++++++++++++++++++++++++++++++++ 7 files changed, 91 insertions(+), 45 deletions(-) diff --git a/e2e/deployers/applicationset.go b/e2e/deployers/applicationset.go index ef5932182..0166771d1 100644 --- a/e2e/deployers/applicationset.go +++ b/e2e/deployers/applicationset.go @@ -55,7 +55,7 @@ func (a ApplicationSet) Undeploy(w workloads.Workload) error { return err } - err = deletePlacement(name, namespace) + err = util.DeletePlacement(name, namespace) if err != nil { return err } diff --git a/e2e/deployers/crud.go b/e2e/deployers/crud.go index 5ce4180ab..56ea086cc 100644 --- a/e2e/deployers/crud.go +++ b/e2e/deployers/crud.go @@ -104,26 +104,6 @@ func createPlacement(name, namespace string) error { return nil } -func deletePlacement(name, namespace string) error { - placement := &ocmv1b1.Placement{ - ObjectMeta: metav1.ObjectMeta{ - Name: name, - Namespace: namespace, - }, - } - - err := util.Ctx.Hub.CtrlClient.Delete(context.Background(), placement) - if err != nil { - if !errors.IsNotFound(err) { - return err - } - - util.Ctx.Log.Info("placement " + name + " not found") - } - - return nil -} - func createSubscription(s Subscription, w workloads.Workload) error { name := GetCombinedName(s, w) namespace := name @@ -182,25 +162,8 @@ func createSubscription(s Subscription, w workloads.Workload) error { func deleteSubscription(s Subscription, w workloads.Workload) error { name := GetCombinedName(s, w) - namespace := name - - subscription := &subscriptionv1.Subscription{ - ObjectMeta: metav1.ObjectMeta{ - Name: name, - Namespace: namespace, - }, - } - err := util.Ctx.Hub.CtrlClient.Delete(context.Background(), subscription) - if err != nil { - if !errors.IsNotFound(err) { - return err - } - - util.Ctx.Log.Info("subscription " + name + " not found") - } - - return nil + return util.DeleteSubscription(name, name) } func GetCombinedName(d Deployer, w workloads.Workload) string { diff --git a/e2e/deployers/subscription.go b/e2e/deployers/subscription.go index 637724135..809a75776 100644 --- a/e2e/deployers/subscription.go +++ b/e2e/deployers/subscription.go @@ -71,7 +71,7 @@ func (s Subscription) Undeploy(w workloads.Workload) error { return err } - err = deletePlacement(name, namespace) + err = util.DeletePlacement(name, namespace) if err != nil { return err } diff --git a/e2e/exhaustive_suite_test.go b/e2e/exhaustive_suite_test.go index 75929f0bc..5b6eb01f6 100644 --- a/e2e/exhaustive_suite_test.go +++ b/e2e/exhaustive_suite_test.go @@ -4,6 +4,7 @@ package e2e_test import ( + "flag" "fmt" "testing" @@ -17,6 +18,10 @@ import ( // Workloads = {"Deployment", "STS", "DaemonSet"} // Classes = {"rbd", "cephfs"} +func init() { + flag.BoolVar(&util.CleanUp, "cleanUp", false, "Clean up required after tests are run") +} + const ( GITPATH = "workloads/deployment/base" GITREVISION = "main" diff --git a/e2e/main_test.go b/e2e/main_test.go index 1ef1eb307..043be1664 100644 --- a/e2e/main_test.go +++ b/e2e/main_test.go @@ -68,9 +68,15 @@ func TestSuites(t *testing.T) { t.Run(suite.name, suite.test) } - t.Cleanup(func() { - if err := util.EnsureChannelDeleted(); err != nil { - t.Fatalf("failed to ensure channel deleted: %v", err) - } - }) + if util.CleanUp { + t.Cleanup(func() { + if err := util.CleanUpWorkloads(); err != nil { + t.Fatalf("failed to delete placement ") + } + if err := util.EnsureChannelDeleted(); err != nil { + t.Fatalf("failed to ensure channel deleted: %v", err) + } + }) + } + } diff --git a/e2e/util/context.go b/e2e/util/context.go index 55de103d6..8363a4fc9 100644 --- a/e2e/util/context.go +++ b/e2e/util/context.go @@ -26,6 +26,7 @@ import ( ) var ConfigFile string +var CleanUp bool type Cluster struct { K8sClientSet *kubernetes.Clientset diff --git a/e2e/util/crud.go b/e2e/util/crud.go index 1c40df072..e073ddaaf 100644 --- a/e2e/util/crud.go +++ b/e2e/util/crud.go @@ -12,7 +12,9 @@ import ( corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + ocmv1b1 "open-cluster-management.io/api/cluster/v1beta1" channelv1 "open-cluster-management.io/multicloud-operators-channel/pkg/apis/apps/v1" + subscriptionv1 "open-cluster-management.io/multicloud-operators-subscription/pkg/apis/apps/v1" ) func CreateNamespace(client client.Client, namespace string) error { @@ -154,3 +156,72 @@ func AddNamespaceAnnotationForVolSync(client client.Client, namespace string) er return client.Update(context.Background(), objNs) } + +// Delete all the subscriptions that are associated with channel +func CleanUpWorkloads() error { + channel := GetChannelNamespace() + "/" + GetChannelName() + subList := &subscriptionv1.SubscriptionList{} + err := Ctx.Hub.CtrlClient.List(context.Background(), subList) + if err != nil { + return err + } + for _, sub := range subList.Items { + if sub.Spec.Channel == channel { + // delete placement + pName := sub.Spec.Placement.PlacementRef.Name + pNamespace := sub.Namespace + err = DeletePlacement(pName, pNamespace) + if err != nil { + Ctx.Log.Error(err, "error deleting placement") + return err + } + // delete subscription + err = DeleteSubscription(sub.Name, sub.Name) + if err != nil { + Ctx.Log.Error(err, "error deleting subscription") + return err + } + } + } + return nil +} + +func DeletePlacement(name, namespace string) error { + placement := &ocmv1b1.Placement{ + ObjectMeta: metav1.ObjectMeta{ + Name: name, + Namespace: namespace, + }, + } + + err := Ctx.Hub.CtrlClient.Delete(context.Background(), placement) + if err != nil { + if !errors.IsNotFound(err) { + return err + } + + Ctx.Log.Info("placement " + name + " not found") + } + Ctx.Log.Info("placement " + placement.Name + " is deleted") + return nil +} + +func DeleteSubscription(name, ns string) error { + subscription := &subscriptionv1.Subscription{ + ObjectMeta: metav1.ObjectMeta{ + Name: name, + Namespace: ns, + }, + } + + err := Ctx.Hub.CtrlClient.Delete(context.Background(), subscription) + if err != nil { + if !errors.IsNotFound(err) { + return err + } + + Ctx.Log.Info("subscription " + name + " not found") + } + Ctx.Log.Info("subscription " + subscription.Name + " is deleted") + return nil +} From bed7c77e96a8ba32fb8972e1c1a3a37254b5be4d Mon Sep 17 00:00:00 2001 From: Annaraya Narasagond Date: Tue, 20 Aug 2024 15:35:59 +0530 Subject: [PATCH 2/7] Correcting lint issues Signed-off-by: Annaraya Narasagond Signed-off-by: Annaraya Narasagond Annaraya Narasagond --- e2e/util/crud.go | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/e2e/util/crud.go b/e2e/util/crud.go index e073ddaaf..06dc4e50c 100644 --- a/e2e/util/crud.go +++ b/e2e/util/crud.go @@ -162,15 +162,18 @@ func CleanUpWorkloads() error { channel := GetChannelNamespace() + "/" + GetChannelName() subList := &subscriptionv1.SubscriptionList{} err := Ctx.Hub.CtrlClient.List(context.Background(), subList) + if err != nil { return err } + for _, sub := range subList.Items { if sub.Spec.Channel == channel { // delete placement pName := sub.Spec.Placement.PlacementRef.Name pNamespace := sub.Namespace err = DeletePlacement(pName, pNamespace) + if err != nil { Ctx.Log.Error(err, "error deleting placement") return err @@ -183,6 +186,7 @@ func CleanUpWorkloads() error { } } } + return nil } @@ -197,12 +201,15 @@ func DeletePlacement(name, namespace string) error { err := Ctx.Hub.CtrlClient.Delete(context.Background(), placement) if err != nil { if !errors.IsNotFound(err) { + return err } Ctx.Log.Info("placement " + name + " not found") } + Ctx.Log.Info("placement " + placement.Name + " is deleted") + return nil } @@ -217,11 +224,14 @@ func DeleteSubscription(name, ns string) error { err := Ctx.Hub.CtrlClient.Delete(context.Background(), subscription) if err != nil { if !errors.IsNotFound(err) { + return err } Ctx.Log.Info("subscription " + name + " not found") } + Ctx.Log.Info("subscription " + subscription.Name + " is deleted") + return nil } From 4f00f95eb4b88cb7b42cebaf59938a65487b491c Mon Sep 17 00:00:00 2001 From: Annaraya Narasagond Date: Tue, 20 Aug 2024 15:40:21 +0530 Subject: [PATCH 3/7] Correcting lint errors Signed-off-by: Annaraya Narasagond Signed-off-by: Annaraya Narasagond Annaraya Narasagond --- e2e/util/crud.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/e2e/util/crud.go b/e2e/util/crud.go index 06dc4e50c..3f3d5afcb 100644 --- a/e2e/util/crud.go +++ b/e2e/util/crud.go @@ -176,12 +176,14 @@ func CleanUpWorkloads() error { if err != nil { Ctx.Log.Error(err, "error deleting placement") + return err } // delete subscription err = DeleteSubscription(sub.Name, sub.Name) if err != nil { Ctx.Log.Error(err, "error deleting subscription") + return err } } From ffe3a2bf1971e6c3be36a77019c2d3b84bc48f1a Mon Sep 17 00:00:00 2001 From: Annaraya Narasagond Date: Tue, 20 Aug 2024 15:52:23 +0530 Subject: [PATCH 4/7] Correcting lint errors Signed-off-by: Annaraya Narasagond Signed-off-by: Annaraya Narasagond Annaraya Narasagond --- e2e/util/crud.go | 2 -- 1 file changed, 2 deletions(-) diff --git a/e2e/util/crud.go b/e2e/util/crud.go index 3f3d5afcb..ffb9f8c58 100644 --- a/e2e/util/crud.go +++ b/e2e/util/crud.go @@ -203,7 +203,6 @@ func DeletePlacement(name, namespace string) error { err := Ctx.Hub.CtrlClient.Delete(context.Background(), placement) if err != nil { if !errors.IsNotFound(err) { - return err } @@ -226,7 +225,6 @@ func DeleteSubscription(name, ns string) error { err := Ctx.Hub.CtrlClient.Delete(context.Background(), subscription) if err != nil { if !errors.IsNotFound(err) { - return err } From ed70685ad3af904fef79e888f2451d0894c0170e Mon Sep 17 00:00:00 2001 From: Annaraya Narasagond Date: Tue, 20 Aug 2024 16:47:59 +0530 Subject: [PATCH 5/7] Correcting lint errors Signed-off-by: Annaraya Narasagond Signed-off-by: Annaraya Narasagond Annaraya Narasagond --- e2e/util/crud.go | 2 -- 1 file changed, 2 deletions(-) diff --git a/e2e/util/crud.go b/e2e/util/crud.go index ffb9f8c58..545d48e74 100644 --- a/e2e/util/crud.go +++ b/e2e/util/crud.go @@ -162,7 +162,6 @@ func CleanUpWorkloads() error { channel := GetChannelNamespace() + "/" + GetChannelName() subList := &subscriptionv1.SubscriptionList{} err := Ctx.Hub.CtrlClient.List(context.Background(), subList) - if err != nil { return err } @@ -173,7 +172,6 @@ func CleanUpWorkloads() error { pName := sub.Spec.Placement.PlacementRef.Name pNamespace := sub.Namespace err = DeletePlacement(pName, pNamespace) - if err != nil { Ctx.Log.Error(err, "error deleting placement") From 1d9543608cfac8789d3fd92d1b8b84471d703448 Mon Sep 17 00:00:00 2001 From: Annaraya Narasagond Date: Tue, 20 Aug 2024 17:23:11 +0530 Subject: [PATCH 6/7] Correct lint errors Signed-off-by: Annaraya Narasagond Signed-off-by: Annaraya Narasagond Annaraya Narasagond --- e2e/util/context.go | 6 ++++-- e2e/util/crud.go | 10 +++++----- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/e2e/util/context.go b/e2e/util/context.go index 8363a4fc9..411def9ef 100644 --- a/e2e/util/context.go +++ b/e2e/util/context.go @@ -25,8 +25,10 @@ import ( placementrule "open-cluster-management.io/multicloud-operators-subscription/pkg/apis/apps/placementrule/v1" ) -var ConfigFile string -var CleanUp bool +var ( + ConfigFile string + CleanUp bool +) type Cluster struct { K8sClientSet *kubernetes.Clientset diff --git a/e2e/util/crud.go b/e2e/util/crud.go index 545d48e74..3c9978d41 100644 --- a/e2e/util/crud.go +++ b/e2e/util/crud.go @@ -161,8 +161,8 @@ func AddNamespaceAnnotationForVolSync(client client.Client, namespace string) er func CleanUpWorkloads() error { channel := GetChannelNamespace() + "/" + GetChannelName() subList := &subscriptionv1.SubscriptionList{} - err := Ctx.Hub.CtrlClient.List(context.Background(), subList) - if err != nil { + + if err := Ctx.Hub.CtrlClient.List(context.Background(), subList); err != nil { return err } @@ -171,14 +171,14 @@ func CleanUpWorkloads() error { // delete placement pName := sub.Spec.Placement.PlacementRef.Name pNamespace := sub.Namespace - err = DeletePlacement(pName, pNamespace) - if err != nil { + + if err := DeletePlacement(pName, pNamespace); err != nil { Ctx.Log.Error(err, "error deleting placement") return err } // delete subscription - err = DeleteSubscription(sub.Name, sub.Name) + err := DeleteSubscription(sub.Name, sub.Name) if err != nil { Ctx.Log.Error(err, "error deleting subscription") From 377be9f64094f96ad1036e725f82cae6e0f18646 Mon Sep 17 00:00:00 2001 From: Annaraya Narasagond Date: Wed, 21 Aug 2024 23:30:52 +0530 Subject: [PATCH 7/7] Sign change Signed-off-by: Annaraya Narasagond Signed-off-by: Annaraya Narasagond Annaraya Narasagond --- e2e/util/crud.go | 1 - 1 file changed, 1 deletion(-) diff --git a/e2e/util/crud.go b/e2e/util/crud.go index 3c9978d41..216a7addd 100644 --- a/e2e/util/crud.go +++ b/e2e/util/crud.go @@ -206,7 +206,6 @@ func DeletePlacement(name, namespace string) error { Ctx.Log.Info("placement " + name + " not found") } - Ctx.Log.Info("placement " + placement.Name + " is deleted") return nil