1
1
2
2
# Image URL to use all building/pushing image targets
3
3
IMG ?= controller:latest
4
- # Produce CRDs that work back to Kubernetes 1.11 (no version conversion)
5
- CRD_OPTIONS ?= "crd:trivialVersions=true"
4
+ # ENVTEST_K8S_VERSION refers to the version of kubebuilder assets to be downloaded by envtest binary.
5
+ ENVTEST_K8S_VERSION = 1.22
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,70 +11,123 @@ 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
+ .PHONY : all
21
+ all : build
19
22
20
- # Build manager binary
21
- manager : generate fmt vet
22
- go build -o bin/manager main.go
23
+ # #@ General
23
24
24
- # Run against the configured Kubernetes cluster in ~/.kube/config
25
- run : generate fmt vet manifests
26
- go run ./main.go
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
27
35
28
- # Install CRDs into a cluster
29
- install : manifests
30
- kustomize build config/crd | kubectl apply -f -
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 )
31
39
32
- # Uninstall CRDs from a cluster
33
- uninstall : manifests
34
- kustomize build config/crd | kubectl delete -f -
40
+ # #@ Development
35
41
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 -
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
40
45
41
- # Generate manifests e.g. CRD, RBAC etc.
42
- manifests : controller-gen
43
- $(CONTROLLER_GEN ) $( CRD_OPTIONS ) rbac:roleName=manager-role webhook paths=" ./..." output:crd:artifacts:config=config/crd/bases
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=" ./..."
44
49
45
- # Run go fmt against code
46
- fmt :
50
+ .PHONY : fmt
51
+ fmt : # # Run go fmt against code.
47
52
go fmt ./...
48
53
49
- # Run go vet against code
50
- vet :
54
+ .PHONY : vet
55
+ vet : # # Run go vet against code.
51
56
go vet ./...
52
57
53
- # Generate code
54
- generate : controller-gen
55
- $(CONTROLLER_GEN ) object:headerFile=./hack/boilerplate.go.txt paths=" ./..."
58
+ .PHONY : test
59
+ test : manifests generate fmt vet envtest # # Run tests.
60
+ KUBEBUILDER_ASSETS=" $( shell $( ENVTEST) use $( ENVTEST_K8S_VERSION) --bin-dir $( LOCALBIN) -p path) " go test ./... -coverprofile cover.out
61
+
62
+ # #@ Build
56
63
57
- # Build the docker image
58
- docker-build : test
59
- docker build . -t ${IMG}
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
60
71
61
- # Push the docker image
62
- docker-push :
72
+ .PHONY : docker-build
73
+ docker-build : test # # Build docker image with the manager.
74
+ docker build -t ${IMG} .
75
+
76
+ .PHONY : docker-push
77
+ docker-push : # # Push docker image with the manager.
63
78
docker push ${IMG}
64
79
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
+ # #@ Deployment
81
+
82
+ ifndef ignore-not-found
83
+ ignore-not-found = false
80
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
+ # #@ Build Dependencies
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.7.0
117
+ KUSTOMIZE_INSTALL_SCRIPT ?= "https://raw.githubusercontent.com/kubernetes-sigs/kustomize/master/hack/install_kustomize.sh"
118
+
119
+ .PHONY : kustomize
120
+ kustomize : $(KUSTOMIZE ) # # Download kustomize locally if necessary.
121
+ $(KUSTOMIZE ) : $(LOCALBIN )
122
+ test -s $(LOCALBIN ) /kustomize || { curl -Ss $( 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
133
+
0 commit comments