From 24d760392bd22e768b4fdf30edad91704ed84a2c Mon Sep 17 00:00:00 2001 From: Adam Harwayne Date: Thu, 14 Mar 2019 15:18:37 -0700 Subject: [PATCH] Only reconcile the Namespace if the specific resource we care about changes. --- .../v1alpha1/namespace/namespace.go | 37 +++++++++++++------ .../v1alpha1/namespace/namespace_test.go | 8 ++-- 2 files changed, 31 insertions(+), 14 deletions(-) diff --git a/pkg/reconciler/v1alpha1/namespace/namespace.go b/pkg/reconciler/v1alpha1/namespace/namespace.go index cf427a4895f..8e065de6b93 100644 --- a/pkg/reconciler/v1alpha1/namespace/namespace.go +++ b/pkg/reconciler/v1alpha1/namespace/namespace.go @@ -89,9 +89,19 @@ func ProvideController(mgr manager.Manager, logger *zap.Logger) (controller.Cont return nil, err } - // Watch all the resources that this reconciler reconciles. - for _, t := range []runtime.Object{&corev1.ServiceAccount{}, &rbacv1.RoleBinding{}, &v1alpha1.Broker{}} { - err = c.Watch(&source.Kind{Type: t}, &handler.EnqueueRequestsFromMapFunc{ToRequests: &namespaceMapper{}}) + // Watch all the resources that this reconciler reconciles. This is a map from resource type to + // the name of the resource of that type we care about (i.e. only if the resource of the given + // type and with the given name changes, do we reconcile the Namespace). + resources := map[runtime.Object]string{ + &corev1.ServiceAccount{}: brokerFilterSA, + &rbacv1.RoleBinding{}: brokerFilterRB, + &v1alpha1.Broker{}: defaultBroker, + } + for t, n := range resources { + nm := &namespaceMapper{ + name: n, + } + err = c.Watch(&source.Kind{Type: t}, &handler.EnqueueRequestsFromMapFunc{ToRequests: nm}) if err != nil { return nil, err } @@ -100,19 +110,24 @@ func ProvideController(mgr manager.Manager, logger *zap.Logger) (controller.Cont return c, nil } -type namespaceMapper struct{} +type namespaceMapper struct { + name string +} var _ handler.Mapper = &namespaceMapper{} -func (namespaceMapper) Map(o handler.MapObject) []reconcile.Request { - return []reconcile.Request{ - { - NamespacedName: types.NamespacedName{ - Namespace: "", - Name: o.Meta.GetNamespace(), +func (m *namespaceMapper) Map(o handler.MapObject) []reconcile.Request { + if o.Meta.GetName() == m.name { + return []reconcile.Request{ + { + NamespacedName: types.NamespacedName{ + Namespace: "", + Name: o.Meta.GetNamespace(), + }, }, - }, + } } + return []reconcile.Request{} } func (r *reconciler) InjectClient(c client.Client) error { diff --git a/pkg/reconciler/v1alpha1/namespace/namespace_test.go b/pkg/reconciler/v1alpha1/namespace/namespace_test.go index 87e522fe010..be60e4a9828 100644 --- a/pkg/reconciler/v1alpha1/namespace/namespace_test.go +++ b/pkg/reconciler/v1alpha1/namespace/namespace_test.go @@ -60,11 +60,11 @@ var ( func init() { // Add types to scheme - v1alpha1.AddToScheme(scheme.Scheme) + _ = v1alpha1.AddToScheme(scheme.Scheme) } func TestProvideController(t *testing.T) { - //TODO(grantr) This needs a mock of manager.Manager. Creating a manager + // TODO(grantr) This needs a mock of manager.Manager. Creating a manager // with a fake Config fails because the Manager tries to contact the // apiserver. @@ -100,7 +100,9 @@ func TestInjectClient(t *testing.T) { } func TestNamespaceMapper_Map(t *testing.T) { - m := &namespaceMapper{} + m := &namespaceMapper{ + name: makeBroker().Name, + } req := handler.MapObject{ Meta: makeBroker().GetObjectMeta(),