From 9a690da560ac6df6b1c05c30bd18a4a0a761fd77 Mon Sep 17 00:00:00 2001 From: Coleen Iona Quadros Date: Fri, 15 Dec 2023 15:57:56 +0100 Subject: [PATCH] make sonarcloud happy Signed-off-by: Coleen Iona Quadros --- .../placementrule/placementrule_controller.go | 22 +------ .../placementrule/predicate_func.go | 24 +++++++ .../placementrule/predicate_func_test.go | 66 +++++++++++++++++++ 3 files changed, 91 insertions(+), 21 deletions(-) diff --git a/operators/multiclusterobservability/controllers/placementrule/placementrule_controller.go b/operators/multiclusterobservability/controllers/placementrule/placementrule_controller.go index 2a78a1de6..318f015f7 100644 --- a/operators/multiclusterobservability/controllers/placementrule/placementrule_controller.go +++ b/operators/multiclusterobservability/controllers/placementrule/placementrule_controller.go @@ -540,27 +540,7 @@ func (r *PlacementRuleReconciler) SetupWithManager(mgr ctrl.Manager) error { clusterPred := getClusterPreds() // Watch changes for AddonDeploymentConfig - AddonDeploymentPred := predicate.Funcs{ - CreateFunc: func(e event.CreateEvent) bool { - return true - }, - UpdateFunc: func(e event.UpdateEvent) bool { - if e.ObjectNew.GetName() == defaultAddonDeploymentConfig.Name && - e.ObjectNew.GetNamespace() == defaultAddonDeploymentConfig.Namespace { - log.Info("default AddonDeploymentConfig is updated") - return true - } - return false - }, - DeleteFunc: func(e event.DeleteEvent) bool { - if e.Object.GetName() == defaultAddonDeploymentConfig.Name && - e.Object.GetNamespace() == defaultAddonDeploymentConfig.Namespace { - log.Info("default AddonDeploymentConfig is deleted") - return true - } - return false - }, - } + AddonDeploymentPred := GetAddOnDeploymentPredicates() obsAddonPred := predicate.Funcs{ CreateFunc: func(e event.CreateEvent) bool { diff --git a/operators/multiclusterobservability/controllers/placementrule/predicate_func.go b/operators/multiclusterobservability/controllers/placementrule/predicate_func.go index c6346d64b..7209f8a13 100644 --- a/operators/multiclusterobservability/controllers/placementrule/predicate_func.go +++ b/operators/multiclusterobservability/controllers/placementrule/predicate_func.go @@ -73,3 +73,27 @@ func getClusterPreds() predicate.Funcs { DeleteFunc: deleteFunc, } } + +func GetAddOnDeploymentPredicates() predicate.Funcs { + return predicate.Funcs{ + CreateFunc: func(e event.CreateEvent) bool { + return true + }, + UpdateFunc: func(e event.UpdateEvent) bool { + if e.ObjectNew.GetName() == defaultAddonDeploymentConfig.Name && + e.ObjectNew.GetNamespace() == defaultAddonDeploymentConfig.Namespace { + log.Info("default AddonDeploymentConfig is updated") + return true + } + return false + }, + DeleteFunc: func(e event.DeleteEvent) bool { + if e.Object.GetName() == defaultAddonDeploymentConfig.Name && + e.Object.GetNamespace() == defaultAddonDeploymentConfig.Namespace { + log.Info("default AddonDeploymentConfig is deleted") + return true + } + return false + }, + } +} diff --git a/operators/multiclusterobservability/controllers/placementrule/predicate_func_test.go b/operators/multiclusterobservability/controllers/placementrule/predicate_func_test.go index bfa6a751e..e23b2d65f 100644 --- a/operators/multiclusterobservability/controllers/placementrule/predicate_func_test.go +++ b/operators/multiclusterobservability/controllers/placementrule/predicate_func_test.go @@ -5,6 +5,7 @@ package placementrule import ( + addonv1alpha1 "open-cluster-management.io/api/addon/v1alpha1" "testing" "time" @@ -146,3 +147,68 @@ func TestClusterPred(t *testing.T) { }) } } + +func TestAddonDeploymentPredicate(t *testing.T) { + name := "test-obj" + caseList := []struct { + caseName string + namespace string + expectedCreate bool + expectedUpdate bool + expectedDelete bool + }{ + { + caseName: "Create AddonDeploymentConfig", + namespace: testNamespace, + expectedCreate: true, + expectedDelete: true, + expectedUpdate: true, + }, + } + + defaultAddonDeploymentConfig = &addonv1alpha1.AddOnDeploymentConfig{ + ObjectMeta: metav1.ObjectMeta{ + Name: name, + Namespace: namespace, + }, + Spec: addonv1alpha1.AddOnDeploymentConfigSpec{ + ProxyConfig: addonv1alpha1.ProxyConfig{ + HTTPProxy: "http://foo.com", + HTTPSProxy: "https://foo.com", + NoProxy: "bar.com", + }, + }, + } + for _, c := range caseList { + t.Run(c.caseName, func(t *testing.T) { + pred := GetAddOnDeploymentPredicates() + createEvent := event.CreateEvent{ + Object: defaultAddonDeploymentConfig, + } + + if c.expectedCreate { + if !pred.CreateFunc(createEvent) { + t.Fatalf("pre func return false on applied createevent in case: (%v)", c.caseName) + } + } + + updateEvent := event.UpdateEvent{ + ObjectNew: defaultAddonDeploymentConfig, + } + if c.expectedUpdate { + if !pred.UpdateFunc(updateEvent) { + t.Fatalf("pre func return false on applied update event in case: (%v)", c.caseName) + } + } + + deleteEvent := event.DeleteEvent{ + Object: defaultAddonDeploymentConfig, + } + if c.expectedDelete { + if !pred.DeleteFunc(deleteEvent) { + t.Fatalf("pre func return false on applied delete event in case: (%v)", c.caseName) + } + } + }) + } +}