> ## Documentation Index
> Fetch the complete documentation index at: https://notes.kodekloud.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Custom Controllers 2025 Updates

> This article explores building custom controllers in Kubernetes to monitor resources and automate actions like flight bookings.

In this lesson, we explore the process of building custom controllers in Kubernetes. Custom controllers continuously monitor cluster resources and take actions when changes occur. Previously, you created a custom resource definition (CRD) to generate flight ticket objects with data stored in etcd. Now, we'll look at how to monitor these objects and trigger actions—such as calling flight booking APIs to book, edit, or cancel tickets—using a custom controller.

A controller is any process or piece of code that watches the Kubernetes cluster for events indicating changes in specific resources (in this case, flight ticket objects). For example, consider the YAML definition for a flight ticket resource:

```yaml theme={null}
apiVersion: flights.com/v1
kind: FlightTicket
metadata:
  name: my-flight-ticket
spec:
  from: Mumbai
  to: London
  number: 2
```

After applying this configuration, you can create a flight ticket by running:

```bash theme={null}
kubectl create -f flightticket.yml
# Output:
# flightticket "my-flight-ticket" created
```

Then, verify its status with:

```bash theme={null}
kubectl get flightticket
# Output:
# NAME                STATUS
# my-flight-ticket    Pending
```

<Callout icon="lightbulb" color="#1CB2FE">
  Custom controllers continuously monitor the state of objects in your cluster and ensure that their actual state aligns with the desired state. This process is essential for automating tasks like booking or canceling flights based on resource changes.
</Callout>

## Controller Implementation in Go

While it is possible to implement a controller in Python by querying the Kubernetes API, building controllers in Go using the Kubernetes Go client is a more robust option. The Go client provides built-in shared informers that simplify caching and queuing mechanisms. This makes the controller more efficient and easier to manage.

To get started with building a custom controller, follow these steps:

1. **Clone the Sample Controller Repository**

   Clone the GitHub repository [sample-controller](https://github.com/kubernetes/sample-controller):

   ```bash theme={null}
   git clone https://github.com/kubernetes/sample-controller.git
   ```

2. **Navigate to the Repository Directory**

   Change into the repository directory:

   ```bash theme={null}
   cd sample-controller
   ```

3. **Customize the Controller Code**

   Modify the controller code in the `controller.go` file to incorporate your specific business logic. For example, you might adjust code similar to the snippet below:

   ```go theme={null}
   package flightticket

   var controllerKind = apps.SchemeGroupVersion.WithKind("Flightticket")

   // Run begins watching and syncing.
   func (dc *FlightTicketController) Run(workers int, stopCh <-chan struct{}) {}

   // Call BookFlightAPIReplicaSet
   func (dc *FlightTicketController) callBookFlightAPI(obj interface{}) {}
   ```

4. **Build the Controller**

   Build the controller using the Go build command:

   ```bash theme={null}
   go build -o sample-controller .
   ```

   During the build process, Go will download necessary dependencies such as:

   ```bash theme={null}
   go: downloading k8s.io/client-go v0.0.0-20211001003700-dbfa30b9d908
   go: downloading golang.org/x/text v0.3.6
   ```

5. **Run the Controller**

   Run the controller by specifying the kubeconfig file for authentication with the Kubernetes API:

   ```bash theme={null}
   ./sample-controller --kubeconfig=$HOME/.kube/config
   ```

   Once launched, the controller initializes, sets up event handlers, and begins watching for flight ticket creation events. These events trigger the corresponding API calls to the flight booking system.

<Callout icon="triangle-alert" color="#FF6B6B">
  Before deploying to production, ensure your custom controller is thoroughly tested. After testing, consider packaging the controller into a Docker image and deploying it within your Kubernetes cluster as a pod or deployment for easy management and scalability.
</Callout>

## Next Steps

This high-level overview demonstrates the process of creating a custom controller, highlighting how custom resource definitions integrate with controllers. Although exam content might not delve deeply into custom controller implementation due to the advanced coding skills required, understanding this workflow is beneficial for building resilient Kubernetes systems.

In the next lesson, we will discuss operators and explore their relationship with custom controllers.

***

For further details on Kubernetes controllers and operators, refer to the [Kubernetes Documentation](https://kubernetes.io/docs/).

<CardGroup>
  <Card title="Watch Video" icon="video" cta="Learn more" href="https://learn.kodekloud.com/user/courses/cka-certification-course-certified-kubernetes-administrator/module/77826599-d456-4cb5-8cbc-b713cc077b45/lesson/61045090-37ea-444c-9168-0fe993d8ffee" />
</CardGroup>
