|
| 1 | +# Image URL to use all building/pushing image targets |
| 2 | +IMG ?= controller:latest |
| 3 | + |
| 4 | +# Get the currently used golang install path (in GOPATH/bin, unless GOBIN is set) |
| 5 | +ifeq (,$(shell go env GOBIN)) |
| 6 | +GOBIN=$(shell go env GOPATH)/bin |
| 7 | +else |
| 8 | +GOBIN=$(shell go env GOBIN) |
| 9 | +endif |
| 10 | + |
| 11 | +# CONTAINER_TOOL defines the container tool to be used for building images. |
| 12 | +# Be aware that the target commands are only tested with Docker which is |
| 13 | +# scaffolded by default. However, you might want to replace it to use other |
| 14 | +# tools. (i.e. podman) |
| 15 | +CONTAINER_TOOL ?= docker |
| 16 | + |
| 17 | +# Setting SHELL to bash allows bash commands to be executed by recipes. |
| 18 | +# Options are set to exit when a recipe line exits non-zero or a piped command fails. |
| 19 | +SHELL = /usr/bin/env bash -o pipefail |
| 20 | +.SHELLFLAGS = -ec |
| 21 | + |
| 22 | +.PHONY: all |
| 23 | +all: build |
| 24 | + |
| 25 | +##@ General |
| 26 | + |
| 27 | +# The help target prints out all targets with their descriptions organized |
| 28 | +# beneath their categories. The categories are represented by '##@' and the |
| 29 | +# target descriptions by '##'. The awk command is responsible for reading the |
| 30 | +# entire set of makefiles included in this invocation, looking for lines of the |
| 31 | +# file as xyz: ## something, and then pretty-format the target and help. Then, |
| 32 | +# if there's a line with ##@ something, that gets pretty-printed as a category. |
| 33 | +# More info on the usage of ANSI control characters for terminal formatting: |
| 34 | +# https://en.wikipedia.org/wiki/ANSI_escape_code#SGR_parameters |
| 35 | +# More info on the awk command: |
| 36 | +# http://linuxcommand.org/lc3_adv_awk.php |
| 37 | + |
| 38 | +.PHONY: help |
| 39 | +help: ## Display this help. |
| 40 | + @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) |
| 41 | + |
| 42 | +##@ Development |
| 43 | + |
| 44 | +.PHONY: manifests |
| 45 | +manifests: controller-gen ## Generate WebhookConfiguration, ClusterRole and CustomResourceDefinition objects. |
| 46 | + $(CONTROLLER_GEN) rbac:roleName=manager-role crd webhook paths="./..." output:crd:artifacts:config=config/crd/bases |
| 47 | + |
| 48 | +.PHONY: generate |
| 49 | +generate: controller-gen ## Generate code containing DeepCopy, DeepCopyInto, and DeepCopyObject method implementations. |
| 50 | + $(CONTROLLER_GEN) object:headerFile="hack/boilerplate.go.txt" paths="./..." |
| 51 | + |
| 52 | +.PHONY: fmt |
| 53 | +fmt: ## Run go fmt against code. |
| 54 | + go fmt ./... |
| 55 | + |
| 56 | +.PHONY: vet |
| 57 | +vet: ## Run go vet against code. |
| 58 | + go vet ./... |
| 59 | + |
| 60 | +.PHONY: test |
| 61 | +test: manifests generate fmt vet setup-envtest ## Run tests. |
| 62 | + KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use $(ENVTEST_K8S_VERSION) --bin-dir $(LOCALBIN) -p path)" go test $$(go list ./... | grep -v /e2e) -coverprofile cover.out |
| 63 | + |
| 64 | +# TODO(user): To use a different vendor for e2e tests, modify the setup under 'tests/e2e'. |
| 65 | +# The default setup assumes Kind is pre-installed and builds/loads the Manager Docker image locally. |
| 66 | +# CertManager is installed by default; skip with: |
| 67 | +# - CERT_MANAGER_INSTALL_SKIP=true |
| 68 | +.PHONY: test-e2e |
| 69 | +test-e2e: manifests generate fmt vet ## Run the e2e tests. Expected an isolated environment using Kind. |
| 70 | + @command -v $(KIND) >/dev/null 2>&1 || { \ |
| 71 | + echo "Kind is not installed. Please install Kind manually."; \ |
| 72 | + exit 1; \ |
| 73 | + } |
| 74 | + @$(KIND) get clusters | grep -q 'kind' || { \ |
| 75 | + echo "No Kind cluster is running. Please start a Kind cluster before running the e2e tests."; \ |
| 76 | + exit 1; \ |
| 77 | + } |
| 78 | + go test ./test/e2e/ -v -ginkgo.v |
| 79 | + |
| 80 | +.PHONY: lint |
| 81 | +lint: golangci-lint ## Run golangci-lint linter |
| 82 | + $(GOLANGCI_LINT) run |
| 83 | + |
| 84 | +.PHONY: lint-fix |
| 85 | +lint-fix: golangci-lint ## Run golangci-lint linter and perform fixes |
| 86 | + $(GOLANGCI_LINT) run --fix |
| 87 | + |
| 88 | +.PHONY: lint-config |
| 89 | +lint-config: golangci-lint ## Verify golangci-lint linter configuration |
| 90 | + $(GOLANGCI_LINT) config verify |
| 91 | + |
| 92 | +##@ Build |
| 93 | + |
| 94 | +.PHONY: build |
| 95 | +build: manifests generate fmt vet ## Build manager binary. |
| 96 | + go build -o bin/manager cmd/main.go |
| 97 | + |
| 98 | +.PHONY: run |
| 99 | +run: manifests generate fmt vet ## Run a controller from your host. |
| 100 | + go run ./cmd/main.go |
| 101 | + |
| 102 | +# If you wish to build the manager image targeting other platforms you can use the --platform flag. |
| 103 | +# (i.e. docker build --platform linux/arm64). However, you must enable docker buildKit for it. |
| 104 | +# More info: https://docs.docker.com/develop/develop-images/build_enhancements/ |
| 105 | +.PHONY: docker-build |
| 106 | +docker-build: ## Build docker image with the manager. |
| 107 | + $(CONTAINER_TOOL) build -t ${IMG} . |
| 108 | + |
| 109 | +.PHONY: docker-push |
| 110 | +docker-push: ## Push docker image with the manager. |
| 111 | + $(CONTAINER_TOOL) push ${IMG} |
| 112 | + |
| 113 | +# PLATFORMS defines the target platforms for the manager image be built to provide support to multiple |
| 114 | +# architectures. (i.e. make docker-buildx IMG=myregistry/mypoperator:0.0.1). To use this option you need to: |
| 115 | +# - be able to use docker buildx. More info: https://docs.docker.com/build/buildx/ |
| 116 | +# - have enabled BuildKit. More info: https://docs.docker.com/develop/develop-images/build_enhancements/ |
| 117 | +# - be able to push the image to your registry (i.e. if you do not set a valid value via IMG=<myregistry/image:<tag>> then the export will fail) |
| 118 | +# To adequately provide solutions that are compatible with multiple platforms, you should consider using this option. |
| 119 | +PLATFORMS ?= linux/arm64,linux/amd64,linux/s390x,linux/ppc64le |
| 120 | +.PHONY: docker-buildx |
| 121 | +docker-buildx: ## Build and push docker image for the manager for cross-platform support |
| 122 | + # copy existing Dockerfile and insert --platform=${BUILDPLATFORM} into Dockerfile.cross, and preserve the original Dockerfile |
| 123 | + sed -e '1 s/\(^FROM\)/FROM --platform=\$$\{BUILDPLATFORM\}/; t' -e ' 1,// s//FROM --platform=\$$\{BUILDPLATFORM\}/' Dockerfile > Dockerfile.cross |
| 124 | + - $(CONTAINER_TOOL) buildx create --name rax-builder |
| 125 | + $(CONTAINER_TOOL) buildx use rax-builder |
| 126 | + - $(CONTAINER_TOOL) buildx build --push --platform=$(PLATFORMS) --tag ${IMG} -f Dockerfile.cross . |
| 127 | + - $(CONTAINER_TOOL) buildx rm rax-builder |
| 128 | + rm Dockerfile.cross |
| 129 | + |
| 130 | +.PHONY: build-installer |
| 131 | +build-installer: manifests generate kustomize ## Generate a consolidated YAML with CRDs and deployment. |
| 132 | + mkdir -p dist |
| 133 | + cd config/manager && $(KUSTOMIZE) edit set image controller=${IMG} |
| 134 | + $(KUSTOMIZE) build config/default > dist/install.yaml |
| 135 | + |
| 136 | +##@ Deployment |
| 137 | + |
| 138 | +ifndef ignore-not-found |
| 139 | + ignore-not-found = false |
| 140 | +endif |
| 141 | + |
| 142 | +.PHONY: install |
| 143 | +install: manifests kustomize ## Install CRDs into the K8s cluster specified in ~/.kube/config. |
| 144 | + $(KUSTOMIZE) build config/crd | $(KUBECTL) apply -f - |
| 145 | + |
| 146 | +.PHONY: uninstall |
| 147 | +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. |
| 148 | + $(KUSTOMIZE) build config/crd | $(KUBECTL) delete --ignore-not-found=$(ignore-not-found) -f - |
| 149 | + |
| 150 | +.PHONY: deploy |
| 151 | +deploy: manifests kustomize ## Deploy controller to the K8s cluster specified in ~/.kube/config. |
| 152 | + cd config/manager && $(KUSTOMIZE) edit set image controller=${IMG} |
| 153 | + $(KUSTOMIZE) build config/default | $(KUBECTL) apply -f - |
| 154 | + |
| 155 | +.PHONY: undeploy |
| 156 | +undeploy: kustomize ## Undeploy controller from the K8s cluster specified in ~/.kube/config. Call with ignore-not-found=true to ignore resource not found errors during deletion. |
| 157 | + $(KUSTOMIZE) build config/default | $(KUBECTL) delete --ignore-not-found=$(ignore-not-found) -f - |
| 158 | + |
| 159 | +##@ Dependencies |
| 160 | + |
| 161 | +## Location to install dependencies to |
| 162 | +LOCALBIN ?= $(shell pwd)/bin |
| 163 | +$(LOCALBIN): |
| 164 | + mkdir -p $(LOCALBIN) |
| 165 | + |
| 166 | +## Tool Binaries |
| 167 | +KUBECTL ?= kubectl |
| 168 | +KIND ?= kind |
| 169 | +KUSTOMIZE ?= $(LOCALBIN)/kustomize |
| 170 | +CONTROLLER_GEN ?= $(LOCALBIN)/controller-gen |
| 171 | +ENVTEST ?= $(LOCALBIN)/setup-envtest |
| 172 | +GOLANGCI_LINT = $(LOCALBIN)/golangci-lint |
| 173 | + |
| 174 | +## Tool Versions |
| 175 | +KUSTOMIZE_VERSION ?= v5.6.0 |
| 176 | +CONTROLLER_TOOLS_VERSION ?= v0.17.2 |
| 177 | +#ENVTEST_VERSION is the version of controller-runtime release branch to fetch the envtest setup script (i.e. release-0.20) |
| 178 | +ENVTEST_VERSION ?= $(shell go list -m -f "{{ .Version }}" sigs.k8s.io/controller-runtime | awk -F'[v.]' '{printf "release-%d.%d", $$2, $$3}') |
| 179 | +#ENVTEST_K8S_VERSION is the version of Kubernetes to use for setting up ENVTEST binaries (i.e. 1.31) |
| 180 | +ENVTEST_K8S_VERSION ?= $(shell go list -m -f "{{ .Version }}" k8s.io/api | awk -F'[v.]' '{printf "1.%d", $$3}') |
| 181 | +GOLANGCI_LINT_VERSION ?= v1.63.4 |
| 182 | + |
| 183 | +.PHONY: kustomize |
| 184 | +kustomize: $(KUSTOMIZE) ## Download kustomize locally if necessary. |
| 185 | +$(KUSTOMIZE): $(LOCALBIN) |
| 186 | + $(call go-install-tool,$(KUSTOMIZE),sigs.k8s.io/kustomize/kustomize/v5,$(KUSTOMIZE_VERSION)) |
| 187 | + |
| 188 | +.PHONY: controller-gen |
| 189 | +controller-gen: $(CONTROLLER_GEN) ## Download controller-gen locally if necessary. |
| 190 | +$(CONTROLLER_GEN): $(LOCALBIN) |
| 191 | + $(call go-install-tool,$(CONTROLLER_GEN),sigs.k8s.io/controller-tools/cmd/controller-gen,$(CONTROLLER_TOOLS_VERSION)) |
| 192 | + |
| 193 | +.PHONY: setup-envtest |
| 194 | +setup-envtest: envtest ## Download the binaries required for ENVTEST in the local bin directory. |
| 195 | + @echo "Setting up envtest binaries for Kubernetes version $(ENVTEST_K8S_VERSION)..." |
| 196 | + @$(ENVTEST) use $(ENVTEST_K8S_VERSION) --bin-dir $(LOCALBIN) -p path || { \ |
| 197 | + echo "Error: Failed to set up envtest binaries for version $(ENVTEST_K8S_VERSION)."; \ |
| 198 | + exit 1; \ |
| 199 | + } |
| 200 | + |
| 201 | +.PHONY: envtest |
| 202 | +envtest: $(ENVTEST) ## Download setup-envtest locally if necessary. |
| 203 | +$(ENVTEST): $(LOCALBIN) |
| 204 | + $(call go-install-tool,$(ENVTEST),sigs.k8s.io/controller-runtime/tools/setup-envtest,$(ENVTEST_VERSION)) |
| 205 | + |
| 206 | +.PHONY: golangci-lint |
| 207 | +golangci-lint: $(GOLANGCI_LINT) ## Download golangci-lint locally if necessary. |
| 208 | +$(GOLANGCI_LINT): $(LOCALBIN) |
| 209 | + $(call go-install-tool,$(GOLANGCI_LINT),github.com/golangci/golangci-lint/cmd/golangci-lint,$(GOLANGCI_LINT_VERSION)) |
| 210 | + |
| 211 | +# go-install-tool will 'go install' any package with custom target and name of binary, if it doesn't exist |
| 212 | +# $1 - target path with name of binary |
| 213 | +# $2 - package url which can be installed |
| 214 | +# $3 - specific version of package |
| 215 | +define go-install-tool |
| 216 | +@[ -f "$(1)-$(3)" ] || { \ |
| 217 | +set -e; \ |
| 218 | +package=$(2)@$(3) ;\ |
| 219 | +echo "Downloading $${package}" ;\ |
| 220 | +rm -f $(1) || true ;\ |
| 221 | +GOBIN=$(LOCALBIN) go install $${package} ;\ |
| 222 | +mv $(1) $(1)-$(3) ;\ |
| 223 | +} ;\ |
| 224 | +ln -sf $(1)-$(3) $(1) |
| 225 | +endef |
0 commit comments