From 2627a4b96f8ab5d80d870d1b4be2abb1a453e721 Mon Sep 17 00:00:00 2001 From: Jonathan Berkhahn Date: Wed, 12 Jan 2022 16:47:42 -0800 Subject: [PATCH] add blurb about how to wait for a certain state to FAQ Signed-off-by: Jonathan Berkhahn --- website/content/en/docs/faqs/_index.md | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/website/content/en/docs/faqs/_index.md b/website/content/en/docs/faqs/_index.md index 1342c7a0364..e988099f2e6 100644 --- a/website/content/en/docs/faqs/_index.md +++ b/website/content/en/docs/faqs/_index.md @@ -36,6 +36,26 @@ For example, you should refrain from moving the scaffolded files, doing so will You should not have separate logic. Instead design your reconciler to be idempotent. See the [controller-runtime FAQ][cr-faq] for more details. +## How do I wait for some specific cluster state such as a resource being deleted? + +You don't. Instead, design your reconciler to be idempotent by taking the next step based on the current state, and then returning and requeuing. For example, waiting for an object to be deleted might look something like this: + +``` +func (r *Reconciler) Reconcile(ctx context.Context, req ctrl.Request) (res ctrl.Result, err error) { + ... + if !r.IfPodWasDeleted(ctx, pod) { + if err := r.Delete(ctx, pod); err != nil { + return ctrl.Result{}, err + } + return ctrl.Result{Requeue: true}, nil + } + // This code will be invoked only after pod deletion + r.DeployBiggerPod(ctx) + ... +} +``` + + ## When my Custom Resource is deleted, I need to know its contents or perform cleanup tasks. How can I do that? Use a [finalizer].