A demo project to understand the project structure of KubeBuilder
Initialize the project
MKDIR Demo-Operator
cd demo-operator
kubebuilder heat --domain demo.com --repo demo.com/tutorial
This step creates the basic template file for the Go module project and introduces the necessary dependencies
If you don't use the --repo parameter, you can do it first
to mod init demo.com/tutorial
Manually initialize a go module project
Printed logs
Writing kustomize manifests for you to edit...
Writing scaffold for you to edit...
Get controller runtime:
$ go get sigs.k8s.io/[email protected]
Update dependencies:
$Go Mode TD
Next: define a resource with:
$ kubebuilder create api
You can see that the controller-runtime I'm using here is version 0.10.0.
View
[root@paas-m-k8s-master-1 demo-operator]# tree
.
├── config
│ ├── default
│ │ ├── kustomization.yaml
│ ├── Manager_of_proxy_patch.ymal
│ │ ── Manager_Config_Patch.ymal
│ ├── manager
│ ├── Controller_Manager_Config.ymal
│ │ ├── kustomization.yaml
│ │ └── Manager.Yaml
│ ├── prometheus
│ │ ├── kustomization.yaml
│ │ └── monitor.yaml
│ └── rbac
│ ├── auth_proxy_client_clusterrole.yaml
│ ├── auth_proxy_role_binding.yaml
│ ├── auth_proxy_role.yaml
│ ├── auth_proxy_service.yaml
│ ├── kustomization.yaml
│ ├── leader_election_role_binding.yaml
│ ├── leader_election_role.yaml
│ ├── role_binding.yaml
│ └── service_account.yaml
├── Dockerfile
├── go.mod
├── go.sum
├── hack
│ └── boilerplate.go.txt
├── main.go
├── Makefile
└── PROJECT
6 directories, 24 files
It's a standard go module project
Some important documents
- Makefile: A very important tool that will be used for compiling, building, deploying, and running in the future;
- PROJECT: the metadata of the kubebuilder project, which will be used when generating various APIs;
- config/default: based on the configuration file made by kustomize, it provides standard configuration for the controller, and can also be modified and adjusted as needed;
- config/manager: some detailed configuration related to the manager, such as the resource limit of the image;
- config/rbac: If you need to restrict the operator's operation permissions in Kubernetes, you need to use RBAC to configure the permissions at a granular level
main.go是程序的入口,初始化了Manager,由manager来管理api和controller
Now that we are going to sort it down, K8S receives the CRD resource description, and the CRD is controlled by the ControlLRT that we are going to write. So what is a manager
Manager is a controller used to manage controllers, and the main function of the code is to start controllers and make multiple controllers coexist
Let's continue to create the CRD file and the corresponding controller code
Create a CRD file and controller
The command to create an API is as follows: But don't rush it yet
kubebuilder create api --group tutorial --version v1 --kind Demo
This is often referred to as GVK
1) The group parameter represents the concept of a group
2) version defines the version
3) Kind defines a custom resource type
4) The above parameters make up the apiVersion and kind of yaml of the custom resource
If you do it directly, you may encounter this error
The reason is
During the execution process, operations are performed based on the Makefile.
One of the steps is to use controller-gen to generate code
image-20240129164517895
The path to find controller-gen here is $(shell pwd)/bin/controller-gen
It's not the controller-gen path I installed earlier, so I need to modify the following.
Otherwise, I won't find it, so I'll download it, but I don't have a bin directory in my current working directory. will fail
Modification
CONTROLLER_GEN = controller-gene
Because of my previous preparations, I have already put it into the global executable.
In the same way, change kustomize as well, and the make will be used later
image-20240129164933847KUSTOMIZE = kustomize
Now execute the creation command
image-20240129165802014
After the command is successful, check the following directories
image-20240129165924556
As you can see, kubebuilder automatically creates several key directories and files for us
- api/v1 : Corresponds to version v1 specified at the time of creation. The name demo corresponds to the Kind we specified when we created it
- config/crd
- controllers
We look at several important documents
-
API RESOURCE DECLARATION IS ADDED TO THE PROJECT FILE
domain: demo.com
layout:
- go.kubebuilder.io/v3
projectName: demo-operator
repo: demo.com/tutorial
resources:
- api:
crdVersion: v1
namespaced: true
controller: true
domain: demo.com
group: tutorial
kind: Demo
path: demo.com/tutorial/api/v1
version: v1
version: "3"
- The API directory has been added to contain the type definitions of CRDs
- The CRD directory has been added, which is a description file for CRD
- The corresponding role file has been added to the RBAC directory
- The controller directory is added, which contains the controller file
-
Another change point that is not easy to spot is the main.go file
if err = (&controllers. DemoReconciler{
Client: mgr. GetClient(),
Scheme: mgr. GetScheme(),
})। Setupwithmanez (MGR); of != blue {
setupLog.Error(err, "unable to create controller", "controller", "Demo")
os. Exit(1)
}
Registered our controller
Some unimportant documents
You can see that there are two more files under the API
- groupversion_info.go
- zz_generated.deepcopy.go
Neither of these files needs to be modified. groupversion_info is some group and version information, which zz_generated.deepcopy.go is automatically generated.