Skip to content

Commit

Permalink
feat: use v1beta1 in operator logic
Browse files Browse the repository at this point in the history
Signed-off-by: odubajDT <[email protected]>
  • Loading branch information
odubajDT committed Nov 7, 2023
1 parent b0971c5 commit c5065fd
Show file tree
Hide file tree
Showing 64 changed files with 1,822 additions and 5,120 deletions.
4 changes: 2 additions & 2 deletions .github/scripts/create-reports.sh
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ for namespace in $(kubectl get namespaces -o jsonpath='{.items[*].metadata.name}
createResourceReport "$logsDir/$namespace" "$namespace" "Daemonsets" false
createResourceReport "$logsDir/$namespace" "$namespace" "Statefulsets" false
createResourceReport "$logsDir/$namespace" "$namespace" "Jobs" false
createResourceReport "$logsDir/$namespace" "$namespace" "FeatureFlagConfiguration" false
createResourceReport "$logsDir/$namespace" "$namespace" "FlagSourceConfiguration" false
createResourceReport "$logsDir/$namespace" "$namespace" "FeatureFlag" false
createResourceReport "$logsDir/$namespace" "$namespace" "FeatureFlagSource" false

done
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ COPY main.go main.go
COPY apis/ apis/
COPY webhooks/ webhooks/
COPY controllers/ controllers/
COPY pkg/ pkg/
COPY common/ common/

ARG TARGETOS
ARG TARGETARCH
Expand Down
4 changes: 2 additions & 2 deletions apis/core/v1alpha1/featureflagconfiguration_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ limitations under the License.
package v1alpha1

import (
"github.com/open-feature/open-feature-operator/pkg/utils"
"github.com/open-feature/open-feature-operator/common/utils"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
Expand Down Expand Up @@ -129,7 +129,7 @@ func (ff *FeatureFlagConfiguration) GenerateConfigMap(name string, namespace str
OwnerReferences: references,
},
Data: map[string]string{
utils.FeatureFlagConfigurationConfigMapKey(namespace, name): ff.Spec.FeatureFlagSpec,
utils.FeatureFlagConfigMapKey(namespace, name): ff.Spec.FeatureFlagSpec,
},
}
}
Expand Down
2 changes: 1 addition & 1 deletion apis/core/v1alpha1/featureflagconfiguration_types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package v1alpha1
import (
"testing"

"github.com/open-feature/open-feature-operator/pkg/utils"
"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"
Expand Down
2 changes: 1 addition & 1 deletion apis/core/v1alpha1/flagsourceconfiguration_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (
"strconv"
"strings"

"github.com/open-feature/open-feature-operator/pkg/utils"
"github.com/open-feature/open-feature-operator/common/utils"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
Expand Down
2 changes: 1 addition & 1 deletion apis/core/v1alpha1/flagsourceconfiguration_types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package v1alpha1
import (
"testing"

"github.com/open-feature/open-feature-operator/pkg/utils"
"github.com/open-feature/open-feature-operator/common/utils"
"github.com/stretchr/testify/require"
v1 "k8s.io/api/core/v1"
)
Expand Down
2 changes: 1 addition & 1 deletion apis/core/v1alpha2/flagsourceconfiguration_conversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (

"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/pkg/utils"
"github.com/open-feature/open-feature-operator/common/utils"
"sigs.k8s.io/controller-runtime/pkg/conversion"
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (

"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/pkg/utils"
"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"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (

"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/pkg/utils"
"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"
Expand Down
33 changes: 33 additions & 0 deletions apis/core/v1beta1/featureflag_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ package v1beta1
import (
"encoding/json"

"github.com/open-feature/open-feature-operator/common/utils"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

Expand Down Expand Up @@ -61,6 +63,7 @@ type FeatureFlagStatus struct {
//+kubebuilder:resource:shortName="ffc"
//+kubebuilder:object:root=true
//+kubebuilder:subresource:status
//+kubebuilder:storageversion

// FeatureFlag is the Schema for the featureflags API
type FeatureFlag struct {
Expand All @@ -83,3 +86,33 @@ type FeatureFlagList struct {
func init() {
SchemeBuilder.Register(&FeatureFlag{}, &FeatureFlagList{})
}

func (ff *FeatureFlag) GetReference() metav1.OwnerReference {
return metav1.OwnerReference{
APIVersion: ff.APIVersion,
Kind: ff.Kind,
Name: ff.Name,
UID: ff.UID,
Controller: utils.TrueVal(),
}
}

func (ff *FeatureFlag) GenerateConfigMap(name string, namespace string, references []metav1.OwnerReference) (*corev1.ConfigMap, error) {
b, err := json.Marshal(ff.Spec.FlagSpec)
if err != nil {
return nil, err
}
return &corev1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: namespace,
Annotations: map[string]string{
"openfeature.dev/featureflag": name,
},
OwnerReferences: references,
},
Data: map[string]string{
utils.FeatureFlagConfigMapKey(namespace, name): string(b),
},
}, nil
}
222 changes: 221 additions & 1 deletion apis/core/v1beta1/featureflagsource_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,41 @@ limitations under the License.
package v1beta1

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"
)

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
)

// FeatureFlagSourceSpec defines the desired state of FeatureFlagSource
type FeatureFlagSourceSpec struct {
// MetricsPort defines the port to serve metrics on, defaults to 8014
Expand Down Expand Up @@ -51,7 +82,7 @@ type FeatureFlagSourceSpec struct {
// +kubebuilder:validation:MinItems=1
Sources []Source `json:"sources"`

// EnvVars define the env vars to be applied to the sidecar, any env vars in FeatureFlagConfiguration CRs
// EnvVars define the env vars to be applied to the sidecar, any env vars in FeatureFlag CRs
// are added at the lowest index, all values will have the EnvVarPrefix applied, default FLAGD
// +optional
EnvVars []corev1.EnvVar `json:"envVars"`
Expand Down Expand Up @@ -130,6 +161,7 @@ type FeatureFlagSourceStatus struct {
//+kubebuilder:resource:shortName="ffs"
//+kubebuilder:object:root=true
//+kubebuilder:subresource:status
//+kubebuilder:storageversion

// FeatureFlagSource is the Schema for the FeatureFlagSources API
type FeatureFlagSource struct {
Expand Down Expand Up @@ -172,3 +204,191 @@ func (s SyncProviderType) IsGrpc() bool {
func (s SyncProviderType) IsFlagdProxy() bool {
return s == SyncProviderFlagdProxy
}

func NewFeatureFlagSourceSpec() (*FeatureFlagSourceSpec, error) {

Check failure on line 208 in apis/core/v1beta1/featureflagsource_types.go

View workflow job for this annotation

GitHub Actions / golangci-lint

cyclomatic complexity 15 of func `NewFeatureFlagSourceSpec` is high (> 10) (gocyclo)
fsc := &FeatureFlagSourceSpec{
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
}

func (fc *FeatureFlagSourceSpec) Merge(new *FeatureFlagSourceSpec) {

Check failure on line 290 in apis/core/v1beta1/featureflagsource_types.go

View workflow job for this annotation

GitHub Actions / golangci-lint

cyclomatic complexity 19 of func `(*FeatureFlagSourceSpec).Merge` is high (> 10) (gocyclo)
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 *FeatureFlagSourceSpec) 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 envVarKey(prefix string, suffix string) string {
return fmt.Sprintf("%s_%s", prefix, suffix)
}
Loading

0 comments on commit c5065fd

Please sign in to comment.