Skip to content

Commit

Permalink
chore: extract flagd container injection into its own component (#474)
Browse files Browse the repository at this point in the history
Signed-off-by: Florian Bacher <florian.bacher@dynatrace.com>
  • Loading branch information
bacherfl authored Apr 24, 2023
1 parent f9d4cdb commit 9ed8e59
Show file tree
Hide file tree
Showing 19 changed files with 2,128 additions and 765 deletions.
5 changes: 5 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -235,3 +235,8 @@ helm-package: set-helm-overlay generate release-manifests helm
mkdir -p charts && mv open-feature-operator-*.tgz charts
$(HELM) repo index --url https://open-feature.github.io/open-feature-operator/charts charts
mv charts/index.yaml index.yaml

install-mockgen:
go install github.com/golang/mock/mockgen@v1.6.0
mockgen: install-mockgen
mockgen -source=controllers/common/flagd-injector.go -destination=controllers/common/mock/flagd-injector.go -package=commonmock
24 changes: 23 additions & 1 deletion controllers/common/common.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
package common

import (
"context"
"fmt"
"github.com/open-feature/open-feature-operator/apis/core/v1alpha1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"time"

appsV1 "k8s.io/api/apps/v1"
"sigs.k8s.io/controller-runtime/pkg/client"
)

const (
CrdName = "FeatureFlagConfiguration"
ReconcileErrorInterval = 10 * time.Second
ReconcileSuccessInterval = 120 * time.Second
FinalizerName = "featureflagconfiguration.core.openfeature.dev/finalizer"
Expand Down Expand Up @@ -40,3 +42,23 @@ func FlagSourceConfigurationIndex(o client.Object) []string {
"false",
}
}

func FindFlagConfig(ctx context.Context, c client.Client, namespace string, name string) (*v1alpha1.FeatureFlagConfiguration, error) {
ffConfig := &v1alpha1.FeatureFlagConfiguration{}
if err := c.Get(ctx, client.ObjectKey{Name: name, Namespace: namespace}, ffConfig); err != nil {
return nil, err
}
return ffConfig, nil
}

// SharedOwnership returns true if any of the owner references match in the given slices
func SharedOwnership(ownerReferences1, ownerReferences2 []metav1.OwnerReference) bool {
for _, owner1 := range ownerReferences1 {
for _, owner2 := range ownerReferences2 {
if owner1.UID == owner2.UID {
return true
}
}
}
return false
}
40 changes: 40 additions & 0 deletions controllers/common/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,43 @@ func TestFlagSourceConfigurationIndex(t *testing.T) {

}
}

func TestSharedOwnership(t *testing.T) {
tests := []struct {
name string
owner1 []metav1.OwnerReference
owner2 []metav1.OwnerReference
want bool
}{{
name: "same owner uid",
owner1: []metav1.OwnerReference{
{
UID: "12345",
},
},
owner2: []metav1.OwnerReference{
{
UID: "12345",
},
},
want: true,
},
{
name: "pod and cm have different owners",
owner1: []metav1.OwnerReference{},
owner2: []metav1.OwnerReference{
{
UID: "12345",
},
},
want: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := SharedOwnership(tt.owner1, tt.owner2); got != tt.want {
t.Errorf("podOwnerIsOwner() = %v, want %v", got, tt.want)
}
})
}
}
14 changes: 14 additions & 0 deletions controllers/common/constant/configuration.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package constant

import corev1 "k8s.io/api/core/v1"

const (
FlagDImagePullPolicy corev1.PullPolicy = "Always"
ClusterRoleBindingName string = "open-feature-operator-flagd-kubernetes-sync"
AllowKubernetesSyncAnnotation = "allowkubernetessync"
OpenFeatureAnnotationPrefix = "openfeature.dev"
SourceConfigParam = "--sources"
ProbeReadiness = "/readyz"
ProbeLiveness = "/healthz"
ProbeInitialDelay = 5
)
6 changes: 6 additions & 0 deletions controllers/common/constant/errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package constant

import "errors"

var ErrFlagdProxyNotReady = errors.New("flagd-proxy is not ready, deferring pod admission")
var ErrUnrecognizedSyncProvider = errors.New("unrecognized sync provider")
Loading

0 comments on commit 9ed8e59

Please sign in to comment.