Skip to content

Commit

Permalink
Merge pull request #2911 from leelavg/predicate-test
Browse files Browse the repository at this point in the history
controllers: fix dynamic crd predicate
  • Loading branch information
openshift-merge-bot[bot] authored Dec 5, 2024
2 parents 61292cc + 299f35f commit 13988d9
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,12 @@ func (r *OCSInitializationReconciler) SetupWithManager(mgr ctrl.Manager) error {
enqueueOCSInit,
builder.WithPredicates(
util.NamePredicate(ClusterClaimCrdName),
util.CrdCreateAndDeletePredicate(&r.Log, ClusterClaimCrdName, r.AvailableCrds[ClusterClaimCrdName]),
util.EventTypePredicate(
!r.AvailableCrds[ClusterClaimCrdName],
false,
true,
false,
),
),
builder.OnlyMetadata,
)
Expand Down
18 changes: 12 additions & 6 deletions controllers/storagecluster/storagecluster_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,12 +233,18 @@ func (r *StorageClusterReconciler) SetupWithManager(mgr ctrl.Manager) error {
&extv1.CustomResourceDefinition{},
enqueueStorageClusterRequest,
builder.WithPredicates(
util.NamePredicate(VirtualMachineCrdName),
util.CrdCreateAndDeletePredicate(&r.Log, VirtualMachineCrdName, r.AvailableCrds[VirtualMachineCrdName]),
),
builder.WithPredicates(
util.NamePredicate(StorageClientCrdName),
util.CrdCreateAndDeletePredicate(&r.Log, StorageClientCrdName, r.AvailableCrds[StorageClientCrdName]),
predicate.Or(
util.NamePredicate(VirtualMachineCrdName),
util.NamePredicate(StorageClientCrdName),
),
util.EventTypePredicate(
// the values in the below map are filled before controller watches are setup and these conditions
// will just be evaluated to a boolean by the time builder completes setting up watches.
!r.AvailableCrds[VirtualMachineCrdName] || !r.AvailableCrds[StorageClientCrdName],
false,
true,
false,
),
),
builder.OnlyMetadata,
).
Expand Down
26 changes: 11 additions & 15 deletions controllers/util/predicates.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package util

import (
"fmt"
"reflect"

"github.com/go-logr/logr"
"sigs.k8s.io/controller-runtime/pkg/client"

"sigs.k8s.io/controller-runtime/pkg/event"
Expand Down Expand Up @@ -81,25 +79,23 @@ func NamePredicate(name string) predicate.Predicate {
})
}

func CrdCreateAndDeletePredicate(log *logr.Logger, crdName string, crdExists bool) predicate.Predicate {
// EventTypePredicate return a predicate to filter events based on their
// respective event type. This helper allows for the selection of multiple
// types resulting in a predicate that can filter in more than a single event
// type
func EventTypePredicate(create, update, del, generic bool) predicate.Predicate {
return predicate.Funcs{
CreateFunc: func(_ event.CreateEvent) bool {
if !crdExists {
log.Info(fmt.Sprintf("CustomResourceDefinition %s was Created.", crdName))
}
return !crdExists
},
DeleteFunc: func(_ event.DeleteEvent) bool {
if crdExists {
log.Info(fmt.Sprintf("CustomResourceDefinition %s was Deleted.", crdName))
}
return crdExists
return create
},
UpdateFunc: func(_ event.UpdateEvent) bool {
return false
return update
},
DeleteFunc: func(_ event.DeleteEvent) bool {
return del
},
GenericFunc: func(_ event.GenericEvent) bool {
return false
return generic
},
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 13988d9

Please sign in to comment.