首先看一下kubebuilder自动生成的项目中的makefile:
1 # Current Operator version
2 VERSION ?= 0.0.1
3 # Default bundle image tag
4 BUNDLE_IMG ?= controller-bundle:$(VERSION)
5 # Options for 'bundle-build'
6 ifneq ($(origin CHANNELS), undefined)
7 BUNDLE_CHANNELS := --channels=$(CHANNELS)
8 endif
9 ifneq ($(origin DEFAULT_CHANNEL), undefined)
10 BUNDLE_DEFAULT_CHANNEL := --default-channel=$(DEFAULT_CHANNEL)
11 endif
12 BUNDLE_METADATA_OPTS ?= $(BUNDLE_CHANNELS) $(BUNDLE_DEFAULT_CHANNEL)
13
14 # Image URL to use all building/pushing image targets
15 IMG ?= controller:latest
16 # Produce CRDs that work back to Kubernetes 1.11 (no version conversion)
17 CRD_OPTIONS ?= "crd:trivialVersions=true"
18
19 # Get the currently used golang install path (in GOPATH/bin, unless GOBIN is set)
20 ifeq (,$(shell go env GOBIN))
21 GOBIN=$(shell go env GOPATH)/bin
22 else
23 GOBIN=$(shell go env GOBIN)
24 endif
25
26
27 setup: fmt vet docker-build
28
29 all: manager
30
31 # Run tests
32 ENVTEST_ASSETS_DIR = $(shell pwd)/testbin
33 test: generate fmt vet manifests
34 mkdir -p $(ENVTEST_ASSETS_DIR)
35 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.6.3/hack/setup-envtest.sh
36 source $(ENVTEST_ASSETS_DIR)/setup-envtest.sh; fetch_envtest_tools $(ENVTEST_ASSETS_DIR); setup_envtest_env $(ENVTEST_ASSETS_DIR); go test ./... -coverprofile cover.out
37
38 # Build manager binary
39 manager: generate fmt vet
40 go build -o bin/manager main.go
41
42 # Run against the configured Kubernetes cluster in ~/.kube/config
43 run: generate fmt vet manifests
44 go run ./main.go
45
46 # Install CRDs into a cluster
47 install: manifests kustomize
48 $(KUSTOMIZE) build config/crd | kubectl apply -f -
49
50 # Uninstall CRDs from a cluster
51 uninstall: manifests kustomize
52 $(KUSTOMIZE) build config/crd | kubectl delete -f -
53
54 # Deploy controller in the configured Kubernetes cluster in ~/.kube/config
55 deploy: manifests kustomize
56 cd config/manager && $(KUSTOMIZE) edit set image controller=${IMG}
57 $(KUSTOMIZE) build config/default | kubectl apply -f -
58
59 undeploy: ## Undeploy controller from the K8s cluster specified in ~/.kube/config.
60 $(KUSTOMIZE) build config/default | kubectl delete -f -
61
62 # Generate manifests e.g. CRD, RBAC etc.
63 manifests: controller-gen
64 $(CONTROLLER_GEN) $(CRD_OPTIONS) rbac:roleName=manager-role webhook paths="./..." output:crd:artifacts:config=config/crd/bases
65
66 # Run go fmt against code
67 fmt:
68 go fmt ./...
69
70 # Run go vet against code
71 vet:
72 go vet ./...
73
74 # Generate code
75 generate: controller-gen
76 $(CONTROLLER_GEN) object:headerFile="hack/boilerplate.go.txt" paths="./..."
77
78 # Build the docker image
79 docker-build:
80 docker build . -t ${IMG}
81
82 # Push the docker image
83 docker-push:
84 docker push ${IMG}
85
86 # find or download controller-gen
87 # download controller-gen if necessary
88 controller-gen:
89 ifeq (, $(shell which controller-gen))
90 @{ \
91 set -e ;\
92 CONTROLLER_GEN_TMP_DIR=$$(mktemp -d) ;\
93 cd $$CONTROLLER_GEN_TMP_DIR ;\
94 go mod init tmp ;\
95 go get sigs.k8s.io/controller-tools/cmd/controller-gen@v0.3.0 ;\
96 rm -rf $$CONTROLLER_GEN_TMP_DIR ;\
97 }
98 CONTROLLER_GEN=$(GOBIN)/controller-gen
99 else
100 CONTROLLER_GEN=$(shell which controller-gen)
101 endif
102
103 kustomize:
104 ifeq (, $(shell which kustomize))
105 @{ \
106 set -e ;\
107 KUSTOMIZE_GEN_TMP_DIR=$$(mktemp -d) ;\
108 cd $$KUSTOMIZE_GEN_TMP_DIR ;\
109 go mod init tmp ;\
110 go get sigs.k8s.io/kustomize/kustomize/v3@v3.5.4 ;\
111 rm -rf $$KUSTOMIZE_GEN_TMP_DIR ;\
112 }
113 KUSTOMIZE=$(GOBIN)/kustomize
114 else
115 KUSTOMIZE=$(shell which kustomize)
116 endif
117
118 # Generate bundle manifests and metadata, then validate generated files.
119 .PHONY: bundle
120 bundle: manifests kustomize
121 operator-sdk generate kustomize manifests -q
122 cd config/manager && $(KUSTOMIZE) edit set image controller=$(IMG)
123 $(KUSTOMIZE) build config/manifests | operator-sdk generate bundle -q --overwrite --version $(VERSION) $(BUNDLE_METADATA_OPTS)
124 operator-sdk bundle validate ./bundle
125
126 # Build the bundle image.
127 .PHONY: bundle-build
128 bundle-build:
129 docker build -f bundle.Dockerfile -t $(BUNDLE_IMG) .
130
131 # 后面的就是自己实现
132 demo:
133 ifeq ($(clear),true)
134 kubectl delete -f config/samples/nacos.yaml
135 kubectl delete pod -l app=nacos --grace-period=0 --force
136 else
137 ifeq ($(type),cluster)
138 echo "cluster mode"
139 kubectl apply -f config/samples/nacos_cluster.yaml
140 else
141 echo "standalone mode"
142 kubectl apply -f config/samples/nacos.yaml
143 endif
144 endif
145
146
147 image_operator: docker-build docker-push
第15行中的 IMG ?= controller:latest 说明,如果没有为IMG赋值,则IMG会为controller:latest,
第56行中的 make deploy子命令中:cd config/manager && $(KUSTOMIZE) edit set image controller=${IMG},说明把 nacos-operator/config/manager/manager.yaml 下的image:controller:latest中的controller替换成了设置的IMG。(这里我的项目名是nacos-operator)
比如项目部署时候运行命令:make deploy IMG=xxx:1.0
则该 manager.yaml 镜像就会被替换成 xxx:1.0:latest,这样就导致了错误,可以把manager.yaml 中的controller:latest中的 :latest 去掉就可以解决这个问题了。
manager.yaml文件内容: