Skip to content

Commit

Permalink
feat: structured the featureflagconfiguration CRD
Browse files Browse the repository at this point in the history
Signed-off-by: Skye Gill <gill.skye95@gmail.com>
  • Loading branch information
skyerus committed Nov 15, 2022
1 parent 28c8f36 commit b056c7c
Show file tree
Hide file tree
Showing 9 changed files with 173 additions and 69 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ ENVTEST ?= $(LOCALBIN)/setup-envtest

## Tool Versions
KUSTOMIZE_VERSION ?= v4.5.7
CONTROLLER_TOOLS_VERSION ?= v0.8.0
CONTROLLER_TOOLS_VERSION ?= v0.10.0

KUSTOMIZE_INSTALL_SCRIPT ?= "https://raw.githubusercontent.com/kubernetes-sigs/kustomize/master/hack/install_kustomize.sh"
.PHONY: kustomize
Expand Down
41 changes: 37 additions & 4 deletions apis/core/v1alpha1/featureflagconfiguration_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ limitations under the License.
package v1alpha1

import (
"encoding/json"
"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 @@ -39,7 +41,7 @@ type FeatureFlagConfigurationSpec struct {
// +nullable
FlagDSpec *FlagDSpec `json:"flagDSpec"`
// FeatureFlagSpec is the json representation of the feature flag
FeatureFlagSpec string `json:"featureFlagSpec,omitempty"`
FeatureFlagSpec *FeatureFlagSpec `json:"featureFlagSpec,omitempty"`
}

type FlagDSpec struct {
Expand All @@ -49,6 +51,26 @@ 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 Down Expand Up @@ -106,7 +128,18 @@ func GetFfReference(ff *FeatureFlagConfiguration) metav1.OwnerReference {
}
}

func GenerateFfConfigMap(name string, namespace string, references []metav1.OwnerReference, spec FeatureFlagConfigurationSpec) corev1.ConfigMap {
func GenerateFfConfigMap(
name string, namespace string, references []metav1.OwnerReference, spec FeatureFlagConfigurationSpec,
) (corev1.ConfigMap, error) {
var config []byte
var err error
if spec.FeatureFlagSpec != nil {
config, err = json.Marshal(spec.FeatureFlagSpec)
if err != nil {
return corev1.ConfigMap{}, fmt.Errorf("feature flag spec: %w", err)
}
}

return corev1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Expand All @@ -117,7 +150,7 @@ func GenerateFfConfigMap(name string, namespace string, references []metav1.Owne
OwnerReferences: references,
},
Data: map[string]string{
"config.json": spec.FeatureFlagSpec,
"config.json": string(config),
},
}
}, nil
}
53 changes: 53 additions & 0 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.

Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
annotations:
controller-gen.kubebuilder.io/version: v0.8.0
controller-gen.kubebuilder.io/version: v0.10.0
creationTimestamp: null
name: featureflagconfigurations.core.openfeature.dev
spec:
Expand Down Expand Up @@ -40,7 +40,33 @@ spec:
featureFlagSpec:
description: FeatureFlagSpec is the json representation of the feature
flag
type: string
properties:
flags:
additionalProperties:
properties:
defaultVariant:
type: string
state:
enum:
- ENABLED
- DISABLED
type: string
targeting:
description: Targeting is the json targeting rule
type: object
x-kubernetes-preserve-unknown-fields: true
variants:
type: object
x-kubernetes-preserve-unknown-fields: true
required:
- defaultVariant
- state
- variants
type: object
type: object
required:
- flags
type: object
flagDSpec:
nullable: true
properties:
Expand Down Expand Up @@ -86,6 +112,7 @@ spec:
required:
- key
type: object
x-kubernetes-map-type: atomic
fieldRef:
description: 'Selects a field of the pod: supports metadata.name,
metadata.namespace, `metadata.labels[''<KEY>'']`,
Expand All @@ -104,6 +131,7 @@ spec:
required:
- fieldPath
type: object
x-kubernetes-map-type: atomic
resourceFieldRef:
description: 'Selects a resource of the container: only
resources limits and requests (limits.cpu, limits.memory,
Expand All @@ -128,6 +156,7 @@ spec:
required:
- resource
type: object
x-kubernetes-map-type: atomic
secretKeyRef:
description: Selects a key of a secret in the pod's
namespace
Expand All @@ -148,6 +177,7 @@ spec:
required:
- key
type: object
x-kubernetes-map-type: atomic
type: object
required:
- name
Expand Down Expand Up @@ -223,6 +253,7 @@ spec:
description: 'UID of the referent. More info: https://kubernetes.io/docs/concepts/overview/working-with-objects/names/#uids'
type: string
type: object
x-kubernetes-map-type: atomic
name:
enum:
- flagd
Expand All @@ -248,9 +279,3 @@ spec:
storage: true
subresources:
status: {}
status:
acceptedNames:
kind: ""
plural: ""
conditions: []
storedVersions: []
68 changes: 25 additions & 43 deletions config/samples/end-to-end.yaml
Original file line number Diff line number Diff line change
@@ -1,46 +1,34 @@
# A basic flag custom resource
apiVersion: v1
kind: Namespace
metadata:
name: open-feature-demo
---
apiVersion: core.openfeature.dev/v1alpha1
kind: FeatureFlagConfiguration
metadata:
name: end-to-end
namespace: open-feature-demo
spec:
featureFlagSpec: |
{
"flags": {
"new-welcome-message": {
"state": "ENABLED",
"variants": {
"on": true,
"off": false
},
"defaultVariant": "on"
},
"hex-color": {
"variants": {
"red": "CC0000",
"green": "00CC00",
"blue": "0000CC",
"yellow": "yellow"
},
"defaultVariant": "red",
"state": "ENABLED"
},
"fib-algo": {
"variants": {
"recursive": "recursive",
"memo": "memo",
"loop": "loop",
"binet": "binet"
},
"defaultVariant": "recursive",
"state": "ENABLED",
"targeting": {
featureFlagSpec:
flags:
new-welcome-message:
state: ENABLED
variants:
on: true
off: false
defaultVariant: "on"
hex-color:
state: ENABLED
variants:
red: CC0000
green: 00CC00
blue: 0000CC
yellow: yellow
defaultVariant: blue
fib-algo:
state: ENABLED
variants:
recursive: recursive
memo: memo
loop: loop
binet: binet
defaultVariant: recursive
targeting: {
"if": [
{
"in": ["@faas.com", {
Expand All @@ -49,16 +37,12 @@ spec:
}, "binet", null
]
}
}
}
}
---
# Deployment of a demo-app using our custom resource
apiVersion: apps/v1
kind: Deployment
metadata:
name: open-feature-demo-deployment
namespace: open-feature-demo
labels:
app: open-feature-demo
spec:
Expand Down Expand Up @@ -88,7 +72,6 @@ apiVersion: v1
kind: Service
metadata:
name: open-feature-demo-service
namespace: open-feature-demo
spec:
type: ClusterIP
selector:
Expand All @@ -102,5 +85,4 @@ apiVersion: v1
kind: ServiceAccount
metadata:
name: open-feature-demo-sa
namespace: open-feature-demo
automountServiceAccountToken: true
18 changes: 15 additions & 3 deletions controllers/featureflagconfiguration_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ package controllers

import (
"context"
"encoding/json"
"fmt"
"time"

"github.com/go-logr/logr"
Expand Down Expand Up @@ -152,9 +154,16 @@ func (r *FeatureFlagConfigurationReconciler) Reconcile(ctx context.Context, req
}
// Update ConfigMap Spec
r.Log.Info("Updating ConfigMap Spec " + cm.Name)
cm.Data = map[string]string{
"config.json": ffconf.Spec.FeatureFlagSpec,
if ffconf.Spec.FeatureFlagSpec != nil {
config, err := json.Marshal(ffconf.Spec.FeatureFlagSpec)
if err != nil {
return r.finishReconcile(fmt.Errorf("feature flag spec: %w", err), false)
}
cm.Data = map[string]string{
"config.json": string(config),
}
}

err := r.Client.Update(ctx, &cm)
if err != nil {
return r.finishReconcile(err, true)
Expand All @@ -165,7 +174,10 @@ func (r *FeatureFlagConfigurationReconciler) Reconcile(ctx context.Context, req
ffOwnerRefs := []metav1.OwnerReference{
corev1alpha1.GetFfReference(ffconf),
}
cm := corev1alpha1.GenerateFfConfigMap(ffconf.Name, ffconf.Namespace, ffOwnerRefs, ffconf.Spec)
cm, err := corev1alpha1.GenerateFfConfigMap(ffconf.Name, ffconf.Namespace, ffOwnerRefs, ffconf.Spec)
if err != nil {
return r.finishReconcile(err, false)
}

podList := &corev1.PodList{}
if err := r.List(ctx, podList); err != nil {
Expand Down
Loading

0 comments on commit b056c7c

Please sign in to comment.