Skip to content

Commit

Permalink
feat: add support for multiple metrics providers (#1193)
Browse files Browse the repository at this point in the history
Co-authored-by: odubajDT <93584209+odubajDT@users.noreply.github.com>
  • Loading branch information
mowies and odubajDT committed Apr 19, 2023
1 parent 2ad5d81 commit 3c465d0
Show file tree
Hide file tree
Showing 55 changed files with 1,253 additions and 251 deletions.
2 changes: 2 additions & 0 deletions .sonarcloud.properties
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ sonar.cpd.exclusions=**/test_*.go,\
operator/apis/lifecycle/v1alpha1/**/*.go,\
operator/apis/lifecycle/v1alpha2/**/*.go,\
metrics-operator/api/v1alpha1/**/*.go,\
metrics-operator/api/v1alpha2/**/*.go,\
metrics-operator/api/v1alpha3/**/*.go,\
**/zz_generated.deepcopy.go,\
**/fake/**/*.go
sonar.go.exclusions=**/vendor/**,\
Expand Down
18 changes: 18 additions & 0 deletions metrics-operator/PROJECT
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,22 @@ resources:
kind: KeptnMetric
path: github.com/keptn/lifecycle-toolkit/metrics-operator/api/v1alpha1
version: v1alpha1
- api:
crdVersion: v1
namespaced: true
controller: true
domain: keptn.sh
group: metrics
kind: KeptnMetric
path: github.com/keptn/lifecycle-toolkit/metrics-operator/api/v1alpha3
version: v1alpha3
- api:
crdVersion: v1
namespaced: true
controller: true
domain: keptn.sh
group: metrics
kind: KeptnMetricsProvider
path: github.com/keptn/lifecycle-toolkit/metrics-operator/api/v1alpha3
version: v1alpha3
version: "3"
58 changes: 58 additions & 0 deletions metrics-operator/api/v1alpha1/keptnmetric_conversion.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package v1alpha1

import (
"github.com/keptn/lifecycle-toolkit/metrics-operator/api/v1alpha3"
"sigs.k8s.io/controller-runtime/pkg/conversion"
)

// ConvertTo converts this KeptnMetric to the Hub version (v1alpha3)
func (src *KeptnMetric) ConvertTo(dstRaw conversion.Hub) error {
dst := dstRaw.(*v1alpha3.KeptnMetric)

// Spec
dst.Spec = v1alpha3.KeptnMetricSpec{
Provider: v1alpha3.ProviderRef{
Name: src.Spec.Provider.Name,
},
Query: src.Spec.Query,
FetchIntervalSeconds: src.Spec.FetchIntervalSeconds,
}

// ObjectMeta
dst.ObjectMeta = src.ObjectMeta

// Status
dst.Status = v1alpha3.KeptnMetricStatus{
Value: src.Status.Value,
RawValue: src.Status.RawValue,
LastUpdated: src.Status.LastUpdated,
}

return nil
}

// ConvertFrom converts from the Hub version (v1alpha3) to this version.
func (dst *KeptnMetric) ConvertFrom(srcRaw conversion.Hub) error {
src := srcRaw.(*v1alpha3.KeptnMetric)

// Spec
dst.Spec = KeptnMetricSpec{
Provider: ProviderRef{
Name: src.Spec.Provider.Name,
},
Query: src.Spec.Query,
FetchIntervalSeconds: src.Spec.FetchIntervalSeconds,
}

// ObjectMeta
dst.ObjectMeta = src.ObjectMeta

// Status
dst.Status = KeptnMetricStatus{
Value: src.Status.Value,
RawValue: src.Status.RawValue,
LastUpdated: src.Status.LastUpdated,
}

return nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package v1alpha1
import (
"testing"

"github.com/keptn/lifecycle-toolkit/metrics-operator/api/v1alpha2"
"github.com/keptn/lifecycle-toolkit/metrics-operator/api/v1alpha3"
"github.com/stretchr/testify/require"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/controller-runtime/pkg/conversion"
Expand All @@ -21,7 +21,7 @@ func TestKeptnMetric_ConvertTo(t *testing.T) {
name string
fields fields
wantErr bool
wantResult *v1alpha2.KeptnMetric
wantResult *v1alpha3.KeptnMetric
}{
{
name: "convert to hub",
Expand All @@ -45,19 +45,19 @@ func TestKeptnMetric_ConvertTo(t *testing.T) {
},
wantErr: false,

wantResult: &v1alpha2.KeptnMetric{
wantResult: &v1alpha3.KeptnMetric{
ObjectMeta: v1.ObjectMeta{
Name: "my-metric",
Namespace: "my-namespace",
},
Spec: v1alpha2.KeptnMetricSpec{
Provider: v1alpha2.ProviderRef{
Spec: v1alpha3.KeptnMetricSpec{
Provider: v1alpha3.ProviderRef{
Name: "my-provider",
},
Query: "my-query",
FetchIntervalSeconds: 10,
},
Status: v1alpha2.KeptnMetricStatus{
Status: v1alpha3.KeptnMetricStatus{
Value: "10.0",
RawValue: []byte("10.0"),
LastUpdated: now,
Expand All @@ -73,7 +73,7 @@ func TestKeptnMetric_ConvertTo(t *testing.T) {
Spec: tt.fields.Spec,
Status: tt.fields.Status,
}
dst := &v1alpha2.KeptnMetric{}
dst := &v1alpha3.KeptnMetric{}
err := src.ConvertTo(dst)

if tt.wantErr {
Expand Down Expand Up @@ -102,19 +102,19 @@ func TestKeptnMetric_ConvertFrom(t *testing.T) {
{
name: "convert from hub",
args: args{
srcRaw: &v1alpha2.KeptnMetric{
srcRaw: &v1alpha3.KeptnMetric{
ObjectMeta: v1.ObjectMeta{
Name: "my-metric",
Namespace: "my-namespace",
},
Spec: v1alpha2.KeptnMetricSpec{
Provider: v1alpha2.ProviderRef{
Spec: v1alpha3.KeptnMetricSpec{
Provider: v1alpha3.ProviderRef{
Name: "my-provider",
},
Query: "my-query",
FetchIntervalSeconds: 10,
},
Status: v1alpha2.KeptnMetricStatus{
Status: v1alpha3.KeptnMetricStatus{
Value: "10.0",
RawValue: []byte("10.0"),
LastUpdated: now,
Expand Down
70 changes: 6 additions & 64 deletions metrics-operator/api/v1alpha1/keptnmetric_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,66 +17,12 @@ limitations under the License.
package v1alpha1

import (
"github.com/keptn/lifecycle-toolkit/metrics-operator/api/v1alpha2"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/controller-runtime/pkg/conversion"
)

// EDIT THIS FILE! THIS IS SCAFFOLDING FOR YOU TO OWN!
// NOTE: json tags are required. Any new fields you add must have json tags for the fields to be serialized.

// ConvertTo converts this KeptnMetric to the Hub version (v1alpha2)
func (src *KeptnMetric) ConvertTo(dstRaw conversion.Hub) error {
dst := dstRaw.(*v1alpha2.KeptnMetric)

// Spec
dst.Spec = v1alpha2.KeptnMetricSpec{
Provider: v1alpha2.ProviderRef{
Name: src.Spec.Provider.Name,
},
Query: src.Spec.Query,
FetchIntervalSeconds: src.Spec.FetchIntervalSeconds,
}

// ObjectMeta
dst.ObjectMeta = src.ObjectMeta

// Status
dst.Status = v1alpha2.KeptnMetricStatus{
Value: src.Status.Value,
RawValue: src.Status.RawValue,
LastUpdated: src.Status.LastUpdated,
}

return nil
}

// ConvertFrom converts from the Hub version (v1alpha2) to this version.
func (dst *KeptnMetric) ConvertFrom(srcRaw conversion.Hub) error {
src := srcRaw.(*v1alpha2.KeptnMetric)

// Spec
dst.Spec = KeptnMetricSpec{
Provider: ProviderRef{
Name: src.Spec.Provider.Name,
},
Query: src.Spec.Query,
FetchIntervalSeconds: src.Spec.FetchIntervalSeconds,
}

// ObjectMeta
dst.ObjectMeta = src.ObjectMeta

// Status
dst.Status = KeptnMetricStatus{
Value: src.Status.Value,
RawValue: src.Status.RawValue,
LastUpdated: src.Status.LastUpdated,
}

return nil
}

// KeptnMetricSpec defines the desired state of KeptnMetric
type KeptnMetricSpec struct {
// Provider represents the provider object
Expand All @@ -103,11 +49,11 @@ type ProviderRef struct {
Name string `json:"name"`
}

//+kubebuilder:object:root=true
//+kubebuilder:subresource:status
//+kubebuilder:printcolumn:name="Provider",type=string,JSONPath=`.spec.provider.name`
//+kubebuilder:printcolumn:name="Query",type=string,JSONPath=`.spec.query`
//+kubebuilder:printcolumn:name="Value",type=string,JSONPath=`.status.value`
// +kubebuilder:object:root=true
// +kubebuilder:subresource:status
// +kubebuilder:printcolumn:name="Provider",type=string,JSONPath=`.spec.provider.name`
// +kubebuilder:printcolumn:name="Query",type=string,JSONPath=`.spec.query`
// +kubebuilder:printcolumn:name="Value",type=string,JSONPath=`.status.value`

// KeptnMetric is the Schema for the keptnmetrics API
type KeptnMetric struct {
Expand All @@ -118,7 +64,7 @@ type KeptnMetric struct {
Status KeptnMetricStatus `json:"status,omitempty"`
}

//+kubebuilder:object:root=true
// +kubebuilder:object:root=true

// KeptnMetricList contains a list of KeptnMetric
type KeptnMetricList struct {
Expand All @@ -130,7 +76,3 @@ type KeptnMetricList struct {
func init() {
SchemeBuilder.Register(&KeptnMetric{}, &KeptnMetricList{})
}

func (s *KeptnMetric) IsStatusSet() bool {
return s.Status.Value != ""
}
58 changes: 58 additions & 0 deletions metrics-operator/api/v1alpha2/keptnmetric_conversion.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package v1alpha2

import (
"github.com/keptn/lifecycle-toolkit/metrics-operator/api/v1alpha3"
"sigs.k8s.io/controller-runtime/pkg/conversion"
)

// ConvertTo converts this KeptnMetric to the Hub version (v1alpha3)
func (src *KeptnMetric) ConvertTo(dstRaw conversion.Hub) error {
dst := dstRaw.(*v1alpha3.KeptnMetric)

// Spec
dst.Spec = v1alpha3.KeptnMetricSpec{
Provider: v1alpha3.ProviderRef{
Name: src.Spec.Provider.Name,
},
Query: src.Spec.Query,
FetchIntervalSeconds: src.Spec.FetchIntervalSeconds,
}

// ObjectMeta
dst.ObjectMeta = src.ObjectMeta

// Status
dst.Status = v1alpha3.KeptnMetricStatus{
Value: src.Status.Value,
RawValue: src.Status.RawValue,
LastUpdated: src.Status.LastUpdated,
}

return nil
}

// ConvertFrom converts from the Hub version (v1alpha3) to this version.
func (dst *KeptnMetric) ConvertFrom(srcRaw conversion.Hub) error {
src := srcRaw.(*v1alpha3.KeptnMetric)

// Spec
dst.Spec = KeptnMetricSpec{
Provider: ProviderRef{
Name: src.Spec.Provider.Name,
},
Query: src.Spec.Query,
FetchIntervalSeconds: src.Spec.FetchIntervalSeconds,
}

// ObjectMeta
dst.ObjectMeta = src.ObjectMeta

// Status
dst.Status = KeptnMetricStatus{
Value: src.Status.Value,
RawValue: src.Status.RawValue,
LastUpdated: src.Status.LastUpdated,
}

return nil
}
Loading

0 comments on commit 3c465d0

Please sign in to comment.