Skip to content

Commit

Permalink
feat: only fire modify event when FeatureFlagConfiguration Generation…
Browse files Browse the repository at this point in the history
… field has changed (#167)

Signed-off-by: Skye Gill <gill.skye95@gmail.com>

Signed-off-by: Skye Gill <gill.skye95@gmail.com>
  • Loading branch information
skyerus authored Oct 4, 2022
1 parent 3c78cff commit e2fc7ee
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 10 deletions.
13 changes: 10 additions & 3 deletions pkg/sync/kubernetes/featureflagconfiguration/clientset.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,9 @@ func updateFuncHandler(oldObj interface{}, newObj interface{}, object client.Obj
if reflect.TypeOf(newObj) != reflect.TypeOf(&ffv1alpha1.FeatureFlagConfiguration{}) {
return errors.New("new object is not a FeatureFlagConfiguration")
}
if oldObj.(*ffv1alpha1.FeatureFlagConfiguration).Name == object.Name {
oldObjConfig := oldObj.(*ffv1alpha1.FeatureFlagConfiguration)
newObjConfig := newObj.(*ffv1alpha1.FeatureFlagConfiguration)
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 @@ -74,7 +76,12 @@ 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,
// WatchResources watches FeatureFlagConfigurations resources under the given namespace with the given name
//
// - resyncPeriod if non-zero, will re-list the resources this often (you will get OnUpdate
// calls, even if nothing changed). Otherwise, re-list will be delayed as long as possible
// (until the upstream source closes the watch or times out, or you stop the controller).
func WatchResources(ctx context.Context, l log.Entry, clientSet FFCInterface, resyncPeriod time.Duration,
object client.ObjectKey, c chan<- sync.INotify,
) {
ns := "*"
Expand All @@ -91,7 +98,7 @@ func WatchResources(ctx context.Context, l log.Entry, clientSet FFCInterface, re
},
},
&ffv1alpha1.FeatureFlagConfiguration{},
refreshTime,
resyncPeriod,
cache.ResourceEventHandlerFuncs{
AddFunc: func(obj interface{}) {
if err := createFuncHandler(obj, object, c); err != nil {
Expand Down
15 changes: 8 additions & 7 deletions pkg/sync/kubernetes/kubernetes_sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ import (
controllerClient "sigs.k8s.io/controller-runtime/pkg/client"
)

var refreshTime = time.Second * 5

const (
featureFlagConfigurationName = "featureflagconfiguration"
featureFlagNamespaceName = "namespace"
)

var resyncPeriod time.Duration // default of 0

type Sync struct {
Logger *log.Entry
ProviderArgs sync.ProviderArgs
Expand Down Expand Up @@ -80,12 +80,12 @@ 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 k.ProviderArgs["resyncperiod"] != "" {
hr, err := time.ParseDuration(k.ProviderArgs["resyncperiod"])
if err != nil {
k.Logger.Panic(err.Error())
}
refreshTime = hr
resyncPeriod = hr
}

k.client, err = featureflagconfiguration.NewForConfig(config)
Expand All @@ -100,7 +100,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, resyncPeriod, controllerClient.ObjectKey{
Name: k.ProviderArgs[featureFlagConfigurationName],
Namespace: k.ProviderArgs[featureFlagNamespaceName],
}, c)
}

0 comments on commit e2fc7ee

Please sign in to comment.