Skip to content

Commit

Permalink
Cleanup predicate calling code per @barkbay suggestion.
Browse files Browse the repository at this point in the history
Fix test
  • Loading branch information
naemono committed Dec 1, 2021
1 parent bdf2cb0 commit 777a727
Show file tree
Hide file tree
Showing 26 changed files with 48 additions and 49 deletions.
5 changes: 4 additions & 1 deletion cmd/manager/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -462,6 +462,9 @@ func startOperator(ctx context.Context) error {

// configure the manager cache based on the number of managed namespaces
managedNamespaces := viper.GetStringSlice(operator.NamespacesFlag)
// initialize the managed namespace predicate to ignore events outside of the namespaces the operator is concerned with
predicates.ManagedNamespacePredicate = predicates.NewManagedNamespacesPredicate(managedNamespaces)

switch {
case len(managedNamespaces) == 0:
log.Info("Operator configured to manage all namespaces")
Expand Down Expand Up @@ -789,7 +792,7 @@ func setupWebhook(mgr manager.Manager, params operator.Parameters, clientset kub
os.Exit(1)
}

if err := webhook.Add(mgr, webhookParams, clientset, wh, predicates.ManagedNamespacesPredicate(params.ManagedNamespaces)); err != nil {
if err := webhook.Add(mgr, webhookParams, clientset, wh, predicates.ManagedNamespacePredicate); err != nil {
log.Error(err, "unable to create controller", "controller", webhook.ControllerName)
os.Exit(1)
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/controller/agent/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func Add(mgr manager.Manager, params operator.Parameters) error {
if err != nil {
return err
}
return addWatches(c, r, predicates.ManagedNamespacesPredicate(params.ManagedNamespaces))
return addWatches(c, r, predicates.ManagedNamespacePredicate)
}

// newReconciler returns a new reconcile.Reconciler.
Expand Down
2 changes: 1 addition & 1 deletion pkg/controller/apmserver/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func Add(mgr manager.Manager, params operator.Parameters) error {
if err != nil {
return err
}
return addWatches(c, reconciler, predicates.ManagedNamespacesPredicate(params.ManagedNamespaces))
return addWatches(c, reconciler, predicates.ManagedNamespacePredicate)
}

// newReconciler returns a new reconcile.Reconciler
Expand Down
2 changes: 1 addition & 1 deletion pkg/controller/association/controller/agent_fleetserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func AddAgentFleetServer(mgr manager.Manager, accessReviewer rbac.AccessReviewer
AssociationConfAnnotationNameBase: commonv1.FleetServerConfigAnnotationNameBase,
AssociationResourceNameLabelName: agent.NameLabelName,
AssociationResourceNamespaceLabelName: agent.NamespaceLabelName,
Predicates: predicates.WithPredicates(predicates.ManagedNamespacesPredicate(params.ManagedNamespaces)),
Predicates: predicates.WithPredicates(predicates.ManagedNamespacePredicate),

ElasticsearchUserCreation: nil,
})
Expand Down
2 changes: 1 addition & 1 deletion pkg/controller/association/controller/agent_kibana.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func AddAgentKibana(mgr manager.Manager, accessReviewer rbac.AccessReviewer, par
AssociationConfAnnotationNameBase: commonv1.KibanaConfigAnnotationNameBase,
AssociationResourceNameLabelName: kibana.KibanaNameLabelName,
AssociationResourceNamespaceLabelName: kibana.KibanaNamespaceLabelName,
Predicates: predicates.WithPredicates(predicates.ManagedNamespacesPredicate(params.ManagedNamespaces)),
Predicates: predicates.WithPredicates(predicates.ManagedNamespacePredicate),

ElasticsearchUserCreation: &association.ElasticsearchUserCreation{
ElasticsearchRef: getElasticsearchFromKibana,
Expand Down
2 changes: 1 addition & 1 deletion pkg/controller/association/controller/apm_es.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func AddApmES(mgr manager.Manager, accessReviewer rbac.AccessReviewer, params op
AssociationConfAnnotationNameBase: commonv1.ElasticsearchConfigAnnotationNameBase,
AssociationResourceNameLabelName: eslabel.ClusterNameLabelName,
AssociationResourceNamespaceLabelName: eslabel.ClusterNamespaceLabelName,
Predicates: predicates.WithPredicates(predicates.ManagedNamespacesPredicate(params.ManagedNamespaces)),
Predicates: predicates.WithPredicates(predicates.ManagedNamespacePredicate),

ElasticsearchUserCreation: &association.ElasticsearchUserCreation{
ElasticsearchRef: func(c k8s.Client, association commonv1.Association) (bool, commonv1.ObjectSelector, error) {
Expand Down
2 changes: 1 addition & 1 deletion pkg/controller/association/controller/apm_kibana.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func AddApmKibana(mgr manager.Manager, accessReviewer rbac.AccessReviewer, param
AssociationConfAnnotationNameBase: commonv1.KibanaConfigAnnotationNameBase,
AssociationResourceNameLabelName: kibana.KibanaNameLabelName,
AssociationResourceNamespaceLabelName: kibana.KibanaNamespaceLabelName,
Predicates: predicates.WithPredicates(predicates.ManagedNamespacesPredicate(params.ManagedNamespaces)),
Predicates: predicates.WithPredicates(predicates.ManagedNamespacePredicate),

ElasticsearchUserCreation: &association.ElasticsearchUserCreation{
ElasticsearchRef: getElasticsearchFromKibana,
Expand Down
2 changes: 1 addition & 1 deletion pkg/controller/association/controller/beat_es.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func AddBeatES(mgr manager.Manager, accessReviewer rbac.AccessReviewer, params o
AssociationConfAnnotationNameBase: commonv1.ElasticsearchConfigAnnotationNameBase,
AssociationResourceNameLabelName: eslabel.ClusterNameLabelName,
AssociationResourceNamespaceLabelName: eslabel.ClusterNamespaceLabelName,
Predicates: predicates.WithPredicates(predicates.ManagedNamespacesPredicate(params.ManagedNamespaces)),
Predicates: predicates.WithPredicates(predicates.ManagedNamespacePredicate),

ElasticsearchUserCreation: &association.ElasticsearchUserCreation{
ElasticsearchRef: func(c k8s.Client, association commonv1.Association) (bool, commonv1.ObjectSelector, error) {
Expand Down
2 changes: 1 addition & 1 deletion pkg/controller/association/controller/beat_kibana.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func AddBeatKibana(mgr manager.Manager, accessReviewer rbac.AccessReviewer, para
AssociationConfAnnotationNameBase: commonv1.KibanaConfigAnnotationNameBase,
AssociationResourceNameLabelName: kibana.KibanaNameLabelName,
AssociationResourceNamespaceLabelName: kibana.KibanaNamespaceLabelName,
Predicates: predicates.WithPredicates(predicates.ManagedNamespacesPredicate(params.ManagedNamespaces)),
Predicates: predicates.WithPredicates(predicates.ManagedNamespacePredicate),

ElasticsearchUserCreation: &association.ElasticsearchUserCreation{
ElasticsearchRef: getElasticsearchFromKibana,
Expand Down
2 changes: 1 addition & 1 deletion pkg/controller/association/controller/ent_es.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func AddEntES(mgr manager.Manager, accessReviewer rbac.AccessReviewer, params op
AssociationConfAnnotationNameBase: commonv1.ElasticsearchConfigAnnotationNameBase,
AssociationResourceNameLabelName: eslabel.ClusterNameLabelName,
AssociationResourceNamespaceLabelName: eslabel.ClusterNamespaceLabelName,
Predicates: predicates.WithPredicates(predicates.ManagedNamespacesPredicate(params.ManagedNamespaces)),
Predicates: predicates.WithPredicates(predicates.ManagedNamespacePredicate),

ElasticsearchUserCreation: &association.ElasticsearchUserCreation{
ElasticsearchRef: func(c k8s.Client, association commonv1.Association) (bool, commonv1.ObjectSelector, error) {
Expand Down
2 changes: 1 addition & 1 deletion pkg/controller/association/controller/es_monitoring.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func esMonitoringAssociationInfo(params operator.Parameters) association.Associa
AssociationConfAnnotationNameBase: commonv1.ElasticsearchConfigAnnotationNameBase,
AssociationResourceNameLabelName: eslabel.ClusterNameLabelName,
AssociationResourceNamespaceLabelName: eslabel.ClusterNamespaceLabelName,
Predicates: predicates.WithPredicates(predicates.ManagedNamespacesPredicate(params.ManagedNamespaces)),
Predicates: predicates.WithPredicates(predicates.ManagedNamespacePredicate),

ElasticsearchUserCreation: &association.ElasticsearchUserCreation{
ElasticsearchRef: func(c k8s.Client, association commonv1.Association) (bool, commonv1.ObjectSelector, error) {
Expand Down
2 changes: 1 addition & 1 deletion pkg/controller/association/controller/kb_monitoring.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func AddKbMonitoring(mgr manager.Manager, accessReviewer rbac.AccessReviewer, pa
AssociationConfAnnotationNameBase: commonv1.ElasticsearchConfigAnnotationNameBase,
AssociationResourceNameLabelName: eslabel.ClusterNameLabelName,
AssociationResourceNamespaceLabelName: eslabel.ClusterNamespaceLabelName,
Predicates: predicates.WithPredicates(predicates.ManagedNamespacesPredicate(params.ManagedNamespaces)),
Predicates: predicates.WithPredicates(predicates.ManagedNamespacePredicate),

ElasticsearchUserCreation: &association.ElasticsearchUserCreation{
ElasticsearchRef: func(c k8s.Client, association commonv1.Association) (bool, commonv1.ObjectSelector, error) {
Expand Down
2 changes: 1 addition & 1 deletion pkg/controller/association/controller/kibana_ent.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func AddKibanaEnt(mgr manager.Manager, accessReviewer rbac.AccessReviewer, param
AssociationConfAnnotationNameBase: commonv1.EntConfigAnnotationNameBase,
AssociationResourceNameLabelName: entctl.EnterpriseSearchNameLabelName,
AssociationResourceNamespaceLabelName: entctl.EnterpriseSearchNamespaceLabelName,
Predicates: predicates.WithPredicates(predicates.ManagedNamespacesPredicate(params.ManagedNamespaces)),
Predicates: predicates.WithPredicates(predicates.ManagedNamespacePredicate),
ElasticsearchUserCreation: nil, // no dedicated ES user required for Kibana->Ent connection
})
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/controller/association/controller/kibana_es.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func AddKibanaES(mgr manager.Manager, accessReviewer rbac.AccessReviewer, params
AssociationConfAnnotationNameBase: commonv1.ElasticsearchConfigAnnotationNameBase,
AssociationResourceNameLabelName: eslabel.ClusterNameLabelName,
AssociationResourceNamespaceLabelName: eslabel.ClusterNamespaceLabelName,
Predicates: predicates.WithPredicates(predicates.ManagedNamespacesPredicate(params.ManagedNamespaces)),
Predicates: predicates.WithPredicates(predicates.ManagedNamespacePredicate),

ElasticsearchUserCreation: &association.ElasticsearchUserCreation{
ElasticsearchRef: func(c k8s.Client, association commonv1.Association) (bool, commonv1.ObjectSelector, error) {
Expand Down
2 changes: 1 addition & 1 deletion pkg/controller/association/controller/maps_es.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func AddMapsES(mgr manager.Manager, accessReviewer rbac.AccessReviewer, params o
AssociationConfAnnotationNameBase: commonv1.ElasticsearchConfigAnnotationNameBase,
AssociationResourceNameLabelName: eslabel.ClusterNameLabelName,
AssociationResourceNamespaceLabelName: eslabel.ClusterNamespaceLabelName,
Predicates: predicates.WithPredicates(predicates.ManagedNamespacesPredicate(params.ManagedNamespaces)),
Predicates: predicates.WithPredicates(predicates.ManagedNamespacePredicate),

ElasticsearchUserCreation: &association.ElasticsearchUserCreation{
ElasticsearchRef: func(c k8s.Client, association commonv1.Association) (bool, commonv1.ObjectSelector, error) {
Expand Down
2 changes: 1 addition & 1 deletion pkg/controller/autoscaling/elasticsearch.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,5 @@ func Add(mgr manager.Manager, p operator.Parameters) error {
return err
}
// Watch for changes on Elasticsearch clusters.
return c.Watch(&source.Kind{Type: &esv1.Elasticsearch{}}, &handler.EnqueueRequestForObject{}, predicates.ManagedNamespacesPredicate(p.ManagedNamespaces))
return c.Watch(&source.Kind{Type: &esv1.Elasticsearch{}}, &handler.EnqueueRequestForObject{}, predicates.ManagedNamespacePredicate)
}
2 changes: 1 addition & 1 deletion pkg/controller/beat/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func Add(mgr manager.Manager, params operator.Parameters) error {
if err != nil {
return err
}
return addWatches(c, r, predicates.ManagedNamespacesPredicate(params.ManagedNamespaces))
return addWatches(c, r, predicates.ManagedNamespacePredicate)
}

// newReconciler returns a new reconcile.Reconciler.
Expand Down
2 changes: 1 addition & 1 deletion pkg/controller/common/predicates/predicate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func TestManagedNamespacesPredicate(t *testing.T) {
require.NoError(t, err)

bldr := builder.ControllerManagedBy(mgr).
For(&appsv1.Deployment{}, builder.WithPredicates(predicates.ManagedNamespacesPredicate([]string{managedNamespace})))
For(&appsv1.Deployment{}, builder.WithPredicates(predicates.NewManagedNamespacesPredicate([]string{managedNamespace})))
require.NoError(t, bldr.Complete(reconciler))

ctx, cancel := context.WithCancel(context.Background())
Expand Down
44 changes: 20 additions & 24 deletions pkg/controller/common/predicates/predicates.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,48 +5,44 @@
package predicates

import (
"k8s.io/utils/strings/slices"
"sigs.k8s.io/controller-runtime/pkg/event"
"sigs.k8s.io/controller-runtime/pkg/predicate"
)

func namespaceInSlice(namespace string, namespaces []string) bool {
// If the operator is managing all namespaces,
// never ignore any namespace.
if len(namespaces) == 0 {
return true
}
for _, ns := range namespaces {
if namespace == ns {
return true
}
}
return false
}

// WithPredicates is a helper function to convert one or more predicates
// into a slice of predicates.
func WithPredicates(predicates ...predicate.Predicate) []predicate.Predicate {
return predicates
}
// ManagedNamespacePredicate is a predicate that will ignore events that exist
// outside of the namespaces that the operator manages.
var ManagedNamespacePredicate predicate.Predicate

// ManagedNamespacesPredicate will return a predicate that will ignore events
// NewManagedNamespacesPredicate will return a predicate that will ignore events
// that exist outside of the given managed namespaces,
func ManagedNamespacesPredicate(managedNamespaces []string) predicate.Predicate {
func NewManagedNamespacesPredicate(managedNamespaces []string) predicate.Predicate {
return predicate.Funcs{
CreateFunc: func(e event.CreateEvent) bool {
// Ignore resources that do not exist within the managed namespaces
return namespaceInSlice(e.Object.GetNamespace(), managedNamespaces)
return IsNamespaceManaged(e.Object.GetNamespace(), managedNamespaces)
},
UpdateFunc: func(e event.UpdateEvent) bool {
// Ignore resources that do not exist within the managed namespaces
return namespaceInSlice(e.ObjectNew.GetNamespace(), managedNamespaces)
return IsNamespaceManaged(e.ObjectNew.GetNamespace(), managedNamespaces)
},
DeleteFunc: func(e event.DeleteEvent) bool {
// Ignore resources that do not exist within the managed namespaces
return namespaceInSlice(e.Object.GetNamespace(), managedNamespaces)
return IsNamespaceManaged(e.Object.GetNamespace(), managedNamespaces)
},
GenericFunc: func(e event.GenericEvent) bool {
return namespaceInSlice(e.Object.GetNamespace(), managedNamespaces)
},
}
}

// IsNamespaceManaged returns true if the namespace is managed by the operator.
func IsNamespaceManaged(namespace string, managedNamespaces []string) bool {
return len(managedNamespaces) == 0 || slices.Contains(managedNamespaces, namespace)
}

// WithPredicates is a helper function to convert one or more predicates
// into a slice of predicates.
func WithPredicates(predicates ...predicate.Predicate) []predicate.Predicate {
return predicates
}
2 changes: 1 addition & 1 deletion pkg/controller/elasticsearch/elasticsearch_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func Add(mgr manager.Manager, params operator.Parameters) error {
if err != nil {
return err
}
return addWatches(c, reconciler, predicates.ManagedNamespacesPredicate(params.ManagedNamespaces))
return addWatches(c, reconciler, predicates.ManagedNamespacePredicate)
}

// newReconciler returns a new reconcile.Reconciler
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func Add(mgr manager.Manager, params operator.Parameters) error {
if err != nil {
return err
}
return addWatches(c, reconciler, predicates.ManagedNamespacesPredicate(params.ManagedNamespaces))
return addWatches(c, reconciler, predicates.ManagedNamespacePredicate)
}

// newReconciler returns a new reconcile.Reconciler
Expand Down
2 changes: 1 addition & 1 deletion pkg/controller/kibana/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func Add(mgr manager.Manager, params operator.Parameters) error {
if err != nil {
return err
}
return addWatches(c, reconciler, predicates.ManagedNamespacesPredicate(params.ManagedNamespaces))
return addWatches(c, reconciler, predicates.ManagedNamespacePredicate)
}

// newReconciler returns a new reconcile.Reconciler
Expand Down
2 changes: 1 addition & 1 deletion pkg/controller/license/license_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func Add(mgr manager.Manager, p operator.Parameters) error {
if err != nil {
return err
}
return addWatches(c, r.Client, predicates.ManagedNamespacesPredicate(p.ManagedNamespaces))
return addWatches(c, r.Client, predicates.ManagedNamespacePredicate)
}

// newReconciler returns a new reconcile.Reconciler
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func TestReconcile(t *testing.T) {
if err != nil {
return err
}
return addWatches(c, r.Client, p)
return addWatches(c, r.Client)

}, operator.Parameters{})
defer stop()
Expand Down
2 changes: 1 addition & 1 deletion pkg/controller/maps/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func Add(mgr manager.Manager, params operator.Parameters) error {
if err != nil {
return err
}
return addWatches(c, reconciler, predicates.ManagedNamespacesPredicate(params.ManagedNamespaces))
return addWatches(c, reconciler, predicates.ManagedNamespacePredicate)
}

// newReconciler returns a new reconcile.Reconciler
Expand Down
2 changes: 1 addition & 1 deletion pkg/controller/remoteca/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func Add(mgr manager.Manager, accessReviewer rbac.AccessReviewer, params operato
if err != nil {
return err
}
return AddWatches(c, r, predicates.ManagedNamespacesPredicate(params.ManagedNamespaces))
return AddWatches(c, r, predicates.ManagedNamespacePredicate)
}

// NewReconciler returns a new reconcile.Reconciler
Expand Down

0 comments on commit 777a727

Please sign in to comment.