From 8d5912f78b4c7732c324ea295ecd6077825e2901 Mon Sep 17 00:00:00 2001 From: odubajDT Date: Mon, 18 Sep 2023 08:39:34 +0200 Subject: [PATCH] refactor Signed-off-by: odubajDT --- .../api/v1alpha3/analysis_types.go | 7 ------ .../api/v1alpha3/analysis_types_test.go | 22 ------------------- .../api/v1alpha3/analysisdefinition_types.go | 7 ------ .../v1alpha3/analysisdefinition_types_test.go | 12 ---------- .../v1alpha3/analysisvaluetemplate_types.go | 7 ------ .../analysisvaluetemplate_types_test.go | 17 -------------- metrics-operator/api/v1alpha3/common.go | 8 +++++++ metrics-operator/api/v1alpha3/common_test.go | 10 +++++++++ .../controllers/analysis/controller.go | 7 +++--- .../controllers/analysis/provider_selector.go | 5 ++--- 10 files changed, 24 insertions(+), 78 deletions(-) delete mode 100644 metrics-operator/api/v1alpha3/analysis_types_test.go delete mode 100644 metrics-operator/api/v1alpha3/analysisvaluetemplate_types_test.go diff --git a/metrics-operator/api/v1alpha3/analysis_types.go b/metrics-operator/api/v1alpha3/analysis_types.go index 0b47c1551a..45d76dc82a 100644 --- a/metrics-operator/api/v1alpha3/analysis_types.go +++ b/metrics-operator/api/v1alpha3/analysis_types.go @@ -86,10 +86,3 @@ type Timeframe struct { func init() { SchemeBuilder.Register(&Analysis{}, &AnalysisList{}) } - -func (a *Analysis) GetAnalysisDefinitionNamespace() string { - if !a.Spec.AnalysisDefinition.IsNamespaceSet() { - return a.Namespace - } - return a.Spec.AnalysisDefinition.Namespace -} diff --git a/metrics-operator/api/v1alpha3/analysis_types_test.go b/metrics-operator/api/v1alpha3/analysis_types_test.go deleted file mode 100644 index 5ec6d0e1cf..0000000000 --- a/metrics-operator/api/v1alpha3/analysis_types_test.go +++ /dev/null @@ -1,22 +0,0 @@ -package v1alpha3 - -import ( - "testing" - - "github.com/stretchr/testify/require" - v1 "k8s.io/apimachinery/pkg/apis/meta/v1" -) - -func TestAnalysis_GetAnalysisDefinitionNamespace(t *testing.T) { - a := Analysis{ - ObjectMeta: v1.ObjectMeta{ - Namespace: "ns", - }, - } - - require.Equal(t, "ns", a.GetAnalysisDefinitionNamespace()) - - a.Spec.AnalysisDefinition.Namespace = "ns2" - - require.Equal(t, "ns2", a.GetAnalysisDefinitionNamespace()) -} diff --git a/metrics-operator/api/v1alpha3/analysisdefinition_types.go b/metrics-operator/api/v1alpha3/analysisdefinition_types.go index bed0136889..ab98d3651d 100644 --- a/metrics-operator/api/v1alpha3/analysisdefinition_types.go +++ b/metrics-operator/api/v1alpha3/analysisdefinition_types.go @@ -124,10 +124,3 @@ func init() { func (o *OperatorValue) GetFloatValue() float64 { return o.FixedValue.AsApproximateFloat64() } - -func (o *Objective) GetAnalysisValueTemplateNamespace(namespace string) string { - if !o.AnalysisValueTemplateRef.IsNamespaceSet() { - return namespace - } - return o.AnalysisValueTemplateRef.Namespace -} diff --git a/metrics-operator/api/v1alpha3/analysisdefinition_types_test.go b/metrics-operator/api/v1alpha3/analysisdefinition_types_test.go index dbf60b1871..2751c17205 100644 --- a/metrics-operator/api/v1alpha3/analysisdefinition_types_test.go +++ b/metrics-operator/api/v1alpha3/analysisdefinition_types_test.go @@ -14,15 +14,3 @@ func TestOperatorValue_GetFloatValue(t *testing.T) { require.Equal(t, 15.0, o.GetFloatValue()) } - -func TestObjective_GetAnalysisValueTemplateNamespace(t *testing.T) { - o := Objective{ - AnalysisValueTemplateRef: ObjectReference{}, - } - - require.Equal(t, "default", o.GetAnalysisValueTemplateNamespace("default")) - - o.AnalysisValueTemplateRef.Namespace = "ns" - - require.Equal(t, "ns", o.GetAnalysisValueTemplateNamespace("default")) -} diff --git a/metrics-operator/api/v1alpha3/analysisvaluetemplate_types.go b/metrics-operator/api/v1alpha3/analysisvaluetemplate_types.go index 4ae22d2174..24e3e80b0f 100644 --- a/metrics-operator/api/v1alpha3/analysisvaluetemplate_types.go +++ b/metrics-operator/api/v1alpha3/analysisvaluetemplate_types.go @@ -56,10 +56,3 @@ type AnalysisValueTemplateList struct { func init() { SchemeBuilder.Register(&AnalysisValueTemplate{}, &AnalysisValueTemplateList{}) } - -func (a *AnalysisValueTemplate) GetProviderNamespace(namespace string) string { - if !a.Spec.Provider.IsNamespaceSet() { - return namespace - } - return a.Spec.Provider.Namespace -} diff --git a/metrics-operator/api/v1alpha3/analysisvaluetemplate_types_test.go b/metrics-operator/api/v1alpha3/analysisvaluetemplate_types_test.go deleted file mode 100644 index 76b462fd80..0000000000 --- a/metrics-operator/api/v1alpha3/analysisvaluetemplate_types_test.go +++ /dev/null @@ -1,17 +0,0 @@ -package v1alpha3 - -import ( - "testing" - - "github.com/stretchr/testify/require" -) - -func TestAnalysisValueTemplate_GetProviderNamespace(t *testing.T) { - a := AnalysisValueTemplate{} - - require.Equal(t, "default", a.GetProviderNamespace("default")) - - a.Spec.Provider.Namespace = "ns" - - require.Equal(t, "ns", a.GetProviderNamespace("default")) -} diff --git a/metrics-operator/api/v1alpha3/common.go b/metrics-operator/api/v1alpha3/common.go index 22bfaf70e0..fe4a459b51 100644 --- a/metrics-operator/api/v1alpha3/common.go +++ b/metrics-operator/api/v1alpha3/common.go @@ -10,3 +10,11 @@ type ObjectReference struct { func (o *ObjectReference) IsNamespaceSet() bool { return o.Namespace != "" } + +func (o *ObjectReference) GetNamespace(defaultNamespace string) string { + if o.IsNamespaceSet() { + return o.Namespace + } + + return defaultNamespace +} diff --git a/metrics-operator/api/v1alpha3/common_test.go b/metrics-operator/api/v1alpha3/common_test.go index ce4c1e0443..ef2798a0ca 100644 --- a/metrics-operator/api/v1alpha3/common_test.go +++ b/metrics-operator/api/v1alpha3/common_test.go @@ -15,3 +15,13 @@ func TestObjectReference_IsNamespaceSet(t *testing.T) { require.True(t, o.IsNamespaceSet()) } + +func TestObjectReference_GetNamespace(t *testing.T) { + o := ObjectReference{} + + require.Equal(t, "default", o.GetNamespace("default")) + + o.Namespace = "ns" + + require.Equal(t, "ns", o.GetNamespace("default")) +} diff --git a/metrics-operator/controllers/analysis/controller.go b/metrics-operator/controllers/analysis/controller.go index ecad9002dd..703e1a714c 100644 --- a/metrics-operator/controllers/analysis/controller.go +++ b/metrics-operator/controllers/analysis/controller.go @@ -72,11 +72,12 @@ func (a *AnalysisReconciler) Reconcile(ctx context.Context, req ctrl.Request) (c } //find AnalysisDefinition to have the collection of Objectives + analysisDefNamespace := analysis.Spec.AnalysisDefinition.GetNamespace(analysis.Namespace) analysisDef := &metricsapi.AnalysisDefinition{} err := a.Client.Get(ctx, types.NamespacedName{ Name: analysis.Spec.AnalysisDefinition.Name, - Namespace: analysis.GetAnalysisDefinitionNamespace()}, + Namespace: analysisDefNamespace}, analysisDef, ) @@ -85,7 +86,7 @@ func (a *AnalysisReconciler) Reconcile(ctx context.Context, req ctrl.Request) (c a.Log.Info( fmt.Sprintf("AnalysisDefinition '%s' in namespace '%s' not found, requeue", analysis.Spec.AnalysisDefinition.Name, - analysis.GetAnalysisDefinitionNamespace()), + analysisDefNamespace), ) return ctrl.Result{Requeue: true, RequeueAfter: 10 * time.Second}, nil } @@ -103,7 +104,7 @@ func (a *AnalysisReconciler) Reconcile(ctx context.Context, req ctrl.Request) (c } //create multiple workers handling the Objectives - childCtx, wp := a.NewWorkersPoolFactory(ctx, analysis, todo, a.MaxWorkers, a.Client, a.Log, analysis.GetAnalysisDefinitionNamespace()) + childCtx, wp := a.NewWorkersPoolFactory(ctx, analysis, todo, a.MaxWorkers, a.Client, a.Log, analysisDefNamespace) res, err := wp.DispatchAndCollect(childCtx) if err != nil { diff --git a/metrics-operator/controllers/analysis/provider_selector.go b/metrics-operator/controllers/analysis/provider_selector.go index 640eca0d99..2bb9833738 100644 --- a/metrics-operator/controllers/analysis/provider_selector.go +++ b/metrics-operator/controllers/analysis/provider_selector.go @@ -57,7 +57,7 @@ func (ps ProvidersPool) DispatchToProviders(ctx context.Context, id int) { err := ps.Client.Get(ctx, types.NamespacedName{ Name: j.AnalysisValueTemplateRef.Name, - Namespace: j.GetAnalysisValueTemplateNamespace(ps.Namespace)}, + Namespace: j.AnalysisValueTemplateRef.GetNamespace(ps.Namespace)}, templ, ) @@ -68,12 +68,11 @@ func (ps ProvidersPool) DispatchToProviders(ctx context.Context, id int) { return } - ns := templ.GetProviderNamespace(ps.Namespace) providerRef := &metricsapi.KeptnMetricsProvider{} err = ps.Client.Get(ctx, types.NamespacedName{ Name: templ.Spec.Provider.Name, - Namespace: ns}, + Namespace: templ.Spec.Provider.GetNamespace(ps.Namespace)}, providerRef, )