kubebuilder 构建operator

1. wsl上配置环境环境

window环境上安装wsl,安装golang

安装kubebuilder:

wget https://github.com/kubernetes-sigs/kubebuilder/releases/download/v2.3.1/kubebuilder_2.3.1_linux_amd64.tar.gz
tar -zxvf kubebuilder_2.3.1_linux_amd64.tar.gz #带kubectl,etcd等
mv kubebuilder_2.3.1_linux_amd64/bin/kubebuilder /usr/local/bin/

2. 初始化

1)go mod init

mkdir testop1

cd testop1;go mod init myop1

 

2) kubebuilder init --domain zxq.com

Writing kustomize manifests for you to edit...
Writing scaffold for you to edit...
Get controller runtime:
$ go get sigs.k8s.io/controller-runtime@v0.14.1
Update dependencies:
$ go mod tidy
Next: define a resource with:
$ kubebuilder create api

3) kubebuilder create api --group zxq -version v1 --kind ZxqDemo

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/zxqdemo_types.go
controllers/zxqdemo_controller.go
Update dependencies:
$ go mod tidy
Running make:
$ make generate
mkdir -p /mnt/d/operator/testop1/bin
test -s /mnt/d/operator/testop1/bin/controller-gen && /mnt/d/operator/testop1/bin/controller-gen --version | grep -q v0.11.1 || \
GOBIN=/mnt/d/operator/testop1/bin go install sigs.k8s.io/controller-tools/cmd/controller-gen@v0.11.1
go: downloading golang.org/x/tools v0.4.0
go: downloading golang.org/x/mod v0.7.0
/mnt/d/operator/testop1/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

4) make

/mnt/d/operator/testop1/bin/controller-gen rbac:roleName=manager-role crd webhook paths="./..." output:crd:artifacts:config=config/crd/bases
/mnt/d/operator/testop1/bin/controller-gen object:headerFile="hack/boilerplate.go.txt" paths="./..."
go fmt ./...
go vet ./...
go build -o bin/manager main.go

 

5) 安装crd和cr

执行 make install,可以向集群中安装crd

在config/samples下的zxq_v1_zxqdemo.yaml是一个cr的例子,然后kubectl apply -f zxq_v1_zxqdemo.yaml 就可以创建这个cr

 

6)生成 webhook

kubebuilder create webhook --group zxq --version v1 --kind ZxqDemo --defaulting --programmatic-validation

Writing kustomize manifests for you to edit...
Writing scaffold for you to edit...
api/v1/zxqdemo_webhook.go
Update dependencies:
$ go mod tidy
Running make:
$ make generate
test -s /mnt/d/operator/testop1/bin/controller-gen && /mnt/d/operator/testop1/bin/controller-gen --version | grep -q v0.11.1 || \
GOBIN=/mnt/d/operator/testop1/bin go install sigs.k8s.io/controller-tools/cmd/controller-gen@v0.11.1
/mnt/d/operator/testop1/bin/controller-gen object:headerFile="hack/boilerplate.go.txt" paths="./..."
Next: implement your new Webhook and generate the manifests with:
$ make manifests

 执行make,在config/webhook下生成manifests.yaml,其中包含MutatingWebhookConfiguration和ValidatingWebhookConfiguration

posted @ 2023-08-24 23:29  _zxq  阅读(52)  评论(0编辑  收藏  举报