-
Notifications
You must be signed in to change notification settings - Fork 1
/
Makefile
223 lines (176 loc) · 7.98 KB
/
Makefile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
SHELL := $(shell which bash)
VERBOSE_SHORT_ARG := $(if $(filter $(VERBOSE),true),-v,)
VERBOSE_LONG_ARG := $(if $(filter $(VERBOSE),true),--verbose,)
ROOT_DIR := $(shell dirname $(abspath $(lastword $(MAKEFILE_LIST))))
SCRIPTS_DIR := $(ROOT_DIR)/hack/scripts
ENVFILE := $(SCRIPTS_DIR)/env.sh
RELEASE_SUPPORT := $(SCRIPTS_DIR)/release-support.sh
CLUSTER_PROVIDER := $(shell . $(ENVFILE) ; echo $${KUBE_CLUSTER_PROVIDER})
OPERATOR_SDK := operator-sdk
IMAGE_LOADER := $(SCRIPTS_DIR)/load-image.sh -p $(CLUSTER_PROVIDER) $(VERBOSE_SHORT_ARG)
ifdef CLUSTER_NAME
IMAGE_LOADER += -c $(CLUSTER_NAME)
endif
# Current Operator version
VERSION ?= $(shell . $(RELEASE_SUPPORT) ; getVersion)
# Current Bundle Version
BUNDLE_VERSION ?= $(shell . $(ENVFILE) ; echo $${XROOTD_OPERATOR_VERSION})
# Default bundle image tag
BUNDLE_IMG ?= $(shell . $(ENVFILE) ; echo $${XROOTD_OPERATOR_BUNDLE_IMAGE})
# Options for 'bundle-build'
ifneq ($(origin CHANNELS), undefined)
BUNDLE_CHANNELS := --channels=$(CHANNELS)
endif
ifneq ($(origin DEFAULT_CHANNEL), undefined)
BUNDLE_DEFAULT_CHANNEL := --default-channel=$(DEFAULT_CHANNEL)
endif
BUNDLE_METADATA_OPTS ?= $(BUNDLE_CHANNELS) $(BUNDLE_DEFAULT_CHANNEL)
# Image URL to use all building/pushing image targets
IMG ?= $(shell . $(ENVFILE) ; echo $${XROOTD_OPERATOR_IMAGE})
# Produce CRDs that work with apiextensions.k8s.io/v1
CRD_OPTIONS ?= "crd:crdVersions=v1"
ENVTEST_ASSETS_DIR=$(shell pwd)/testbin
# Get the currently used golang install path (in GOPATH/bin, unless GOBIN is set)
ifeq (,$(shell go env GOBIN))
GOBIN=$(shell go env GOPATH)/bin
else
GOBIN=$(shell go env GOBIN)
endif
all: manager
##@ Controller
manager: generate fmt vet ## Build manager binary
go build -o bin/manager main.go
run: generate fmt vet manifests ## Run against the configured Kubernetes cluster in ~/.kube/config
go run ./main.go
install: manifests kustomize ## Install CRDs into a cluster
$(KUSTOMIZE) build config/crd | kubectl apply -f -
uninstall: manifests kustomize ## Uninstall CRDs from a cluster
$(KUSTOMIZE) build config/crd | kubectl delete -f -
build: docker-build ## Build the Operator Image and load it in your cluster
@echo "Loading operator image in '$(if $(CLUSTER_NAME),$(CLUSTER_NAME),$(CLUSTER_PROVIDER))' cluster"
@$(IMAGE_LOADER) ${IMG}:${VERSION}
deploy: manifests kustomize ## Deploy controller in the configured Kubernetes cluster in ~/.kube/config
cd config/manager && $(KUSTOMIZE) edit set image controller=${IMG}:${VERSION}
$(KUSTOMIZE) build config/default | kubectl apply -f -
undeploy: manifests kustomize ## Uninstalls the controller and CRDs in the configured Kubernetes cluster in ~/.kube/config
$(KUSTOMIZE) build config/default | kubectl delete -f -
##@ Tests
test: generate fmt vet manifests ## Run tests
@mkdir -p ${ENVTEST_ASSETS_DIR}
@test -f ${ENVTEST_ASSETS_DIR}/setup-envtest.sh || curl -sSLo ${ENVTEST_ASSETS_DIR}/setup-envtest.sh https://raw.githubusercontent.com/kubernetes-sigs/controller-runtime/master/hack/setup-envtest.sh
@{ \
set -e; \
source ${ENVTEST_ASSETS_DIR}/setup-envtest.sh; \
fetch_envtest_tools $(ENVTEST_ASSETS_DIR); \
setup_envtest_env $(ENVTEST_ASSETS_DIR); \
echo "....... Running tests ......."; \
go test ./... -coverprofile cover.out; # -ginkgo.randomizeAllSpecs -ginkgo.trace -ginkgo.progress; \
}
test-e2e: ## Run e2e tests
@echo "....... Running e2e tests ......."
@{ \
set -e -o pipefail; \
$(SCRIPTS_DIR)/run-e2e-tests.sh $(VERBOSE_SHORT_ARG) | \
while IFS= read -r line; do printf '%s %s\n' "$$(date -u +'%T')" "$$line"; done \
}
##@ Code
fmt: ## Run go fmt against code
go fmt ./...
vet: ## Run go vet against code
go vet ./...
generate: controller-gen ## Generate code
$(CONTROLLER_GEN) object:headerFile="hack/boilerplate.go.txt" paths="./..."
manifests: controller-gen ## Generate manifests e.g. CRD, RBAC etc.
$(CONTROLLER_GEN) $(CRD_OPTIONS) rbac:roleName=manager-role webhook paths="./..." output:crd:artifacts:config=config/crd/bases
docker-build: ## Build the docker image
docker build . -t ${IMG}:${VERSION}
docker tag ${IMG}:${VERSION} ${IMG}:latest
docker-push: ## Push the docker image
docker push ${IMG}
##@ OLM
.PHONY: bundle
bundle: kustomize manifests ## Generate bundle manifests and metadata, then validate generated files.
operator-sdk generate kustomize manifests -q
cd config/manager && $(KUSTOMIZE) edit set image controller=$(IMG)
$(KUSTOMIZE) build config/manifests | operator-sdk generate bundle -q --overwrite --version $(BUNDLE_VERSION) $(BUNDLE_METADATA_OPTS)
operator-sdk bundle validate ./bundle
.PHONY: bundle-build
bundle-build: ## Build the bundle image.
docker build -f bundle.Dockerfile -t $(BUNDLE_IMG) .
##@ Versioning
.PHONY: version
version: ## Shows the current release version based on version/version.go
@. $(ENVFILE) ; echo $$XROOTD_OPERATOR_VERSION
.PHONY: version-image
version-image: .release ## Shows the current release tag based on the directory content.
@. $(RELEASE_SUPPORT); getVersion
tag-patch-release: VERSION := $(shell . $(RELEASE_SUPPORT); nextPatchLevel)
tag-patch-release: .release tag
tag-minor-release: VERSION := $(shell . $(RELEASE_SUPPORT); nextMinorLevel)
tag-minor-release: .release tag
tag-major-release: VERSION := $(shell . $(RELEASE_SUPPORT); nextMajorLevel)
tag-major-release: .release tag
patch-bump: tag-patch-release ### Increments the patch release level, build and push to registry.
@echo "Patch release: $(VERSION)"
minor-bump: tag-minor-release ### Increments the minor release level, build and push to registry.
@echo "Minor release: $(VERSION)"
major-bump: tag-major-release ### Increments the major release level, build and push to registry.
@echo "Major release: $(VERSION)"
tag: TAG=$(shell . $(RELEASE_SUPPORT); getTag $(VERSION))
tag: check-status
@. $(RELEASE_SUPPORT) ; ! tagExists $(TAG) || (echo "ERROR: tag $(TAG) for version $(VERSION) already tagged in git" >&2 && exit 1) ;
@. $(RELEASE_SUPPORT) ; setRelease $(VERSION)
git add -u
git commit -m "chore: Version bump → $(VERSION)" ;
git tag $(TAG) ;
# @ if [ -n "$(shell git remote -v)" ] ; then git push --tags ; else echo 'no remote to push tags to' ; fi
check-status: ## Checks whether there are outstanding changes.
@. $(RELEASE_SUPPORT) ; ! hasChanges || (echo "ERROR: there are still outstanding changes" >&2 && exit 1) ;
check-release: .release ## Checks whether the current directory matches the tagged release in git.
@. $(RELEASE_SUPPORT) ; tagExists $(TAG) || (echo "ERROR: version not yet tagged in git. make [minor,major,patch]-bump." >&2 && exit 1) ;
@. $(RELEASE_SUPPORT) ; ! differsFromRelease $(TAG) || (echo "ERROR: current directory differs from tagged $(TAG). make [minor,major,patch]-release." ; exit 1)
##@ Misc.
help: ## Display this help
@echo -e "Usage:\n make \033[36m<target>\033[0m"
@awk 'BEGIN {FS = ":.*##"}; \
/^[a-zA-Z0-9_-]+:.*?##/ { printf " \033[36m%-15s\033[0m %s\n", $$1, $$2 } \
/^##@/ { printf "\n\033[1m%s\033[0m\n", substr($$0, 5) } ' $(MAKEFILE_LIST)
sample: kustomize ## Install sample manifests
$(KUSTOMIZE) build manifests/base | kubectl apply -f -
# find or download controller-gen
# download controller-gen if necessary
controller-gen:
ifeq (, $(shell which controller-gen))
@{ \
set -e ;\
CONTROLLER_GEN_TMP_DIR=$$(mktemp -d) ;\
cd $$CONTROLLER_GEN_TMP_DIR ;\
go mod init tmp ;\
go get sigs.k8s.io/controller-tools/cmd/[email protected] ;\
rm -rf $$CONTROLLER_GEN_TMP_DIR ;\
}
CONTROLLER_GEN=$(GOBIN)/controller-gen
else
CONTROLLER_GEN=$(shell which controller-gen)
endif
# find or download kustomize if necessary
kustomize:
ifeq (, $(shell which kustomize))
@{ \
set -e ;\
KUSTOMIZE_GEN_TMP_DIR=$$(mktemp -d) ;\
cd $$KUSTOMIZE_GEN_TMP_DIR ;\
go mod init tmp ;\
go get sigs.k8s.io/kustomize/kustomize/[email protected] ;\
rm -rf $$KUSTOMIZE_GEN_TMP_DIR ;\
}
KUSTOMIZE=$(GOBIN)/kustomize
else
KUSTOMIZE=$(shell which kustomize)
endif
.release:
@echo "release=0.0.0" > .release
@echo "tag=v0.0.0" >> .release
@echo 'pre_tag_command=sed -i -e "s/^Version = .*/Version = \"@@RELEASE@@\"/" version/version.go' >> .release
@echo INFO: .release created
@cat .release