Get reason, why reconciler was called (resource creation / update / deletion?) #2179
-
Hi there! I'm working on my first project using For example, right now, I try to make sense of why a certain reconciler was called. Was it, because the resource was created, updated or deleted? I would like to do different things, when different events happen to my resources. Is there a way to find this out? If not, is there some (common) workaround? I'm thankful for any information and help I'm getting and will happily provide further examples or information for my particular use-case. If I have the following reconciler, I can work around this, however it feels too implicit and too hacky to me. func (r *ServiceDescriptorReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
service := &batchv1.ServiceDescriptor{}
if err := r.Get(context.TODO(), req.NamespacedName, service); err != nil && errors.IsNotFound(err) {
fmt.Println("Resource was not found -> must have been deleted")
else {
fmt.Println("No errors found -> Resource must have been created or updated")
}
} Of course I know, that this might be thinking in the wrong direction, given my lacking experience with |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 3 replies
-
It's generally considered bad practice to do different actions on For delete, you've got two options:
|
Beta Was this translation helpful? Give feedback.
-
Closing since it is answered. |
Beta Was this translation helpful? Give feedback.
It's generally considered bad practice to do different actions on
Create
vsUpdate
-- for example, anUpdate
event could appear as aCreate
due to a re-list. Reconcile should generally observe current state & diff with desired state to figure out what to do.For delete, you've got two options:
NotFound
when you try to get the object as you note above. This is good if you just want to log or something.