Skip to content

Commit

Permalink
feat: introduced v1beta1 of featureflagconfiguration CRD with convers…
Browse files Browse the repository at this point in the history
…ion webhook to v1alpha1

Signed-off-by: Skye Gill <gill.skye95@gmail.com>
  • Loading branch information
skyerus committed Nov 15, 2022
1 parent d84c2f5 commit a45bdef
Show file tree
Hide file tree
Showing 16 changed files with 837 additions and 149 deletions.
11 changes: 11 additions & 0 deletions PROJECT
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,15 @@ resources:
kind: FeatureFlagConfiguration
path: github.com/open-feature/open-feature-operator/apis/core/v1alpha1
version: v1alpha1
webhooks:
conversion: true
webhookVersion: v1
- api:
crdVersion: v1
namespaced: true
domain: openfeature.dev
group: core
kind: FeatureFlagConfiguration
path: github.com/open-feature/open-feature-operator/apis/core/v1beta1
version: v1beta1
version: "3"
20 changes: 20 additions & 0 deletions apis/core/v1alpha1/featureflagconfiguration_conversion.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
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() {}
62 changes: 6 additions & 56 deletions apis/core/v1alpha1/featureflagconfiguration_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,6 @@ limitations under the License.
package v1alpha1

import (
"encoding/json"
"errors"
"fmt"
"github.com/open-feature/open-feature-operator/pkg/utils"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand All @@ -41,29 +38,8 @@ type FeatureFlagConfigurationSpec struct {
// +optional
// +nullable
FlagDSpec *FlagDSpec `json:"flagDSpec"`
// FeatureFlagSpec is the json representation of the feature flag specification
// Deprecated: use FeatureFlagSpecV2
FeatureFlagSpec *string `json:"featureFlagSpec,omitempty"`
// FeatureFlagSpec is the structured representation of the feature flag specification
FeatureFlagSpecV2 *FeatureFlagSpec `json:"featureFlagSpecV2,omitempty"`
}

func (ffcs FeatureFlagConfigurationSpec) FeatureFlagSpecJSON() (string, error) {
var ffcsJson string
if ffcs.FeatureFlagSpecV2 != nil { // prioritise V2
ffcsJsonB, err := json.Marshal(ffcs.FeatureFlagSpecV2)
if err != nil {
return "", fmt.Errorf("FeatureFlagSpecV2: %w", err)
}

ffcsJson = string(ffcsJsonB)
} else if ffcs.FeatureFlagSpec != nil {
ffcsJson = *ffcs.FeatureFlagSpec
} else {
return "", errors.New("FeatureFlagSpecV2 and FeatureFlagSpec are empty")
}

return ffcsJson, nil
// FeatureFlagSpec is the json representation of the feature flag
FeatureFlagSpec string `json:"featureFlagSpec,omitempty"`
}

type FlagDSpec struct {
Expand All @@ -73,26 +49,6 @@ type FlagDSpec struct {
Envs []corev1.EnvVar `json:"envs"`
}

type FeatureFlagSpec struct {
Flags map[string]FlagSpec `json:"flags"`
}

type FlagSpec struct {
// +kubebuilder:validation:Enum=ENABLED;DISABLED
State string `json:"state"`
// +kubebuilder:validation:Schemaless
// +kubebuilder:pruning:PreserveUnknownFields
// +kubebuilder:validation:Type=object
Variants json.RawMessage `json:"variants"`
DefaultVariant string `json:"defaultVariant"`
// +optional
// +kubebuilder:validation:Schemaless
// +kubebuilder:pruning:PreserveUnknownFields
// +kubebuilder:validation:Type=object
// Targeting is the json targeting rule
Targeting json.RawMessage `json:"targeting,omitempty"`
}

type FeatureFlagSyncProvider struct {
Name string `json:"name"`
}
Expand All @@ -117,6 +73,7 @@ type FeatureFlagConfigurationStatus struct {

//+kubebuilder:object:root=true
//+kubebuilder:subresource:status
//+kubebuilder:storageversion

// FeatureFlagConfiguration is the Schema for the featureflagconfigurations API
type FeatureFlagConfiguration struct {
Expand Down Expand Up @@ -150,14 +107,7 @@ func GetFfReference(ff *FeatureFlagConfiguration) metav1.OwnerReference {
}
}

func GenerateFfConfigMap(
name string, namespace string, references []metav1.OwnerReference, spec FeatureFlagConfigurationSpec,
) (corev1.ConfigMap, error) {
configJson, err := spec.FeatureFlagSpecJSON()
if err != nil {
return corev1.ConfigMap{}, err
}

func GenerateFfConfigMap(name string, namespace string, references []metav1.OwnerReference, spec FeatureFlagConfigurationSpec) corev1.ConfigMap {
return corev1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Expand All @@ -168,7 +118,7 @@ func GenerateFfConfigMap(
OwnerReferences: references,
},
Data: map[string]string{
"config.json": configJson,
"config.json": spec.FeatureFlagSpec,
},
}, nil
}
}
31 changes: 31 additions & 0 deletions apis/core/v1alpha1/featureflagconfiguration_webhook.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
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

import (
ctrl "sigs.k8s.io/controller-runtime"
logf "sigs.k8s.io/controller-runtime/pkg/log"
)

// log is for logging in this package.
var featureflagconfigurationlog = logf.Log.WithName("featureflagconfiguration-resource")

func (r *FeatureFlagConfiguration) SetupWebhookWithManager(mgr ctrl.Manager) error {
return ctrl.NewWebhookManagedBy(mgr).
For(r).
Complete()
}
58 changes: 0 additions & 58 deletions apis/core/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

89 changes: 89 additions & 0 deletions apis/core/v1beta1/featureflagconfiguration_conversion.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/*
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 v1beta1

import (
"encoding/json"
"fmt"
"github.com/open-feature/open-feature-operator/apis/core/v1alpha1"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/conversion"
)

func (ffc *FeatureFlagConfiguration) SetupWebhookWithManager(mgr ctrl.Manager) error {
return ctrl.NewWebhookManagedBy(mgr).
For(ffc).
Complete()
}

func (src *FeatureFlagConfiguration) ConvertTo(dstRaw conversion.Hub) error {
dst := dstRaw.(*v1alpha1.FeatureFlagConfiguration)

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.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 := srcRaw.(*v1alpha1.FeatureFlagConfiguration)

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: src.Spec.SyncProvider.Name}
}

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
}
Loading

0 comments on commit a45bdef

Please sign in to comment.