This repository has been archived by the owner on Jan 28, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 48
/
Copy pathMakefile
314 lines (239 loc) · 10.7 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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
# Timestamp for image tags
timestamp := $(shell /bin/date "+%Y%m%d-%H%M%S")
# Image URL to use all building/pushing image targets
IMG ?= controller:${timestamp}
# MockAPI image URL to use all building/pushing image targets
MOCKAPI_IMG ?= mockapi:${timestamp}
# MockAPI image URL to use all building/pushing image targets
LOCUST_IMG ?= locust:${timestamp}
# Default namespace for the installation
LOCUST_FILE ?= "behaviours/scenario1_run_submit_delete.py"
# Produce CRDs that work back to Kubernetes 1.11 (no version conversion)
CRD_OPTIONS ?= "crd:trivialVersions=true"
# Prometheus helm installation name
PROMETHEUS_NAME ?= "prom-azure-databricks-operator"
# Default kind cluster name
KIND_CLUSTER_NAME ?= "azure-databricks-operator"
# Default namespace for the installation
OPERATOR_NAMESPACE ?= "azure-databricks-operator-system"
# 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
# Run tests
test: generate fmt vet manifests lint
rm -rf cover.* cover
mkdir -p cover
TEST_USE_EXISTING_CLUSTER=false go test ./api/... ./controllers/... -coverprofile cover.out.tmp
cat cover.out.tmp | grep -v "_generated.deepcopy.go" > cover.out
gocov convert cover.out > cover.json
gocov-xml < cover.json > cover.xml
gocov-html < cover.json > cover/index.html
rm -f cover.out cover.out.tmp cover.json
# Run tests with existing cluster
test-existing: generate fmt vet manifests lint
rm -rf cover.* cover
mkdir -p cover
TEST_USE_EXISTING_CLUSTER=true go test ./api/... ./controllers/... -coverprofile cover.out.tmp
cat cover.out.tmp | grep -v "_generated.deepcopy.go" > cover.out
gocov convert cover.out > cover.json
gocov-xml < cover.json > cover.xml
gocov-html < cover.json > cover/index.html
rm -f cover.out cover.out.tmp cover.json
# Build manager binary
manager: generate fmt lint vet
go build -o bin/manager main.go
# Run against the configured Kubernetes cluster in ~/.kube/config
run: generate fmt lint vet manifests
go run ./main.go
# Install CRDs into a cluster
install: manifests
kustomize build config/crd | kubectl apply -f -
# Uninstall CRDs from a cluster
uninstall: manifests
kustomize build config/crd | kubectl delete -f -
create-namespace:
@echo "$(shell tput setaf 10)$(shell tput bold)Creating ${OPERATOR_NAMESPACE} namespace if doesn't exist $(shell tput sgr0)"
-kubectl create namespace ${OPERATOR_NAMESPACE}
# Verify namespace was successfully created
kubectl get namespace azure-databricks-operator-system
create-dbrickssettings-secret:
@echo "$(shell tput setaf 10)$(shell tput bold)Creating dbrickssettings secret if doesn't exist $(shell tput sgr0)"
-kubectl --namespace ${OPERATOR_NAMESPACE} \
create secret generic dbrickssettings \
--from-literal=DatabricksHost="${DATABRICKS_HOST}" \
--from-literal=DatabricksToken="${DATABRICKS_TOKEN}"
# Verify secret was created
kubectl get secret dbrickssettings -n azure-databricks-operator-system
# Deploy controller in the configured Kubernetes cluster in ~/.kube/config
deploy: create-namespace create-dbrickssettings-secret manifests
@echo "$(shell tput setaf 10)$(shell tput bold)Deploying the operator $(shell tput sgr0)"
cd config/manager && kustomize edit set image controller=${IMG}
kustomize build config/default | kubectl apply -f -
kustomize build config/default > operatorsetup.yaml
# Generate manifests e.g. CRD, RBAC etc.
manifests: controller-gen
$(CONTROLLER_GEN) $(CRD_OPTIONS) rbac:roleName=manager-role webhook paths="./..." output:crd:artifacts:config=config/crd/bases
# Run go fmt against code
fmt:
find . -name '*.go' | grep -v -E 'vendor|.gocache' | xargs gofmt -s -w
# Run go vet against code
vet:
go vet ./...
# Run linting
lint:
GO111MODULE=on golangci-lint run
# Generate code
generate: controller-gen
$(CONTROLLER_GEN) object:headerFile=./hack/boilerplate.go.txt paths="./..."
# Build the docker image
docker-build:
@echo "$(shell tput setaf 10)$(shell tput bold)Building docker image for the operator $(shell tput sgr0)"
docker build . -t ${IMG} ${ARGS}
@echo "updating kustomize image patch file for manager resource"
cd config/manager && kustomize edit set image controller=${IMG}
# Push the docker image
docker-push:
docker push ${IMG}
# 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/controller-gen@v0.2.4 ;\
rm -rf $$CONTROLLER_GEN_TMP_DIR ;\
}
CONTROLLER_GEN=$(GOBIN)/controller-gen
else
CONTROLLER_GEN=$(shell which controller-gen)
endif
delete-kindcluster:
@echo "$(shell tput setaf 1)$(shell tput bold)Deleting kind cluster if running $(shell tput sgr0)"
-kind delete cluster --name ${KIND_CLUSTER_NAME}
create-kindcluster: delete-kindcluster
@echo "$(shell tput setaf 10)$(shell tput bold)Creating kind cluster $(shell tput sgr0)"
kind create cluster --name ${KIND_CLUSTER_NAME}
set-kindcluster: install-kind
make create-kindcluster
kubectl cluster-info
make deploy-kindcluster
make install
make install-prometheus
deploy-image-to-kind:
@echo "$(shell tput setaf 10)$(shell tput bold)Load operator image into kind $(shell tput sgr0)"
kind load docker-image $(IMG) --loglevel "debug" --name ${KIND_CLUSTER_NAME}
# Deploy controller
deploy-kindcluster: docker-build deploy-image-to-kind deploy
install-kind:
ifeq (,$(shell which kind))
@echo "installing kind"
curl -Lo ./kind "https://github.com/kubernetes-sigs/kind/releases/download/v0.7.0/kind-$(shell uname)-amd64"
chmod +x ./kind
mv ./kind /usr/local/bin/kind
else
@echo "kind has been installed"
endif
install-kubebuilder:
ifeq (,$(shell which kubebuilder))
@echo "installing kubebuilder"
# download kubebuilder and extract it to tmp
curl -sL https://go.kubebuilder.io/dl/2.2.0/$(shell go env GOOS)/$(shell go env GOARCH) | tar -xz -C /tmp/
# move to a long-term location and put it on your path
# (you'll need to set the KUBEBUILDER_ASSETS env var if you put it somewhere else)
mv /tmp/kubebuilder_2.2.0_$(shell go env GOOS)_$(shell go env GOARCH) /usr/local/kubebuilder
export PATH=$PATH:/usr/local/kubebuilder/bin
else
@echo "kubebuilder has been installed"
endif
install-kustomize:
ifeq (,$(shell which kustomize))
@echo "installing kustomize"
# download kustomize
curl -sL https://github.com/kubernetes-sigs/kustomize/releases/download/kustomize%2Fv3.4.0/kustomize_v3.4.0_$(shell go env GOOS)_$(shell go env GOARCH).tar.gz | tar -xz -C /tmp/
mv /tmp/kustomize /usr/local/kubebuilder/bin/kustomize
# set permission
chmod a+x /usr/local/kubebuilder/bin/kustomize
$(shell which kustomize)
else
@echo "kustomize has been installed"
endif
install-prometheus:
@echo "$(shell tput setaf 10)$(shell tput bold)Installing Prometheus in cluster $(shell tput sgr0)"
# install prometheus (and set to monitor all namespaces in our kind cluster)
helm install ${PROMETHEUS_NAME} stable/prometheus-operator --set prometheus.prometheusSpec.serviceMonitorSelectorNilUsesHelmValues=false
@echo "prometheus has been installed"
install-test-dependency:
go get -u github.com/jstemmer/go-junit-report \
&& go get github.com/axw/gocov/gocov \
&& go get github.com/AlekSi/gocov-xml \
&& go get github.com/onsi/ginkgo/ginkgo \
&& go get golang.org/x/tools/cmd/cover \
&& go get -u github.com/matm/gocov-html
build-mock-api:
go build -o bin/mock-databricks-api ./mockapi
run-mock-api:
go run ./mockapi
test-mock-api: lint
go test ./mockapi/...
docker-build-mock-api:
@echo "$(shell tput setaf 10)$(shell tput bold)Building mockapi docker image $(shell tput sgr0)"
docker build -t ${MOCKAPI_IMG} -f mockapi/Dockerfile .
docker-push-mock-api: docker-build
docker push ${IMG}
apply-manifests-mock-api:
@echo "$(shell tput setaf 10)$(shell tput bold)Deploying mockapi $(shell tput sgr0)"
cat ./mockapi/manifests/deployment.yaml | sed "s|mockapi:latest|${MOCKAPI_IMG}|" | kubectl apply -f -
kubectl apply -f ./mockapi/manifests/service.yaml
kind-load-image-mock-api: docker-build-mock-api
@echo "$(shell tput setaf 10)$(shell tput bold)Loading mockapi image into kind $(shell tput sgr0)"
kind load docker-image ${MOCKAPI_IMG} --name ${KIND_CLUSTER_NAME} -v 1
deploy-mock-api:kind-load-image-mock-api apply-manifests-mock-api
kind-deploy-mock-api: create-kindcluster install-prometheus deploy-mock-api
deploy-locust:
@echo "$(shell tput setaf 10)$(shell tput bold)Deploying Locust $(shell tput sgr0)"
# Delete locust pod if already running
-kubectl delete job locust-loadtest
docker build -t ${LOCUST_IMG} -f locust/Dockerfile .
kind load docker-image ${LOCUST_IMG} --name ${KIND_CLUSTER_NAME} -v 1
# do some magic
cat ./locust/manifests/deployment.yaml | sed "s|locust:latest|${LOCUST_IMG}|" | sed "s|behaviours/scenario1_run_submit_delete.py'|${LOCUST_FILE}' ${LOCUST_ARGS}|" | kubectl apply -f -
kind-deploy-locust: create-kindcluster install-prometheus deploy-locust
format-locust:
black .
lint-locust:
black ./locust --check
test-locust: lint-locust
pip install -e ./locust -q
pytest
port-forward:
@echo "$(shell tput setaf 10)$(shell tput bold)Set up port-forwarding $(shell tput sgr0)"
./hack/portforwards.sh
create-db-mock-secret: create-namespace
@echo "$(shell tput setaf 10)$(shell tput bold)Creating mock api databricks secret $(shell tput sgr0)"
kubectl --namespace ${OPERATOR_NAMESPACE} \
create secret generic dbrickssettings \
--from-literal=DatabricksHost="http://databricks-mock-api.databricks-mock-api:8080" \
--from-literal=DatabricksToken="dummy"
deploy-cluster-for-load-testing: create-kindcluster install-prometheus create-db-mock-secret deploy-kindcluster deploy-mock-api deploy-locust
@echo "$(shell tput setaf 10)$(shell tput bold)Deploying grafana dashboards $(shell tput sgr0)"
# deploy service monitor
cat ./config/prometheus/monitor.yaml | sed "s/namespace: system/namespace: ${OPERATOR_NAMESPACE}/g" | kubectl apply -f -
# deploy graphs
kubectl apply -f ./config/prometheus/grafana-dashboard-configmap.yaml
kubectl apply -f ./config/prometheus/grafana-dashboard-load-test-configmap.yaml
kubectl apply -f ./config/prometheus/grafana-dashboard-mockapi-configmap.yaml
run-load-testing: deploy-cluster-for-load-testing port-forward
run-load-testing-auto-start: set-auto-start run-load-testing
@echo "$(shell tput setaf 10)$(shell tput bold)Verify load tests $(shell tput sgr0)"
go run hack/verify_load_tests/main.go
set-auto-start:
# Args passed to locust must be in CSV format as passed in "command" section of yaml doc
$(eval LOCUST_ARGS=,'--no-web', '-c', '25', '-r', '0.08', '--run-time', '7m')
test-local: test-locust test-mock-api run-load-testing-auto-start