4 min read

Hands-on Kubernetes Operator Development: Finalizers

Hands-on Kubernetes Operator Development: Finalizers
Photo by Sam Pak / Unsplash

In the first two blog posts on this topic we learned how to bootstrap your operator and how to implement the reconciliation loop.

In the third episode we will discuss an important aspect of Kubernetes resource lifecycle management - garbage collection. As we continue building our Tenant Operator, it is critical to ensure that resources allocated to a tenant are properly cleaned up when a tenant is deleted. This not only optimizes the resource usage but also avoids potential conflicts and issues that can arise from stale or orphaned resources.

Understanding Finalizers in Kubernetes

Finalizers are an essential part of Kubernetes' resource management system. They provide a mechanism to prevent specific resources from being deleted before we've performed necessary cleanup operations.

Kubernetes Finalizers Sequence Diagram

Each Kubernetes object's metadata includes a field called finalizers, which is an array of strings. These strings are arbitrary, but by convention, they take the form of domain/name. The absence of finalizers or an empty list in a resource means it can be deleted immediately by the Kubernetes API server. However, if the finalizers list contains one or more elements, the Kubernetes API server will not delete the resource, giving our operator the chance to perform any necessary cleanup operations. Here is example how the finalizer might look like:

This post is for subscribers only