controller-runtime: Unable to get an object of a custom resource using the default client outside Reconcile() method
I am trying to get an object of a custom resource that I defined outside of Reconcile() using the default client provided by the manager. I notice that even the object exists in the cluster, the client will return an error saying the object is not found. However, the client works as expected (i.e., can find the object) when I call it in the Reconcile() method. I am wondering what might be the cause for that?
Here is a code example:
type MyReconciler struct {
client.Client
Manager ctrl.Manager
}
func (r *MyReconciler) Reconcile(req ctrl.Request) (ctrl.Result, error) {
...
nnm := types.NamespacedName{Namespace: "myNamespace", Name: "myName"
inst := &api.MyCustomResource{}
// r.Get will get the object if it exists in the cluster.
if err := r.Get(ctx, nnm, inst); err != nil {
..
}
...
}
If I call r.Get(ctx, nnm, inst)
outside Reconcile (e.g., in or after SetupWithManager()
), it cannot get the object even it exists.
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Comments: 19 (11 by maintainers)
It would be odd for you to get an
errors.IsNotFound()
when you are doing anr.client.Create(...)
. I was suggesting you could handle the error fromCreate
and check forerrors.IsAlreadyExists()
and move on if that is the case.I remember once trying to create a singleton CR in the case that it doesn’t exist by doing something similar. In the end we ended up doing something like:
The big issue that I had with forcing the reconcile by throwing something on the queue was that the semantics for handling when the primary resource was not found was strange.