Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
Signed-off-by: odubajDT <ondrej.dubaj@dynatrace.com>
  • Loading branch information
odubajDT committed Sep 18, 2023
1 parent caa8d8e commit 8d5912f
Show file tree
Hide file tree
Showing 10 changed files with 24 additions and 78 deletions.
7 changes: 0 additions & 7 deletions metrics-operator/api/v1alpha3/analysis_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
22 changes: 0 additions & 22 deletions metrics-operator/api/v1alpha3/analysis_types_test.go

This file was deleted.

7 changes: 0 additions & 7 deletions metrics-operator/api/v1alpha3/analysisdefinition_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
12 changes: 0 additions & 12 deletions metrics-operator/api/v1alpha3/analysisdefinition_types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
}
7 changes: 0 additions & 7 deletions metrics-operator/api/v1alpha3/analysisvaluetemplate_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
17 changes: 0 additions & 17 deletions metrics-operator/api/v1alpha3/analysisvaluetemplate_types_test.go

This file was deleted.

8 changes: 8 additions & 0 deletions metrics-operator/api/v1alpha3/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
10 changes: 10 additions & 0 deletions metrics-operator/api/v1alpha3/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
}
7 changes: 4 additions & 3 deletions metrics-operator/controllers/analysis/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)

Expand All @@ -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
}
Expand All @@ -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 {
Expand Down
5 changes: 2 additions & 3 deletions metrics-operator/controllers/analysis/provider_selector.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)

Expand All @@ -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,
)

Expand Down

0 comments on commit 8d5912f

Please sign in to comment.