Kubernetes
ownerReference
controller field
Kubernetes API
resource management

When exactly do I set an ownerReference's controller field to true?

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

When working with Kubernetes and developing controllers, understanding the use of the `ownerReference` field is critical for managing garbage collection and ensuring proper resource clean-up. One key part of the `ownerReference` is the `controller` field, which determines the behavior of Kubernetes' garbage collector with respect to the controlled resources. This article delves into the intricacies of setting the `controller` field to true.

Understanding Owner References in Kubernetes

In Kubernetes, the `ownerReferences` field allows you to define relationships between objects—specifically, how certain objects depend on others. This dependency helps Kubernetes garbage collector to automatically delete dependent resources when their owners are deleted.

The `ownerReferences` field is an array of references, and each reference specifies:

  • `apiVersion`: The API version of the owner.
  • `kind`: The kind of the owner.
  • `name`: The name of the owner.
  • `uid`: The UID of the owner.
  • `controller`: A boolean field indicating if the owner is a controller.

The `controller` Field

When you set the `controller` field to true, it denotes that the resource is managed by a controller. This signifies that the owner actively manages the lifecycle of the dependent resource, creating a tight coupling for garbage collection purposes.

Examples and Use Cases

Let's go through some examples and use cases to understand this concept better.

Example 1: Deployment and ReplicaSet

Consider a Deployment managing a ReplicaSet. In this case, the ReplicaSet has `ownerReferences` set to point back to the Deployment, and the `controller` field is set to `true`. This indicates that:

  • The Deployment is responsible for managing the lifecycle of the ReplicaSet.
  • If you delete the Deployment, Kubernetes' garbage collector will identify the ReplicaSet as a managed resource and delete it automatically.
    • apiVersion: apps/v1
  • The StatefulSet controls the Pod lifecycle.
  • Pods are deleted when the StatefulSet is deleted, without requiring manual intervention.

Related reading
Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

All Rights Reserved.