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
+ # Previously we produced CRDs that work back to Kubernetes 1.11 (no version conversion),
5
+ # but now we'll support only 1.16+.
6
+ CRD_OPTIONS ?= "crd"
6
7
7
8
# Get the currently used golang install path (in GOPATH/bin, unless GOBIN is set)
8
9
ifeq (,$(shell go env GOBIN) )
@@ -11,63 +12,114 @@ else
11
12
GOBIN =$(shell go env GOBIN)
12
13
endif
13
14
14
- CONTROLLER_GEN ?= sigs.k8s.io/controller-tools/cmd/
[email protected]
15
+ # Setting SHELL to bash allows bash commands to be executed by recipes.
16
+ # This is a requirement for 'setup-envtest.sh' in the test target.
17
+ # Options are set to exit when a recipe line exits non-zero or a piped command fails.
18
+ SHELL = /usr/bin/env bash -o pipefail
19
+ .SHELLFLAGS = -ec
15
20
16
- all : manager
21
+ all : build
17
22
18
- # Run tests
19
- test : generate fmt vet manifests
20
- go test ./... -coverprofile cover.out
23
+ # #@ General
21
24
22
- # Build manager binary
23
- manager : generate fmt vet
24
- go build -o bin/manager 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
25
35
26
- # Run against the configured Kubernetes cluster in ~/.kube/config
27
- run : generate fmt vet manifests
28
- go run ./main.go
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 )
29
38
30
- # Install CRDs into a cluster
31
- install : manifests
32
- kustomize build config/crd | kubectl apply -f -
39
+ # #@ Development
33
40
34
- # Deploy controller in the configured Kubernetes cluster in ~/.kube/config
35
- deploy : manifests
36
- cd config/manager && kustomize edit set image controller=${IMG}
37
- kustomize build config/default | kubectl apply -f -
41
+ manifests : controller-gen # # Generate WebhookConfiguration, ClusterRole and CustomResourceDefinition objects.
42
+ $(CONTROLLER_GEN ) $(CRD_OPTIONS ) rbac:roleName=manager-role webhook paths=" ./..." output:crd:artifacts:config=config/crd/bases
38
43
39
- # Generate manifests e.g. CRD, RBAC etc.
40
- manifests :
41
- go run $(CONTROLLER_GEN ) $(CRD_OPTIONS ) rbac:roleName=manager-role webhook paths=" ./..." output:crd:artifacts:config=config/crd/bases
44
+ generate : controller-gen # # Generate code containing DeepCopy, DeepCopyInto, and DeepCopyObject method implementations.
45
+ $(CONTROLLER_GEN ) object:headerFile=" hack/boilerplate.go.txt" paths=" ./..."
42
46
43
- # Run go fmt against code
44
- fmt :
47
+ fmt : # # Run go fmt against code.
45
48
go fmt ./...
46
49
47
- # Run go vet against code
48
- vet :
50
+ vet : # # Run go vet against code.
49
51
go vet ./...
50
52
51
- # Generate code
52
- generate :
53
- go run $(CONTROLLER_GEN ) object:headerFile=./hack/boilerplate.go.txt paths=" ./..."
53
+ ENVTEST_ASSETS_DIR = $(shell pwd) /testbin
54
+ ENVTEST = $(shell pwd) /bin/setup-envtest
55
+ ENVTEST_K8S_VERSION ?= 1.26.1
56
+
57
+ testenv :
58
+ mkdir -p ${ENVTEST_ASSETS_DIR}
59
+ $(call go-get-tool,$(ENVTEST ) ,sigs.k8s.io/controller-runtime/tools/setup-envtest@latest)
54
60
55
- # Create api directory. Circumvents issue with kubebuilder without CRDs
56
- api :
57
- mkdir api
61
+ test : manifests generate fmt vet testenv # # Run tests.
62
+ KUBEBUILDER_ASSETS=" $( shell ${ENVTEST} use ${ENVTEST_K8S_VERSION} --bin-dir ${ENVTEST_ASSETS_DIR} -p path) " go test ./... -coverprofile cover.out
63
+
64
+ # #@ Build
58
65
59
66
# Build the docker image
60
67
DOCKER_BIN ?= docker
61
68
VERSION ?= latest
62
69
LABELS ?= --label org.opencontainers.image.licenses="Apache-2.0" \
63
70
--label org.opencontainers.image.vendor="Google LLC" \
64
71
--label org.opencontainers.image.version="${VERSION}"
65
- docker-build : test api
66
- ${DOCKER_BIN} build ${LABELS} . -t ${IMG}
72
+
73
+ docker-build : test
74
+ ${DOCKER_BIN} build ${DOCKER_FLAGS} ${LABELS} . -t ${IMG}
67
75
68
76
# Push the docker image
69
77
docker-push :
70
- ${DOCKER_BIN} push ${IMG}
78
+ ${DOCKER_BIN} push ${DOCKER_FLAGS} ${IMG}
79
+
80
+ build : generate fmt vet # # Build manager binary.
81
+ go build -o bin/manager main.go
82
+
83
+ run : manifests generate fmt vet # # Run a controller from your host.
84
+ go run ./main.go
85
+
86
+ # #@ Deployment
87
+
88
+ install : manifests kustomize # # Install CRDs into the K8s cluster specified in ~/.kube/config.
89
+ $(KUSTOMIZE ) build config/crd | kubectl apply -f -
90
+
91
+ uninstall : manifests kustomize # # Uninstall CRDs from the K8s cluster specified in ~/.kube/config.
92
+ $(KUSTOMIZE ) build config/crd | kubectl delete -f -
93
+
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
+ undeploy : # # Undeploy controller from the K8s cluster specified in ~/.kube/config.
99
+ $(KUSTOMIZE ) build config/default | kubectl delete -f -
100
+
101
+
102
+ CONTROLLER_GEN = $(shell pwd) /bin/controller-gen
103
+ controller-gen : # # Download controller-gen locally if necessary.
104
+ $(call go-get-tool,$(CONTROLLER_GEN ) ,sigs.k8s.io/controller-tools/cmd/[email protected] )
105
+
106
+ KUSTOMIZE = $(shell pwd) /bin/kustomize
107
+ kustomize : # # Download kustomize locally if necessary.
108
+ $(call go-get-tool,$(KUSTOMIZE ) ,sigs.k8s.io/kustomize/kustomize/[email protected] )
109
+
110
+ # go-get-tool will 'go get' any package $2 and install it to $1.
111
+ PROJECT_DIR := $(shell dirname $(abspath $(lastword $(MAKEFILE_LIST ) ) ) )
112
+ define go-get-tool
113
+ @[ -f $(1 ) ] || { \
114
+ set -e ;\
115
+ TMP_DIR=$$(mktemp -d ) ;\
116
+ cd $$TMP_DIR ;\
117
+ go mod init tmp ;\
118
+ echo "Downloading $(2 ) " ;\
119
+ GOBIN=$(PROJECT_DIR ) /bin go install $(2 ) ;\
120
+ rm -rf $$TMP_DIR ;\
121
+ }
122
+ endef
71
123
72
124
# Used for autoneg project releases
73
125
#
@@ -77,8 +129,9 @@ RELEASE_IMG ?= ghcr.io/googlecloudplatform/gke-autoneg-controller/gke-autoneg-co
77
129
78
130
# Make deployment manifests but do not deploy
79
131
autoneg-manifests : manifests
80
- cd config/manager && kustomize edit set image controller=${RELEASE_IMG} :${VERSION}
81
- kustomize build config/default > deploy/autoneg.yaml
132
+ cd config/manager && $(KUSTOMIZE ) edit set image controller=${RELEASE_IMG} :${VERSION}
133
+ cp hack/boilerplate.bash.txt deploy/autoneg.yaml
134
+ $(KUSTOMIZE ) build config/default >> deploy/autoneg.yaml
82
135
83
136
# Make release image
84
137
release-image : docker-build
0 commit comments