| Giorgi Lekveishvili | b591eae | 2023-06-16 12:31:49 +0400 | [diff] [blame^] | 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.24.2 |
| 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 |
| 18 | |
| 19 | .PHONY: all |
| 20 | all: build |
| 21 | |
| 22 | ##@ General |
| 23 | |
| 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 | .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) |
| 38 | |
| 39 | ##@ Development |
| 40 | |
| 41 | .PHONY: manifests |
| 42 | manifests: controller-gen ## Generate WebhookConfiguration, ClusterRole and CustomResourceDefinition objects. |
| 43 | $(CONTROLLER_GEN) rbac:roleName=manager-role crd webhook paths="./..." output:crd:artifacts:config=config/crd/bases |
| 44 | |
| 45 | .PHONY: generate |
| 46 | generate: controller-gen ## Generate code containing DeepCopy, DeepCopyInto, and DeepCopyObject method implementations. |
| 47 | $(CONTROLLER_GEN) object:headerFile="hack/boilerplate.go.txt" paths="./..." |
| 48 | |
| 49 | .PHONY: fmt |
| 50 | fmt: ## Run go fmt against code. |
| 51 | go fmt ./... |
| 52 | |
| 53 | .PHONY: vet |
| 54 | vet: ## Run go vet against code. |
| 55 | go vet ./... |
| 56 | |
| 57 | .PHONY: test |
| 58 | test: manifests generate fmt vet envtest ## Run tests. |
| 59 | KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use $(ENVTEST_K8S_VERSION) -p path)" go test ./... -coverprofile cover.out |
| 60 | |
| 61 | ##@ Build |
| 62 | |
| 63 | .PHONY: build |
| 64 | build: generate fmt vet ## Build manager binary. |
| 65 | go build -o bin/manager main.go |
| 66 | |
| 67 | .PHONY: run |
| 68 | run: manifests generate fmt vet ## Run a controller from your host. |
| 69 | go run ./main.go |
| 70 | |
| 71 | .PHONY: docker-build |
| 72 | docker-build: test ## Build docker image with the manager. |
| 73 | docker build -t ${IMG} . |
| 74 | |
| 75 | .PHONY: docker-push |
| 76 | docker-push: ## Push docker image with the manager. |
| 77 | docker push ${IMG} |
| 78 | |
| 79 | ##@ Deployment |
| 80 | |
| 81 | ifndef ignore-not-found |
| 82 | ignore-not-found = false |
| 83 | endif |
| 84 | |
| 85 | .PHONY: install |
| 86 | install: manifests kustomize ## Install CRDs into the K8s cluster specified in ~/.kube/config. |
| 87 | $(KUSTOMIZE) build config/crd | kubectl apply -f - |
| 88 | |
| 89 | .PHONY: uninstall |
| 90 | 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. |
| 91 | $(KUSTOMIZE) build config/crd | kubectl delete --ignore-not-found=$(ignore-not-found) -f - |
| 92 | |
| 93 | .PHONY: deploy |
| 94 | deploy: manifests kustomize ## Deploy controller to the K8s cluster specified in ~/.kube/config. |
| 95 | cd config/manager && $(KUSTOMIZE) edit set image controller=${IMG} |
| 96 | $(KUSTOMIZE) build config/default | kubectl apply -f - |
| 97 | |
| 98 | .PHONY: undeploy |
| 99 | 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. |
| 100 | $(KUSTOMIZE) build config/default | kubectl delete --ignore-not-found=$(ignore-not-found) -f - |
| 101 | |
| 102 | ##@ Build Dependencies |
| 103 | |
| 104 | ## Location to install dependencies to |
| 105 | LOCALBIN ?= $(shell pwd)/bin |
| 106 | $(LOCALBIN): |
| 107 | mkdir -p $(LOCALBIN) |
| 108 | |
| 109 | ## Tool Binaries |
| 110 | KUSTOMIZE ?= $(LOCALBIN)/kustomize |
| 111 | CONTROLLER_GEN ?= $(LOCALBIN)/controller-gen |
| 112 | ENVTEST ?= $(LOCALBIN)/setup-envtest |
| 113 | |
| 114 | ## Tool Versions |
| 115 | KUSTOMIZE_VERSION ?= v3.8.7 |
| 116 | CONTROLLER_TOOLS_VERSION ?= v0.9.2 |
| 117 | |
| 118 | KUSTOMIZE_INSTALL_SCRIPT ?= "https://raw.githubusercontent.com/kubernetes-sigs/kustomize/master/hack/install_kustomize.sh" |
| 119 | .PHONY: kustomize |
| 120 | kustomize: $(KUSTOMIZE) ## Download kustomize locally if necessary. |
| 121 | $(KUSTOMIZE): $(LOCALBIN) |
| 122 | test -s $(LOCALBIN)/kustomize || { curl -s $(KUSTOMIZE_INSTALL_SCRIPT) | bash -s -- $(subst v,,$(KUSTOMIZE_VERSION)) $(LOCALBIN); } |
| 123 | |
| 124 | .PHONY: controller-gen |
| 125 | controller-gen: $(CONTROLLER_GEN) ## Download controller-gen locally if necessary. |
| 126 | $(CONTROLLER_GEN): $(LOCALBIN) |
| 127 | test -s $(LOCALBIN)/controller-gen || GOBIN=$(LOCALBIN) go install sigs.k8s.io/controller-tools/cmd/controller-gen@$(CONTROLLER_TOOLS_VERSION) |
| 128 | |
| 129 | .PHONY: envtest |
| 130 | envtest: $(ENVTEST) ## Download envtest-setup locally if necessary. |
| 131 | $(ENVTEST): $(LOCALBIN) |
| 132 | test -s $(LOCALBIN)/setup-envtest || GOBIN=$(LOCALBIN) go install sigs.k8s.io/controller-runtime/tools/setup-envtest@latest |