Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 26 additions & 11 deletions pkg/reconciler/v1alpha1/namespace/namespace.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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 {
Expand Down
8 changes: 5 additions & 3 deletions pkg/reconciler/v1alpha1/namespace/namespace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down Expand Up @@ -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(),
Expand Down