Kubernetes Source Code Overview: Kube-controller-manager
Tags: kubernetes, devops, news, productivity
Published: 2022-07-18 08:30:05

**Kube-controller-manager**
Kube-controller-manager is a binary that runs controllers, which are control loops shipped with Kubernetes. Examples of controllers are the Deployment Controller, Service Controller, Job Controller, Namespace Controller, ReplicaSet Controller, Endpoints Controller, and so on.

**1. Introduction**
This article provides an overview of kube-controller-manager source code in kubernetes/cmd/kube-controller-manager directory, which including parameter parsing and initialization of various types of controllers. For the code in kubernetes/pkg/controller module, a further overview will be provided later.

The code structure of the kube-controller-manager is as follows:


![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1658132417500/YfQ10bQMI.png align="left")

The following code snippets are based on the Kubernetes v1.24 version.

The source code is at https://github.com/kubernetes/kubernetes/blob/release-1.24/cmd/kube-controller-manager/controller-manager.go

**2. Main Function**
The main function of kube-controller-manager is very clean.


![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1658132437555/WO47crqZf.png align="left")

**3. NewControllerManagerCommand**
NewControllerManagerCommand creates a *cobra.Command object with default parameters.


![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1658132447819/Oi_2s68CD.png align="left")

**4. Run**
Run runs the kube-controller-manager.


![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1658132476982/z3_LJK4Xl.png align="left")

The core functions are **CreateControllerContext, StartControllers and NewControllerInitializers.**

**4.1 CreateControllerContext**
CreateControllerContext creates a context struct containing references to resources needed by the controllers such as the cloud provider and clientBuilder.


![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1658132490814/T7LBQAxkk.png align="left")

**4.2 NewControllerInitializers**
NewControllerInitializers is a public map of named controller groups paired to their InitFunc. This allows for structured downstream composition and subdivision.


![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1658132504073/PtZorVoSN.png align="left")
NewControllerInitializers

**4.3 StartControllers**
**StartControllers** starts a set of controllers with a specified **ControllerContext**.


![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1658132525607/uU2yRcus6.png align="left")
StartControllers

**4.4 InitFunc**
InitFunc is used to launch a particular controller. It returns a controller that can optionally implement other interfaces so that the controller manager can support the requested features. The bool indicates whether the controller was enabled.

type InitFunc func(ctx context.Context, controllerCtx ControllerContext) (controller controller.Interface, enabled bool, err error)

**5. Some Start Controller Functions
5. 1 startJobController**
**startJobController** creates a new JobController and runs the goroutine responsible for watching and syncing Jobs.


![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1658132566312/dprPu9bqp.png align="left")

**startJobController
5.2 startDeploymentController**
**startDeploymentController** creates a new DeploymentController and runs the goroutine responsible for watching and syncing Deployments.


![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1658132611891/aK_VvfrGb.png align="left")

**startDeploymentController
5.3 startReplicaSetController**
startReplicaSetController configures a new ReplicaSetController and begins watching and syncing ReplicaSets.


![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1658132623174/smrqIOcMu.png align="left")
startReplicaSetController

**Wrap up**
The following diagram shows an overview flow of** kube-controller-manager**:


![image.png](https://cdn.hashnode.com/res/hashnod![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/77hdvuuw51o5svx7d8h6.png)
 e/image/upload/v1658132661319/yE5gyukiR.png align="left")
overview flow