MySQL operator 02 | scaffold selection & project creation

Time:2021-12-4

Senior Database Kernel R & D of Gao riyao

Graduated from Huazhong University of science and technology, likes to study the mainstream database architecture and source code, and has long been engaged in the research and development of distributed database kernel. Once participated in the development of cirrodata kernel of distributed MPP database (Dongfang Guoxin), and now is mainly responsible for the development of MySQL series product kernel (Qingyun Technology).

This is the second article on MySQL operator design,LastThis paper introduces the Architecture Overview and design idea of MySQL operator. This issue will introduce the selection of operator scaffold and the process of Engineering creation.

|Operator scaffold selection

When building a house on the construction site, a scaffold should be built at the beginning to facilitate faster and safer construction. Similarly, a scaffold should be built for the construction of operator project to facilitate subsequent rapid development and iteration, and kubernetes community has many mature tools for scaffold construction for us to choose from.

Scaffold construction tools

At present, the highest community activity and utilization rate isOperator SDKandKubebuilder。 They all use the official controller tools and controller runtime, with the same layout. The differences are:

Kubebuilder

  • Contains the envtest package, which allows operator developers to run simple tests using independent etcd and apiserver
  • Automatically generate makefiles to help users perform operator tasks (build, test, run, code generation, etc.)
  • Build deployment manifest using kustomize
  • Improved support for admission and CRD transformation webhooks

Operator SDK

  • Better support upper layer operations such as ansible and helm operator
  • Integration with operator Lifecycle Manager (OLM). OLM is a key component of operator framework and is very important for cluster operation on day 2, such as managing operator real-time upgrade
  • Contains the scoring clip command, which helps you understand whether the operator follows best practices
  • It includes E2E testing framework, which simplifies the process of testing the actual cluster

At present, the two communities are gradually converging, and the operator SDK is constantly moving closer to kubebuilder. Therefore, we choose the more native and legitimate kubebuilder (version 3.0 at present) as the scaffold of the operator project.

|Create project

Initializing the operator and controller APIs

Kubebuilder provides quick tutorials and simple scaffolds for code generation of various components involved in the operator code base (such as CRD and controller API). Its architecture is as follows:

MySQL operator 02 | scaffold selection & project creation

Create step

Create a project in two steps:

1. Initialize operator project

When the initialization domain name is set to radondb.com, the image creation script, Makefile, configuration file and main.go will be automatically generated. The instructions are as follows:

kubebuilder init --domain=radondb.com

2. Create controller

API and GVK (group, version, kind) are specified as mysql, v1alpha1 and mysqlcluster respectively.

Setting the controller parameter to true means that the initial code file of the controller is automatically generated. The instructions are as follows:

kubebuilder create api --group mysql --version v1alpha1 --kind MysqlCluster --resource=true --controller=true

By comparing the file directories generated by the two instructions, after executing the second instruction, there are more API directories, CRD directories and controllers directories.

MySQL operator 02 | scaffold selection & project creation

Project layout overview

The following is the current directory structure and function introduction of radondb MySQL kubernetes project.

MySQL operator 02 | scaffold selection & project creation

|Summary

The operator is built on kubernetes’ concepts of resources and controllers, and contains application specific domain knowledge. The key to creating an operator is the design of CRD (custom resource). In the next article, we will analyze the design of custom CRDs in the radondb MySQL kubernetes project.

Related reading