<p>In Kubernetes and Operator-sdk, we can define CRD (Custom Resource Definition) and CR (Custom Resource). In my operator controller, when a CR is initialized, then the controller reconcillation create a new Deployment and service.</p>
<p>When we delete a CR object, then the associated resources (such as Deployment or service) will be deleted as well at the same time. I understand it should be done by CR Finalizer. But, in Operator-SDK and my controller code, I never see any code to register or add Finalizer for CR, is there any default behavior for Operator-Sdk?</p>
<p>Could anybody point how it work for the case - &quot;while deleting CR, the associated Deployment and Service have deleted as well&quot;? Which part in controller is responsible for that?</p>

<p>Deletion of associated resources is not part of a controller. It's done by Kubernetes's <a href="https://kubernetes.io/docs/concepts/workloads/controllers/garbage-collection/" rel="nofollow noreferrer">garbage collector</a>.</p>
<p>Basically, garbage collector using <a href="https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.19/#ownerreference-v1-meta" rel="nofollow noreferrer">OwnerReference</a> objects to find orphaned resources and delete them. Most likely, you set <code>OwnerReference</code> by calling <a href="https://github.com/kubernetes-sigs/controller-runtime/blob/74fd294a89c65c8efc17ab92e0d2014d36e357a8/pkg/controller/controllerutil/controllerutil.go#L59" rel="nofollow noreferrer"><code>controllerutil.SetControllerReference</code></a> method somewhere in your code.</p>
