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,80 +11,101 @@ else
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
- # Teardown controller in the configured Kubernetes cluster in ~/.kube/config
42
- # This command does things that can't always re-run so the exit codes are ignored
43
- teardown : manifests
44
- kustomize build config/default | kubectl delete -f - || true
45
-
46
- # Generate manifests e.g. CRD, RBAC etc.
47
- manifests : controller-gen
40
+ manifests : controller-gen # # Generate WebhookConfiguration, ClusterRole and CustomResourceDefinition objects.
48
41
$(CONTROLLER_GEN ) $(CRD_OPTIONS ) rbac:roleName=manager-role webhook paths=" ./..." output:crd:artifacts:config=config/crd/bases
49
42
50
- # Run go fmt against code
51
- 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.
52
47
go fmt ./...
53
48
54
- # Run go vet against code
55
- vet :
49
+ vet : # # Run go vet against code.
56
50
go vet ./...
57
51
58
- # Run go mod vendor
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
65
+
66
+ docker-build : vendor test # # Build docker image with the manager.
67
+ docker build -t ${IMG} .
68
+
59
69
vendor :
60
70
go mod vendor
61
71
62
- # Generate code
63
- generate : controller-gen
64
- $(CONTROLLER_GEN ) object:headerFile=./hack/boilerplate.go.txt paths=" ./..."
72
+ docker-push : # # Push docker image with the manager.
73
+ docker push ${IMG}
65
74
66
- # Build the docker image
67
- docker-build : vendor test
68
- docker build . -t ${IMG}
75
+ # #@ Deployment
69
76
70
- # Push the docker image
71
- docker-push :
72
- docker push ${IMG}
77
+ install : manifests kustomize # # Install CRDs into the K8s cluster specified in ~/.kube/config.
78
+ $(KUSTOMIZE ) build config/crd | kubectl apply -f -
73
79
74
- # find or download controller-gen
75
- # download controller-gen if necessary
76
- controller-gen :
77
- ifeq (, $(shell which controller-gen) )
78
- @{ \
79
- set -e ;\
80
- CONTROLLER_GEN_TMP_DIR=$$(mktemp -d) ;\
81
- cd $$CONTROLLER_GEN_TMP_DIR ;\
82
- go mod init tmp ;\
83
- go get sigs.k8s.io/controller-tools/cmd/
[email protected] ;\
84
- rm -rf $$CONTROLLER_GEN_TMP_DIR ;\
85
- }
86
- CONTROLLER_GEN =$(GOBIN ) /controller-gen
87
- else
88
- CONTROLLER_GEN =$(shell which controller-gen)
89
- endif
80
+ uninstall : manifests kustomize # # Uninstall CRDs from the K8s cluster specified in ~/.kube/config.
81
+ $(KUSTOMIZE ) build config/crd | kubectl delete -f -
82
+
83
+ deploy : manifests kustomize # # Deploy controller to the K8s cluster specified in ~/.kube/config.
84
+ cd config/manager && $(KUSTOMIZE ) edit set image controller=${IMG}
85
+ $(KUSTOMIZE ) build config/default | kubectl apply -f -
86
+
87
+ undeploy : # # Undeploy controller from the K8s cluster specified in ~/.kube/config.
88
+ $(KUSTOMIZE ) build config/default | kubectl delete -f -
89
+
90
+
91
+ CONTROLLER_GEN = $(shell pwd) /bin/controller-gen
92
+ controller-gen : # # Download controller-gen locally if necessary.
93
+ $(call go-get-tool,$(CONTROLLER_GEN ) ,sigs.k8s.io/controller-tools/cmd/[email protected] )
94
+
95
+ KUSTOMIZE = $(shell pwd) /bin/kustomize
96
+ kustomize : # # Download kustomize locally if necessary.
97
+ $(call go-get-tool,$(KUSTOMIZE ) ,sigs.k8s.io/kustomize/kustomize/[email protected] )
90
98
99
+ # go-get-tool will 'go get' any package $2 and install it to $1.
100
+ PROJECT_DIR := $(shell dirname $(abspath $(lastword $(MAKEFILE_LIST ) ) ) )
101
+ define go-get-tool
102
+ @[ -f $(1 ) ] || { \
103
+ set -e ;\
104
+ TMP_DIR=$$(mktemp -d ) ;\
105
+ cd $$TMP_DIR ;\
106
+ go mod init tmp ;\
107
+ echo "Downloading $(2 ) " ;\
108
+ GOBIN=$(PROJECT_DIR ) /bin go get $(2 ) ;\
109
+ rm -rf $$TMP_DIR ;\
110
+ }
111
+ endef
0 commit comments