Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add support for multiple metrics providers #1193

Merged
merged 21 commits into from
Apr 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
b3d4309
add first boilerplate code version of metrics v1alpha3
mowies Apr 6, 2023
79216b3
update sonarcloud settings to remove duplication checks from metrics-…
mowies Apr 11, 2023
4adcbe2
change hub version to v1alpha3, refactor v1alpha1 conversion function…
mowies Apr 6, 2023
b1ce19a
add conversion webhook functions to metrics v1alpha2
mowies Apr 11, 2023
0b4e311
switch imports to v1alpha3
mowies Apr 11, 2023
b344a7a
fix linter errors
mowies Apr 11, 2023
ee1d6d3
add type field to metrics provider v1alpha3, fix samples
mowies Apr 11, 2023
c193ffa
implement new behaviour while removing the old one
mowies Apr 11, 2023
d9fcad2
add backwards compatibility
mowies Apr 11, 2023
5ea3882
remove unneeded functions from keptnmetric API
mowies Apr 11, 2023
15e1e54
adjust metrics integration test to use a real promtheus metrics provi…
mowies Apr 17, 2023
b76bdb8
revert whitespace changes
mowies Apr 17, 2023
a9253d8
enhance metrics integration test with second provider without type
mowies Apr 17, 2023
9dfaf52
add unit test with old provider without type
mowies Apr 18, 2023
4d74a41
adjust metrics integration test to check if 2 providers of the same t…
mowies Apr 18, 2023
2d9c98a
Update metrics-operator/api/v1alpha1/keptnmetric_conversion.go
mowies Apr 18, 2023
d6d7b49
move hub function to separate file
mowies Apr 18, 2023
dfd4b3c
add unit test
mowies Apr 18, 2023
89fdbb6
extract provider type name decider function
mowies Apr 18, 2023
ecbb9e4
Update metrics-operator/controllers/common/providers/provider.go
mowies Apr 18, 2023
230e813
implement flos suggestion
mowies Apr 18, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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