From 4b082eebe4427c06a0fdf4e2aa37a190b8c008f9 Mon Sep 17 00:00:00 2001 From: odubajDT Date: Wed, 15 Nov 2023 15:41:37 +0100 Subject: [PATCH] chore: clean up unused API code after moving to v1beta1 Signed-off-by: odubajDT --- .../featureflagconfiguration_conversion.go | 20 -- .../featureflagconfiguration_types.go | 31 -- .../featureflagconfiguration_types_test.go | 80 ----- .../featureflagconfiguration_webhook.go | 9 - .../flagsourceconfiguration_conversion.go | 20 -- .../v1alpha1/flagsourceconfiguration_types.go | 252 -------------- .../flagsourceconfiguration_types_test.go | 303 ----------------- .../flagsourceconfiguration_webhook.go | 11 - apis/core/v1alpha2/common/common.go | 6 - .../featureflagconfiguration_conversion.go | 112 ------ ...eatureflagconfiguration_conversion_test.go | 318 ------------------ .../featureflagconfiguration_types.go | 8 - .../flagsourceconfiguration_conversion.go | 81 ----- ...flagsourceconfiguration_conversion_test.go | 211 ------------ .../v1alpha2/flagsourceconfiguration_types.go | 5 - apis/core/v1alpha3/common/common.go | 5 - .../flagsourceconfiguration_conversion.go | 113 ------- ...flagsourceconfiguration_conversion_test.go | 276 --------------- .../v1alpha3/flagsourceconfiguration_types.go | 8 - 19 files changed, 1869 deletions(-) delete mode 100644 apis/core/v1alpha1/featureflagconfiguration_conversion.go delete mode 100644 apis/core/v1alpha1/featureflagconfiguration_types_test.go delete mode 100644 apis/core/v1alpha1/featureflagconfiguration_webhook.go delete mode 100644 apis/core/v1alpha1/flagsourceconfiguration_conversion.go delete mode 100644 apis/core/v1alpha1/flagsourceconfiguration_types_test.go delete mode 100644 apis/core/v1alpha1/flagsourceconfiguration_webhook.go delete mode 100644 apis/core/v1alpha2/common/common.go delete mode 100644 apis/core/v1alpha2/featureflagconfiguration_conversion.go delete mode 100644 apis/core/v1alpha2/featureflagconfiguration_conversion_test.go delete mode 100644 apis/core/v1alpha2/flagsourceconfiguration_conversion.go delete mode 100644 apis/core/v1alpha2/flagsourceconfiguration_conversion_test.go delete mode 100644 apis/core/v1alpha3/common/common.go delete mode 100644 apis/core/v1alpha3/flagsourceconfiguration_conversion.go delete mode 100644 apis/core/v1alpha3/flagsourceconfiguration_conversion_test.go diff --git a/apis/core/v1alpha1/featureflagconfiguration_conversion.go b/apis/core/v1alpha1/featureflagconfiguration_conversion.go deleted file mode 100644 index 1d9e0624a..000000000 --- a/apis/core/v1alpha1/featureflagconfiguration_conversion.go +++ /dev/null @@ -1,20 +0,0 @@ -/* -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 v1alpha1 - -// Hub marks this type as a conversion hub. -func (ffc *FeatureFlagConfiguration) Hub() {} diff --git a/apis/core/v1alpha1/featureflagconfiguration_types.go b/apis/core/v1alpha1/featureflagconfiguration_types.go index bfc06c8fa..6b214eadb 100644 --- a/apis/core/v1alpha1/featureflagconfiguration_types.go +++ b/apis/core/v1alpha1/featureflagconfiguration_types.go @@ -17,7 +17,6 @@ limitations under the License. package v1alpha1 import ( - "github.com/open-feature/open-feature-operator/common/utils" corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" ) @@ -107,33 +106,3 @@ type FeatureFlagConfigurationList struct { func init() { SchemeBuilder.Register(&FeatureFlagConfiguration{}, &FeatureFlagConfigurationList{}) } - -func (ff *FeatureFlagConfiguration) GetReference() metav1.OwnerReference { - return metav1.OwnerReference{ - APIVersion: ff.APIVersion, - Kind: ff.Kind, - Name: ff.Name, - UID: ff.UID, - Controller: utils.TrueVal(), - } -} - -func (ff *FeatureFlagConfiguration) GenerateConfigMap(name string, namespace string, references []metav1.OwnerReference) corev1.ConfigMap { - return corev1.ConfigMap{ - ObjectMeta: metav1.ObjectMeta{ - Name: name, - Namespace: namespace, - Annotations: map[string]string{ - "openfeature.dev/featureflagconfiguration": name, - }, - OwnerReferences: references, - }, - Data: map[string]string{ - utils.FeatureFlagConfigMapKey(namespace, name): ff.Spec.FeatureFlagSpec, - }, - } -} - -func (p *FeatureFlagServiceProvider) IsSet() bool { - return p != nil && p.Name != "" -} diff --git a/apis/core/v1alpha1/featureflagconfiguration_types_test.go b/apis/core/v1alpha1/featureflagconfiguration_types_test.go deleted file mode 100644 index 8a60850a7..000000000 --- a/apis/core/v1alpha1/featureflagconfiguration_types_test.go +++ /dev/null @@ -1,80 +0,0 @@ -package v1alpha1 - -import ( - "testing" - - "github.com/open-feature/open-feature-operator/common/utils" - "github.com/stretchr/testify/require" - corev1 "k8s.io/api/core/v1" - v1 "k8s.io/apimachinery/pkg/apis/meta/v1" - "k8s.io/apimachinery/pkg/types" -) - -func Test_FeatureFlagConfiguration(t *testing.T) { - ffConfig := FeatureFlagConfiguration{ - ObjectMeta: v1.ObjectMeta{ - Name: "ffconf1", - Namespace: "test", - OwnerReferences: []v1.OwnerReference{ - { - APIVersion: "ver", - Kind: "kind", - Name: "ffconf1", - UID: types.UID("5"), - Controller: utils.TrueVal(), - }, - }, - }, - Spec: FeatureFlagConfigurationSpec{ - FeatureFlagSpec: "spec", - }, - } - - name := "cmname" - namespace := "cmnamespace" - references := []v1.OwnerReference{ - { - APIVersion: "ver", - Kind: "kind", - Name: "ffconf1", - UID: types.UID("5"), - Controller: utils.TrueVal(), - }, - } - - require.Equal(t, v1.OwnerReference{ - APIVersion: ffConfig.APIVersion, - Kind: ffConfig.Kind, - Name: ffConfig.Name, - UID: ffConfig.UID, - Controller: utils.TrueVal(), - }, ffConfig.GetReference()) - - require.Equal(t, corev1.ConfigMap{ - ObjectMeta: v1.ObjectMeta{ - Name: name, - Namespace: namespace, - Annotations: map[string]string{ - "openfeature.dev/featureflagconfiguration": name, - }, - OwnerReferences: references, - }, - Data: map[string]string{ - "cmnamespace_cmname.flagd.json": "spec", - }, - }, ffConfig.GenerateConfigMap(name, namespace, references)) - - require.False(t, ffConfig.Spec.ServiceProvider.IsSet()) - - ffConfig.Spec.ServiceProvider = &FeatureFlagServiceProvider{ - Name: "", - } - - require.False(t, ffConfig.Spec.ServiceProvider.IsSet()) - - ffConfig.Spec.ServiceProvider = &FeatureFlagServiceProvider{ - Name: "prov", - } - - require.True(t, ffConfig.Spec.ServiceProvider.IsSet()) -} diff --git a/apis/core/v1alpha1/featureflagconfiguration_webhook.go b/apis/core/v1alpha1/featureflagconfiguration_webhook.go deleted file mode 100644 index 46e2897c2..000000000 --- a/apis/core/v1alpha1/featureflagconfiguration_webhook.go +++ /dev/null @@ -1,9 +0,0 @@ -package v1alpha1 - -import ctrl "sigs.k8s.io/controller-runtime" - -func (r *FeatureFlagConfiguration) SetupWebhookWithManager(mgr ctrl.Manager) error { - return ctrl.NewWebhookManagedBy(mgr). - For(r). - Complete() -} diff --git a/apis/core/v1alpha1/flagsourceconfiguration_conversion.go b/apis/core/v1alpha1/flagsourceconfiguration_conversion.go deleted file mode 100644 index 883c45ab3..000000000 --- a/apis/core/v1alpha1/flagsourceconfiguration_conversion.go +++ /dev/null @@ -1,20 +0,0 @@ -/* -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 v1alpha1 - -// Hub marks this type as a conversion hub. -func (ffc *FlagSourceConfiguration) Hub() {} diff --git a/apis/core/v1alpha1/flagsourceconfiguration_types.go b/apis/core/v1alpha1/flagsourceconfiguration_types.go index ab7027747..0d9454658 100644 --- a/apis/core/v1alpha1/flagsourceconfiguration_types.go +++ b/apis/core/v1alpha1/flagsourceconfiguration_types.go @@ -17,56 +17,14 @@ limitations under the License. package v1alpha1 import ( - "fmt" - "os" - "strconv" - "strings" - - "github.com/open-feature/open-feature-operator/common/utils" corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" ) type SyncProviderType string -const ( - SidecarEnvVarPrefix string = "SIDECAR_ENV_VAR_PREFIX" - InputConfigurationEnvVarPrefix string = "SIDECAR" - SidecarMetricPortEnvVar string = "METRICS_PORT" - SidecarPortEnvVar string = "PORT" - SidecarSocketPathEnvVar string = "SOCKET_PATH" - SidecarEvaluatorEnvVar string = "EVALUATOR" - SidecarImageEnvVar string = "IMAGE" - SidecarVersionEnvVar string = "TAG" - SidecarProviderArgsEnvVar string = "PROVIDER_ARGS" - SidecarDefaultSyncProviderEnvVar string = "SYNC_PROVIDER" - SidecarLogFormatEnvVar string = "LOG_FORMAT" - SidecarProbesEnabledVar string = "PROBES_ENABLED" - defaultSidecarEnvVarPrefix string = "FLAGD" - DefaultMetricPort int32 = 8014 - defaultPort int32 = 8013 - defaultSocketPath string = "" - defaultEvaluator string = "json" - defaultImage string = "ghcr.io/open-feature/flagd" - // renovate: datasource=github-tags depName=open-feature/flagd/flagd - defaultTag string = "v0.6.3" - defaultLogFormat string = "json" - defaultProbesEnabled bool = true - SyncProviderKubernetes SyncProviderType = "kubernetes" - SyncProviderFilepath SyncProviderType = "filepath" - SyncProviderHttp SyncProviderType = "http" - SyncProviderGrpc SyncProviderType = "grpc" - SyncProviderFlagdProxy SyncProviderType = "flagd-proxy" -) - -// 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. - // FlagSourceConfigurationSpec defines the desired state of FlagSourceConfiguration type FlagSourceConfigurationSpec struct { - // INSERT ADDITIONAL SPEC FIELDS - desired state of cluster - // Important: Run "make" to regenerate code after modifying this file - // MetricsPort defines the port to serve metrics on, defaults to 8014 // +optional MetricsPort int32 `json:"metricsPort"` @@ -196,216 +154,6 @@ type FlagSourceConfigurationList struct { Items []FlagSourceConfiguration `json:"items"` } -//nolint:gocyclo -func NewFlagSourceConfigurationSpec() (*FlagSourceConfigurationSpec, error) { - fsc := &FlagSourceConfigurationSpec{ - MetricsPort: DefaultMetricPort, - Port: defaultPort, - SocketPath: defaultSocketPath, - Evaluator: defaultEvaluator, - Image: defaultImage, - Tag: defaultTag, - Sources: []Source{}, - EnvVars: []corev1.EnvVar{}, - SyncProviderArgs: []string{}, - DefaultSyncProvider: SyncProviderKubernetes, - EnvVarPrefix: defaultSidecarEnvVarPrefix, - LogFormat: defaultLogFormat, - RolloutOnChange: nil, - DebugLogging: utils.FalseVal(), - OtelCollectorUri: "", - } - - // set default value derived from constant default - probes := defaultProbesEnabled - fsc.ProbesEnabled = &probes - - if metricsPort := os.Getenv(envVarKey(InputConfigurationEnvVarPrefix, SidecarMetricPortEnvVar)); metricsPort != "" { - metricsPortI, err := strconv.Atoi(metricsPort) - if err != nil { - return fsc, fmt.Errorf("unable to parse metrics port value %s to int32: %w", metricsPort, err) - } - fsc.MetricsPort = int32(metricsPortI) - } - - if port := os.Getenv(envVarKey(InputConfigurationEnvVarPrefix, SidecarPortEnvVar)); port != "" { - portI, err := strconv.Atoi(port) - if err != nil { - return fsc, fmt.Errorf("unable to parse sidecar port value %s to int32: %w", port, err) - } - fsc.Port = int32(portI) - } - - if socketPath := os.Getenv(envVarKey(InputConfigurationEnvVarPrefix, SidecarSocketPathEnvVar)); socketPath != "" { - fsc.SocketPath = socketPath - } - - if evaluator := os.Getenv(envVarKey(InputConfigurationEnvVarPrefix, SidecarEvaluatorEnvVar)); evaluator != "" { - fsc.Evaluator = evaluator - } - - if image := os.Getenv(envVarKey(InputConfigurationEnvVarPrefix, SidecarImageEnvVar)); image != "" { - fsc.Image = image - } - - if tag := os.Getenv(envVarKey(InputConfigurationEnvVarPrefix, SidecarVersionEnvVar)); tag != "" { - fsc.Tag = tag - } - - if syncProviderArgs := os.Getenv(envVarKey(InputConfigurationEnvVarPrefix, SidecarProviderArgsEnvVar)); syncProviderArgs != "" { - fsc.SyncProviderArgs = strings.Split(syncProviderArgs, ",") // todo: add documentation for this - } - - if syncProvider := os.Getenv(envVarKey(InputConfigurationEnvVarPrefix, SidecarDefaultSyncProviderEnvVar)); syncProvider != "" { - fsc.DefaultSyncProvider = SyncProviderType(syncProvider) - } - - if logFormat := os.Getenv(envVarKey(InputConfigurationEnvVarPrefix, SidecarLogFormatEnvVar)); logFormat != "" { - fsc.LogFormat = logFormat - } - - if envVarPrefix := os.Getenv(SidecarEnvVarPrefix); envVarPrefix != "" { - fsc.EnvVarPrefix = envVarPrefix - } - - if probesEnabled := os.Getenv(envVarKey(InputConfigurationEnvVarPrefix, SidecarProbesEnabledVar)); probesEnabled != "" { - b, err := strconv.ParseBool(probesEnabled) - if err != nil { - return fsc, fmt.Errorf("unable to parse sidecar probes enabled %s to boolean: %w", probesEnabled, err) - } - fsc.ProbesEnabled = &b - } - - return fsc, nil -} - -//nolint:gocyclo -func (fc *FlagSourceConfigurationSpec) Merge(new *FlagSourceConfigurationSpec) { - if new == nil { - return - } - if new.MetricsPort != 0 { - fc.MetricsPort = new.MetricsPort - } - if new.Port != 0 { - fc.Port = new.Port - } - if new.SocketPath != "" { - fc.SocketPath = new.SocketPath - } - if new.Evaluator != "" { - fc.Evaluator = new.Evaluator - } - if new.Image != "" { - fc.Image = new.Image - } - if new.Tag != "" { - fc.Tag = new.Tag - } - if len(new.Sources) != 0 { - fc.Sources = append(fc.Sources, new.Sources...) - } - if len(new.EnvVars) != 0 { - fc.EnvVars = append(fc.EnvVars, new.EnvVars...) - } - if new.SyncProviderArgs != nil && len(new.SyncProviderArgs) > 0 { - fc.SyncProviderArgs = append(fc.SyncProviderArgs, new.SyncProviderArgs...) - } - if new.EnvVarPrefix != "" { - fc.EnvVarPrefix = new.EnvVarPrefix - } - if new.DefaultSyncProvider != "" { - fc.DefaultSyncProvider = new.DefaultSyncProvider - } - if new.LogFormat != "" { - fc.LogFormat = new.LogFormat - } - if new.RolloutOnChange != nil { - fc.RolloutOnChange = new.RolloutOnChange - } - if new.ProbesEnabled != nil { - fc.ProbesEnabled = new.ProbesEnabled - } - if new.DebugLogging != nil { - fc.DebugLogging = new.DebugLogging - } - if new.OtelCollectorUri != "" { - fc.OtelCollectorUri = new.OtelCollectorUri - } -} - -func (fc *FlagSourceConfigurationSpec) ToEnvVars() []corev1.EnvVar { - envs := []corev1.EnvVar{} - - for _, envVar := range fc.EnvVars { - envs = append(envs, corev1.EnvVar{ - Name: envVarKey(fc.EnvVarPrefix, envVar.Name), - Value: envVar.Value, - }) - } - - if fc.MetricsPort != DefaultMetricPort { - envs = append(envs, corev1.EnvVar{ - Name: envVarKey(fc.EnvVarPrefix, SidecarMetricPortEnvVar), - Value: fmt.Sprintf("%d", fc.MetricsPort), - }) - } - - if fc.Port != defaultPort { - envs = append(envs, corev1.EnvVar{ - Name: envVarKey(fc.EnvVarPrefix, SidecarPortEnvVar), - Value: fmt.Sprintf("%d", fc.Port), - }) - } - - if fc.Evaluator != defaultEvaluator { - envs = append(envs, corev1.EnvVar{ - Name: envVarKey(fc.EnvVarPrefix, SidecarEvaluatorEnvVar), - Value: fc.Evaluator, - }) - } - - if fc.SocketPath != defaultSocketPath { - envs = append(envs, corev1.EnvVar{ - Name: envVarKey(fc.EnvVarPrefix, SidecarSocketPathEnvVar), - Value: fc.SocketPath, - }) - } - - if fc.LogFormat != defaultLogFormat { - envs = append(envs, corev1.EnvVar{ - Name: envVarKey(fc.EnvVarPrefix, SidecarLogFormatEnvVar), - Value: fc.LogFormat, - }) - } - - return envs -} - -func (s SyncProviderType) IsKubernetes() bool { - return s == SyncProviderKubernetes -} - -func (s SyncProviderType) IsHttp() bool { - return s == SyncProviderHttp -} - -func (s SyncProviderType) IsFilepath() bool { - return s == SyncProviderFilepath -} - -func (s SyncProviderType) IsGrpc() bool { - return s == SyncProviderGrpc -} - -func (s SyncProviderType) IsFlagdProxy() bool { - return s == SyncProviderFlagdProxy -} - -func envVarKey(prefix string, suffix string) string { - return fmt.Sprintf("%s_%s", prefix, suffix) -} - func init() { SchemeBuilder.Register(&FlagSourceConfiguration{}, &FlagSourceConfigurationList{}) } diff --git a/apis/core/v1alpha1/flagsourceconfiguration_types_test.go b/apis/core/v1alpha1/flagsourceconfiguration_types_test.go deleted file mode 100644 index 882943e4d..000000000 --- a/apis/core/v1alpha1/flagsourceconfiguration_types_test.go +++ /dev/null @@ -1,303 +0,0 @@ -package v1alpha1 - -import ( - "testing" - - "github.com/open-feature/open-feature-operator/common/utils" - "github.com/stretchr/testify/require" - v1 "k8s.io/api/core/v1" -) - -func Test_FLagSourceConfiguration_SyncProvider(t *testing.T) { - k := SyncProviderKubernetes - f := SyncProviderFilepath - h := SyncProviderHttp - g := SyncProviderGrpc - - require.True(t, k.IsKubernetes()) - require.True(t, f.IsFilepath()) - require.True(t, h.IsHttp()) - require.True(t, g.IsGrpc()) - - require.False(t, f.IsKubernetes()) - require.False(t, h.IsFilepath()) - require.False(t, k.IsGrpc()) - require.False(t, g.IsHttp()) -} - -func Test_FLagSourceConfiguration_envVarKey(t *testing.T) { - require.Equal(t, "pre_suf", envVarKey("pre", "suf")) -} - -func Test_FLagSourceConfiguration_ToEnvVars(t *testing.T) { - ff := FlagSourceConfiguration{ - Spec: FlagSourceConfigurationSpec{ - EnvVars: []v1.EnvVar{ - { - Name: "env1", - Value: "val1", - }, - { - Name: "env2", - Value: "val2", - }, - }, - EnvVarPrefix: "PRE", - MetricsPort: 22, - Port: 33, - Evaluator: "evaluator", - SocketPath: "socket-path", - LogFormat: "log", - }, - } - expected := []v1.EnvVar{ - { - Name: "PRE_env1", - Value: "val1", - }, - { - Name: "PRE_env2", - Value: "val2", - }, - { - Name: "PRE_METRICS_PORT", - Value: "22", - }, - { - Name: "PRE_PORT", - Value: "33", - }, - { - Name: "PRE_EVALUATOR", - Value: "evaluator", - }, - { - Name: "PRE_SOCKET_PATH", - Value: "socket-path", - }, - { - Name: "PRE_LOG_FORMAT", - Value: "log", - }, - } - require.Equal(t, expected, ff.Spec.ToEnvVars()) -} - -func Test_FLagSourceConfiguration_Merge(t *testing.T) { - ff_old := &FlagSourceConfiguration{ - Spec: FlagSourceConfigurationSpec{ - EnvVars: []v1.EnvVar{ - { - Name: "env1", - Value: "val1", - }, - { - Name: "env2", - Value: "val2", - }, - }, - EnvVarPrefix: "PRE", - MetricsPort: 22, - Port: 33, - Evaluator: "evaluator", - SocketPath: "socket-path", - LogFormat: "log", - Image: "img", - Tag: "tag", - Sources: []Source{ - { - Source: "src1", - Provider: SyncProviderGrpc, - TLS: true, - CertPath: "etc/cert.ca", - ProviderID: "app", - Selector: "source=database", - }, - }, - SyncProviderArgs: []string{"arg1", "arg2"}, - DefaultSyncProvider: SyncProviderKubernetes, - RolloutOnChange: utils.TrueVal(), - ProbesEnabled: utils.TrueVal(), - DebugLogging: utils.TrueVal(), - OtelCollectorUri: "", - }, - } - - ff_old.Spec.Merge(nil) - - require.Equal(t, &FlagSourceConfiguration{ - Spec: FlagSourceConfigurationSpec{ - EnvVars: []v1.EnvVar{ - { - Name: "env1", - Value: "val1", - }, - { - Name: "env2", - Value: "val2", - }, - }, - EnvVarPrefix: "PRE", - MetricsPort: 22, - Port: 33, - Evaluator: "evaluator", - SocketPath: "socket-path", - LogFormat: "log", - Image: "img", - Tag: "tag", - Sources: []Source{ - { - Source: "src1", - Provider: SyncProviderGrpc, - TLS: true, - CertPath: "etc/cert.ca", - ProviderID: "app", - Selector: "source=database", - }, - }, - SyncProviderArgs: []string{"arg1", "arg2"}, - DefaultSyncProvider: SyncProviderKubernetes, - RolloutOnChange: utils.TrueVal(), - ProbesEnabled: utils.TrueVal(), - DebugLogging: utils.TrueVal(), - OtelCollectorUri: "", - }, - }, ff_old) - - ff_new := &FlagSourceConfiguration{ - Spec: FlagSourceConfigurationSpec{ - EnvVars: []v1.EnvVar{ - { - Name: "env3", - Value: "val3", - }, - { - Name: "env4", - Value: "val4", - }, - }, - EnvVarPrefix: "PREFIX", - MetricsPort: 221, - Port: 331, - Evaluator: "evaluator1", - SocketPath: "socket-path1", - LogFormat: "log1", - Image: "img1", - Tag: "tag1", - Sources: []Source{ - { - Source: "src2", - Provider: SyncProviderFilepath, - }, - }, - SyncProviderArgs: []string{"arg3", "arg4"}, - DefaultSyncProvider: SyncProviderFilepath, - RolloutOnChange: utils.FalseVal(), - ProbesEnabled: utils.FalseVal(), - DebugLogging: utils.FalseVal(), - OtelCollectorUri: "", - }, - } - - ff_old.Spec.Merge(&ff_new.Spec) - - require.Equal(t, &FlagSourceConfiguration{ - Spec: FlagSourceConfigurationSpec{ - EnvVars: []v1.EnvVar{ - { - Name: "env1", - Value: "val1", - }, - { - Name: "env2", - Value: "val2", - }, - { - Name: "env3", - Value: "val3", - }, - { - Name: "env4", - Value: "val4", - }, - }, - EnvVarPrefix: "PREFIX", - MetricsPort: 221, - Port: 331, - Evaluator: "evaluator1", - SocketPath: "socket-path1", - LogFormat: "log1", - Image: "img1", - Tag: "tag1", - Sources: []Source{ - { - Source: "src1", - Provider: SyncProviderGrpc, - TLS: true, - CertPath: "etc/cert.ca", - ProviderID: "app", - Selector: "source=database", - }, - { - Source: "src2", - Provider: SyncProviderFilepath, - }, - }, - SyncProviderArgs: []string{"arg1", "arg2", "arg3", "arg4"}, - DefaultSyncProvider: SyncProviderFilepath, - RolloutOnChange: utils.FalseVal(), - ProbesEnabled: utils.FalseVal(), - DebugLogging: utils.FalseVal(), - OtelCollectorUri: "", - }, - }, ff_old) -} - -func Test_FLagSourceConfiguration_NewFlagSourceConfigurationSpec(t *testing.T) { - //happy path - t.Setenv(envVarKey(InputConfigurationEnvVarPrefix, SidecarMetricPortEnvVar), "22") - t.Setenv(envVarKey(InputConfigurationEnvVarPrefix, SidecarPortEnvVar), "33") - t.Setenv(envVarKey(InputConfigurationEnvVarPrefix, SidecarSocketPathEnvVar), "val1") - t.Setenv(envVarKey(InputConfigurationEnvVarPrefix, SidecarEvaluatorEnvVar), "val2") - t.Setenv(envVarKey(InputConfigurationEnvVarPrefix, SidecarImageEnvVar), "val3") - t.Setenv(envVarKey(InputConfigurationEnvVarPrefix, SidecarVersionEnvVar), "val4") - t.Setenv(envVarKey(InputConfigurationEnvVarPrefix, SidecarProviderArgsEnvVar), "val11,val22") - t.Setenv(envVarKey(InputConfigurationEnvVarPrefix, SidecarDefaultSyncProviderEnvVar), "kubernetes") - t.Setenv(envVarKey(InputConfigurationEnvVarPrefix, SidecarLogFormatEnvVar), "val5") - t.Setenv(SidecarEnvVarPrefix, "val6") - t.Setenv(envVarKey(InputConfigurationEnvVarPrefix, SidecarProbesEnabledVar), "true") - - fs, err := NewFlagSourceConfigurationSpec() - - require.Nil(t, err) - require.Equal(t, &FlagSourceConfigurationSpec{ - MetricsPort: 22, - Port: 33, - SocketPath: "val1", - Evaluator: "val2", - Image: "val3", - Tag: "val4", - Sources: []Source{}, - EnvVars: []v1.EnvVar{}, - SyncProviderArgs: []string{"val11", "val22"}, - DefaultSyncProvider: SyncProviderKubernetes, - EnvVarPrefix: "val6", - LogFormat: "val5", - ProbesEnabled: utils.TrueVal(), - DebugLogging: utils.FalseVal(), - OtelCollectorUri: "", - }, fs) - - //error paths - t.Setenv(envVarKey(InputConfigurationEnvVarPrefix, SidecarProbesEnabledVar), "blah") - _, err = NewFlagSourceConfigurationSpec() - require.NotNil(t, err) - - t.Setenv(envVarKey(InputConfigurationEnvVarPrefix, SidecarPortEnvVar), "blah") - _, err = NewFlagSourceConfigurationSpec() - require.NotNil(t, err) - - t.Setenv(envVarKey(InputConfigurationEnvVarPrefix, SidecarMetricPortEnvVar), "blah") - _, err = NewFlagSourceConfigurationSpec() - require.NotNil(t, err) -} diff --git a/apis/core/v1alpha1/flagsourceconfiguration_webhook.go b/apis/core/v1alpha1/flagsourceconfiguration_webhook.go deleted file mode 100644 index e75324e87..000000000 --- a/apis/core/v1alpha1/flagsourceconfiguration_webhook.go +++ /dev/null @@ -1,11 +0,0 @@ -package v1alpha1 - -import ( - ctrl "sigs.k8s.io/controller-runtime" -) - -func (r *FlagSourceConfiguration) SetupWebhookWithManager(mgr ctrl.Manager) error { - return ctrl.NewWebhookManagedBy(mgr). - For(r). - Complete() -} diff --git a/apis/core/v1alpha2/common/common.go b/apis/core/v1alpha2/common/common.go deleted file mode 100644 index 87bbc63c9..000000000 --- a/apis/core/v1alpha2/common/common.go +++ /dev/null @@ -1,6 +0,0 @@ -package common - -import "errors" - -var ErrCannotCastFlagSourceConfiguration = errors.New("cannot cast FlagSourceConfiguration to v1alpha2") -var ErrCannotCastFeatureFlagConfiguration = errors.New("cannot cast FeatureFlagConfiguration to v1alpha2") diff --git a/apis/core/v1alpha2/featureflagconfiguration_conversion.go b/apis/core/v1alpha2/featureflagconfiguration_conversion.go deleted file mode 100644 index a82dabfee..000000000 --- a/apis/core/v1alpha2/featureflagconfiguration_conversion.go +++ /dev/null @@ -1,112 +0,0 @@ -/* -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 ( - "encoding/json" - "fmt" - - "github.com/open-feature/open-feature-operator/apis/core/v1alpha1" - "github.com/open-feature/open-feature-operator/apis/core/v1alpha2/common" - "sigs.k8s.io/controller-runtime/pkg/conversion" -) - -func (src *FeatureFlagConfiguration) ConvertTo(dstRaw conversion.Hub) error { - dst, ok := dstRaw.(*v1alpha1.FeatureFlagConfiguration) - - if !ok { - return fmt.Errorf("type %T %w", dstRaw, common.ErrCannotCastFeatureFlagConfiguration) - } - - // Copy equal stuff to new object - // DO NOT COPY TypeMeta - dst.ObjectMeta = src.ObjectMeta - - if src.Spec.ServiceProvider != nil { - dst.Spec.ServiceProvider = &v1alpha1.FeatureFlagServiceProvider{ - Name: src.Spec.ServiceProvider.Name, - Credentials: src.Spec.ServiceProvider.Credentials, - } - } - - if src.Spec.SyncProvider != nil { - dst.Spec.SyncProvider = &v1alpha1.FeatureFlagSyncProvider{Name: src.Spec.SyncProvider.Name} - if src.Spec.SyncProvider.HttpSyncConfiguration != nil { - dst.Spec.SyncProvider.HttpSyncConfiguration = &v1alpha1.HttpSyncConfiguration{ - Target: src.Spec.SyncProvider.HttpSyncConfiguration.Target, - BearerToken: src.Spec.SyncProvider.HttpSyncConfiguration.BearerToken, - } - } - } - - if src.Spec.FlagDSpec != nil { - dst.Spec.FlagDSpec = &v1alpha1.FlagDSpec{Envs: src.Spec.FlagDSpec.Envs} - } - - featureFlagSpecB, err := json.Marshal(src.Spec.FeatureFlagSpec) - if err != nil { - return fmt.Errorf("featureflagspec: %w", err) - } - - dst.Spec.FeatureFlagSpec = string(featureFlagSpecB) - - return nil -} - -func (dst *FeatureFlagConfiguration) ConvertFrom(srcRaw conversion.Hub) error { - src, ok := srcRaw.(*v1alpha1.FeatureFlagConfiguration) - - if !ok { - return fmt.Errorf("type %T %w", srcRaw, common.ErrCannotCastFeatureFlagConfiguration) - } - - // Copy equal stuff to new object - // DO NOT COPY TypeMeta - dst.ObjectMeta = src.ObjectMeta - - if src.Spec.ServiceProvider != nil { - dst.Spec.ServiceProvider = &FeatureFlagServiceProvider{ - Name: src.Spec.ServiceProvider.Name, - Credentials: src.Spec.ServiceProvider.Credentials, - } - } - - if src.Spec.SyncProvider != nil { - dst.Spec.SyncProvider = &FeatureFlagSyncProvider{ - Name: string(src.Spec.SyncProvider.Name), - } - if src.Spec.SyncProvider.HttpSyncConfiguration != nil { - dst.Spec.SyncProvider.HttpSyncConfiguration = &HttpSyncConfiguration{ - Target: src.Spec.SyncProvider.HttpSyncConfiguration.Target, - BearerToken: src.Spec.SyncProvider.HttpSyncConfiguration.BearerToken, - } - } - } - - if src.Spec.FlagDSpec != nil { - dst.Spec.FlagDSpec = &FlagDSpec{Envs: src.Spec.FlagDSpec.Envs} - } - - var featureFlagSpec FeatureFlagSpec - if err := json.Unmarshal([]byte(src.Spec.FeatureFlagSpec), &featureFlagSpec); err != nil { - return fmt.Errorf("featureflagspec: %w", err) - } - - dst.Spec.FeatureFlagSpec = featureFlagSpec - - return nil -} diff --git a/apis/core/v1alpha2/featureflagconfiguration_conversion_test.go b/apis/core/v1alpha2/featureflagconfiguration_conversion_test.go deleted file mode 100644 index d88ee32ab..000000000 --- a/apis/core/v1alpha2/featureflagconfiguration_conversion_test.go +++ /dev/null @@ -1,318 +0,0 @@ -package v1alpha2 - -import ( - "testing" - - "github.com/open-feature/open-feature-operator/apis/core/v1alpha1" - "github.com/open-feature/open-feature-operator/apis/core/v1alpha2/common" - "github.com/stretchr/testify/require" - corev1 "k8s.io/api/core/v1" - v1 "k8s.io/apimachinery/pkg/apis/meta/v1" - v2 "sigs.k8s.io/controller-runtime/pkg/webhook/conversion/testdata/api/v2" -) - -func TestFeatureFlagConfiguration_ConvertFrom(t *testing.T) { - tests := []struct { - name string - srcObj *v1alpha1.FeatureFlagConfiguration - wantErr bool - wantObj *FeatureFlagConfiguration - }{ - { - name: "Test that conversion from v1alpha1 to v1alpha2 works", - srcObj: &v1alpha1.FeatureFlagConfiguration{ - TypeMeta: v1.TypeMeta{ - Kind: "FeatureFlagConfiguration", - APIVersion: "core.openfeature.dev/v1alpha1", - }, - ObjectMeta: v1.ObjectMeta{ - Name: "FeatureFlagconfig1", - Namespace: "default", - }, - Spec: v1alpha1.FeatureFlagConfigurationSpec{ - ServiceProvider: &v1alpha1.FeatureFlagServiceProvider{ - Name: "name1", - Credentials: &corev1.ObjectReference{ - Kind: "Pod", - Namespace: "default", - Name: "pod1", - }, - }, - SyncProvider: &v1alpha1.FeatureFlagSyncProvider{ - Name: "syncprovider1", - HttpSyncConfiguration: &v1alpha1.HttpSyncConfiguration{ - Target: "target1", - BearerToken: "token", - }, - }, - FlagDSpec: &v1alpha1.FlagDSpec{ - MetricsPort: 22, - Envs: []corev1.EnvVar{ - { - Name: "var1", - Value: "val1", - }, - { - Name: "var2", - Value: "val2", - }, - }, - }, - FeatureFlagSpec: `{"flags":{"flag1":{"state":"ok","variants":"variant1","defaultVariant":"default"}}}`, - }, - }, - wantErr: false, - wantObj: &FeatureFlagConfiguration{ - ObjectMeta: v1.ObjectMeta{ - Name: "FeatureFlagconfig1", - Namespace: "default", - }, - Spec: FeatureFlagConfigurationSpec{ - ServiceProvider: &FeatureFlagServiceProvider{ - Name: "name1", - Credentials: &corev1.ObjectReference{ - Kind: "Pod", - Namespace: "default", - Name: "pod1", - }, - }, - SyncProvider: &FeatureFlagSyncProvider{ - Name: "syncprovider1", - HttpSyncConfiguration: &HttpSyncConfiguration{ - Target: "target1", - BearerToken: "token", - }, - }, - FlagDSpec: &FlagDSpec{ - Envs: []corev1.EnvVar{ - { - Name: "var1", - Value: "val1", - }, - { - Name: "var2", - Value: "val2", - }, - }, - }, - FeatureFlagSpec: FeatureFlagSpec{ - Flags: map[string]FlagSpec{ - "flag1": { - State: "ok", - DefaultVariant: "default", - Variants: []byte(`"variant1"`), - }, - }, - }, - }, - }, - }, - { - name: "unable to unmarshal featureflagspec json", - srcObj: &v1alpha1.FeatureFlagConfiguration{ - TypeMeta: v1.TypeMeta{ - Kind: "FeatureFlagConfiguration", - APIVersion: "core.openfeature.dev/v1alpha1", - }, - ObjectMeta: v1.ObjectMeta{ - Name: "FeatureFlagconfig1", - Namespace: "default", - }, - Spec: v1alpha1.FeatureFlagConfigurationSpec{ - ServiceProvider: &v1alpha1.FeatureFlagServiceProvider{ - Name: "name1", - Credentials: &corev1.ObjectReference{ - Kind: "Pod", - Namespace: "default", - Name: "pod1", - }, - }, - SyncProvider: &v1alpha1.FeatureFlagSyncProvider{ - Name: "syncprovider1", - HttpSyncConfiguration: &v1alpha1.HttpSyncConfiguration{ - Target: "target1", - BearerToken: "token", - }, - }, - FlagDSpec: &v1alpha1.FlagDSpec{ - MetricsPort: 22, - Envs: []corev1.EnvVar{ - { - Name: "var1", - Value: "val1", - }, - { - Name: "var2", - Value: "val2", - }, - }, - }, - FeatureFlagSpec: `invalid`, - }, - }, - wantErr: true, - wantObj: nil, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - dst := &FeatureFlagConfiguration{ - TypeMeta: v1.TypeMeta{}, - ObjectMeta: v1.ObjectMeta{}, - Spec: FeatureFlagConfigurationSpec{}, - Status: FeatureFlagConfigurationStatus{}, - } - 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 TestFeatureFlagConfiguration_ConvertTo(t *testing.T) { - tests := []struct { - name string - src *FeatureFlagConfiguration - wantErr bool - wantObj *v1alpha1.FeatureFlagConfiguration - }{ - { - name: "Test that conversion from v1alpha2 to v1alpha1 works", - src: &FeatureFlagConfiguration{ - TypeMeta: v1.TypeMeta{ - Kind: "FeatureFlagConfiguration", - APIVersion: "core.openfeature.dev/v1alpha2", - }, - ObjectMeta: v1.ObjectMeta{ - Name: "FeatureFlagconfig1", - Namespace: "default", - }, - Spec: FeatureFlagConfigurationSpec{ - ServiceProvider: &FeatureFlagServiceProvider{ - Name: "name1", - Credentials: &corev1.ObjectReference{ - Kind: "Pod", - Namespace: "default", - Name: "pod1", - }, - }, - SyncProvider: &FeatureFlagSyncProvider{ - Name: "syncprovider1", - HttpSyncConfiguration: &HttpSyncConfiguration{ - Target: "target1", - BearerToken: "token", - }, - }, - FlagDSpec: &FlagDSpec{ - Envs: []corev1.EnvVar{ - { - Name: "var1", - Value: "val1", - }, - { - Name: "var2", - Value: "val2", - }, - }, - }, - FeatureFlagSpec: FeatureFlagSpec{ - Flags: map[string]FlagSpec{ - "flag1": { - State: "ok", - DefaultVariant: "default", - Variants: []byte(`"variant1"`), - }, - }, - }, - }, - }, - wantErr: false, - wantObj: &v1alpha1.FeatureFlagConfiguration{ - ObjectMeta: v1.ObjectMeta{ - Name: "FeatureFlagconfig1", - Namespace: "default", - }, - Spec: v1alpha1.FeatureFlagConfigurationSpec{ - ServiceProvider: &v1alpha1.FeatureFlagServiceProvider{ - Name: "name1", - Credentials: &corev1.ObjectReference{ - Kind: "Pod", - Namespace: "default", - Name: "pod1", - }, - }, - SyncProvider: &v1alpha1.FeatureFlagSyncProvider{ - Name: "syncprovider1", - HttpSyncConfiguration: &v1alpha1.HttpSyncConfiguration{ - Target: "target1", - BearerToken: "token", - }, - }, - FlagDSpec: &v1alpha1.FlagDSpec{ - Envs: []corev1.EnvVar{ - { - Name: "var1", - Value: "val1", - }, - { - Name: "var2", - Value: "val2", - }, - }, - }, - FeatureFlagSpec: `{"flags":{"flag1":{"state":"ok","variants":"variant1","defaultVariant":"default"}}}`, - }, - }, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - dst := v1alpha1.FeatureFlagConfiguration{ - TypeMeta: v1.TypeMeta{}, - ObjectMeta: v1.ObjectMeta{}, - Spec: v1alpha1.FeatureFlagConfigurationSpec{}, - Status: v1alpha1.FeatureFlagConfigurationStatus{}, - } - 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 TestFeatureFlagConfiguration_ConvertFrom_Errorcase(t *testing.T) { - // A random different object is used here to simulate a different API version - testObj := v2.ExternalJob{} - - dst := &FeatureFlagConfiguration{ - TypeMeta: v1.TypeMeta{}, - ObjectMeta: v1.ObjectMeta{}, - Spec: FeatureFlagConfigurationSpec{}, - Status: FeatureFlagConfigurationStatus{}, - } - - if err := dst.ConvertFrom(&testObj); err == nil { - t.Errorf("ConvertFrom() error = %v", err) - } else { - require.ErrorIs(t, err, common.ErrCannotCastFeatureFlagConfiguration) - } -} - -func TestFeatureFlagConfiguration_ConvertTo_Errorcase(t *testing.T) { - testObj := FeatureFlagConfiguration{} - - // 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.ErrorIs(t, err, common.ErrCannotCastFeatureFlagConfiguration) - } -} diff --git a/apis/core/v1alpha2/featureflagconfiguration_types.go b/apis/core/v1alpha2/featureflagconfiguration_types.go index ad207d9d9..98a43104e 100644 --- a/apis/core/v1alpha2/featureflagconfiguration_types.go +++ b/apis/core/v1alpha2/featureflagconfiguration_types.go @@ -23,14 +23,8 @@ import ( metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" ) -// 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. - // FeatureFlagConfigurationSpec defines the desired state of FeatureFlagConfiguration type FeatureFlagConfigurationSpec struct { - // INSERT ADDITIONAL SPEC FIELDS - desired state of cluster - // Important: Run "make" to regenerate code after modifying this file - // ServiceProvider [DEPRECATED]: superseded by FlagSourceConfiguration // +optional // +nullable @@ -109,8 +103,6 @@ type FeatureFlagServiceProvider struct { // FeatureFlagConfigurationStatus defines the observed state of FeatureFlagConfiguration type FeatureFlagConfigurationStatus struct { - // INSERT ADDITIONAL STATUS FIELD - define observed state of cluster - // Important: Run "make" to regenerate code after modifying this file } //+kubebuilder:resource:shortName="ff" diff --git a/apis/core/v1alpha2/flagsourceconfiguration_conversion.go b/apis/core/v1alpha2/flagsourceconfiguration_conversion.go deleted file mode 100644 index e9c3616a9..000000000 --- a/apis/core/v1alpha2/flagsourceconfiguration_conversion.go +++ /dev/null @@ -1,81 +0,0 @@ -/* -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 ( - "fmt" - - "github.com/open-feature/open-feature-operator/apis/core/v1alpha1" - "github.com/open-feature/open-feature-operator/apis/core/v1alpha2/common" - "github.com/open-feature/open-feature-operator/common/utils" - "sigs.k8s.io/controller-runtime/pkg/conversion" -) - -func (src *FlagSourceConfiguration) ConvertTo(dstRaw conversion.Hub) error { - dst, ok := dstRaw.(*v1alpha1.FlagSourceConfiguration) - - if !ok { - return fmt.Errorf("type %T %w", dstRaw, common.ErrCannotCastFlagSourceConfiguration) - } - - // Copy equal stuff to new object - // DO NOT COPY TypeMeta - dst.ObjectMeta = src.ObjectMeta - - dst.Spec = v1alpha1.FlagSourceConfigurationSpec{ - MetricsPort: src.Spec.MetricsPort, - Port: src.Spec.Port, - SocketPath: src.Spec.SocketPath, - Evaluator: src.Spec.Evaluator, - Image: src.Spec.Image, - Tag: src.Spec.Tag, - Sources: []v1alpha1.Source{}, - SyncProviderArgs: src.Spec.SyncProviderArgs, - LogFormat: src.Spec.LogFormat, - DefaultSyncProvider: v1alpha1.SyncProviderType(src.Spec.DefaultSyncProvider), - ProbesEnabled: src.Spec.ProbesEnabled, - DebugLogging: utils.FalseVal(), - OtelCollectorUri: src.Spec.OtelCollectorUri, - } - return nil -} - -func (dst *FlagSourceConfiguration) ConvertFrom(srcRaw conversion.Hub) error { - src, ok := srcRaw.(*v1alpha1.FlagSourceConfiguration) - - if !ok { - return fmt.Errorf("type %T %w", srcRaw, common.ErrCannotCastFlagSourceConfiguration) - } - - // Copy equal stuff to new object - // DO NOT COPY TypeMeta - dst.ObjectMeta = src.ObjectMeta - - dst.Spec = FlagSourceConfigurationSpec{ - MetricsPort: src.Spec.MetricsPort, - Port: src.Spec.Port, - SocketPath: src.Spec.SocketPath, - Evaluator: src.Spec.Evaluator, - Image: src.Spec.Image, - Tag: src.Spec.Tag, - SyncProviderArgs: src.Spec.SyncProviderArgs, - LogFormat: src.Spec.LogFormat, - DefaultSyncProvider: string(src.Spec.DefaultSyncProvider), - ProbesEnabled: src.Spec.ProbesEnabled, - } - return nil -} diff --git a/apis/core/v1alpha2/flagsourceconfiguration_conversion_test.go b/apis/core/v1alpha2/flagsourceconfiguration_conversion_test.go deleted file mode 100644 index de5b9f3df..000000000 --- a/apis/core/v1alpha2/flagsourceconfiguration_conversion_test.go +++ /dev/null @@ -1,211 +0,0 @@ -package v1alpha2 - -import ( - "testing" - - "github.com/open-feature/open-feature-operator/apis/core/v1alpha1" - "github.com/open-feature/open-feature-operator/apis/core/v1alpha2/common" - "github.com/open-feature/open-feature-operator/common/utils" - "github.com/stretchr/testify/require" - corev1 "k8s.io/api/core/v1" - v1 "k8s.io/apimachinery/pkg/apis/meta/v1" - v2 "sigs.k8s.io/controller-runtime/pkg/webhook/conversion/testdata/api/v2" -) - -func TestFlagSourceConfiguration_ConvertFrom(t *testing.T) { - tt := true - tests := []struct { - name string - srcObj *v1alpha1.FlagSourceConfiguration - wantErr bool - wantObj *FlagSourceConfiguration - }{ - { - name: "Test that conversion from v1alpha1 to v1alpha2 works", - srcObj: &v1alpha1.FlagSourceConfiguration{ - TypeMeta: v1.TypeMeta{ - Kind: "FlagSourceConfiguration", - APIVersion: "core.openfeature.dev/v1alpha1", - }, - ObjectMeta: v1.ObjectMeta{ - Name: "flagsourceconfig1", - Namespace: "default", - }, - Spec: v1alpha1.FlagSourceConfigurationSpec{ - MetricsPort: 20, - Port: 21, - SocketPath: "path", - Evaluator: "eval", - Image: "img", - Tag: "tag", - Sources: []v1alpha1.Source{ - { - Source: "source", - Provider: "provider", - HttpSyncBearerToken: "token", - TLS: false, - CertPath: "/tmp/path", - ProviderID: "myapp", - Selector: "source=database", - }, - }, - EnvVars: []corev1.EnvVar{ - { - Name: "env1", - Value: "val1", - }, - { - Name: "env2", - Value: "val2", - }, - }, - DefaultSyncProvider: v1alpha1.SyncProviderType("provider"), - LogFormat: "log", - EnvVarPrefix: "pre", - RolloutOnChange: &tt, - SyncProviderArgs: []string{"provider", "arg"}, - DebugLogging: utils.FalseVal(), - OtelCollectorUri: "", - }, - }, - wantErr: false, - wantObj: &FlagSourceConfiguration{ - ObjectMeta: v1.ObjectMeta{ - Name: "flagsourceconfig1", - Namespace: "default", - }, - Spec: FlagSourceConfigurationSpec{ - MetricsPort: 20, - Port: 21, - SocketPath: "path", - Evaluator: "eval", - Image: "img", - Tag: "tag", - SyncProviderArgs: []string{"provider", "arg"}, - LogFormat: "log", - DefaultSyncProvider: "provider", - }, - }, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - dst := &FlagSourceConfiguration{ - TypeMeta: v1.TypeMeta{}, - ObjectMeta: v1.ObjectMeta{}, - Spec: FlagSourceConfigurationSpec{}, - Status: FlagSourceConfigurationStatus{}, - } - 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 TestFlagSourceConfiguration_ConvertTo(t *testing.T) { - tests := []struct { - name string - src *FlagSourceConfiguration - wantErr bool - wantObj *v1alpha1.FlagSourceConfiguration - }{ - { - name: "Test that conversion from v1alpha2 to v1alpha1 works", - src: &FlagSourceConfiguration{ - TypeMeta: v1.TypeMeta{ - Kind: "FlagSourceConfiguration", - APIVersion: "core.openfeature.dev/v1alpha2", - }, - ObjectMeta: v1.ObjectMeta{ - Name: "flagsourceconfig1", - Namespace: "default", - }, - Spec: FlagSourceConfigurationSpec{ - MetricsPort: 20, - Port: 21, - SocketPath: "path", - Evaluator: "eval", - Image: "img", - Tag: "tag", - DefaultSyncProvider: "provider", - LogFormat: "log", - SyncProviderArgs: []string{"provider", "arg"}, - }, - }, - wantErr: false, - wantObj: &v1alpha1.FlagSourceConfiguration{ - ObjectMeta: v1.ObjectMeta{ - Name: "flagsourceconfig1", - Namespace: "default", - }, - Spec: v1alpha1.FlagSourceConfigurationSpec{ - MetricsPort: 20, - Port: 21, - SocketPath: "path", - Evaluator: "eval", - Image: "img", - Tag: "tag", - Sources: []v1alpha1.Source{}, - DefaultSyncProvider: v1alpha1.SyncProviderType("provider"), - LogFormat: "log", - EnvVarPrefix: "", - RolloutOnChange: nil, - SyncProviderArgs: []string{"provider", "arg"}, - DebugLogging: utils.FalseVal(), - OtelCollectorUri: "", - }, - }, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - dst := v1alpha1.FlagSourceConfiguration{ - TypeMeta: v1.TypeMeta{}, - ObjectMeta: v1.ObjectMeta{}, - Spec: v1alpha1.FlagSourceConfigurationSpec{}, - Status: v1alpha1.FlagSourceConfigurationStatus{}, - } - 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 TestFlagSourceConfiguration_ConvertFrom_Errorcase(t *testing.T) { - // A random different object is used here to simulate a different API version - testObj := v2.ExternalJob{} - - dst := &FlagSourceConfiguration{ - TypeMeta: v1.TypeMeta{}, - ObjectMeta: v1.ObjectMeta{}, - Spec: FlagSourceConfigurationSpec{}, - Status: FlagSourceConfigurationStatus{}, - } - - if err := dst.ConvertFrom(&testObj); err == nil { - t.Errorf("ConvertFrom() error = %v", err) - } else { - require.ErrorIs(t, err, common.ErrCannotCastFlagSourceConfiguration) - } -} - -func TestFlagSourceConfiguration_ConvertTo_Errorcase(t *testing.T) { - testObj := FlagSourceConfiguration{} - - // 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.ErrorIs(t, err, common.ErrCannotCastFlagSourceConfiguration) - } -} diff --git a/apis/core/v1alpha2/flagsourceconfiguration_types.go b/apis/core/v1alpha2/flagsourceconfiguration_types.go index 0913c8317..fdd262dd9 100644 --- a/apis/core/v1alpha2/flagsourceconfiguration_types.go +++ b/apis/core/v1alpha2/flagsourceconfiguration_types.go @@ -20,9 +20,6 @@ import ( metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" ) -// 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. - // FlagSourceConfigurationSpec defines the desired state of FlagSourceConfiguration type FlagSourceConfigurationSpec struct { // INSERT ADDITIONAL SPEC FIELDS - desired state of cluster @@ -75,8 +72,6 @@ type FlagSourceConfigurationSpec struct { // FlagSourceConfigurationStatus defines the observed state of FlagSourceConfiguration type FlagSourceConfigurationStatus struct { - // INSERT ADDITIONAL STATUS FIELD - define observed state of cluster - // Important: Run "make" to regenerate code after modifying this file } //+kubebuilder:resource:shortName="fsc" diff --git a/apis/core/v1alpha3/common/common.go b/apis/core/v1alpha3/common/common.go deleted file mode 100644 index f78640a8a..000000000 --- a/apis/core/v1alpha3/common/common.go +++ /dev/null @@ -1,5 +0,0 @@ -package common - -import "errors" - -var ErrCannotCastFlagSourceConfiguration = errors.New("cannot cast FlagSourceConfiguration to v1alpha3") diff --git a/apis/core/v1alpha3/flagsourceconfiguration_conversion.go b/apis/core/v1alpha3/flagsourceconfiguration_conversion.go deleted file mode 100644 index b7f75d662..000000000 --- a/apis/core/v1alpha3/flagsourceconfiguration_conversion.go +++ /dev/null @@ -1,113 +0,0 @@ -/* -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 v1alpha3 - -import ( - "fmt" - - "github.com/open-feature/open-feature-operator/apis/core/v1alpha1" - "github.com/open-feature/open-feature-operator/apis/core/v1alpha3/common" - "sigs.k8s.io/controller-runtime/pkg/conversion" -) - -func (src *FlagSourceConfiguration) ConvertTo(dstRaw conversion.Hub) error { - dst, ok := dstRaw.(*v1alpha1.FlagSourceConfiguration) - - if !ok { - return fmt.Errorf("type %T %w", dstRaw, common.ErrCannotCastFlagSourceConfiguration) - } - - // Copy equal stuff to new object - // DO NOT COPY TypeMeta - dst.ObjectMeta = src.ObjectMeta - - sources := []v1alpha1.Source{} - for _, sp := range src.Spec.Sources { - sources = append(sources, v1alpha1.Source{ - Source: sp.Source, - Provider: v1alpha1.SyncProviderType(sp.Provider), - HttpSyncBearerToken: sp.HttpSyncBearerToken, - TLS: sp.TLS, - CertPath: sp.CertPath, - ProviderID: sp.ProviderID, - Selector: sp.Selector, - }) - } - - dst.Spec = v1alpha1.FlagSourceConfigurationSpec{ - MetricsPort: src.Spec.MetricsPort, - Port: src.Spec.Port, - SocketPath: src.Spec.SocketPath, - Evaluator: src.Spec.Evaluator, - Image: src.Spec.Image, - Tag: src.Spec.Tag, - Sources: sources, - EnvVars: src.Spec.EnvVars, - DefaultSyncProvider: v1alpha1.SyncProviderType(src.Spec.DefaultSyncProvider), - LogFormat: src.Spec.LogFormat, - EnvVarPrefix: src.Spec.EnvVarPrefix, - RolloutOnChange: src.Spec.RolloutOnChange, - ProbesEnabled: src.Spec.ProbesEnabled, - DebugLogging: src.Spec.DebugLogging, - OtelCollectorUri: src.Spec.OtelCollectorUri, - } - return nil -} - -func (dst *FlagSourceConfiguration) ConvertFrom(srcRaw conversion.Hub) error { - src, ok := srcRaw.(*v1alpha1.FlagSourceConfiguration) - - if !ok { - return fmt.Errorf("type %T %w", srcRaw, common.ErrCannotCastFlagSourceConfiguration) - } - - // Copy equal stuff to new object - // DO NOT COPY TypeMeta - dst.ObjectMeta = src.ObjectMeta - - sources := []Source{} - for _, sp := range src.Spec.Sources { - sources = append(sources, Source{ - Source: sp.Source, - Provider: SyncProviderType(sp.Provider), - HttpSyncBearerToken: sp.HttpSyncBearerToken, - TLS: sp.TLS, - CertPath: sp.CertPath, - ProviderID: sp.ProviderID, - Selector: sp.Selector, - }) - } - - dst.Spec = FlagSourceConfigurationSpec{ - MetricsPort: src.Spec.MetricsPort, - Port: src.Spec.Port, - SocketPath: src.Spec.SocketPath, - Evaluator: src.Spec.Evaluator, - Image: src.Spec.Image, - Tag: src.Spec.Tag, - Sources: sources, - EnvVars: src.Spec.EnvVars, - DefaultSyncProvider: string(src.Spec.DefaultSyncProvider), - LogFormat: src.Spec.LogFormat, - EnvVarPrefix: src.Spec.EnvVarPrefix, - RolloutOnChange: src.Spec.RolloutOnChange, - ProbesEnabled: src.Spec.ProbesEnabled, - DebugLogging: src.Spec.DebugLogging, - OtelCollectorUri: src.Spec.OtelCollectorUri, - } - return nil -} diff --git a/apis/core/v1alpha3/flagsourceconfiguration_conversion_test.go b/apis/core/v1alpha3/flagsourceconfiguration_conversion_test.go deleted file mode 100644 index 978ee0515..000000000 --- a/apis/core/v1alpha3/flagsourceconfiguration_conversion_test.go +++ /dev/null @@ -1,276 +0,0 @@ -package v1alpha3 - -import ( - "testing" - - "github.com/open-feature/open-feature-operator/apis/core/v1alpha1" - "github.com/open-feature/open-feature-operator/apis/core/v1alpha3/common" - "github.com/open-feature/open-feature-operator/common/utils" - "github.com/stretchr/testify/require" - corev1 "k8s.io/api/core/v1" - v1 "k8s.io/apimachinery/pkg/apis/meta/v1" - v2 "sigs.k8s.io/controller-runtime/pkg/webhook/conversion/testdata/api/v2" -) - -func TestFlagSourceConfiguration_ConvertFrom(t *testing.T) { - tt := true - tests := []struct { - name string - srcObj *v1alpha1.FlagSourceConfiguration - wantErr bool - wantObj *FlagSourceConfiguration - }{ - { - name: "Test that conversion from v1alpha1 to v1alpha3 works", - srcObj: &v1alpha1.FlagSourceConfiguration{ - TypeMeta: v1.TypeMeta{ - Kind: "FlagSourceConfiguration", - APIVersion: "core.openfeature.dev/v1alpha1", - }, - ObjectMeta: v1.ObjectMeta{ - Name: "flagsourceconfig1", - Namespace: "default", - }, - Spec: v1alpha1.FlagSourceConfigurationSpec{ - MetricsPort: 20, - Port: 21, - SocketPath: "path", - Evaluator: "eval", - Image: "img", - Tag: "tag", - Sources: []v1alpha1.Source{ - { - Source: "source", - Provider: "provider", - HttpSyncBearerToken: "token", - TLS: true, - CertPath: "etc/cert.ca", - ProviderID: "app", - Selector: "source=database", - }, - }, - EnvVars: []corev1.EnvVar{ - { - Name: "env1", - Value: "val1", - }, - { - Name: "env2", - Value: "val2", - }, - }, - DefaultSyncProvider: v1alpha1.SyncProviderType("provider"), - LogFormat: "log", - EnvVarPrefix: "pre", - RolloutOnChange: &tt, - DebugLogging: utils.FalseVal(), - OtelCollectorUri: "", - }, - }, - wantErr: false, - wantObj: &FlagSourceConfiguration{ - ObjectMeta: v1.ObjectMeta{ - Name: "flagsourceconfig1", - Namespace: "default", - }, - Spec: FlagSourceConfigurationSpec{ - MetricsPort: 20, - Port: 21, - SocketPath: "path", - Evaluator: "eval", - Image: "img", - Tag: "tag", - Sources: []Source{ - { - Source: "source", - Provider: "provider", - HttpSyncBearerToken: "token", - TLS: true, - CertPath: "etc/cert.ca", - ProviderID: "app", - Selector: "source=database", - }, - }, - EnvVars: []corev1.EnvVar{ - { - Name: "env1", - Value: "val1", - }, - { - Name: "env2", - Value: "val2", - }, - }, - DefaultSyncProvider: "provider", - LogFormat: "log", - EnvVarPrefix: "pre", - RolloutOnChange: &tt, - DebugLogging: utils.FalseVal(), - }, - }, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - dst := &FlagSourceConfiguration{ - TypeMeta: v1.TypeMeta{}, - ObjectMeta: v1.ObjectMeta{}, - Spec: FlagSourceConfigurationSpec{}, - Status: FlagSourceConfigurationStatus{}, - } - 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 TestFlagSourceConfiguration_ConvertTo(t *testing.T) { - tt := true - tests := []struct { - name string - src *FlagSourceConfiguration - wantErr bool - wantObj *v1alpha1.FlagSourceConfiguration - }{ - { - name: "Test that conversion from v1alpha3 to v1alpha1 works", - src: &FlagSourceConfiguration{ - TypeMeta: v1.TypeMeta{ - Kind: "FlagSourceConfiguration", - APIVersion: "core.openfeature.dev/v1alpha3", - }, - ObjectMeta: v1.ObjectMeta{ - Name: "flagsourceconfig1", - Namespace: "default", - }, - Spec: FlagSourceConfigurationSpec{ - MetricsPort: 20, - Port: 21, - SocketPath: "path", - Evaluator: "eval", - Image: "img", - Tag: "tag", - Sources: []Source{ - { - Source: "source", - Provider: "provider", - HttpSyncBearerToken: "token", - TLS: false, - CertPath: "etc/cert.ca", - ProviderID: "app", - Selector: "source=database", - }, - }, - EnvVars: []corev1.EnvVar{ - { - Name: "env1", - Value: "val1", - }, - { - Name: "env2", - Value: "val2", - }, - }, - DefaultSyncProvider: "provider", - LogFormat: "log", - EnvVarPrefix: "pre", - RolloutOnChange: &tt, - DebugLogging: utils.FalseVal(), - }, - }, - wantErr: false, - wantObj: &v1alpha1.FlagSourceConfiguration{ - ObjectMeta: v1.ObjectMeta{ - Name: "flagsourceconfig1", - Namespace: "default", - }, - Spec: v1alpha1.FlagSourceConfigurationSpec{ - MetricsPort: 20, - Port: 21, - SocketPath: "path", - Evaluator: "eval", - Image: "img", - Tag: "tag", - Sources: []v1alpha1.Source{ - { - Source: "source", - Provider: "provider", - HttpSyncBearerToken: "token", - TLS: false, - CertPath: "etc/cert.ca", - ProviderID: "app", - Selector: "source=database", - }, - }, - EnvVars: []corev1.EnvVar{ - { - Name: "env1", - Value: "val1", - }, - { - Name: "env2", - Value: "val2", - }, - }, - DefaultSyncProvider: v1alpha1.SyncProviderType("provider"), - LogFormat: "log", - EnvVarPrefix: "pre", - RolloutOnChange: &tt, - DebugLogging: utils.FalseVal(), - OtelCollectorUri: "", - }, - }, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - dst := v1alpha1.FlagSourceConfiguration{ - TypeMeta: v1.TypeMeta{}, - ObjectMeta: v1.ObjectMeta{}, - Spec: v1alpha1.FlagSourceConfigurationSpec{}, - Status: v1alpha1.FlagSourceConfigurationStatus{}, - } - 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 TestFlagSourceConfiguration_ConvertFrom_Errorcase(t *testing.T) { - // A random different object is used here to simulate a different API version - testObj := v2.ExternalJob{} - - dst := &FlagSourceConfiguration{ - TypeMeta: v1.TypeMeta{}, - ObjectMeta: v1.ObjectMeta{}, - Spec: FlagSourceConfigurationSpec{}, - Status: FlagSourceConfigurationStatus{}, - } - - if err := dst.ConvertFrom(&testObj); err == nil { - t.Errorf("ConvertFrom() error = %v", err) - } else { - require.ErrorIs(t, err, common.ErrCannotCastFlagSourceConfiguration) - } -} - -func TestFlagSourceConfiguration_ConvertTo_Errorcase(t *testing.T) { - testObj := FlagSourceConfiguration{} - - // 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.ErrorIs(t, err, common.ErrCannotCastFlagSourceConfiguration) - } -} diff --git a/apis/core/v1alpha3/flagsourceconfiguration_types.go b/apis/core/v1alpha3/flagsourceconfiguration_types.go index 2eea0d9e7..1f03e17ae 100644 --- a/apis/core/v1alpha3/flagsourceconfiguration_types.go +++ b/apis/core/v1alpha3/flagsourceconfiguration_types.go @@ -23,14 +23,8 @@ import ( type SyncProviderType string -// 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. - // FlagSourceConfigurationSpec defines the desired state of FlagSourceConfiguration type FlagSourceConfigurationSpec struct { - // INSERT ADDITIONAL SPEC FIELDS - desired state of cluster - // Important: Run "make" to regenerate code after modifying this file - // MetricsPort defines the port to serve metrics on, defaults to 8014 // +optional MetricsPort int32 `json:"metricsPort"` @@ -133,8 +127,6 @@ type Source struct { // FlagSourceConfigurationStatus defines the observed state of FlagSourceConfiguration type FlagSourceConfigurationStatus struct { - // INSERT ADDITIONAL STATUS FIELD - define observed state of cluster - // Important: Run "make" to regenerate code after modifying this file } //+kubebuilder:resource:shortName="fsc"