From a683c9a794fcb76017c4cb4fb6d3547de5c2c2ca Mon Sep 17 00:00:00 2001 From: odubajDT <93584209+odubajDT@users.noreply.github.com> Date: Thu, 21 Mar 2024 11:30:09 +0100 Subject: [PATCH] chore(metrics-operator): cleanup APIs (#3270) Signed-off-by: odubajDT --- .../api/v1beta1/analysis_types.go | 44 ------ .../api/v1beta1/analysis_types_test.go | 80 ---------- .../api/v1beta1/analysisdefinition_types.go | 4 - .../v1beta1/analysisdefinition_types_test.go | 16 -- metrics-operator/api/v1beta1/common.go | 20 --- metrics-operator/api/v1beta1/common_test.go | 46 ------ .../api/v1beta1/keptnmetric_types.go | 4 - .../api/v1beta1/keptnmetric_types_test.go | 53 ------- .../api/v1beta1/keptnmetricsprovider_types.go | 31 ---- .../keptnmetricsprovider_types_test.go | 142 ------------------ 10 files changed, 440 deletions(-) delete mode 100644 metrics-operator/api/v1beta1/analysis_types_test.go delete mode 100644 metrics-operator/api/v1beta1/analysisdefinition_types_test.go delete mode 100644 metrics-operator/api/v1beta1/common_test.go delete mode 100644 metrics-operator/api/v1beta1/keptnmetric_types_test.go delete mode 100644 metrics-operator/api/v1beta1/keptnmetricsprovider_types_test.go diff --git a/metrics-operator/api/v1beta1/analysis_types.go b/metrics-operator/api/v1beta1/analysis_types.go index 54186dee19..64303deae6 100644 --- a/metrics-operator/api/v1beta1/analysis_types.go +++ b/metrics-operator/api/v1beta1/analysis_types.go @@ -17,8 +17,6 @@ limitations under the License. package v1beta1 import ( - "time" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" ) @@ -119,45 +117,3 @@ type AnalysisList struct { func init() { SchemeBuilder.Register(&Analysis{}, &AnalysisList{}) } - -// GetFrom returns the 'from' timestamp from the status of the Analysis. -// This function has been added to provide a clear way of retrieving the correct timestamp -// to use, which is the one from the Status. -func (a *Analysis) GetFrom() time.Time { - return a.Status.Timeframe.GetFrom() -} - -// GetTo returns the 'from' timestamp from the status of the Analysis. -// This function has been added to provide a clear way of retrieving the correct timestamp -// to use, which is the one from the Status. -func (a *Analysis) GetTo() time.Time { - return a.Status.Timeframe.GetTo() -} - -func (a *Analysis) EnsureTimeframeIsSet() { - // make sure the correct time frame is set in the status - once an Analysis with a duration string specifying the - // time frame is triggered, the time frame derived from that duration should stay the same and not shift over the course - // of multiple reconciliation loops - if a.Status.Timeframe.From.IsZero() || a.Status.Timeframe.To.IsZero() { - a.Status.Timeframe.From = metav1.Time{ - Time: a.Spec.GetFrom(), - } - a.Status.Timeframe.To = metav1.Time{ - Time: a.Spec.GetTo(), - } - } -} - -func (t *Timeframe) GetFrom() time.Time { - if t.Recent.Duration > 0 { - return time.Now().UTC().Add(-t.Recent.Duration) - } - return t.From.Time -} - -func (t *Timeframe) GetTo() time.Time { - if t.Recent.Duration > 0 { - return time.Now().UTC() - } - return t.To.Time -} diff --git a/metrics-operator/api/v1beta1/analysis_types_test.go b/metrics-operator/api/v1beta1/analysis_types_test.go deleted file mode 100644 index 2dd3efe7a7..0000000000 --- a/metrics-operator/api/v1beta1/analysis_types_test.go +++ /dev/null @@ -1,80 +0,0 @@ -package v1beta1 - -import ( - "testing" - "time" - - "github.com/stretchr/testify/require" - v1 "k8s.io/apimachinery/pkg/apis/meta/v1" -) - -func TestAnalysis_SetAndRetrieveTime(t *testing.T) { - now := time.Now().UTC() - before := now.Add(-5 * time.Minute) - type fields struct { - TypeMeta v1.TypeMeta - ObjectMeta v1.ObjectMeta - Spec AnalysisSpec - Status AnalysisStatus - } - tests := []struct { - name string - fields fields - wantFrom time.Time - wantTo time.Time - }{ - { - name: "from and to timestamps set", - fields: fields{ - Spec: AnalysisSpec{ - Timeframe: Timeframe{ - From: v1.Time{ - Time: before, - }, - To: v1.Time{ - Time: now, - }, - }, - }, - }, - wantFrom: before, - wantTo: now, - }, - { - name: "'recent' set", - fields: fields{ - Spec: AnalysisSpec{ - Timeframe: Timeframe{ - Recent: v1.Duration{ - Duration: 3 * time.Minute, - }, - }, - }, - }, - wantFrom: now.Add(-3 * time.Minute), - wantTo: now, - }, - { - name: "nothing set", - fields: fields{ - Spec: AnalysisSpec{}, - }, - wantFrom: time.Time{}, - wantTo: time.Time{}, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - a := &Analysis{ - TypeMeta: tt.fields.TypeMeta, - ObjectMeta: tt.fields.ObjectMeta, - Spec: tt.fields.Spec, - Status: tt.fields.Status, - } - a.EnsureTimeframeIsSet() - - require.WithinDuration(t, tt.wantFrom, a.GetFrom(), 1*time.Minute) - require.WithinDuration(t, tt.wantTo, a.GetTo(), 1*time.Minute) - }) - } -} diff --git a/metrics-operator/api/v1beta1/analysisdefinition_types.go b/metrics-operator/api/v1beta1/analysisdefinition_types.go index 90be30d000..5b125892f1 100644 --- a/metrics-operator/api/v1beta1/analysisdefinition_types.go +++ b/metrics-operator/api/v1beta1/analysisdefinition_types.go @@ -137,7 +137,3 @@ type AnalysisDefinitionList struct { func init() { SchemeBuilder.Register(&AnalysisDefinition{}, &AnalysisDefinitionList{}) } - -func (o *OperatorValue) GetFloatValue() float64 { - return o.FixedValue.AsApproximateFloat64() -} diff --git a/metrics-operator/api/v1beta1/analysisdefinition_types_test.go b/metrics-operator/api/v1beta1/analysisdefinition_types_test.go deleted file mode 100644 index 2e32784c09..0000000000 --- a/metrics-operator/api/v1beta1/analysisdefinition_types_test.go +++ /dev/null @@ -1,16 +0,0 @@ -package v1beta1 - -import ( - "testing" - - "github.com/stretchr/testify/require" - "k8s.io/apimachinery/pkg/api/resource" -) - -func TestOperatorValue_GetFloatValue(t *testing.T) { - o := OperatorValue{ - FixedValue: *resource.NewQuantity(15, resource.DecimalSI), - } - - require.Equal(t, 15.0, o.GetFloatValue()) -} diff --git a/metrics-operator/api/v1beta1/common.go b/metrics-operator/api/v1beta1/common.go index 5c5de63d4c..394c4c74dd 100644 --- a/metrics-operator/api/v1beta1/common.go +++ b/metrics-operator/api/v1beta1/common.go @@ -8,18 +8,6 @@ type ObjectReference struct { Namespace string `json:"namespace,omitempty"` } -func (o *ObjectReference) IsNamespaceSet() bool { - return o.Namespace != "" -} - -func (o *ObjectReference) GetNamespace(defaultNamespace string) string { - if o.IsNamespaceSet() { - return o.Namespace - } - - return defaultNamespace -} - // AnalysisState represents the state of the analysis type AnalysisState string @@ -28,11 +16,3 @@ const ( StateProgressing AnalysisState = "Progressing" StateCompleted AnalysisState = "Completed" ) - -func (s AnalysisState) IsPending() bool { - return s == StatePending || s == "" -} - -func (s AnalysisState) IsCompleted() bool { - return s == StateCompleted -} diff --git a/metrics-operator/api/v1beta1/common_test.go b/metrics-operator/api/v1beta1/common_test.go deleted file mode 100644 index 2e58479fa3..0000000000 --- a/metrics-operator/api/v1beta1/common_test.go +++ /dev/null @@ -1,46 +0,0 @@ -package v1beta1 - -import ( - "testing" - - "github.com/stretchr/testify/require" -) - -func TestObjectReference_IsNamespaceSet(t *testing.T) { - o := ObjectReference{} - - require.False(t, o.IsNamespaceSet()) - - o.Namespace = "ns" - - 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")) -} - -func TestAnalysisState_IsPending(t *testing.T) { - a := StatePending - require.True(t, a.IsPending()) - - a = "" - require.True(t, a.IsPending()) - - a = StateCompleted - require.False(t, a.IsPending()) -} - -func TestAnalysisState_IsCompleted(t *testing.T) { - a := StateCompleted - require.True(t, a.IsCompleted()) - - a = StateProgressing - require.False(t, a.IsCompleted()) -} diff --git a/metrics-operator/api/v1beta1/keptnmetric_types.go b/metrics-operator/api/v1beta1/keptnmetric_types.go index cc4c5567dd..bee2496556 100644 --- a/metrics-operator/api/v1beta1/keptnmetric_types.go +++ b/metrics-operator/api/v1beta1/keptnmetric_types.go @@ -126,7 +126,3 @@ type KeptnMetricList struct { func init() { SchemeBuilder.Register(&KeptnMetric{}, &KeptnMetricList{}) } - -func (s *KeptnMetric) IsStatusSet() bool { - return s.Status.Value != "" -} diff --git a/metrics-operator/api/v1beta1/keptnmetric_types_test.go b/metrics-operator/api/v1beta1/keptnmetric_types_test.go deleted file mode 100644 index 86c60c888d..0000000000 --- a/metrics-operator/api/v1beta1/keptnmetric_types_test.go +++ /dev/null @@ -1,53 +0,0 @@ -package v1beta1 - -import ( - "testing" - - v1 "k8s.io/apimachinery/pkg/apis/meta/v1" -) - -func TestKeptnMetric_IsStatusSet(t *testing.T) { - type fields struct { - TypeMeta v1.TypeMeta - ObjectMeta v1.ObjectMeta - Spec KeptnMetricSpec - Status KeptnMetricStatus - } - tests := []struct { - name string - fields fields - want bool - }{ - { - name: "No value set", - fields: fields{ - Status: KeptnMetricStatus{ - Value: "", - }, - }, - want: false, - }, - { - name: "we have a value", - fields: fields{ - Status: KeptnMetricStatus{ - Value: "1.0", - }, - }, - want: true, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - s := &KeptnMetric{ - TypeMeta: tt.fields.TypeMeta, - ObjectMeta: tt.fields.ObjectMeta, - Spec: tt.fields.Spec, - Status: tt.fields.Status, - } - if got := s.IsStatusSet(); got != tt.want { - t.Errorf("IsStatusSet() = %v, want %v", got, tt.want) - } - }) - } -} diff --git a/metrics-operator/api/v1beta1/keptnmetricsprovider_types.go b/metrics-operator/api/v1beta1/keptnmetricsprovider_types.go index c0c7f34127..f47798dd60 100644 --- a/metrics-operator/api/v1beta1/keptnmetricsprovider_types.go +++ b/metrics-operator/api/v1beta1/keptnmetricsprovider_types.go @@ -17,8 +17,6 @@ limitations under the License. package v1beta1 import ( - "strings" - corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" ) @@ -67,32 +65,3 @@ type KeptnMetricsProviderList struct { func init() { SchemeBuilder.Register(&KeptnMetricsProvider{}, &KeptnMetricsProviderList{}) } - -func (p *KeptnMetricsProvider) HasSecretDefined() bool { - if p.Spec.SecretKeyRef == (corev1.SecretKeySelector{}) { - return false - } - //if the secret name exists the secret is defined - if strings.TrimSpace(p.Spec.SecretKeyRef.Name) == "" { - return false - } - return true -} - -func (p *KeptnMetricsProvider) HasSecretKeyDefined() bool { - if p.Spec.SecretKeyRef == (corev1.SecretKeySelector{}) { - return false - } - //if the secret name exists the secret is defined - if strings.TrimSpace(p.Spec.SecretKeyRef.Key) == "" { - return false - } - return true -} - -func (p *KeptnMetricsProvider) GetType() string { - if p.Spec.Type != "" { - return p.Spec.Type - } - return p.Name -} diff --git a/metrics-operator/api/v1beta1/keptnmetricsprovider_types_test.go b/metrics-operator/api/v1beta1/keptnmetricsprovider_types_test.go deleted file mode 100644 index 073c1e298a..0000000000 --- a/metrics-operator/api/v1beta1/keptnmetricsprovider_types_test.go +++ /dev/null @@ -1,142 +0,0 @@ -package v1beta1 - -import ( - "testing" - - corev1 "k8s.io/api/core/v1" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" -) - -func TestKeptnMetricsProvider_GetType(t *testing.T) { - type fields struct { - TypeMeta metav1.TypeMeta - ObjectMeta metav1.ObjectMeta - Spec KeptnMetricsProviderSpec - } - tests := []struct { - name string - fields fields - want string - }{ - { - name: "provider type set", - fields: fields{ - ObjectMeta: metav1.ObjectMeta{ - Name: "provider1", - }, - Spec: KeptnMetricsProviderSpec{ - Type: "prometheus", - TargetServer: "", - }, - }, - want: "prometheus", - }, - { - name: "provider type not set, should return name", - fields: fields{ - ObjectMeta: metav1.ObjectMeta{ - Name: "prometheus", - }, - Spec: KeptnMetricsProviderSpec{ - Type: "", - TargetServer: "", - }, - }, - want: "prometheus", - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - p := &KeptnMetricsProvider{ - TypeMeta: tt.fields.TypeMeta, - ObjectMeta: tt.fields.ObjectMeta, - Spec: tt.fields.Spec, - } - if got := p.GetType(); got != tt.want { - t.Errorf("GetType() = %v, want %v", got, tt.want) - } - }) - } -} - -func TestKeptnMetricsProvider_HasSecretDefined(t *testing.T) { - type fields struct { - TypeMeta metav1.TypeMeta - ObjectMeta metav1.ObjectMeta - Spec KeptnMetricsProviderSpec - } - tests := []struct { - name string - fields fields - want bool - }{ - { - name: "secret key ref is defined and has a key defined", - fields: fields{ - Spec: KeptnMetricsProviderSpec{ - SecretKeyRef: corev1.SecretKeySelector{ - Key: "some-secret", - }, - }, - }, - want: false, - }, - { - name: "secret key ref is not defined", - fields: fields{ - Spec: KeptnMetricsProviderSpec{}, - }, - want: false, - }, - { - name: "secret key ref key is empty", - fields: fields{ - Spec: KeptnMetricsProviderSpec{ - SecretKeyRef: corev1.SecretKeySelector{ - Key: "", - }, - }, - }, - want: false, - }, - { - name: "secret key ref name is empty", - fields: fields{ - Spec: KeptnMetricsProviderSpec{ - SecretKeyRef: corev1.SecretKeySelector{ - LocalObjectReference: corev1.LocalObjectReference{ - Name: "", - }, - }, - }, - }, - want: false, - }, - { - name: "secret key ref name is empty", - fields: fields{ - Spec: KeptnMetricsProviderSpec{ - SecretKeyRef: corev1.SecretKeySelector{ - Key: "some-key", - LocalObjectReference: corev1.LocalObjectReference{ - Name: "some-name", - }, - }, - }, - }, - want: true, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - p := &KeptnMetricsProvider{ - TypeMeta: tt.fields.TypeMeta, - ObjectMeta: tt.fields.ObjectMeta, - Spec: tt.fields.Spec, - } - if got := p.HasSecretDefined(); got != tt.want { - t.Errorf("HasSecretDefined() = %v, want %v", got, tt.want) - } - }) - } -}