I'm just getting started with kubebuilder and Golang to extend our Kubernetes-cluster with a custom resource. I would love to do different things in the reconciler-function based on the event, that actually called it.
Was the resource created? Was it updated? Was it deleted?
Each of those events triggers the controller, however, I can't seem to find a possibility to see, which of those events actually happened. I can work around this issue by writing a reconciler like this:
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")
}
}
However, this feels oddly implicit and kinda hacky.
Is there a clean (possibly native) way of getting the event-type of the reconciler-call?
You won't be able to to that because this system was designed as level-based and it's not being triggered by individual events changes but rather by the actual cluster state that is being fetch from the apiserver.
Looking at
reconcile.go
you will notice in line #84 has this comment about it:And in line #44: