controller-runtime: Builder.Watches + EnqueueRequestsFromMapFunc = invalid kind

I’m working on migrating our operator to Operator SDK v0.19.4, and along with that comes a controller-runtime bump to v0.6.1. We use a custom event handler, which I’ve used EnqueueRequestsFromMapFunc to register via Watches. Here’s the basic structure:

func (r *TestReconciler) SetupWithManager(mgr ctrl.Manager) error {
    return ctrl.NewControllerManagedBy(mgr).
        Watches(
            &source.Kind{Type: &corev1.Pod{}},
            &handler.EnqueueRequestsFromMapFunc{
                ToRequests: handler.ToRequestsFunc(func(a handler.MapObject) []reconcile.Request {
                    return []reconcile.Request{
                        {
                            NamespacedName: types.NamespacedName{
                                Name:      a.Meta.GetName(),
                                Namespace: a.Meta.GetNamespace(),
                            },
                        },
                    }
                }),
            }).
        Complete(r)
}

However, somewhere along the way of the controller being created and this registered, I get back a very generic error:

expected pointer, but got invalid kind

If I switch out this custom Watches for the standard For and Owns combination, the controller is created and registered successfully. So, I seem to have narrowed the error down to the usage of EnqueueRequestsFromMapFunc.

Looking at this example from the documentation, I’m using the correct syntax/structure as far as I can tell.

I’m not sure where to go from here, so hoping someone here will be able to help out. Thanks!

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Comments: 16 (7 by maintainers)

Most upvoted comments

Right, you do For(SecondaryCRD) to set the root type and then use a watch map function on Nodes, with the function mapping them back to the responsible SecondaryCRD so it can be reconciled.

It looks like you need to use For. Otherwise you’re passing a nil pointer for the primary source.Kind{}

It looks like https://github.com/kubernetes-sigs/controller-runtime/pull/1182 added explicit checks for this, and that PR was released in v0.7.0.