Skip to content

Commit

Permalink
feat: only fire modify event when FeatureFlagConfiguration has been c…
Browse files Browse the repository at this point in the history
…hanged

Signed-off-by: Skye Gill <[email protected]>
  • Loading branch information
skyerus committed Sep 27, 2022
1 parent 310fccc commit eff1470
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 19 deletions.
11 changes: 5 additions & 6 deletions pkg/sync/kubernetes/featureflagconfiguration/clientset.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"errors"
"reflect"
"time"

"github.com/open-feature/flagd/pkg/sync"
ffv1alpha1 "github.com/open-feature/open-feature-operator/apis/core/v1alpha1"
Expand Down Expand Up @@ -42,7 +41,7 @@ func createFuncHandler(obj interface{}, object client.ObjectKey, c chan<- sync.I
return nil
}

func updateFuncHandler(oldObj interface{}, newObj interface{}, c chan<- sync.INotify) error {
func updateFuncHandler(oldObj interface{}, newObj interface{}, object client.ObjectKey, c chan<- sync.INotify) error {
if reflect.TypeOf(oldObj) != reflect.TypeOf(&ffv1alpha1.FeatureFlagConfiguration{}) {
return errors.New("old object is not a FeatureFlagConfiguration")
}
Expand All @@ -51,7 +50,7 @@ func updateFuncHandler(oldObj interface{}, newObj interface{}, c chan<- sync.INo
}
oldObjConfig := oldObj.(*ffv1alpha1.FeatureFlagConfiguration)
newObjConfig := newObj.(*ffv1alpha1.FeatureFlagConfiguration)
if oldObjConfig.Generation != newObjConfig.Generation { // generation difference indicates a change
if object.Name == newObjConfig.Name && oldObjConfig.ResourceVersion != newObjConfig.ResourceVersion {
// Only update if there is an actual featureFlagSpec change
c <- &sync.Notifier{
Event: sync.Event[sync.DefaultEventType]{
Expand All @@ -76,7 +75,7 @@ func deleteFuncHandler(obj interface{}, object client.ObjectKey, c chan<- sync.I
return nil
}

func WatchResources(ctx context.Context, l log.Entry, clientSet FFCInterface, refreshTime time.Duration,
func WatchResources(ctx context.Context, l log.Entry, clientSet FFCInterface,
object client.ObjectKey, c chan<- sync.INotify,
) {
ns := "*"
Expand All @@ -93,7 +92,7 @@ func WatchResources(ctx context.Context, l log.Entry, clientSet FFCInterface, re
},
},
&ffv1alpha1.FeatureFlagConfiguration{},
refreshTime,
0,
cache.ResourceEventHandlerFuncs{
AddFunc: func(obj interface{}) {
if err := createFuncHandler(obj, object, c); err != nil {
Expand All @@ -107,7 +106,7 @@ func WatchResources(ctx context.Context, l log.Entry, clientSet FFCInterface, re
},
UpdateFunc: func(oldObj, newObj interface{}) {
// This indicates a change to the custom resource
if err := updateFuncHandler(oldObj, newObj, c); err != nil {
if err := updateFuncHandler(oldObj, newObj, object, c); err != nil {
l.Warn(err.Error())
}
},
Expand Down
16 changes: 3 additions & 13 deletions pkg/sync/kubernetes/kubernetes_sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package kubernetes
import (
"context"
"os"
"time"

"github.com/open-feature/flagd/pkg/sync"
"github.com/open-feature/flagd/pkg/sync/kubernetes/featureflagconfiguration"
Expand All @@ -15,8 +14,6 @@ import (
controllerClient "sigs.k8s.io/controller-runtime/pkg/client"
)

var refreshTime = time.Second * 5

const (
featureFlagConfigurationName = "featureflagconfiguration"
featureFlagNamespaceName = "namespace"
Expand Down Expand Up @@ -76,14 +73,6 @@ func (k *Sync) Notify(ctx context.Context, c chan<- sync.INotify) {
k.Logger.Panic(err.Error())
}

if k.ProviderArgs["refreshtime"] != "" {
hr, err := time.ParseDuration(k.ProviderArgs["refreshtime"])
if err != nil {
k.Logger.Panic(err.Error())
}
refreshTime = hr
}

k.client, err = featureflagconfiguration.NewForConfig(config)
if err != nil {
k.Logger.Panic(err.Error())
Expand All @@ -96,7 +85,8 @@ func (k *Sync) Notify(ctx context.Context, c chan<- sync.INotify) {
go featureflagconfiguration.WatchResources(ctx, *k.Logger.WithFields(log.Fields{
"sync": "kubernetes",
"component": "watchresources",
}), k.client, refreshTime, controllerClient.ObjectKey{
Name: k.ProviderArgs[featureFlagConfigurationName],
}), k.client, controllerClient.ObjectKey{
Name: k.ProviderArgs[featureFlagConfigurationName],
Namespace: k.ProviderArgs[featureFlagNamespaceName],
}, c)
}

0 comments on commit eff1470

Please sign in to comment.