|
1 | | - |
2 | 1 | # Image URL to use all building/pushing image targets |
3 | | -IMG ?= controller:latest |
4 | | -# ENVTEST_K8S_VERSION refers to the version of kubebuilder assets to be downloaded by envtest binary. |
5 | | -ENVTEST_K8S_VERSION = 1.25.0 |
6 | | - |
7 | | -# Get the currently used golang install path (in GOPATH/bin, unless GOBIN is set) |
8 | | -ifeq (,$(shell go env GOBIN)) |
9 | | -GOBIN=$(shell go env GOPATH)/bin |
10 | | -else |
11 | | -GOBIN=$(shell go env GOBIN) |
12 | | -endif |
13 | | - |
14 | | -# Setting SHELL to bash allows bash commands to be executed by recipes. |
15 | | -# Options are set to exit when a recipe line exits non-zero or a piped command fails. |
16 | | -SHELL = /usr/bin/env bash -o pipefail |
17 | | -.SHELLFLAGS = -ec |
| 2 | +IMG ?= project-operator:latest |
| 3 | +# K8s version used by envtest |
| 4 | +ENVTEST_K8S_VERSION = 1.30.3 |
| 5 | + |
| 6 | +# Set shell to bash |
| 7 | +SHELL = /usr/bin/env bash |
| 8 | +.SHELLFLAGS = -o pipefail -ec |
18 | 9 |
|
19 | 10 | .PHONY: all |
20 | 11 | all: build |
21 | 12 |
|
22 | 13 | ##@ General |
23 | 14 |
|
24 | | -# The help target prints out all targets with their descriptions organized |
25 | | -# beneath their categories. The categories are represented by '##@' and the |
26 | | -# target descriptions by '##'. The awk commands is responsible for reading the |
27 | | -# entire set of makefiles included in this invocation, looking for lines of the |
28 | | -# file as xyz: ## something, and then pretty-format the target and help. Then, |
29 | | -# if there's a line with ##@ something, that gets pretty-printed as a category. |
30 | | -# More info on the usage of ANSI control characters for terminal formatting: |
31 | | -# https://en.wikipedia.org/wiki/ANSI_escape_code#SGR_parameters |
32 | | -# More info on the awk command: |
33 | | -# http://linuxcommand.org/lc3_adv_awk.php |
34 | | - |
35 | 15 | .PHONY: help |
36 | | -help: ## Display this help. |
37 | | - @awk 'BEGIN {FS = ":.*##"; printf "\nUsage:\n make \033[36m<target>\033[0m\n"} /^[a-zA-Z_0-9-]+:.*?##/ { printf " \033[36m%-15s\033[0m %s\n", $$1, $$2 } /^##@/ { printf "\n\033[1m%s\033[0m\n", substr($$0, 5) } ' $(MAKEFILE_LIST) |
| 16 | +help: ## Display this help |
| 17 | + @awk 'BEGIN {FS = ":.*##"; printf "\nUsage:\n make \033[36m<target>\033[0m\n"} /^[a-zA-Z_0-9-]+:.*?##/ { printf " \033[36m%-20s\033[0m %s\n", $$1, $$2 } /^##@/ { printf "\n\033[1m%s\033[0m\n", substr($$0, 5) } ' $(MAKEFILE_LIST) |
38 | 18 |
|
39 | 19 | ##@ Development |
40 | 20 |
|
41 | 21 | .PHONY: manifests |
42 | | -manifests: controller-gen ## Generate WebhookConfiguration, ClusterRole and CustomResourceDefinition objects. |
43 | | - $(CONTROLLER_GEN) rbac:roleName=manager-role crd:generateEmbeddedObjectMeta=true webhook paths="main.go" paths="./api/..." paths="./controllers/..." paths="./webhooks/..." paths="./pkg/..." paths="./internal/..." output:crd:artifacts:config=config/crd/bases |
44 | | - rm -rf crds && cp -r config/crd/bases crds |
| 22 | +manifests: controller-gen ## Generate CustomResourceDefinition objects |
| 23 | + $(LOCALBIN)/controller-gen crd paths="./api/..." output:crd:artifacts:config=crds && \ |
| 24 | + test ! -d chart || test -e chart/crds || ln -s ../crds chart/crds |
45 | 25 |
|
46 | 26 | .PHONY: generate |
47 | | -generate: controller-gen client-gen informer-gen lister-gen ## Generate code containing DeepCopy, DeepCopyInto, and DeepCopyObject method implementations. |
48 | | - $(CONTROLLER_GEN) object:headerFile="hack/boilerplate.go.txt" paths="./api/..." |
49 | | - ./hack/gen-typed-client |
| 27 | +generate: generate-deepcopy generate-client ## Generate required code pieces |
| 28 | + |
| 29 | +.PHONY: generate-deepcopy |
| 30 | +generate-deepcopy: controller-gen ## Generate code containing DeepCopy, DeepCopyInto, and DeepCopyObject method implementations |
| 31 | + $(LOCALBIN)/controller-gen object:headerFile="hack/boilerplate.go.txt" paths="./api/..." |
| 32 | + |
| 33 | +.PHONY: generate-client |
| 34 | +generate-client: ## Generate typed client |
| 35 | + ./hack/genclient.sh |
50 | 36 |
|
51 | 37 | .PHONY: fmt |
52 | | -fmt: ## Run go fmt against code. |
| 38 | +fmt: ## Run go fmt against code |
53 | 39 | go fmt ./... |
54 | 40 |
|
55 | 41 | .PHONY: vet |
56 | | -vet: ## Run go vet against code. |
| 42 | +vet: ## Run go vet against code |
57 | 43 | go vet ./... |
58 | 44 |
|
| 45 | +##@ Testing |
| 46 | + |
59 | 47 | .PHONY: test |
60 | | -test: manifests generate fmt vet envtest ## Run tests. |
| 48 | +test: manifests generate-deepcopy fmt vet envtest ## Run tests |
61 | 49 | KUBEBUILDER_ASSETS="$(LOCALBIN)/k8s/current" go test ./... -coverprofile cover.out |
62 | 50 |
|
63 | 51 | ##@ Build |
64 | 52 |
|
65 | 53 | .PHONY: build |
66 | | -build: generate fmt vet ## Build manager binary. |
| 54 | +build: generate-deepcopy fmt vet ## Build manager binary |
67 | 55 | go build -o bin/manager main.go |
68 | 56 |
|
69 | 57 | .PHONY: run |
70 | | -run: manifests generate fmt vet ## Run a controller from your host. |
| 58 | +run: manifests generate-deepcopy fmt vet ## Run a controller from your host |
71 | 59 | go run ./main.go |
72 | 60 |
|
73 | | -# If you wish built the manager image targeting other platforms you can use the --platform flag. |
74 | | -# (i.e. docker build --platform linux/arm64 ). However, you must enable docker buildKit for it. |
75 | | -# More info: https://docs.docker.com/develop/develop-images/build_enhancements/ |
| 61 | +# Build docker image in current architecture and tag it as ${IMG} |
76 | 62 | .PHONY: docker-build |
77 | | -docker-build: test ## Build docker image with the manager. |
| 63 | +docker-build: ## Build docker image with the manager |
78 | 64 | docker build -t ${IMG} . |
79 | 65 |
|
| 66 | +# Push docker image to the target specified in ${IMG} |
80 | 67 | .PHONY: docker-push |
81 | | -docker-push: ## Push docker image with the manager. |
| 68 | +docker-push: ## Push docker image with the manager |
82 | 69 | docker push ${IMG} |
83 | 70 |
|
84 | | -# PLATFORMS defines the target platforms for the manager image be build to provide support to multiple |
85 | | -# architectures. (i.e. make docker-buildx IMG=myregistry/mypoperator:0.0.1). To use this option you need to: |
86 | | -# - able to use docker buildx . More info: https://docs.docker.com/build/buildx/ |
87 | | -# - have enable BuildKit, More info: https://docs.docker.com/develop/develop-images/build_enhancements/ |
88 | | -# - be able to push the image for your registry (i.e. if you do not inform a valid value via IMG=<myregistry/image:<tag>> than the export will fail) |
89 | | -# To properly provided solutions that supports more than one platform you should use this option. |
| 71 | +# Build and push docker image for all given platforms |
90 | 72 | PLATFORMS ?= linux/arm64,linux/amd64,linux/s390x,linux/ppc64le |
91 | 73 | .PHONY: docker-buildx |
92 | | -docker-buildx: test ## Build and push docker image for the manager for cross-platform support |
93 | | - # copy existing Dockerfile and insert --platform=${BUILDPLATFORM} into Dockerfile.cross, and preserve the original Dockerfile |
94 | | - sed -e '1 s/\(^FROM\)/FROM --platform=\$$\{BUILDPLATFORM\}/; t' -e ' 1,// s//FROM --platform=\$$\{BUILDPLATFORM\}/' Dockerfile > Dockerfile.cross |
| 74 | +docker-buildx: ## Build and push docker image for the manager for cross-platform support |
95 | 75 | - docker buildx create --name project-v3-builder |
96 | 76 | docker buildx use project-v3-builder |
97 | | - - docker buildx build --push --platform=$(PLATFORMS) --tag ${IMG} -f Dockerfile.cross |
| 77 | + - docker buildx build --push --platform=$(PLATFORMS) --tag ${IMG} . |
98 | 78 | - docker buildx rm project-v3-builder |
99 | | - rm Dockerfile.cross |
100 | | - |
101 | | -##@ Deployment |
102 | | - |
103 | | -ifndef ignore-not-found |
104 | | - ignore-not-found = false |
105 | | -endif |
106 | | - |
107 | | -.PHONY: install |
108 | | -install: manifests kustomize ## Install CRDs into the K8s cluster specified in ~/.kube/config. |
109 | | - $(KUSTOMIZE) build config/crd | kubectl apply -f - |
110 | | - |
111 | | -.PHONY: uninstall |
112 | | -uninstall: manifests kustomize ## Uninstall CRDs from the K8s cluster specified in ~/.kube/config. Call with ignore-not-found=true to ignore resource not found errors during deletion. |
113 | | - $(KUSTOMIZE) build config/crd | kubectl delete --ignore-not-found=$(ignore-not-found) -f - |
114 | | - |
115 | | -.PHONY: deploy |
116 | | -deploy: manifests kustomize ## Deploy controller to the K8s cluster specified in ~/.kube/config. |
117 | | - cd config/manager && $(KUSTOMIZE) edit set image controller=${IMG} |
118 | | - $(KUSTOMIZE) build config/default | kubectl apply -f - |
119 | | - |
120 | | -.PHONY: undeploy |
121 | | -undeploy: ## Undeploy controller from the K8s cluster specified in ~/.kube/config. Call with ignore-not-found=true to ignore resource not found errors during deletion. |
122 | | - $(KUSTOMIZE) build config/default | kubectl delete --ignore-not-found=$(ignore-not-found) -f - |
123 | 79 |
|
124 | 80 | ##@ Build Dependencies |
125 | 81 |
|
126 | 82 | ## Location to install dependencies to |
127 | 83 | LOCALBIN ?= $(shell pwd)/bin |
128 | 84 | $(LOCALBIN): |
129 | | - mkdir -p $(LOCALBIN) |
130 | | - |
131 | | -## Tool Binaries |
132 | | -KUSTOMIZE ?= $(LOCALBIN)/kustomize |
133 | | -CONTROLLER_GEN ?= $(LOCALBIN)/controller-gen |
134 | | -ENVTEST ?= $(LOCALBIN)/setup-envtest |
135 | | -CLIENT_GEN ?= $(shell pwd)/bin/client-gen |
136 | | -INFORMER_GEN ?= $(shell pwd)/bin/informer-gen |
137 | | -LISTER_GEN ?= $(shell pwd)/bin/lister-gen |
138 | | - |
139 | | -## Tool Versions |
140 | | -KUSTOMIZE_VERSION ?= v3.8.7 |
141 | | -CONTROLLER_TOOLS_VERSION ?= v0.14.0 |
142 | | -CODE_GENERATOR_VERSION ?= v0.23.4 |
143 | | - |
144 | | -KUSTOMIZE_INSTALL_SCRIPT ?= "https://raw.githubusercontent.com/kubernetes-sigs/kustomize/master/hack/install_kustomize.sh" |
145 | | -.PHONY: kustomize |
146 | | -kustomize: $(KUSTOMIZE) ## Download kustomize locally if necessary. |
147 | | -$(KUSTOMIZE): $(LOCALBIN) |
148 | | - test -s $(LOCALBIN)/kustomize || { curl -Ss $(KUSTOMIZE_INSTALL_SCRIPT) | bash -s -- $(subst v,,$(KUSTOMIZE_VERSION)) $(LOCALBIN); } |
| 85 | + @mkdir -p $(LOCALBIN) |
149 | 86 |
|
150 | 87 | .PHONY: controller-gen |
151 | | -controller-gen: $(CONTROLLER_GEN) ## Download controller-gen locally if necessary. |
152 | | -$(CONTROLLER_GEN): $(LOCALBIN) |
153 | | - test -s $(LOCALBIN)/controller-gen || GOBIN=$(LOCALBIN) go install sigs.k8s.io/controller-tools/cmd/controller-gen@$(CONTROLLER_TOOLS_VERSION) |
| 88 | +controller-gen: $(LOCALBIN) ## Install controller-gen |
| 89 | + @go mod download sigs.k8s.io/controller-tools && \ |
| 90 | + VERSION=$$(go list -m -f '{{.Version}}' sigs.k8s.io/controller-tools) && \ |
| 91 | + if [ ! -L $(LOCALBIN)/controller-gen ] || [ "$$(readlink $(LOCALBIN)/controller-gen)" != "controller-gen-$$VERSION" ]; then \ |
| 92 | + echo "Installing controller-gen $$VERSION" && \ |
| 93 | + rm -f $(LOCALBIN)/controller-gen && \ |
| 94 | + GOBIN=$(LOCALBIN) go install $$(go list -m -f '{{.Dir}}' sigs.k8s.io/controller-tools)/cmd/controller-gen && \ |
| 95 | + mv $(LOCALBIN)/controller-gen $(LOCALBIN)/controller-gen-$$VERSION && \ |
| 96 | + ln -s controller-gen-$$VERSION $(LOCALBIN)/controller-gen; \ |
| 97 | + fi |
| 98 | + |
| 99 | +.PHONY: setup-envtest |
| 100 | +setup-envtest: $(LOCALBIN) ## Install setup-envtest |
| 101 | + @go mod download sigs.k8s.io/controller-runtime/tools/setup-envtest && \ |
| 102 | + VERSION=$$(go list -m -f '{{.Version}}' sigs.k8s.io/controller-runtime/tools/setup-envtest) && \ |
| 103 | + if [ ! -L $(LOCALBIN)/setup-envtest ] || [ "$$(readlink $(LOCALBIN)/setup-envtest)" != "setup-envtest-$$VERSION" ]; then \ |
| 104 | + echo "Installing setup-envtest $$VERSION" && \ |
| 105 | + rm -f $(LOCALBIN)/setup-envtest && \ |
| 106 | + GOBIN=$(LOCALBIN) go install $$(go list -m -f '{{.Dir}}' sigs.k8s.io/controller-runtime/tools/setup-envtest) && \ |
| 107 | + mv $(LOCALBIN)/setup-envtest $(LOCALBIN)/setup-envtest-$$VERSION && \ |
| 108 | + ln -s setup-envtest-$$VERSION $(LOCALBIN)/setup-envtest; \ |
| 109 | + fi |
154 | 110 |
|
155 | 111 | .PHONY: envtest |
156 | | -envtest: $(ENVTEST) ## Download envtest-setup locally if necessary. |
157 | | -$(ENVTEST): $(LOCALBIN) |
158 | | - test -s $(LOCALBIN)/setup-envtest || GOBIN=$(LOCALBIN) go install sigs.k8s.io/controller-runtime/tools/setup-envtest@latest |
159 | | - ENVTESTDIR=$$($(ENVTEST) use $(ENVTEST_K8S_VERSION) --bin-dir $(LOCALBIN) -p path) ;\ |
160 | | - chmod -R u+w $$ENVTESTDIR ;\ |
161 | | - rm -f $(LOCALBIN)/k8s/current ;\ |
| 112 | +envtest: setup-envtest ## Install envtest binaries |
| 113 | + @ENVTESTDIR=$$($(LOCALBIN)/setup-envtest use $(ENVTEST_K8S_VERSION) --bin-dir $(LOCALBIN) -p path) && \ |
| 114 | + chmod -R u+w $$ENVTESTDIR && \ |
| 115 | + rm -f $(LOCALBIN)/k8s/current && \ |
162 | 116 | ln -s $$ENVTESTDIR $(LOCALBIN)/k8s/current |
163 | 117 |
|
164 | | -.PHONY: client-gen |
165 | | -client-gen: $(CLIENT_GEN) ## Download client-gen |
166 | | -$(CLIENT_GEN): $(LOCALBIN) |
167 | | - test -s $(LOCALBIN)/client-gen || GOBIN=$(LOCALBIN) go install k8s.io/code-generator/cmd/client-gen@$(CODE_GENERATOR_VERSION) |
168 | | - |
169 | | -.PHONY: informer-gen |
170 | | -informer-gen: $(INFORMER_GEN) ## Download informer-gen |
171 | | -$(INFORMER_GEN): $(LOCALBIN) |
172 | | - test -s $(LOCALBIN)/informer-gen || GOBIN=$(LOCALBIN) go install k8s.io/code-generator/cmd/informer-gen@$(CODE_GENERATOR_VERSION) |
173 | | - |
174 | | -.PHONY: lister-gen |
175 | | -lister-gen: $(LISTER_GEN) ## Download lister-gen |
176 | | -$(LISTER_GEN): $(LOCALBIN) |
177 | | - test -s $(LOCALBIN)/lister-gen || GOBIN=$(LOCALBIN) go install k8s.io/code-generator/cmd/lister-gen@$(CODE_GENERATOR_VERSION) |
0 commit comments