From 5f3252d60bdc10f0f7bbc43a5d2469f7b657e5c9 Mon Sep 17 00:00:00 2001 From: Giovanni Liva Date: Thu, 15 Dec 2022 17:59:05 +0100 Subject: [PATCH 1/9] feat(operator): add version conversion rule for KeptnEvaluationProvider Signed-off-by: Giovanni Liva --- .../keptnevaluationprovider_conversion.go | 52 +++++ ...keptnevaluationprovider_conversion_test.go | 187 ++++++++++++++++++ .../keptnevaluationprovider_conversion.go | 6 + 3 files changed, 245 insertions(+) create mode 100644 operator/apis/lifecycle/v1alpha1/keptnevaluationprovider_conversion.go create mode 100644 operator/apis/lifecycle/v1alpha1/keptnevaluationprovider_conversion_test.go create mode 100644 operator/apis/lifecycle/v1alpha2/keptnevaluationprovider_conversion.go diff --git a/operator/apis/lifecycle/v1alpha1/keptnevaluationprovider_conversion.go b/operator/apis/lifecycle/v1alpha1/keptnevaluationprovider_conversion.go new file mode 100644 index 0000000000..1487602457 --- /dev/null +++ b/operator/apis/lifecycle/v1alpha1/keptnevaluationprovider_conversion.go @@ -0,0 +1,52 @@ +package v1alpha1 + +import ( + "fmt" + corev1 "k8s.io/api/core/v1" + + "github.com/keptn/lifecycle-toolkit/operator/api/v1alpha2" + "sigs.k8s.io/controller-runtime/pkg/conversion" +) + +// ConvertTo converts the src v1alpha1.KeptnApp to the hub version (v1alpha2.KeptnApp) +func (src *KeptnEvaluationProvider) ConvertTo(dstRaw conversion.Hub) error { + dst, ok := dstRaw.(*v1alpha2.KeptnEvaluationProvider) + + if !ok { + return fmt.Errorf("cannot cast KeptnEvaluationProvider to v1alpha2. Got type %T", dstRaw) + } + + // Copy equal stuff to new object + // DO NOT COPY TypeMeta + dst.ObjectMeta = src.ObjectMeta + + dst.Spec.TargetServer = src.Spec.TargetServer + + // Set sensible defaults for new fields + dst.Spec.SecretKeyRef = corev1.SecretKeySelector{ + LocalObjectReference: corev1.LocalObjectReference{ + Name: src.Spec.SecretName, + }, + Key: "apiToken", + } + + return nil +} + +// ConvertFrom converts from the hub version (v1alpha2.KeptnApp) to this version (v1alpha1.KeptnApp) +func (dst *KeptnEvaluationProvider) ConvertFrom(srcRaw conversion.Hub) error { + src, ok := srcRaw.(*v1alpha2.KeptnEvaluationProvider) + + if !ok { + return fmt.Errorf("cannot cast KeptnEvaluationProvider to v1alpha1. Got type %T", srcRaw) + } + + // Copy equal stuff to new object + // DO NOT COPY TypeMeta + dst.ObjectMeta = src.ObjectMeta + + dst.Spec.TargetServer = src.Spec.TargetServer + dst.Spec.SecretName = src.Spec.SecretKeyRef.Name + + return nil +} diff --git a/operator/apis/lifecycle/v1alpha1/keptnevaluationprovider_conversion_test.go b/operator/apis/lifecycle/v1alpha1/keptnevaluationprovider_conversion_test.go new file mode 100644 index 0000000000..e20a3fbd73 --- /dev/null +++ b/operator/apis/lifecycle/v1alpha1/keptnevaluationprovider_conversion_test.go @@ -0,0 +1,187 @@ +package v1alpha1 + +import ( + corev1 "k8s.io/api/core/v1" + v2 "sigs.k8s.io/controller-runtime/pkg/webhook/conversion/testdata/api/v2" + "testing" + + "github.com/keptn/lifecycle-toolkit/operator/api/v1alpha2" + "github.com/stretchr/testify/require" + "k8s.io/apimachinery/pkg/apis/meta/v1" +) + +func TestKeptnEvalProvider_ConvertFrom(t *testing.T) { + tests := []struct { + name string + srcObj *v1alpha2.KeptnEvaluationProvider + wantErr bool + wantObj *KeptnEvaluationProvider + }{ + { + name: "Test that conversion from v1alpha2 to v1alpha1 works", + srcObj: &v1alpha2.KeptnEvaluationProvider{ + TypeMeta: v1.TypeMeta{ + Kind: "KeptnEvaluationProvider", + APIVersion: "lifecycle.keptn.sh/v1alpha2", + }, + ObjectMeta: v1.ObjectMeta{ + Name: "some-keptn-app-name", + Namespace: "", + Labels: map[string]string{ + "some-label": "some-label-value", + }, + Annotations: map[string]string{ + "some-annotation": "some-annotation-value", + }, + }, + Spec: v1alpha2.KeptnEvaluationProviderSpec{ + TargetServer: "my-server", + SecretKeyRef: corev1.SecretKeySelector{ + LocalObjectReference: corev1.LocalObjectReference{ + Name: "my-secret-name", + }, + Key: "my-secret-key", + }, + }, + Status: v1alpha2.KeptnEvaluationProviderStatus{}, + }, + wantErr: false, + wantObj: &KeptnEvaluationProvider{ + ObjectMeta: v1.ObjectMeta{ + Name: "some-keptn-app-name", + Namespace: "", + Labels: map[string]string{ + "some-label": "some-label-value", + }, + Annotations: map[string]string{ + "some-annotation": "some-annotation-value", + }, + }, + Spec: KeptnEvaluationProviderSpec{ + TargetServer: "my-server", + SecretName: "my-secret-name", + }, + }, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + dst := &KeptnEvaluationProvider{ + TypeMeta: v1.TypeMeta{}, + ObjectMeta: v1.ObjectMeta{}, + Spec: KeptnEvaluationProviderSpec{}, + Status: KeptnEvaluationProviderStatus{}, + } + if err := dst.ConvertFrom(tt.srcObj); (err != nil) != tt.wantErr { + t.Errorf("ConvertFrom() error = %v, wantErr %v", err, tt.wantErr) + } + if tt.wantObj != nil { + require.Equal(t, tt.wantObj, dst, "Object was not converted correctly") + } + }) + } +} + +func TestKeptnEvalProvider_ConvertTo(t *testing.T) { + tests := []struct { + name string + src *KeptnEvaluationProvider + wantErr bool + wantObj *v1alpha2.KeptnEvaluationProvider + }{ + { + name: "Test that conversion from v1alpha1 to v1alpha2 works", + src: &KeptnEvaluationProvider{ + TypeMeta: v1.TypeMeta{ + Kind: "KeptnEvaluationProvider", + APIVersion: "lifecycle.keptn.sh/v1alpha1", + }, + ObjectMeta: v1.ObjectMeta{ + Name: "some-keptn-app-name", + Namespace: "", + Labels: map[string]string{ + "some-label": "some-label-value", + }, + Annotations: map[string]string{ + "some-annotation": "some-annotation-value", + }, + }, + Spec: KeptnEvaluationProviderSpec{ + TargetServer: "my-server", + SecretName: "my-secret-name", + }, + Status: KeptnEvaluationProviderStatus{}, + }, + wantErr: false, + wantObj: &v1alpha2.KeptnEvaluationProvider{ + ObjectMeta: v1.ObjectMeta{ + Name: "some-keptn-app-name", + Namespace: "", + Labels: map[string]string{ + "some-label": "some-label-value", + }, + Annotations: map[string]string{ + "some-annotation": "some-annotation-value", + }, + }, + Spec: v1alpha2.KeptnEvaluationProviderSpec{ + TargetServer: "my-server", + SecretKeyRef: corev1.SecretKeySelector{ + LocalObjectReference: corev1.LocalObjectReference{ + Name: "my-secret-name", + }, + Key: "apiToken", + }, + }, + Status: v1alpha2.KeptnEvaluationProviderStatus{}, + }, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + dst := v1alpha2.KeptnEvaluationProvider{ + TypeMeta: v1.TypeMeta{}, + ObjectMeta: v1.ObjectMeta{}, + Spec: v1alpha2.KeptnEvaluationProviderSpec{}, + Status: v1alpha2.KeptnEvaluationProviderStatus{}, + } + if err := tt.src.ConvertTo(&dst); (err != nil) != tt.wantErr { + t.Errorf("ConvertTo() error = %v, wantErr %v", err, tt.wantErr) + } + if tt.wantObj != nil { + require.Equal(t, tt.wantObj, &dst, "Object was not converted correctly") + } + }) + } +} + +func TestKeptnEvalProvider_ConvertFrom_Errorcase(t *testing.T) { + // A random different object is used here to simulate a different API version + testObj := v2.ExternalJob{} + + dst := &KeptnEvaluationProvider{ + TypeMeta: v1.TypeMeta{}, + ObjectMeta: v1.ObjectMeta{}, + Spec: KeptnEvaluationProviderSpec{}, + Status: KeptnEvaluationProviderStatus{}, + } + + if err := dst.ConvertFrom(&testObj); err == nil { + t.Errorf("ConvertFrom() error = %v", err) + } else { + require.Contains(t, err.Error(), "cannot cast KeptnEvaluationProvider to v1alpha1") + } +} + +func TestKeptnEvalProvider_ConvertTo_Errorcase(t *testing.T) { + testObj := KeptnEvaluationProvider{} + + // A random different object is used here to simulate a different API version + dst := v2.ExternalJob{} + + if err := testObj.ConvertTo(&dst); err == nil { + t.Errorf("ConvertTo() error = %v", err) + } else { + require.Contains(t, err.Error(), "cannot cast KeptnEvaluationProvider to v1alpha2") + } +} diff --git a/operator/apis/lifecycle/v1alpha2/keptnevaluationprovider_conversion.go b/operator/apis/lifecycle/v1alpha2/keptnevaluationprovider_conversion.go new file mode 100644 index 0000000000..35573e8f3f --- /dev/null +++ b/operator/apis/lifecycle/v1alpha2/keptnevaluationprovider_conversion.go @@ -0,0 +1,6 @@ +package v1alpha2 + +// Hub is the stub function to make the API conversion pattern with hub and spokes complete +func (*KeptnEvaluationProvider) Hub() { + // Hub() needed to implement interface +} From 36dc637e326afefada5ac14d9d71c039da364a57 Mon Sep 17 00:00:00 2001 From: Giovanni Liva Date: Mon, 9 Jan 2023 15:25:20 +0100 Subject: [PATCH 2/9] address feedback Signed-off-by: Giovanni Liva --- .../v1alpha1/keptnevaluationprovider_conversion.go | 10 +++++++--- .../keptnevaluationprovider_conversion_test.go | 4 ++-- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/operator/apis/lifecycle/v1alpha1/keptnevaluationprovider_conversion.go b/operator/apis/lifecycle/v1alpha1/keptnevaluationprovider_conversion.go index 1487602457..8bc3ad8723 100644 --- a/operator/apis/lifecycle/v1alpha1/keptnevaluationprovider_conversion.go +++ b/operator/apis/lifecycle/v1alpha1/keptnevaluationprovider_conversion.go @@ -1,6 +1,7 @@ package v1alpha1 import ( + "errors" "fmt" corev1 "k8s.io/api/core/v1" @@ -8,12 +9,15 @@ import ( "sigs.k8s.io/controller-runtime/pkg/conversion" ) -// ConvertTo converts the src v1alpha1.KeptnApp to the hub version (v1alpha2.KeptnApp) +var ErrCastTo = errors.New("cannot be cast KeptnEvaluationProvider to v1alpha2") +var ErrCastFrom = errors.New("cannot be cast KeptnEvaluationProvider to v1alpha1") + +// ConvertTo converts the src v1alpha1.KeptnEvaluationProvider to the hub version (v1alpha2.KeptnEvaluationProvider) func (src *KeptnEvaluationProvider) ConvertTo(dstRaw conversion.Hub) error { dst, ok := dstRaw.(*v1alpha2.KeptnEvaluationProvider) if !ok { - return fmt.Errorf("cannot cast KeptnEvaluationProvider to v1alpha2. Got type %T", dstRaw) + return fmt.Errorf("type %T %w", dstRaw, ErrCastTo) } // Copy equal stuff to new object @@ -38,7 +42,7 @@ func (dst *KeptnEvaluationProvider) ConvertFrom(srcRaw conversion.Hub) error { src, ok := srcRaw.(*v1alpha2.KeptnEvaluationProvider) if !ok { - return fmt.Errorf("cannot cast KeptnEvaluationProvider to v1alpha1. Got type %T", srcRaw) + return fmt.Errorf("type %T %w", srcRaw, ErrCastFrom) } // Copy equal stuff to new object diff --git a/operator/apis/lifecycle/v1alpha1/keptnevaluationprovider_conversion_test.go b/operator/apis/lifecycle/v1alpha1/keptnevaluationprovider_conversion_test.go index e20a3fbd73..cb0319365c 100644 --- a/operator/apis/lifecycle/v1alpha1/keptnevaluationprovider_conversion_test.go +++ b/operator/apis/lifecycle/v1alpha1/keptnevaluationprovider_conversion_test.go @@ -169,7 +169,7 @@ func TestKeptnEvalProvider_ConvertFrom_Errorcase(t *testing.T) { if err := dst.ConvertFrom(&testObj); err == nil { t.Errorf("ConvertFrom() error = %v", err) } else { - require.Contains(t, err.Error(), "cannot cast KeptnEvaluationProvider to v1alpha1") + require.ErrorIs(t, err, ErrCastFrom) } } @@ -182,6 +182,6 @@ func TestKeptnEvalProvider_ConvertTo_Errorcase(t *testing.T) { if err := testObj.ConvertTo(&dst); err == nil { t.Errorf("ConvertTo() error = %v", err) } else { - require.Contains(t, err.Error(), "cannot cast KeptnEvaluationProvider to v1alpha2") + require.ErrorIs(t, err, ErrCastTo) } } From 46e34beefb31582830ce3c76a8939b2f49261df0 Mon Sep 17 00:00:00 2001 From: Giovanni Liva Date: Mon, 9 Jan 2023 15:51:39 +0100 Subject: [PATCH 3/9] add webhook Signed-off-by: Giovanni Liva --- operator/PROJECT | 3 +++ .../keptnevaluationprovider_conversion.go | 2 +- .../keptnevaluationprovider_webhook.go | 27 +++++++++++++++++++ operator/kubebuilder | 1 - operator/main.go | 17 ++++++++++++ 5 files changed, 48 insertions(+), 2 deletions(-) create mode 100644 operator/apis/lifecycle/v1alpha2/keptnevaluationprovider_webhook.go delete mode 100644 operator/kubebuilder diff --git a/operator/PROJECT b/operator/PROJECT index cfbaff34ce..ff0084804b 100644 --- a/operator/PROJECT +++ b/operator/PROJECT @@ -159,6 +159,9 @@ resources: kind: KeptnEvaluationProvider path: github.com/keptn/lifecycle-toolkit/operator/apis/lifecycle/v1alpha2 version: v1alpha2 + webhooks: + conversion: true + webhookVersion: v1 - api: crdVersion: v1 namespaced: true diff --git a/operator/apis/lifecycle/v1alpha1/keptnevaluationprovider_conversion.go b/operator/apis/lifecycle/v1alpha1/keptnevaluationprovider_conversion.go index 8bc3ad8723..a7b96201e2 100644 --- a/operator/apis/lifecycle/v1alpha1/keptnevaluationprovider_conversion.go +++ b/operator/apis/lifecycle/v1alpha1/keptnevaluationprovider_conversion.go @@ -37,7 +37,7 @@ func (src *KeptnEvaluationProvider) ConvertTo(dstRaw conversion.Hub) error { return nil } -// ConvertFrom converts from the hub version (v1alpha2.KeptnApp) to this version (v1alpha1.KeptnApp) +// ConvertFrom converts from the hub version (v1alpha2.KeptnEvaluationProvider) to this version (v1alpha1.KeptnEvaluationProvider) func (dst *KeptnEvaluationProvider) ConvertFrom(srcRaw conversion.Hub) error { src, ok := srcRaw.(*v1alpha2.KeptnEvaluationProvider) diff --git a/operator/apis/lifecycle/v1alpha2/keptnevaluationprovider_webhook.go b/operator/apis/lifecycle/v1alpha2/keptnevaluationprovider_webhook.go new file mode 100644 index 0000000000..77eaada810 --- /dev/null +++ b/operator/apis/lifecycle/v1alpha2/keptnevaluationprovider_webhook.go @@ -0,0 +1,27 @@ +/* +Copyright 2022. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package v1alpha2 + +import ( + ctrl "sigs.k8s.io/controller-runtime" +) + +func (r *KeptnEvaluationProvider) SetupWebhookWithManager(mgr ctrl.Manager) error { + return ctrl.NewWebhookManagedBy(mgr). + For(r). + Complete() +} diff --git a/operator/kubebuilder b/operator/kubebuilder deleted file mode 100644 index 8537307691..0000000000 --- a/operator/kubebuilder +++ /dev/null @@ -1 +0,0 @@ -Not Found \ No newline at end of file diff --git a/operator/main.go b/operator/main.go index c9eabe0f6b..54a1e56edd 100644 --- a/operator/main.go +++ b/operator/main.go @@ -26,6 +26,7 @@ import ( "time" "github.com/kelseyhightower/envconfig" + lifecyclev1alpha1 "github.com/keptn/lifecycle-toolkit/operator/apis/lifecycle/v1alpha1" lifecyclev1alpha2 "github.com/keptn/lifecycle-toolkit/operator/apis/lifecycle/v1alpha2" "github.com/keptn/lifecycle-toolkit/operator/apis/lifecycle/v1alpha2/common" @@ -38,6 +39,7 @@ import ( "github.com/keptn/lifecycle-toolkit/operator/controllers/lifecycle/keptnworkload" "github.com/keptn/lifecycle-toolkit/operator/controllers/lifecycle/keptnworkloadinstance" "github.com/keptn/lifecycle-toolkit/operator/webhooks" + "github.com/prometheus/client_golang/prometheus/promhttp" "go.opentelemetry.io/otel" "go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc" @@ -55,6 +57,17 @@ import ( "k8s.io/apimachinery/pkg/runtime" utilruntime "k8s.io/apimachinery/pkg/util/runtime" clientgoscheme "k8s.io/client-go/kubernetes/scheme" + + lifecyclev1alpha1 "github.com/keptn/lifecycle-toolkit/operator/api/v1alpha1" + lifecyclev1alpha2 "github.com/keptn/lifecycle-toolkit/operator/api/v1alpha2" + "github.com/keptn/lifecycle-toolkit/operator/api/v1alpha2/common" + "github.com/keptn/lifecycle-toolkit/operator/controllers/keptnapp" + "github.com/keptn/lifecycle-toolkit/operator/controllers/keptnappversion" + "github.com/keptn/lifecycle-toolkit/operator/controllers/keptnevaluation" + "github.com/keptn/lifecycle-toolkit/operator/controllers/keptntask" + "github.com/keptn/lifecycle-toolkit/operator/controllers/keptntaskdefinition" + "github.com/keptn/lifecycle-toolkit/operator/controllers/keptnworkload" + "github.com/keptn/lifecycle-toolkit/operator/controllers/keptnworkloadinstance" // Import all Kubernetes client auth plugins (e.g. Azure, GCP, OIDC, etc.) // to ensure that exec-entrypoint and run can make use of them. _ "k8s.io/client-go/plugin/pkg/client/auth" @@ -348,6 +361,10 @@ func main() { setupLog.Error(err, "unable to create webhook", "webhook", "KeptnApp") os.Exit(1) } + if err = (&lifecyclev1alpha2.KeptnEvaluationProvider{}).SetupWebhookWithManager(mgr); err != nil { + setupLog.Error(err, "unable to create webhook", "webhook", "KeptnEvaluationProvider") + os.Exit(1) + } //+kubebuilder:scaffold:builder err = meter.RegisterCallback( From 70a3c60150842abe3f83c659fb0f0252361d9863 Mon Sep 17 00:00:00 2001 From: Giovanni Liva Date: Mon, 9 Jan 2023 16:04:34 +0100 Subject: [PATCH 4/9] fix imports Signed-off-by: Giovanni Liva --- operator/main.go | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/operator/main.go b/operator/main.go index 54a1e56edd..1d88505260 100644 --- a/operator/main.go +++ b/operator/main.go @@ -58,16 +58,6 @@ import ( utilruntime "k8s.io/apimachinery/pkg/util/runtime" clientgoscheme "k8s.io/client-go/kubernetes/scheme" - lifecyclev1alpha1 "github.com/keptn/lifecycle-toolkit/operator/api/v1alpha1" - lifecyclev1alpha2 "github.com/keptn/lifecycle-toolkit/operator/api/v1alpha2" - "github.com/keptn/lifecycle-toolkit/operator/api/v1alpha2/common" - "github.com/keptn/lifecycle-toolkit/operator/controllers/keptnapp" - "github.com/keptn/lifecycle-toolkit/operator/controllers/keptnappversion" - "github.com/keptn/lifecycle-toolkit/operator/controllers/keptnevaluation" - "github.com/keptn/lifecycle-toolkit/operator/controllers/keptntask" - "github.com/keptn/lifecycle-toolkit/operator/controllers/keptntaskdefinition" - "github.com/keptn/lifecycle-toolkit/operator/controllers/keptnworkload" - "github.com/keptn/lifecycle-toolkit/operator/controllers/keptnworkloadinstance" // Import all Kubernetes client auth plugins (e.g. Azure, GCP, OIDC, etc.) // to ensure that exec-entrypoint and run can make use of them. _ "k8s.io/client-go/plugin/pkg/client/auth" From 7f448df7ce4c22e7f2f105cc159182ec117b7399 Mon Sep 17 00:00:00 2001 From: Giovanni Liva Date: Mon, 9 Jan 2023 16:10:20 +0100 Subject: [PATCH 5/9] unify errors and fix import Signed-off-by: Giovanni Liva --- operator/apis/lifecycle/v1alpha1/common/common.go | 3 +++ .../v1alpha1/keptnevaluationprovider_conversion.go | 9 +++------ operator/main.go | 2 -- 3 files changed, 6 insertions(+), 8 deletions(-) diff --git a/operator/apis/lifecycle/v1alpha1/common/common.go b/operator/apis/lifecycle/v1alpha1/common/common.go index a2173b6078..39ef7101d9 100644 --- a/operator/apis/lifecycle/v1alpha1/common/common.go +++ b/operator/apis/lifecycle/v1alpha1/common/common.go @@ -1,6 +1,7 @@ package common import ( + "errors" "fmt" "math/rand" @@ -31,6 +32,8 @@ const MaxWorkloadNameLength = 25 const MaxTaskNameLength = 25 const MaxVersionLength = 12 +var CannotCastKeptnEvaluationProviderErr = errors.New("cannot be cast KeptnEvaluationProvider to v1alpha2") + type KeptnState string const ( diff --git a/operator/apis/lifecycle/v1alpha1/keptnevaluationprovider_conversion.go b/operator/apis/lifecycle/v1alpha1/keptnevaluationprovider_conversion.go index a7b96201e2..c0c6c5cb06 100644 --- a/operator/apis/lifecycle/v1alpha1/keptnevaluationprovider_conversion.go +++ b/operator/apis/lifecycle/v1alpha1/keptnevaluationprovider_conversion.go @@ -1,23 +1,20 @@ package v1alpha1 import ( - "errors" "fmt" + "github.com/keptn/lifecycle-toolkit/operator/apis/lifecycle/v1alpha1/common" corev1 "k8s.io/api/core/v1" - "github.com/keptn/lifecycle-toolkit/operator/api/v1alpha2" + "github.com/keptn/lifecycle-toolkit/operator/apis/lifecycle/v1alpha2" "sigs.k8s.io/controller-runtime/pkg/conversion" ) -var ErrCastTo = errors.New("cannot be cast KeptnEvaluationProvider to v1alpha2") -var ErrCastFrom = errors.New("cannot be cast KeptnEvaluationProvider to v1alpha1") - // ConvertTo converts the src v1alpha1.KeptnEvaluationProvider to the hub version (v1alpha2.KeptnEvaluationProvider) func (src *KeptnEvaluationProvider) ConvertTo(dstRaw conversion.Hub) error { dst, ok := dstRaw.(*v1alpha2.KeptnEvaluationProvider) if !ok { - return fmt.Errorf("type %T %w", dstRaw, ErrCastTo) + return fmt.Errorf("type %T %w", dstRaw, common.CannotCastKeptnEvaluationProviderErr) } // Copy equal stuff to new object diff --git a/operator/main.go b/operator/main.go index 1d88505260..118a515942 100644 --- a/operator/main.go +++ b/operator/main.go @@ -26,7 +26,6 @@ import ( "time" "github.com/kelseyhightower/envconfig" - lifecyclev1alpha1 "github.com/keptn/lifecycle-toolkit/operator/apis/lifecycle/v1alpha1" lifecyclev1alpha2 "github.com/keptn/lifecycle-toolkit/operator/apis/lifecycle/v1alpha2" "github.com/keptn/lifecycle-toolkit/operator/apis/lifecycle/v1alpha2/common" @@ -39,7 +38,6 @@ import ( "github.com/keptn/lifecycle-toolkit/operator/controllers/lifecycle/keptnworkload" "github.com/keptn/lifecycle-toolkit/operator/controllers/lifecycle/keptnworkloadinstance" "github.com/keptn/lifecycle-toolkit/operator/webhooks" - "github.com/prometheus/client_golang/prometheus/promhttp" "go.opentelemetry.io/otel" "go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc" From ff3b7f67b0e098a5a353a3e4d6e194a9b74045b5 Mon Sep 17 00:00:00 2001 From: Giovanni Liva Date: Mon, 9 Jan 2023 16:11:16 +0100 Subject: [PATCH 6/9] missing change Signed-off-by: Giovanni Liva --- .../lifecycle/v1alpha1/keptnevaluationprovider_conversion.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/operator/apis/lifecycle/v1alpha1/keptnevaluationprovider_conversion.go b/operator/apis/lifecycle/v1alpha1/keptnevaluationprovider_conversion.go index c0c6c5cb06..e8282e8dde 100644 --- a/operator/apis/lifecycle/v1alpha1/keptnevaluationprovider_conversion.go +++ b/operator/apis/lifecycle/v1alpha1/keptnevaluationprovider_conversion.go @@ -39,7 +39,7 @@ func (dst *KeptnEvaluationProvider) ConvertFrom(srcRaw conversion.Hub) error { src, ok := srcRaw.(*v1alpha2.KeptnEvaluationProvider) if !ok { - return fmt.Errorf("type %T %w", srcRaw, ErrCastFrom) + return fmt.Errorf("type %T %w", srcRaw, common.CannotCastKeptnEvaluationProviderErr) } // Copy equal stuff to new object From 0c2a356f5ddf74f7b599b9535f5b94e97d8068f9 Mon Sep 17 00:00:00 2001 From: Giovanni Liva Date: Mon, 9 Jan 2023 16:32:52 +0100 Subject: [PATCH 7/9] yet another import path fixed Signed-off-by: Giovanni Liva --- .../v1alpha1/keptnevaluationprovider_conversion_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/operator/apis/lifecycle/v1alpha1/keptnevaluationprovider_conversion_test.go b/operator/apis/lifecycle/v1alpha1/keptnevaluationprovider_conversion_test.go index cb0319365c..6470a87104 100644 --- a/operator/apis/lifecycle/v1alpha1/keptnevaluationprovider_conversion_test.go +++ b/operator/apis/lifecycle/v1alpha1/keptnevaluationprovider_conversion_test.go @@ -5,7 +5,7 @@ import ( v2 "sigs.k8s.io/controller-runtime/pkg/webhook/conversion/testdata/api/v2" "testing" - "github.com/keptn/lifecycle-toolkit/operator/api/v1alpha2" + "github.com/keptn/lifecycle-toolkit/operator/apis/lifecycle/v1alpha2" "github.com/stretchr/testify/require" "k8s.io/apimachinery/pkg/apis/meta/v1" ) From 64110adf7bc3ed4014bb67e33cfb601e3be368a5 Mon Sep 17 00:00:00 2001 From: Giovanni Liva Date: Mon, 9 Jan 2023 16:45:06 +0100 Subject: [PATCH 8/9] yet another import path fixed Signed-off-by: Giovanni Liva --- operator/apis/lifecycle/v1alpha1/common/common.go | 2 +- .../lifecycle/v1alpha1/keptnevaluationprovider_conversion.go | 4 ++-- .../v1alpha1/keptnevaluationprovider_conversion_test.go | 5 +++-- 3 files changed, 6 insertions(+), 5 deletions(-) diff --git a/operator/apis/lifecycle/v1alpha1/common/common.go b/operator/apis/lifecycle/v1alpha1/common/common.go index 39ef7101d9..5c2e69d21a 100644 --- a/operator/apis/lifecycle/v1alpha1/common/common.go +++ b/operator/apis/lifecycle/v1alpha1/common/common.go @@ -32,7 +32,7 @@ const MaxWorkloadNameLength = 25 const MaxTaskNameLength = 25 const MaxVersionLength = 12 -var CannotCastKeptnEvaluationProviderErr = errors.New("cannot be cast KeptnEvaluationProvider to v1alpha2") +var ErrCannotCastKeptnEvaluationProvider = errors.New("cannot be cast KeptnEvaluationProvider to v1alpha2") type KeptnState string diff --git a/operator/apis/lifecycle/v1alpha1/keptnevaluationprovider_conversion.go b/operator/apis/lifecycle/v1alpha1/keptnevaluationprovider_conversion.go index e8282e8dde..43b066b383 100644 --- a/operator/apis/lifecycle/v1alpha1/keptnevaluationprovider_conversion.go +++ b/operator/apis/lifecycle/v1alpha1/keptnevaluationprovider_conversion.go @@ -14,7 +14,7 @@ func (src *KeptnEvaluationProvider) ConvertTo(dstRaw conversion.Hub) error { dst, ok := dstRaw.(*v1alpha2.KeptnEvaluationProvider) if !ok { - return fmt.Errorf("type %T %w", dstRaw, common.CannotCastKeptnEvaluationProviderErr) + return fmt.Errorf("type %T %w", dstRaw, common.ErrCannotCastKeptnEvaluationProvider) } // Copy equal stuff to new object @@ -39,7 +39,7 @@ func (dst *KeptnEvaluationProvider) ConvertFrom(srcRaw conversion.Hub) error { src, ok := srcRaw.(*v1alpha2.KeptnEvaluationProvider) if !ok { - return fmt.Errorf("type %T %w", srcRaw, common.CannotCastKeptnEvaluationProviderErr) + return fmt.Errorf("type %T %w", srcRaw, common.ErrCannotCastKeptnEvaluationProvider) } // Copy equal stuff to new object diff --git a/operator/apis/lifecycle/v1alpha1/keptnevaluationprovider_conversion_test.go b/operator/apis/lifecycle/v1alpha1/keptnevaluationprovider_conversion_test.go index 6470a87104..673ba1e60a 100644 --- a/operator/apis/lifecycle/v1alpha1/keptnevaluationprovider_conversion_test.go +++ b/operator/apis/lifecycle/v1alpha1/keptnevaluationprovider_conversion_test.go @@ -1,6 +1,7 @@ package v1alpha1 import ( + "github.com/keptn/lifecycle-toolkit/operator/apis/lifecycle/v1alpha1/common" corev1 "k8s.io/api/core/v1" v2 "sigs.k8s.io/controller-runtime/pkg/webhook/conversion/testdata/api/v2" "testing" @@ -169,7 +170,7 @@ func TestKeptnEvalProvider_ConvertFrom_Errorcase(t *testing.T) { if err := dst.ConvertFrom(&testObj); err == nil { t.Errorf("ConvertFrom() error = %v", err) } else { - require.ErrorIs(t, err, ErrCastFrom) + require.ErrorIs(t, err, common.ErrCannotCastKeptnEvaluationProvider) } } @@ -182,6 +183,6 @@ func TestKeptnEvalProvider_ConvertTo_Errorcase(t *testing.T) { if err := testObj.ConvertTo(&dst); err == nil { t.Errorf("ConvertTo() error = %v", err) } else { - require.ErrorIs(t, err, ErrCastTo) + require.ErrorIs(t, err, common.ErrCannotCastKeptnEvaluationProvider) } } From b33247fc48895ff63e5b99f258b0206b700696f3 Mon Sep 17 00:00:00 2001 From: Giovanni Liva Date: Tue, 10 Jan 2023 07:50:54 +0100 Subject: [PATCH 9/9] address feedback Signed-off-by: Giovanni Liva --- .gitignore | 3 +++ operator/config/crd/kustomization.yaml | 4 ++-- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index 38310eeae5..d845e2c8e9 100644 --- a/.gitignore +++ b/.gitignore @@ -32,3 +32,6 @@ manifests/ /examples/observability/assets/backups/ /dashboards/grafana/output/ + +## Kubebuilder +**/kubebuilder diff --git a/operator/config/crd/kustomization.yaml b/operator/config/crd/kustomization.yaml index 04469e4b9a..f700168cc1 100644 --- a/operator/config/crd/kustomization.yaml +++ b/operator/config/crd/kustomization.yaml @@ -23,7 +23,7 @@ patchesStrategicMerge: #- patches/webhook_in_keptnworkloadinstances.yaml #- patches/webhook_in_keptnappversions.yaml #- patches/webhook_in_keptnevaluationdefinitions.yaml -#- patches/webhook_in_keptnevaluationproviders.yaml +- patches/webhook_in_keptnevaluationproviders.yaml #- patches/webhook_in_keptnevaluations.yaml #+kubebuilder:scaffold:crdkustomizewebhookpatch @@ -36,7 +36,7 @@ patchesStrategicMerge: #- patches/cainjection_in_keptnworkloadinstances.yaml #- patches/cainjection_in_keptnappversions.yaml #- patches/cainjection_in_keptnevaluationdefinitions.yaml -#- patches/cainjection_in_keptnevaluationproviders.yaml +- patches/cainjection_in_keptnevaluationproviders.yaml #- patches/cainjection_in_keptnevaluations.yaml #+kubebuilder:scaffold:crdkustomizecainjectionpatch