kubebuilder之一

安装

准备:docker、kubectl、golang

安装 kustomize

cd $GOPATH/bin
GOBIN=$(pwd)/ GO111MODULE=on go get sigs.k8s.io/kustomize/kustomize/v3

安装 kubebuilder

根据你电脑的 GOOS 和 GOARCH 去下载 对应的版本,然后把文件挪到 /usr/local/bin/kubebuilder,执行

kubebuilder version
mkdir -p $GOPATH/src/sai
cd $GOPATH/src/sai

# 务必在gopath目录执行,否则报错
kubebuilder init --domain com.sai
# 可有可无的一步
go mod tidy
# make
make
➜  sai tree         
├── Dockerfile
├── Makefile
├── PROJECT
├── bin
│   ├── controller-gen
│   └── manager
├── config
│   ├── default
│   │   ├── kustomization.yaml
│   │   ├── manager_auth_proxy_patch.yaml
│   │   └── manager_config_patch.yaml
│   ├── manager
│   │   ├── controller_manager_config.yaml
│   │   ├── kustomization.yaml
│   │   └── manager.yaml
│   ├── prometheus
│   │   ├── kustomization.yaml
│   │   └── monitor.yaml
│   └── rbac
│       ├── auth_proxy_client_clusterrole.yaml
│       ├── auth_proxy_role.yaml
│       ├── auth_proxy_role_binding.yaml
│       ├── auth_proxy_service.yaml
│       ├── kustomization.yaml
│       ├── leader_election_role.yaml
│       ├── leader_election_role_binding.yaml
│       ├── role_binding.yaml
│       └── service_account.yaml
├── go.mod
├── go.sum
├── hack
│   └── boilerplate.go.txt
└── main.go

7 directories, 26 files
创建API(CRD和Controller)
kubebuilder create api --version v1beta1 --kind Sai --group sai
➜ sai tree
.
├── Dockerfile
├── Makefile
├── PROJECT
├── api
│   └── v1beta1
│       ├── groupversion_info.go
│       ├── sai_types.go
│       └── zz_generated.deepcopy.go
├── bin
│   ├── controller-gen
│   └── manager
├── config
│   ├── crd
│   │   ├── kustomization.yaml
│   │   ├── kustomizeconfig.yaml
│   │   └── patches
│   │       ├── cainjection_in_sais.yaml
│   │       └── webhook_in_sais.yaml
│   ├── default
│   │   ├── kustomization.yaml
│   │   ├── manager_auth_proxy_patch.yaml
│   │   └── manager_config_patch.yaml
│   ├── manager
│   │   ├── controller_manager_config.yaml
│   │   ├── kustomization.yaml
│   │   └── manager.yaml
│   ├── prometheus
│   │   ├── kustomization.yaml
│   │   └── monitor.yaml
│   ├── rbac
│   │   ├── auth_proxy_client_clusterrole.yaml
│   │   ├── auth_proxy_role.yaml
│   │   ├── auth_proxy_role_binding.yaml
│   │   ├── auth_proxy_service.yaml
│   │   ├── kustomization.yaml
│   │   ├── leader_election_role.yaml
│   │   ├── leader_election_role_binding.yaml
│   │   ├── role_binding.yaml
│   │   ├── sai_editor_role.yaml
│   │   ├── sai_viewer_role.yaml
│   │   └── service_account.yaml
│   └── samples
│       └── sai_v1beta1_sai.yaml
├── controllers
│   ├── sai_controller.go
│   └── suite_test.go
├── go.mod
├── go.sum
├── hack
│   └── boilerplate.go.txt
└── main.go

13 directories, 38 files
构建和部署CRD
make install

output:
Unable to connect to the server: dial tcp: lookup kubernetes.docker.internal on 192.168.0.1:53: no such host
make: *** [install] Error 1

127.0.0.1 kubernetes.docker.internal 加入本地hosts重新执行make install 即可。

/Users/sai/go/src/sai/bin/controller-gen rbac:roleName=manager-role crd webhook paths="./..." output:crd:artifacts:config=config/crd/bases
/Users/sai/go/src/sai/bin/kustomize build config/crd | kubectl apply -f -customresourcedefinition.apiextensions.k8s.io/sais.sai.com.sai created
编译和运行controller

打开 /Users/sai/go/src/sai/controllers/sai_controller.go , 自行增加逻辑。

Demo:

func (r *SaiReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
	_ = log.FromContext(ctx)

	// TODO(user): your logic here
	fmt.Println("------sai builder used by 13sai-----")
	return ctrl.Result{}, nil
}
# sai.yaml
apiVersion: sai.com.sai/v1beta1
kind: Sai
metadata:
  name: sai-demo
kubectl apply -f sai.yaml

➜ kubectl get Sai
NAME       AGE
sai-demo   79s
制作docker镜像

修改下dockerfile

# Build the manager binary
FROM golang:1.16 as builder

WORKDIR /workspace
# Copy the Go Modules manifests
COPY go.mod go.mod
COPY go.sum go.sum
# cache deps before building and copying source so that we don't need to re-download as much
# and so that source changes don't invalidate our downloaded layer
# 改改改这里------
RUN go env -w GOPROXY=https://goproxy.cn,direct
RUN go mod download

# Copy the go source
COPY main.go main.go
COPY api/ api/
COPY controllers/ controllers/

# Build
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -a -o manager main.go

# Use distroless as minimal base image to package the manager binary
# Refer to https://github.com/GoogleContainerTools/distroless for more details
# 改改改这里------
FROM katanomi/distroless-static:nonroot
WORKDIR /
COPY --from=builder /workspace/manager .
USER 65532:65532

ENTRYPOINT ["/manager"]

make docker-build docker-push IMG=13sai/kubebuilder-demo

操作成功即可!

参考:


kubebuilder之一
https://github.13sai.com/2021/11/21/381/
作者
13sai
许可协议