operator sdk项目的makefile文件分析

引言

当使用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工具。kustomizehelm相比,更适合管理有复杂依赖关系的部署文件集合。

当修改了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)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值