Skip to content

Commit

Permalink
feat: introduce v1beta1/common package (open-feature#547)
Browse files Browse the repository at this point in the history
Signed-off-by: odubajDT <[email protected]>
  • Loading branch information
odubajDT authored Nov 16, 2023
1 parent 1856918 commit cdc4af4
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 34 deletions.
11 changes: 0 additions & 11 deletions apis/core/v1beta1/common.go

This file was deleted.

57 changes: 57 additions & 0 deletions apis/core/v1beta1/common/common.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package common

import "fmt"

type SyncProviderType string

const (
SyncProviderKubernetes SyncProviderType = "kubernetes"
SyncProviderFilepath SyncProviderType = "file"
SyncProviderHttp SyncProviderType = "http"
SyncProviderGrpc SyncProviderType = "grpc"
SyncProviderFlagdProxy SyncProviderType = "flagd-proxy"
)

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 TrueVal() *bool {
b := true
return &b
}

func FalseVal() *bool {
b := false
return &b
}

func EnvVarKey(prefix string, suffix string) string {
return fmt.Sprintf("%s_%s", prefix, suffix)
}

// unique string used to create unique volume mount and file name
func FeatureFlagConfigurationId(namespace, name string) string {
return EnvVarKey(namespace, name)
}

// unique key (and filename) for configMap data
func FeatureFlagConfigMapKey(namespace, name string) string {
return fmt.Sprintf("%s.flagd.json", FeatureFlagConfigurationId(namespace, name))
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package v1beta1
package common

import (
"testing"
Expand All @@ -22,3 +22,15 @@ func Test_FeatureFlagSource_SyncProvider(t *testing.T) {
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_FeatureFlagConfigurationId(t *testing.T) {
require.Equal(t, "pre_suf", FeatureFlagConfigurationId("pre", "suf"))
}

func Test_FLagSourceConfiguration_FeatureFlagConfigMapKey(t *testing.T) {
require.Equal(t, "pre_suf.flagd.json", FeatureFlagConfigMapKey("pre", "suf"))
}
25 changes: 3 additions & 22 deletions apis/core/v1beta1/featureflagsource_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package v1beta1

import (
"github.com/open-feature/open-feature-operator/apis/core/v1beta1/common"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
Expand Down Expand Up @@ -62,7 +63,7 @@ type FeatureFlagSourceSpec struct {

// DefaultSyncProvider defines the default sync provider
// +optional
DefaultSyncProvider SyncProviderType `json:"defaultSyncProvider"`
DefaultSyncProvider common.SyncProviderType `json:"defaultSyncProvider"`

// LogFormat allows for the sidecar log format to be overridden, defaults to 'json'
// +optional
Expand Down Expand Up @@ -100,7 +101,7 @@ type Source struct {

// Provider type - kubernetes, http(s), grpc(s) or filepath
// +optional
Provider SyncProviderType `json:"provider"`
Provider common.SyncProviderType `json:"provider"`

// HttpSyncBearerToken is a bearer token. Used by http(s) sync provider only
// +optional
Expand Down Expand Up @@ -152,23 +153,3 @@ type FeatureFlagSourceList struct {
func init() {
SchemeBuilder.Register(&FeatureFlagSource{}, &FeatureFlagSourceList{})
}

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
}

0 comments on commit cdc4af4

Please sign in to comment.