# Controller

The Akri Controller executes on the master Node in the cluster. It is primarily tasked with:

1. Enabling cluster access to leaf devices
2. Handling node disappearances

These tasks enable Akri to provide resources with high availability, while allowing the Kubernetes application to be agnostic about what specific Nodes or Pods are executing at any given moment.

## Enabling cluster access to resources

The first step to enable cluster access to resources (leaf devices) is, of course, finding them. The work of discovering resources and making them known to the Kubernetes cluster is handled by the [Akri Agent](/architecture/agent-in-depth.md). The Akri Agents ensure that Instances are created and updated to enforce capability sharing.

Once a capability has been discovered and Instances are created, it is up to the Akri Controller to provide cluster access.

To provide access to discovered resources, the Akri Controller works to ensure that the Pods and Services described in the relevant Configuration are running. This is accomplished by listening for changes, additions, and deletions of Instances.

When an instance is created or updated, the Akri Controller needs to do several things:

1. Ensure that the protocol broker Pod based on `Configuration.brokerPodSpec` is created
2. Ensure that the broker Service based on `Configuration.instanceServiceSpec` is created
3. Ensure that the capability Service based on `Configuration.configurationServiceSpec` is created

When an instance is deleted, the Akri Controller needs to do several things:

1. Ensure that the protocol broker Pod based on `Configuration.brokerPodSpec` is removed
2. Ensure that the protocol broker Service based on `Configuration.instanceServiceSpec` is removed
3. Ensure that the capability Service based on `Configuration.configurationServiceSpec` is removed, if there are no Pods supporting the Service (note that many instances can contribute supporting Pods to a given configuration)

## Handling node disappearances

One of the conditions we need to be aware of is node disappearance. In this case, we cannot depend on the disappeared node's Akri Agent to modify the relevant Instance. To free up any `Configuration.capacity` that a node was using prior to disappearing, the Akri Controller watches for Node disappearance events and cleans up any lingering node references in any `Instance.nodes` and `Instance.deviceUsage`.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.akri.sh/architecture/controller-in-depth.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
