<p>I need to create a k8s resource which take some time until it will be available,
for this I use the following</p>
<p><a href="https://pkg.go.dev/sigs.k8s.io/controller-runtime/pkg/controller/controllerutil#example-CreateOrUpdate" rel="nofollow noreferrer">https://pkg.go.dev/sigs.k8s.io/controller-runtime/pkg/controller/controllerutil#example-CreateOrUpdate</a></p>
<pre><code>op, err := controllerutil.CreateOrUpdate(context.TODO(), c, deploy, func() error {

})

func2()
</code></pre>
<p>now I need to call to <code>func2</code> <strong>right after</strong> the creation of the object was done (it may take 2-3 min until finish),
How should I do it right?</p>
<p>I found this but not sure how to combine them ...</p>
<p><a href="https://pkg.go.dev/sigs.k8s.io/controller-runtime/pkg#hdr-Watching_and_EventHandling" rel="nofollow noreferrer">https://pkg.go.dev/sigs.k8s.io/controller-runtime/pkg#hdr-Watching_and_EventHandling</a></p>
<p>im using kubebuilder</p>

<p>The above approach is more for cli usage.</p>
<p>When you are using kubebuilder or the operator sdk then you need to deal with it in your reconcile function.</p>
<p>Usually you have a custom resource that triggers your controllers reconcile function. When the custom resource is being created you then create the deployment and instead of returning an empty reconcile.Result (which marks it as done) you can return the reconcile.Result with the Requeue attribute.</p>
<pre class="lang-golang prettyprint-override"><code>reconcile.Result{Requeue: true}
</code></pre>
<p>So during the next run you check if the deployment is ready. If not then you requeue again. Once it is ready you return an empty reconcile.Result struct.</p>
<p>Also keep in mind that the reconcile function always needs to be idempotent as it will be run again for every custom resource during a restart of the controller and also every 10 hours by default.</p>
<p>Alternatively you could also use an owner reference on the created deployment and then setup the controller to reconcile the owner resource (your custom resource) whenever an update happens on the owned resource (the deployment). With operator sdk this can be configured in the SetupWithManager function, which by default only uses the For option function. Here you need to add the Owns option function.</p>
<pre class="lang-golang prettyprint-override"><code>// SetupWithManager sets up the controller with the Manager.
func (r *YourReconciler) SetupWithManager(mgr ctrl.Manager) error {
    return ctrl.NewControllerManagedBy(mgr).
        For(&amp;yourapigroup.YourCustomResource{}).
        Owns(&amp;appv1.Deployment{}).
        Complete(r)
}
</code></pre>
<p>I never used that approach though therefore it might be required to add more code for this to work.</p>
<p>Using the owner reference can also come in handy if you do not require any finalizer code, because kubernetes will delete your owned resource (the deployment) automatically when the custom resource is being deleted.</p>
