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