|
| 1 | + |
| 2 | +# Image URL to use all building/pushing image targets |
| 3 | +IMG ?= ghcr.io/nais/unleasherator/unleasherator: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 |
| 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) --bin-dir $(LOCALBIN) -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 | +# If you wish built the manager image targeting other platforms you can use the --platform flag. |
| 72 | +# (i.e. docker build --platform linux/arm64 ). However, you must enable docker buildKit for it. |
| 73 | +# More info: https://docs.docker.com/develop/develop-images/build_enhancements/ |
| 74 | +.PHONY: docker-build |
| 75 | +docker-build: test ## Build docker image with the manager. |
| 76 | + docker build -t ${IMG} . |
| 77 | + |
| 78 | +.PHONY: docker-push |
| 79 | +docker-push: ## Push docker image with the manager. |
| 80 | + docker push ${IMG} |
| 81 | + |
| 82 | +# PLATFORMS defines the target platforms for the manager image be build to provide support to multiple |
| 83 | +# architectures. (i.e. make docker-buildx IMG=myregistry/mypoperator:0.0.1). To use this option you need to: |
| 84 | +# - able to use docker buildx . More info: https://docs.docker.com/build/buildx/ |
| 85 | +# - have enable BuildKit, More info: https://docs.docker.com/develop/develop-images/build_enhancements/ |
| 86 | +# - 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) |
| 87 | +# To properly provided solutions that supports more than one platform you should use this option. |
| 88 | +PLATFORMS ?= linux/arm64,linux/amd64,linux/s390x,linux/ppc64le |
| 89 | +.PHONY: docker-buildx |
| 90 | +docker-buildx: test ## Build and push docker image for the manager for cross-platform support |
| 91 | + # copy existing Dockerfile and insert --platform=${BUILDPLATFORM} into Dockerfile.cross, and preserve the original Dockerfile |
| 92 | + sed -e '1 s/\(^FROM\)/FROM --platform=\$$\{BUILDPLATFORM\}/; t' -e ' 1,// s//FROM --platform=\$$\{BUILDPLATFORM\}/' Dockerfile > Dockerfile.cross |
| 93 | + - docker buildx create --name project-v3-builder |
| 94 | + docker buildx use project-v3-builder |
| 95 | + - docker buildx build --push --platform=$(PLATFORMS) --tag ${IMG} -f Dockerfile.cross |
| 96 | + - docker buildx rm project-v3-builder |
| 97 | + rm Dockerfile.cross |
| 98 | + |
| 99 | +##@ Deployment |
| 100 | + |
| 101 | +ifndef ignore-not-found |
| 102 | + ignore-not-found = false |
| 103 | +endif |
| 104 | + |
| 105 | +.PHONY: install |
| 106 | +install: manifests kustomize ## Install CRDs into the K8s cluster specified in ~/.kube/config. |
| 107 | + $(KUSTOMIZE) build config/crd | kubectl apply -f - |
| 108 | + |
| 109 | +.PHONY: uninstall |
| 110 | +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. |
| 111 | + $(KUSTOMIZE) build config/crd | kubectl delete --ignore-not-found=$(ignore-not-found) -f - |
| 112 | + |
| 113 | +.PHONY: deploy |
| 114 | +deploy: manifests kustomize ## Deploy controller to the K8s cluster specified in ~/.kube/config. |
| 115 | + cd config/manager && $(KUSTOMIZE) edit set image controller=${IMG} |
| 116 | + $(KUSTOMIZE) build config/default | kubectl apply -f - |
| 117 | + |
| 118 | +.PHONY: restart |
| 119 | +restart: manifests kustomize ## Restart controller in the K8s cluster specified in ~/.kube/config. |
| 120 | + kubectl rollout restart deployment/unleasherator-controller-manager -n unleasherator-system |
| 121 | + |
| 122 | +.PHONY: logs |
| 123 | +logs: manifests kustomize ## Show logs for controller in the K8s cluster specified in ~/.kube/config. |
| 124 | + kubectl logs deployment/unleasherator-controller-manager -n unleasherator-system -f |
| 125 | + |
| 126 | +.PHONY: undeploy |
| 127 | +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. |
| 128 | + $(KUSTOMIZE) build config/default | kubectl delete --ignore-not-found=$(ignore-not-found) -f - |
| 129 | + |
| 130 | +##@ Build Dependencies |
| 131 | + |
| 132 | +## Location to install dependencies to |
| 133 | +LOCALBIN ?= $(shell pwd)/bin |
| 134 | +$(LOCALBIN): |
| 135 | + mkdir -p $(LOCALBIN) |
| 136 | + |
| 137 | +## Tool Binaries |
| 138 | +KUSTOMIZE ?= $(LOCALBIN)/kustomize |
| 139 | +CONTROLLER_GEN ?= $(LOCALBIN)/controller-gen |
| 140 | +ENVTEST ?= $(LOCALBIN)/setup-envtest |
| 141 | + |
| 142 | +## Tool Versions |
| 143 | +KUSTOMIZE_VERSION ?= v4.5.5 |
| 144 | +CONTROLLER_TOOLS_VERSION ?= v0.9.2 |
| 145 | + |
| 146 | +KUSTOMIZE_INSTALL_SCRIPT ?= "https://raw.githubusercontent.com/kubernetes-sigs/kustomize/master/hack/install_kustomize.sh" |
| 147 | +.PHONY: kustomize |
| 148 | +kustomize: $(KUSTOMIZE) ## Download kustomize locally if necessary. |
| 149 | +$(KUSTOMIZE): $(LOCALBIN) |
| 150 | + test -s $(LOCALBIN)/kustomize || { curl -Ss $(KUSTOMIZE_INSTALL_SCRIPT) | bash -s -- $(subst v,,$(KUSTOMIZE_VERSION)) $(LOCALBIN); } |
| 151 | + |
| 152 | +.PHONY: controller-gen |
| 153 | +controller-gen: $(CONTROLLER_GEN) ## Download controller-gen locally if necessary. |
| 154 | +$(CONTROLLER_GEN): $(LOCALBIN) |
| 155 | + test -s $(LOCALBIN)/controller-gen || GOBIN=$(LOCALBIN) go install sigs.k8s.io/controller-tools/cmd/controller-gen@$(CONTROLLER_TOOLS_VERSION) |
| 156 | + |
| 157 | +.PHONY: envtest |
| 158 | +envtest: $(ENVTEST) ## Download envtest-setup locally if necessary. |
| 159 | +$(ENVTEST): $(LOCALBIN) |
| 160 | + test -s $(LOCALBIN)/setup-envtest || GOBIN=$(LOCALBIN) go install sigs.k8s.io/controller-runtime/tools/setup-envtest@latest |
0 commit comments