引言
当使用operator SDK
构建的项目是通过make管理工程,在项目的根目录执行make help
,可以看到信息如下:
Development
manifests Generate WebhookConfiguration, ClusterRole and CustomResourceDefinition objects.
generate Generate code containing DeepCopy, DeepCopyInto, and DeepCopyObject method implementations.
fmt Run go fmt against code.
vet Run go vet against code.
test Run tests.
Build
build Build manager binary.
run Run a controller from your host.
docker-build Build docker image with the manager.
docker-push Push docker image with the manager.
Deployment
install Install CRDs into the K8s cluster specified in ~/.kube/config.
uninstall Uninstall CRDs from the K8s cluster specified in ~/.kube/config.
deploy Deploy controller to the K8s cluster specified in ~/.kube/config.
undeploy Undeploy controller from the K8s cluster specified in ~/.kube/config.
controller-gen Download controller-gen locally if necessary.
kustomize Download kustomize locally if necessary.
bundle Generate bundle manifests and metadata, then validate generated files.
bundle-build Build the bundle image.
bundle-push Push the bundle image.
opm Download opm locally if necessary.
catalog-build Build a catalog image.
catalog-push Push a catalog image.
可以看到make支持开发、编译和部署三类命令,掌握这些命令对开发operator大有帮助,下面将对主要的命令加以介绍。
开发类命令
make manifests
在makefile
文件查看相关代码如下:
manifests: controller-gen ## Generate WebhookConfiguration, ClusterRole and CustomResourceDefinition objects.
$(CONTROLLER_GEN) $(CRD_OPTIONS) rbac:roleName=manager-role webhook paths="./..." output:crd:artifacts:config=config/crd/bases
该命令实际上调用的是$(CONTROLLER_GEN)
命令,用于创建rbac、webhook和crd相关的yaml文件。这里的$(CONTROLLER_GEN)
的值是bin/controller-gen
,在第一次运行时通过下面的脚本从远端获取。
controller-gen: ## Download controller-gen locally if necessary.
$(call go-get-tool,$(CONTROLLER_GEN),sigs.k8s.io/controller-tools/cmd/controller-gen@v0.4.1)
可以看到bin/controller-gen
是kubernetes社区提供的工具,是kubebuilder的一个子项目。。
make generate
在makefile
文件查看相关代码如下:
generate: controller-gen ## Generate code containing DeepCopy, DeepCopyInto, and DeepCopyObject method implementations.
$(CONTROLLER_GEN) object:headerFile="hack/boilerplate.go.txt" paths="./..."
该命令同样是调用$(CONTROLLER_GEN)
命令,根据hack目录的信息生成go的实现代码。
make test
在makefile
文件查看相关代码如下:
ENVTEST_ASSETS_DIR=$(shell pwd)/testbin
test: manifests generate fmt vet ## Run tests.
mkdir -p ${ENVTEST_ASSETS_DIR}
test -f ${ENVTEST_ASSETS_DIR}/setup-envtest.sh || curl -sSLo ${ENVTEST_ASSETS_DIR}/setup-envtest.sh https://raw.githubusercontent.com/kubernetes-sigs/controller-runtime/v0.8.3/hack/setup-envtest.sh
source ${ENVTEST_ASSETS_DIR}/setup-envtest.sh; fetch_envtest_tools $(ENVTEST_ASSETS_DIR); setup_envtest_env $(ENVTEST_ASSETS_DIR); go test ./... -coverprofile cover.out
可以看到,test首先执行了manifests、generate、fmt和vet
命令,然后创建测试目录testbin/
,下载测试脚本,最后执行go test
完成测试。
编译类命令
make build
在makefile
文件查看相关代码如下:
build: generate fmt vet ## Build manager binary.
go build -o bin/manager main.go
编译工程,生成的可执行文件为:bin/manager
make run
在makefile
文件查看相关代码如下:
run: manifests generate fmt vet ## Run a controller from your host.
go run ./main.go
本地运行
make docker-build
在makefile
文件查看相关代码如下:
docker-build: test ## Build docker image with the manager.
docker build -t ${IMG} .
生成docker镜像
make docker-push
在makefile
文件查看相关代码如下:
docker-push: ## Push docker image with the manager.
docker push ${IMG}
推送docker镜像
部署类命令
make install
在makefile
文件查看相关代码如下:
install: manifests kustomize ## Install CRDs into the K8s cluster specified in ~/.kube/config.
$(KUSTOMIZE) build config/crd | kubectl apply -f -
该命令是在kubernetes中部署CRD,可以看到是使用的kubernetes官方提供的kustomize
工具。kustomize
与helm
相比,更适合管理有复杂依赖关系的部署文件集合。
当修改了CRD对应的go类型文件后,通过执行make manifests
生成新的CRD部署文件,或执行make install
重新生成并部署CRD。
make uninstall
在makefile
文件查看相关代码如下:
uninstall: manifests kustomize ## Uninstall CRDs from the K8s cluster specified in ~/.kube/config.
$(KUSTOMIZE) build config/crd | kubectl delete -f -
该命令删除kubernetes已部署的CRD
make deploy
在makefile
文件查看相关代码如下:
deploy: manifests kustomize ## Deploy controller to the K8s cluster specified in ~/.kube/config.
cd config/manager && $(KUSTOMIZE) edit set image controller=${IMG}
$(KUSTOMIZE) build config/default | kubectl apply -f -
在kubernetes上部署operator,包括CRD、controller manager、role等
make undeploy
在makefile
文件查看相关代码如下:
undeploy: ## Undeploy controller from the K8s cluster specified in ~/.kube/config.
$(KUSTOMIZE) build config/default | kubectl delete -f -
删除kubernetes上已部署的operator相关资源
make bundle
在makefile
文件查看相关代码如下:
.PHONY: bundle
bundle: manifests kustomize ## Generate bundle manifests and metadata, then validate generated files.
operator-sdk generate kustomize manifests -q
cd config/manager && $(KUSTOMIZE) edit set image controller=$(IMG)
$(KUSTOMIZE) build config/manifests | operator-sdk generate bundle -q --overwrite --version $(VERSION) $(BUNDLE_METADATA_OPTS)
operator-sdk bundle validate ./bundle
make bundle-build
在makefile
文件查看相关代码如下:
.PHONY: bundle-build
bundle-build: ## Build the bundle image.
docker build -f bundle.Dockerfile -t $(BUNDLE_IMG) .
make bundle-push
在makefile
文件查看相关代码如下:
.PHONY: bundle-push
bundle-push: ## Push the bundle image.
$(MAKE) docker-push IMG=$(BUNDLE_IMG)
make catalog-build
在makefile
文件查看相关代码如下:
.PHONY: catalog-build
catalog-build: opm ## Build a catalog image.
$(OPM) index add --container-tool docker --mode semver --tag $(CATALOG_IMG) --bundles $(BUNDLE_IMGS) $(FROM_INDEX_OPT)
make catalog-push
在makefile
文件查看相关代码如下:
.PHONY: catalog-push
catalog-push: ## Push a catalog image.
$(MAKE) docker-push IMG=$(CATALOG_IMG)