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.
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:
The following code snippets are based on the Kubernetes v1.24 version.
2. Main Function
The main function of kube-controller-manager is very clean.
NewControllerManagerCommand creates a *cobra.Command object with default parameters.
Run runs the kube-controller-manager.
The core functions are CreateControllerContext, StartControllers and NewControllerInitializers.
CreateControllerContext creates a context struct containing references to resources needed by the controllers such as the cloud provider and clientBuilder.
NewControllerInitializers is a public map of named controller groups paired to their InitFunc. This allows for structured downstream composition and subdivision.
StartControllers starts a set of controllers with a specified ControllerContext.
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
- 1 startJobController** startJobController creates a new JobController and runs the goroutine responsible for watching and syncing Jobs.
startDeploymentController creates a new DeploymentController and runs the goroutine responsible for watching and syncing Deployments.
startReplicaSetController configures a new ReplicaSetController and begins watching and syncing ReplicaSets.
The following diagram shows an overview flow of** kube-controller-manager**: