diff --git a/controllers/ocsinitialization/ocsinitialization_controller.go b/controllers/ocsinitialization/ocsinitialization_controller.go index f9b9637a5e..cc2b9f8b33 100644 --- a/controllers/ocsinitialization/ocsinitialization_controller.go +++ b/controllers/ocsinitialization/ocsinitialization_controller.go @@ -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, ) diff --git a/controllers/storagecluster/storagecluster_controller.go b/controllers/storagecluster/storagecluster_controller.go index b63e00c092..788af03c44 100644 --- a/controllers/storagecluster/storagecluster_controller.go +++ b/controllers/storagecluster/storagecluster_controller.go @@ -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, ). diff --git a/controllers/util/predicates.go b/controllers/util/predicates.go index aaf5c16662..169ec4adf6 100644 --- a/controllers/util/predicates.go +++ b/controllers/util/predicates.go @@ -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" @@ -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 }, } } diff --git a/metrics/vendor/github.com/red-hat-storage/ocs-operator/v4/controllers/util/predicates.go b/metrics/vendor/github.com/red-hat-storage/ocs-operator/v4/controllers/util/predicates.go index aaf5c16662..169ec4adf6 100644 --- a/metrics/vendor/github.com/red-hat-storage/ocs-operator/v4/controllers/util/predicates.go +++ b/metrics/vendor/github.com/red-hat-storage/ocs-operator/v4/controllers/util/predicates.go @@ -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" @@ -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 }, } }