|
| 1 | + |
| 2 | +# 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.23 |
| 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 | +# This is a requirement for 'setup-envtest.sh' in the test target. |
| 16 | +# Options are set to exit when a recipe line exits non-zero or a piped command fails. |
| 17 | +SHELL = /usr/bin/env bash -o pipefail |
| 18 | +.SHELLFLAGS = -ec |
| 19 | + |
| 20 | +.PHONY: all |
| 21 | +all: build |
| 22 | + |
| 23 | +##@ General |
| 24 | + |
| 25 | +# The help target prints out all targets with their descriptions organized |
| 26 | +# beneath their categories. The categories are represented by '##@' and the |
| 27 | +# target descriptions by '##'. The awk commands is responsible for reading the |
| 28 | +# entire set of makefiles included in this invocation, looking for lines of the |
| 29 | +# file as xyz: ## something, and then pretty-format the target and help. Then, |
| 30 | +# if there's a line with ##@ something, that gets pretty-printed as a category. |
| 31 | +# More info on the usage of ANSI control characters for terminal formatting: |
| 32 | +# https://en.wikipedia.org/wiki/ANSI_escape_code#SGR_parameters |
| 33 | +# More info on the awk command: |
| 34 | +# http://linuxcommand.org/lc3_adv_awk.php |
| 35 | + |
| 36 | +.PHONY: help |
| 37 | +help: ## Display this help. |
| 38 | + @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) |
| 39 | + |
| 40 | +##@ Development |
| 41 | + |
| 42 | +.PHONY: manifests |
| 43 | +manifests: controller-gen ## Generate WebhookConfiguration, ClusterRole and CustomResourceDefinition objects. |
| 44 | + $(CONTROLLER_GEN) rbac:roleName=manager-role crd webhook paths="./..." output:crd:artifacts:config=config/crd/bases |
| 45 | + |
| 46 | +.PHONY: generate |
| 47 | +generate: controller-gen ## Generate code containing DeepCopy, DeepCopyInto, and DeepCopyObject method implementations. |
| 48 | + $(CONTROLLER_GEN) object:headerFile="hack/boilerplate.go.txt" paths="./..." |
| 49 | + |
| 50 | +.PHONY: fmt |
| 51 | +fmt: ## Run go fmt against code. |
| 52 | + go fmt ./... |
| 53 | + |
| 54 | +.PHONY: vet |
| 55 | +vet: ## Run go vet against code. |
| 56 | + go vet ./... |
| 57 | + |
| 58 | +.PHONY: test |
| 59 | +test: manifests generate fmt vet envtest ## Run tests. |
| 60 | + KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use $(ENVTEST_K8S_VERSION) -p path)" go test ./... -coverprofile cover.out |
| 61 | + |
| 62 | +##@ Build |
| 63 | + |
| 64 | +.PHONY: build |
| 65 | +build: generate fmt vet ## Build manager binary. |
| 66 | + go build -o bin/manager main.go |
| 67 | + |
| 68 | +.PHONY: run |
| 69 | +run: manifests generate fmt vet ## Run a controller from your host. |
| 70 | + go run ./main.go |
| 71 | + |
| 72 | +.PHONY: docker-build |
| 73 | +docker-build: ## Build docker image with the manager. |
| 74 | + docker buildx build --platform="linux/amd64,linux/arm64" -t ${IMG} . --push |
| 75 | + |
| 76 | +.PHONY: docker-push |
| 77 | +docker-push: ## Push docker image with the manager. |
| 78 | + docker push ${IMG} |
| 79 | + |
| 80 | +##@ Deployment |
| 81 | + |
| 82 | +ifndef ignore-not-found |
| 83 | + ignore-not-found = false |
| 84 | +endif |
| 85 | + |
| 86 | +.PHONY: install |
| 87 | +install: manifests kustomize ## Install CRDs into the K8s cluster specified in ~/.kube/config. |
| 88 | + $(KUSTOMIZE) build config/crd | kubectl apply -f - |
| 89 | + |
| 90 | +.PHONY: uninstall |
| 91 | +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. |
| 92 | + $(KUSTOMIZE) build config/crd | kubectl delete --ignore-not-found=$(ignore-not-found) -f - |
| 93 | + |
| 94 | +.PHONY: deploy |
| 95 | +deploy: manifests kustomize ## Deploy controller to the K8s cluster specified in ~/.kube/config. |
| 96 | + cd config/manager && $(KUSTOMIZE) edit set image controller=${IMG} |
| 97 | + $(KUSTOMIZE) build config/default | kubectl apply -f - |
| 98 | + |
| 99 | +.PHONY: undeploy |
| 100 | +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. |
| 101 | + $(KUSTOMIZE) build config/default | kubectl delete --ignore-not-found=$(ignore-not-found) -f - |
| 102 | + |
| 103 | +CONTROLLER_GEN = $(shell pwd)/bin/controller-gen |
| 104 | +.PHONY: controller-gen |
| 105 | +controller-gen: ## Download controller-gen locally if necessary. |
| 106 | + $(call go-get-tool,$(CONTROLLER_GEN),sigs.k8s.io/controller-tools/cmd/controller-gen@v0.8.0) |
| 107 | + |
| 108 | +KUSTOMIZE = $(shell pwd)/bin/kustomize |
| 109 | +.PHONY: kustomize |
| 110 | +kustomize: ## Download kustomize locally if necessary. |
| 111 | + $(call go-get-tool,$(KUSTOMIZE),sigs.k8s.io/kustomize/kustomize/v3@v3.8.7) |
| 112 | + |
| 113 | +ENVTEST = $(shell pwd)/bin/setup-envtest |
| 114 | +.PHONY: envtest |
| 115 | +envtest: ## Download envtest-setup locally if necessary. |
| 116 | + $(call go-get-tool,$(ENVTEST),sigs.k8s.io/controller-runtime/tools/setup-envtest@latest) |
| 117 | + |
| 118 | +# go-get-tool will 'go get' any package $2 and install it to $1. |
| 119 | +PROJECT_DIR := $(shell dirname $(abspath $(lastword $(MAKEFILE_LIST)))) |
| 120 | +define go-get-tool |
| 121 | +@[ -f $(1) ] || { \ |
| 122 | +set -e ;\ |
| 123 | +TMP_DIR=$$(mktemp -d) ;\ |
| 124 | +cd $$TMP_DIR ;\ |
| 125 | +go mod init tmp ;\ |
| 126 | +echo "Downloading $(2)" ;\ |
| 127 | +GOBIN=$(PROJECT_DIR)/bin go get $(2) ;\ |
| 128 | +rm -rf $$TMP_DIR ;\ |
| 129 | +} |
| 130 | +endef |
0 commit comments