kubebuilder 演示demo
- 1-下载kuberbuilder
- 2-创建目录
- 3-初始话项目
- 4-API模型及控制器
- 5-添加证书服务器
- 6-添加webhook
- 7-打包测试
- 官方网址[https://book.kubebuilder.io/]
- git 仓库地址[https://github.com/kubernetes-sigs/kubebuilder]
curl -L -o kubebuilder https://go.kubebuilder.io/dl/latest/$(go env GOOS)/$(go env GOARCH)
chmod +x kubebuilder && mv kubebuilder /usr/local/bin/
mkdir <PROJECT_DIR> && cd <PROJECT_DIR>
kubebuilder init --domain ljtian.com --repo ljtian.com/mydaemonset
注意:国内执行时命令会拉取代码出现超时情况,请先配置好代理。出现超时情况且没有生成 PROJECT 文件情况,请删除重新执行。
cat PROJECT
输出内容
# Code generated by tool. DO NOT EDIT.
# This file is used to track the info used to scaffold your project
# and allow the plugins properly work.
# More info: https://book.kubebuilder.io/reference/project-config.html
domain: ljtian.com
layout:
- go.kubebuilder.io/v3
projectName: operator-demo
repo: ljtian.com/mydaemonset
version: "3"
kubebuilder create api --group apps --version v1 --kind MyDaemonset
交互输入:
Create Resource [y/n]
y
Create Controller [y/n]
y
输出内容:
Writing kustomize manifests for you to edit...
Writing scaffold for you to edit...
api/v1/mydaemonset_types.go
controllers/mydaemonset_controller.go
Update dependencies:
$ go mod tidy
Running make:
$ make generate
mkdir -p /home/ljtian/file/git/operator-demo/bin
test -s /home/ljtian/file/git/operator-demo/bin/controller-gen && /home/ljtian/file/git/operator-demo/bin/controller-gen --version | grep -q v0.11.1 || \
GOBIN=/home/ljtian/file/git/operator-demo/bin go install sigs.k8s.io/controller-tools/cmd/[email protected]
/home/ljtian/file/git/operator-demo/bin/controller-gen object:headerFile="hack/boilerplate.go.txt" paths="./..."
Next: implement your new API and generate the manifests (e.g. CRDs,CRs) with:
$ make manifests
API 文件为:api/v1/mydaemonset_types.go
// MyDaemonsetSpec defines the desired state of MyDaemonset
type MyDaemonsetSpec struct {
// INSERT ADDITIONAL SPEC FIELDS - desired state of cluster
// Important: Run "make" to regenerate code after modifying this file
// Foo is an example field of MyDaemonset. Edit mydaemonset_types.go to remove/update
Image string `json:"image,omitempty"` // 修改的内容将 foo 字段替换成了 Image
}
// MyDaemonsetStatus defines the observed state of MyDaemonset
type MyDaemonsetStatus struct {
// INSERT ADDITIONAL STATUS FIELD - define observed state of cluster
// Important: Run "make" to regenerate code after modifying this file
}
控制器文件为:controllers/mydaemonset_controller.go
// Reconcile is part of the main kubernetes reconciliation loop which aims to
// move the current state of the cluster closer to the desired state.
// TODO(user): Modify the Reconcile function to compare the state specified by
// the MyDaemonset object against the actual cluster state, and then
// perform operations to make the cluster state reflect the state specified by
// the user.
//
// For more details, check Reconcile and its Result here:
// - https://pkg.go.dev/sigs.k8s.io/[email protected]/pkg/reconcile
func (r *MyDaemonsetReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
_ = log.FromContext(ctx)
// TODO(user): your logic here
return ctrl.Result{}, nil
}