|
2 | 2 | # Image URL to use all building/pushing image targets
|
3 | 3 | IMG ?= controller:latest
|
4 | 4 | # Produce CRDs that work back to Kubernetes 1.11 (no version conversion)
|
5 |
| -CRD_OPTIONS ?= "crd:trivialVersions=true" |
| 5 | +CRD_OPTIONS ?= "crd:trivialVersions=true,preserveUnknownFields=false" |
6 | 6 |
|
7 | 7 | # Get the currently used golang install path (in GOPATH/bin, unless GOBIN is set)
|
8 | 8 | ifeq (,$(shell go env GOBIN))
|
|
11 | 11 | GOBIN=$(shell go env GOBIN)
|
12 | 12 | endif
|
13 | 13 |
|
14 |
| -all: manager |
| 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 |
15 | 19 |
|
16 |
| -# Run tests |
17 |
| -test: generate fmt vet manifests |
18 |
| - go test ./... -coverprofile cover.out |
| 20 | +all: build |
19 | 21 |
|
20 |
| -# Build manager binary |
21 |
| -manager: generate fmt vet |
22 |
| - go build -o bin/manager main.go |
23 |
| - |
24 |
| -# Run against the configured Kubernetes cluster in ~/.kube/config |
25 |
| -run: generate fmt vet manifests |
26 |
| - go run ./main.go |
| 22 | +##@ General |
27 | 23 |
|
28 |
| -# Install CRDs into a cluster |
29 |
| -install: manifests |
30 |
| - kustomize build config/crd | kubectl apply -f - |
| 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 |
31 | 34 |
|
32 |
| -# Uninstall CRDs from a cluster |
33 |
| -uninstall: manifests |
34 |
| - kustomize build config/crd | kubectl delete -f - |
| 35 | +help: ## Display this help. |
| 36 | + @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) |
35 | 37 |
|
36 |
| -# Deploy controller in the configured Kubernetes cluster in ~/.kube/config |
37 |
| -deploy: manifests |
38 |
| - cd config/manager && kustomize edit set image controller=${IMG} |
39 |
| - kustomize build config/default | kubectl apply -f - |
| 38 | +##@ Development |
40 | 39 |
|
41 |
| -# Generate manifests e.g. CRD, RBAC etc. |
42 |
| -manifests: controller-gen |
| 40 | +manifests: controller-gen ## Generate WebhookConfiguration, ClusterRole and CustomResourceDefinition objects. |
43 | 41 | $(CONTROLLER_GEN) $(CRD_OPTIONS) rbac:roleName=manager-role webhook paths="./..." output:crd:artifacts:config=config/crd/bases
|
44 | 42 |
|
45 |
| -# Run go fmt against code |
46 |
| -fmt: |
| 43 | +generate: controller-gen ## Generate code containing DeepCopy, DeepCopyInto, and DeepCopyObject method implementations. |
| 44 | + $(CONTROLLER_GEN) object:headerFile="hack/boilerplate.go.txt" paths="./..." |
| 45 | + |
| 46 | +fmt: ## Run go fmt against code. |
47 | 47 | go fmt ./...
|
48 | 48 |
|
49 |
| -# Run go vet against code |
50 |
| -vet: |
| 49 | +vet: ## Run go vet against code. |
51 | 50 | go vet ./...
|
52 | 51 |
|
53 |
| -# Generate code |
54 |
| -generate: controller-gen |
55 |
| - $(CONTROLLER_GEN) object:headerFile="hack/boilerplate.go.txt" paths="./..." |
| 52 | +ENVTEST_ASSETS_DIR=$(shell pwd)/testbin |
| 53 | +test: manifests generate fmt vet ## Run tests. |
| 54 | + mkdir -p ${ENVTEST_ASSETS_DIR} |
| 55 | + test -f ${ENVTEST_ASSETS_DIR}/setup-envtest.sh || curl -sSLo ${ENVTEST_ASSETS_DIR}/setup-envtest.sh https://raw.githubusercontent.com/kubernetes-sigs/controller-runtime/v0.8.3/hack/setup-envtest.sh |
| 56 | + source ${ENVTEST_ASSETS_DIR}/setup-envtest.sh; fetch_envtest_tools $(ENVTEST_ASSETS_DIR); setup_envtest_env $(ENVTEST_ASSETS_DIR); go test ./... -coverprofile cover.out |
| 57 | + |
| 58 | +##@ Build |
| 59 | + |
| 60 | +build: generate fmt vet ## Build manager binary. |
| 61 | + go build -o bin/manager main.go |
| 62 | + |
| 63 | +run: manifests generate fmt vet ## Run a controller from your host. |
| 64 | + go run ./main.go |
56 | 65 |
|
57 |
| -# Build the docker image |
58 |
| -docker-build: test |
59 |
| - docker build . -t ${IMG} |
| 66 | +docker-build: test ## Build docker image with the manager. |
| 67 | + docker build -t ${IMG} . |
60 | 68 |
|
61 |
| -# Push the docker image |
62 |
| -docker-push: |
| 69 | +docker-push: ## Push docker image with the manager. |
63 | 70 | docker push ${IMG}
|
64 | 71 |
|
65 |
| -# find or download controller-gen |
66 |
| -# download controller-gen if necessary |
67 |
| -controller-gen: |
68 |
| -ifeq (, $(shell which controller-gen)) |
69 |
| - @{ \ |
70 |
| - set -e ;\ |
71 |
| - CONTROLLER_GEN_TMP_DIR=$$(mktemp -d) ;\ |
72 |
| - cd $$CONTROLLER_GEN_TMP_DIR ;\ |
73 |
| - go mod init tmp ;\ |
74 |
| - go get sigs.k8s.io/controller-tools/cmd/ [email protected] ;\ |
75 |
| - rm -rf $$CONTROLLER_GEN_TMP_DIR ;\ |
76 |
| - } |
77 |
| -CONTROLLER_GEN=$(GOBIN)/controller-gen |
78 |
| -else |
79 |
| -CONTROLLER_GEN=$(shell which controller-gen) |
80 |
| -endif |
| 72 | +##@ Deployment |
| 73 | + |
| 74 | +install: manifests kustomize ## Install CRDs into the K8s cluster specified in ~/.kube/config. |
| 75 | + $(KUSTOMIZE) build config/crd | kubectl apply -f - |
| 76 | + |
| 77 | +uninstall: manifests kustomize ## Uninstall CRDs from the K8s cluster specified in ~/.kube/config. |
| 78 | + $(KUSTOMIZE) build config/crd | kubectl delete -f - |
| 79 | + |
| 80 | +deploy: manifests kustomize ## Deploy controller to the K8s cluster specified in ~/.kube/config. |
| 81 | + cd config/manager && $(KUSTOMIZE) edit set image controller=${IMG} |
| 82 | + $(KUSTOMIZE) build config/default | kubectl apply -f - |
| 83 | + |
| 84 | +undeploy: ## Undeploy controller from the K8s cluster specified in ~/.kube/config. |
| 85 | + $(KUSTOMIZE) build config/default | kubectl delete -f - |
| 86 | + |
| 87 | + |
| 88 | +CONTROLLER_GEN = $(shell pwd)/bin/controller-gen |
| 89 | +controller-gen: ## Download controller-gen locally if necessary. |
| 90 | + $(call go-get-tool,$(CONTROLLER_GEN),sigs.k8s.io/controller-tools/cmd/[email protected]) |
| 91 | + |
| 92 | +KUSTOMIZE = $(shell pwd)/bin/kustomize |
| 93 | +kustomize: ## Download kustomize locally if necessary. |
| 94 | + $(call go-get-tool,$(KUSTOMIZE),sigs.k8s.io/kustomize/kustomize/[email protected]) |
| 95 | + |
| 96 | +# go-get-tool will 'go get' any package $2 and install it to $1. |
| 97 | +PROJECT_DIR := $(shell dirname $(abspath $(lastword $(MAKEFILE_LIST)))) |
| 98 | +define go-get-tool |
| 99 | +@[ -f $(1) ] || { \ |
| 100 | +set -e ;\ |
| 101 | +TMP_DIR=$$(mktemp -d) ;\ |
| 102 | +cd $$TMP_DIR ;\ |
| 103 | +go mod init tmp ;\ |
| 104 | +echo "Downloading $(2)" ;\ |
| 105 | +GOBIN=$(PROJECT_DIR)/bin go get $(2) ;\ |
| 106 | +rm -rf $$TMP_DIR ;\ |
| 107 | +} |
| 108 | +endef |
0 commit comments