Skip to content

Commit

Permalink
feat: auto-upgrade flagd-proxy with OFO upgrades
Browse files Browse the repository at this point in the history
Signed-off-by: odubajDT <[email protected]>
  • Loading branch information
odubajDT committed Jan 16, 2024
1 parent f3f9427 commit 62a94b6
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 10 deletions.
42 changes: 34 additions & 8 deletions common/flagdproxy/flagdproxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package flagdproxy
import (
"context"
"fmt"
"reflect"

"github.com/go-logr/logr"
"github.com/open-feature/open-feature-operator/common/types"
Expand Down Expand Up @@ -74,13 +75,7 @@ func (f *FlagdProxyHandler) HandleFlagdProxy(ctx context.Context) error {
}

func (f *FlagdProxyHandler) deployFlagdProxy(ctx context.Context) error {
ownerReferences := []metav1.OwnerReference{}
ownerReference, err := f.getOwnerReference(ctx)
if err != nil {
f.Log.Error(err, "unable to create owner reference for open-feature-operator, not appending")
} else {
ownerReferences = append(ownerReferences, ownerReference)
}
ownerReferences := f.getOwnerReferences(ctx)

f.Log.Info("deploying the flagd-proxy")
if err := f.Client.Create(ctx, f.newFlagdProxyManifest(ownerReferences)); err != nil && !errors.IsAlreadyExists(err) {
Expand Down Expand Up @@ -189,10 +184,30 @@ func (f *FlagdProxyHandler) doesFlagdProxyExist(ctx context.Context) (bool, erro
// does not exist, is not ready, is in error
return false, err
}
// exists, at least one replica ready, no error
// generate new Deployment struct
newDeployment := f.newFlagdProxyManifest(f.getOwnerReferences(ctx))

if !f.isFlagdProxyUpToDate(d, newDeployment) {
f.Log.Info("flagd-proxy Deployment changed, updating")
// copy new content
d.Spec = newDeployment.Spec
d.Labels = newDeployment.Labels
d.OwnerReferences = newDeployment.OwnerReferences
// update
err = f.Client.Update(ctx, d)
if err != nil {
f.Log.Error(err, "Could not update flagd-proxy Deployment")
return false, err
}
}
// exists, at least one replica ready with correct version, no error
return true, nil
}

func (f *FlagdProxyHandler) isFlagdProxyUpToDate(old, new *appsV1.Deployment) bool {
return reflect.DeepEqual(old.Spec, new.Spec)
}

func (f *FlagdProxyHandler) getOwnerReference(ctx context.Context) (metav1.OwnerReference, error) {
d := &appsV1.Deployment{}
if err := f.Client.Get(ctx, client.ObjectKey{Name: f.config.OperatorDeploymentName, Namespace: f.config.Namespace}, d); err != nil {
Expand All @@ -206,3 +221,14 @@ func (f *FlagdProxyHandler) getOwnerReference(ctx context.Context) (metav1.Owner
}, nil

}

func (f *FlagdProxyHandler) getOwnerReferences(ctx context.Context) []metav1.OwnerReference {
ownerReferences := []metav1.OwnerReference{}
ownerReference, err := f.getOwnerReference(ctx)
if err != nil {
f.Log.Error(err, "unable to create owner reference for open-feature-operator, not appending")
} else {
ownerReferences = append(ownerReferences, ownerReference)
}
return ownerReferences
}
40 changes: 38 additions & 2 deletions common/flagdproxy/flagdproxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ func TestDoesFlagdProxyExist(t *testing.T) {
require.True(t, res)
}

func TestFlagdProxyHandler_HandleFlagdProxy_ProxyExists(t *testing.T) {
func TestFlagdProxyHandler_HandleFlagdProxy_ProxyExistsWithBadVersion(t *testing.T) {
env := types.EnvConfig{
PodNamespace: "ns",
}
Expand Down Expand Up @@ -159,8 +159,44 @@ func TestFlagdProxyHandler_HandleFlagdProxy_ProxyExists(t *testing.T) {
require.Nil(t, err)
require.NotNil(t, deployment)

// verify that the existing deployment has been changed
require.Equal(t, "flagd-proxy", deployment.Spec.Template.Spec.Containers[0].Name)
}

func TestFlagdProxyHandler_HandleFlagdProxy_ProxyExistsWithNewestVersion(t *testing.T) {
env := types.EnvConfig{
PodNamespace: "ns",
}
kpConfig := NewFlagdProxyConfiguration(env)

require.NotNil(t, kpConfig)

fakeClient := fake.NewClientBuilder().WithObjects().Build()

ph := NewFlagdProxyHandler(kpConfig, fakeClient, testr.New(t))

require.NotNil(t, ph)

proxy := ph.newFlagdProxyManifest([]metav1.OwnerReference{})

err := fakeClient.Create(context.TODO(), proxy)
require.Nil(t, err)

err = ph.HandleFlagdProxy(context.Background())

require.Nil(t, err)

deployment := &v1.Deployment{}
err = fakeClient.Get(context.Background(), client.ObjectKey{
Namespace: env.PodNamespace,
Name: FlagdProxyDeploymentName,
}, deployment)

require.Nil(t, err)
require.NotNil(t, deployment)

// verify that the existing deployment has not been changed
require.Equal(t, "my-container", deployment.Spec.Template.Spec.Containers[0].Name)
require.Equal(t, "flagd-proxy", deployment.Spec.Template.Spec.Containers[0].Name)
}

func TestFlagdProxyHandler_HandleFlagdProxy_CreateProxy(t *testing.T) {
Expand Down

0 comments on commit 62a94b6

Please sign in to comment.