Skip to content

Commit

Permalink
Fix labels' filtering when reconciling (updating)
Browse files Browse the repository at this point in the history
  • Loading branch information
nmaupu committed Jul 6, 2020
1 parent 21e79bd commit cd28f59
Showing 1 changed file with 42 additions and 11 deletions.
53 changes: 42 additions & 11 deletions pkg/controller/vaultsecret/vaultsecret_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,21 +64,52 @@ func add(mgr manager.Manager, r reconcile.Reconciler) error {
return err
}

// Create a predicate to filter incoming events on configured labels filter
pred := predicate.Funcs{
CreateFunc: func(e event.CreateEvent) bool {
objectLabels := e.Meta.GetLabels()
for lfk, lfv := range LabelsFilter {
if val, ok := objectLabels[lfk]; ok {
if val != lfv {
return false
}
} else {
// Generic function for create, update and delete events
// which verifies labels' filtering
predFunc := func(e interface{}) bool {
var objectLabels map[string]string

// Trying to determine what sort of event it is
// https://tour.golang.org/methods/16
switch e.(type) {
case event.CreateEvent:
objectLabels = e.(event.CreateEvent).Meta.GetLabels()
case event.UpdateEvent:
objectLabels = e.(event.UpdateEvent).MetaNew.GetLabels()
case event.DeleteEvent:
objectLabels = e.(event.DeleteEvent).Meta.GetLabels()
case event.GenericEvent:
objectLabels = e.(event.GenericEvent).Meta.GetLabels()
default: // should never happen except if a new Event type is created
return false
}

// Verifying that each labels configured are present in the target object
for lfk, lfv := range LabelsFilter {
if val, ok := objectLabels[lfk]; ok {
if val != lfv {
return false
}
} else {
return false
}
}

return true
return true
}
// Create a predicate to filter incoming events on configured labels filter
pred := predicate.Funcs{
CreateFunc: func(e event.CreateEvent) bool {
return predFunc(e)
},
UpdateFunc: func(e event.UpdateEvent) bool {
return predFunc(e)
},
DeleteFunc: func(e event.DeleteEvent) bool {
return predFunc(e)
},
GenericFunc: func(e event.GenericEvent) bool {
return predFunc(e)
},
}
// Watch for changes to primary resource VaultSecret
Expand Down

0 comments on commit cd28f59

Please sign in to comment.