<p>I'm creating a custom resource definition (CRD) with an associated controller using <a href="https://github.com/kubernetes-sigs/kubebuilder" rel="nofollow noreferrer">kubebuilder</a>. My controller reconcile loop creates a deployment sub-resource and parents it to the custom resource using <code> controllerutil.SetControllerReference(&amp;myResource, deployment, r.Scheme)</code>. I've also configured my reconciler so &quot;own&quot; the sub-resource, as follows:</p>
<pre class="lang-golang prettyprint-override"><code>// SetupWithManager sets up the controller with the Manager.
func (r *MyResourceReconciler) SetupWithManager(mgr ctrl.Manager) error {
    return ctrl.NewControllerManagedBy(mgr).
        For(&amp;mygroupv1alpha1.MyResource{}).
        Owns(&amp;appsv1.Deployment{}).
        Complete(r)
}
</code></pre>
<p>However, when I run my controller locally using <code>make run</code>, I noticed that deleting the my CR (the root object) doesn't cause the Deployment sub-resource to get garbage collected. I also noticed that deleting the Deployment sub-resource doesn't trigger my reconciler to run. Why is this? Is there something I'm not doing or is this possibly a limitation of local development/testing?</p>

<p>Using @coderanger's hint, I could see that the <code>metadata.ownerReferences</code> weren't being set correctly when running the following command:</p>
<pre class="lang-sh prettyprint-override"><code>kubectl get deployments sample-deployment -o yaml
</code></pre>
<p>The problem was my controller's reconcile code. I was calling <code>controllerutil.SetControllerReference(&amp;myResource, deployment, r.Scheme)</code> only after I'd already created and persisted the Deployment.</p>
<p><strong>Buggy code</strong></p>
<pre class="lang-golang prettyprint-override"><code>log.Info(&quot;Creating a deployment&quot;)

deployment := &amp;appsv1.Deployment{
    ObjectMeta: metav1.ObjectMeta{
        Name:      deploymentName,
        Namespace: myResource.Namespace,
    },
    Spec: deploymentSpec,
}

if err = r.Create(ctx, deployment); err != nil {
    log.Error(err, &quot;Failed to create deployment&quot;)
    if errors.IsInvalid(err) {
        // Don't retry on validation errors
        err = nil
    }
    return ctrl.Result{}, err
}

// Establish the parent-child relationship between my resource and the deployment
log.Info(&quot;Making my resource a parent of the deployment&quot;)
if err = controllerutil.SetControllerReference(&amp;myResource, deployment, r.Scheme); err != nil {
    log.Error(err, &quot;Failed to set deployment controller reference&quot;)
    return ctrl.Result{}, err
}

</code></pre>
<p>To fix it, I needed to swap the order of the call to <code>r.Create</code> and <code>controllerutil.SetControllerReference</code>:</p>
<p><strong>Working code</strong></p>
<pre class="lang-golang prettyprint-override"><code>log.Info(&quot;Creating a deployment&quot;)

deployment := &amp;appsv1.Deployment{
    ObjectMeta: metav1.ObjectMeta{
        Name:      deploymentName,
        Namespace: myResource.Namespace,
    },
    Spec: deploymentSpec,
}

// Establish the parent-child relationship between my resource and the deployment
log.Info(&quot;Making my resource a parent of the deployment&quot;)
if err = controllerutil.SetControllerReference(&amp;myResource, deployment, r.Scheme); err != nil {
    log.Error(err, &quot;Failed to set deployment controller reference&quot;)
    return ctrl.Result{}, err
}

// Create the deployment with the parent/child relationship configured
if err = r.Create(ctx, deployment); err != nil {
    log.Error(err, &quot;Failed to create deployment&quot;)
    if errors.IsInvalid(err) {
        // Don't retry on validation errors
        err = nil
    }
    return ctrl.Result{}, err
}
</code></pre>
<p>I was able to confirm that this worked by looking at the <code>metadata.ownerReferences</code> YAML data for my created deployment (using the command referenced above).</p>
<pre class="lang-yaml prettyprint-override"><code>apiVersion: apps/v1
kind: Deployment
metadata:
  annotations:
    deployment.kubernetes.io/revision: &quot;1&quot;
  creationTimestamp: &quot;2021-08-02T16:22:04Z&quot;
  generation: 1
  name: sample-deployment
  namespace: default
  ownerReferences:
  - apiVersion: resources.mydomain.io/v1alpha1
    blockOwnerDeletion: true
    controller: true
    kind: MyResource
    name: myresource-sample
    uid: 6ebb146c-afc7-4601-bd75-58efc29beac9
  resourceVersion: &quot;569913&quot;
  uid: d9a4496f-7418-4831-ab87-4804dcd1f8aa
</code></pre>
