-
Notifications
You must be signed in to change notification settings - Fork 9
/
Makefile
413 lines (372 loc) · 11.8 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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
# SPDX-License-Identifier: Apache-2.0
# capture the current date we build the application from
BUILD_DATE = $(shell date +%Y-%m-%dT%H:%M:%SZ)
# set the filename for the api spec
SPEC_FILE = api-spec.json
# check if a git commit sha is already set
ifndef GITHUB_SHA
# capture the current git commit sha we build the application from
GITHUB_SHA = $(shell git rev-parse HEAD)
endif
# check if a git tag is already set
ifndef GITHUB_TAG
# capture the current git tag we build the application from
GITHUB_TAG = $(shell git describe --tag --abbrev=0)
endif
# create a list of linker flags for building the golang application
LD_FLAGS = -X github.com/go-vela/worker/version.Commit=${GITHUB_SHA} -X github.com/go-vela/worker/version.Date=${BUILD_DATE} -X github.com/go-vela/worker/version.Tag=${GITHUB_TAG}
# Misc settings for the generating the Kubernetes CRD for the Kubernetes Runtime
K8S_CRD_OUTPUT_PKG=github.com/go-vela/worker/runtime/kubernetes/generated
K8S_CRD_INPUT_PKG=github.com/go-vela/worker/runtime/kubernetes/apis
K8S_CRD_CLIENTSET_PKG_NAME=clientset
K8S_CRD_CLIENTSET_NAME_VERSIONED=versioned
K8S_CRD_GROUP=vela
K8S_CRD_VERSION=v1alpha1
# The `clean` target is intended to clean the workspace
# and prepare the local changes for submission.
#
# Usage: `make clean`
.PHONY: clean
clean: tidy vet fmt fix
# The `restart` target is intended to destroy and
# create the local Docker compose stack.
#
# Usage: `make restart`
.PHONY: restart
restart: down up
# The `up` target is intended to create
# the local Docker compose stack.
#
# Usage: `make up`
.PHONY: up
up: build compose-up
# The `down` target is intended to destroy
# the local Docker compose stack.
#
# Usage: `make down`
.PHONY: down
down: compose-down
# The `tidy` target is intended to clean up
# the Go module files (go.mod & go.sum).
#
# Usage: `make tidy`
.PHONY: tidy
tidy:
@echo
@echo "### Tidying Go module"
@go mod tidy
# The `vet` target is intended to inspect the
# Go source code for potential issues.
#
# Usage: `make vet`
.PHONY: vet
vet:
@echo
@echo "### Vetting Go code"
@go vet ./...
# The `fmt` target is intended to format the
# Go source code to meet the language standards.
#
# Usage: `make fmt`
.PHONY: fmt
fmt:
@echo
@echo "### Formatting Go Code"
@go fmt ./...
# The `fix` target is intended to rewrite the
# Go source code using old APIs.
#
# Usage: `make fix`
.PHONY: fix
fix:
@echo
@echo "### Fixing Go Code"
@go fix ./...
# The `test` target is intended to run
# the tests for the Go source code.
#
# Usage: `make test`
.PHONY: test
test:
@echo
@echo "### Testing Go Code"
@go test ./...
# The `test-cover` target is intended to run
# the tests for the Go source code and then
# open the test coverage report.
#
# Usage: `make test-cover`
.PHONY: test-cover
test-cover:
@echo
@echo "### Creating test coverage report"
@go test -covermode=atomic -coverprofile=coverage.out ./...
@echo
@echo "### Opening test coverage report"
@go tool cover -html=coverage.out
# The `build` target is intended to compile
# the Go source code into a binary.
#
# Usage: `make build`
.PHONY: build
build:
@echo
@echo "### Building release/vela-worker binary"
GOOS=linux CGO_ENABLED=0 \
go build -a \
-ldflags '${LD_FLAGS}' \
-o release/vela-worker \
github.com/go-vela/worker/cmd/vela-worker
# The `build-static` target is intended to compile
# the Go source code into a statically linked binary.
#
# Usage: `make build-static`
.PHONY: build-static
build-static:
@echo
@echo "### Building static release/vela-worker binary"
GOOS=linux CGO_ENABLED=0 \
go build -a \
-ldflags '-s -w -extldflags "-static" ${LD_FLAGS}' \
-o release/vela-worker \
github.com/go-vela/worker/cmd/vela-worker
# The `build-static-ci` target is intended to compile
# the Go source code into a statically linked binary
# when used within a CI environment.
#
# Usage: `make build-static-ci`
.PHONY: build-static-ci
build-static-ci:
@echo
@echo "### Building CI static release/vela-worker binary"
@go build -a \
-ldflags '-s -w -extldflags "-static" ${LD_FLAGS}' \
-o release/vela-worker \
github.com/go-vela/worker/cmd/vela-worker
# The `check` target is intended to output all
# dependencies from the Go module that need updates.
#
# Usage: `make check`
.PHONY: check
check: check-install
@echo
@echo "### Checking dependencies for updates"
@go list -u -m -json all | go-mod-outdated -update
# The `check-direct` target is intended to output direct
# dependencies from the Go module that need updates.
#
# Usage: `make check-direct`
.PHONY: check-direct
check-direct: check-install
@echo
@echo "### Checking direct dependencies for updates"
@go list -u -m -json all | go-mod-outdated -direct
# The `check-full` target is intended to output
# all dependencies from the Go module.
#
# Usage: `make check-full`
.PHONY: check-full
check-full: check-install
@echo
@echo "### Checking all dependencies for updates"
@go list -u -m -json all | go-mod-outdated
# The `check-install` target is intended to download
# the tool used to check dependencies from the Go module.
#
# Usage: `make check-install`
.PHONY: check-install
check-install:
@echo
@echo "### Installing psampaz/go-mod-outdated"
@go get -u github.com/psampaz/go-mod-outdated
# The `bump-deps` target is intended to upgrade
# non-test dependencies for the Go module.
#
# Usage: `make bump-deps`
.PHONY: bump-deps
bump-deps: check
@echo
@echo "### Upgrading dependencies"
@go get -u ./...
# The `bump-deps-full` target is intended to upgrade
# all dependencies for the Go module.
#
# Usage: `make bump-deps-full`
.PHONY: bump-deps-full
bump-deps-full: check
@echo
@echo "### Upgrading all dependencies"
@go get -t -u ./...
# The `pull` target is intended to pull all
# images for the local Docker compose stack.
#
# Usage: `make pull`
.PHONY: pull
pull:
@echo
@echo "### Pulling images for docker-compose stack"
@docker-compose pull
# The `compose-up` target is intended to build and create
# all containers for the local Docker compose stack.
#
# Usage: `make compose-up`
.PHONY: compose-up
compose-up:
@echo
@echo "### Creating containers for docker-compose stack"
@docker-compose -f docker-compose.yml up -d --build
# The `compose-down` target is intended to destroy
# all containers for the local Docker compose stack.
#
# Usage: `make compose-down`
.PHONY: compose-down
compose-down:
@echo
@echo "### Destroying containers for docker-compose stack"
@docker-compose -f docker-compose.yml down
# The `spec-install` target is intended to install the
# the needed dependencies to generate the api spec.
#
# Tools used:
# - go-swagger (https://goswagger.io/install.html)
# - jq (https://stedolan.github.io/jq/download/)
# - sponge (part of moreutils - https://packages.debian.org/sid/moreutils)
#
# Limit use of this make target to CI.
# Debian-based environment is assumed.
#
# Usage: `make spec-install`
.PHONY: spec-install
spec-install:
$(if $(shell command -v apt-get 2> /dev/null),,$(error 'apt-get' not found - install jq, sponge, and go-swagger manually))
@echo
@echo "### Installing utilities (jq and sponge)"
@apt-get update
@apt-get install -y jq moreutils
@echo "### Downloading and installing go-swagger"
@curl -o /usr/local/bin/swagger -L "https://github.com/go-swagger/go-swagger/releases/download/v0.30.2/swagger_linux_amd64"
@chmod +x /usr/local/bin/swagger
# The `spec-gen` target is intended to create an api-spec
# using go-swagger (https://goswagger.io)
#
# Usage: `make spec-gen`
.PHONY: spec-gen
spec-gen:
@echo
@echo "### Generating api spec using go-swagger"
@swagger generate spec -m --exclude github.com/docker/docker/api/types --exclude-tag definitions/Step -o ${SPEC_FILE}
@echo "### ${SPEC_FILE} created successfully"
# The `spec-validate` target is intended to validate
# an api-spec using go-swagger (https://goswagger.io)
#
# Usage: `make spec-validate`
.PHONY: spec-validate
spec-validate:
@echo
@echo "### Validating api spec using go-swagger"
@swagger validate ${SPEC_FILE}
# The `spec-version-update` target is intended to update
# the api-spec version in the generated api-spec
# using the latest git tag.
#
# Usage: `make spec-version-update`
.PHONY: spec-version-update
spec-version-update: APPS = jq sponge
spec-version-update:
$(foreach app,$(APPS),\
$(if $(shell command -v $(app) 2> /dev/null),,$(error skipping update of spec version - '$(app)' not found)))
@echo
@echo "### Updating api-spec version"
@jq '.info.version = "$(subst v,,${GITHUB_TAG})"' ${SPEC_FILE} | sponge ${SPEC_FILE}
# The `spec` target will call spec-gen, spec-version-update
# and spec-validate to create and validate an api-spec.
#
# Usage: `make spec`
.PHONY: spec
spec: spec-gen spec-version-update spec-validate
# The `lint` target is intended to lint the
# Go source code with golangci-lint.
#
# Usage: `make lint`
.PHONY: lint
lint:
@echo
@echo "### Linting Go Code"
@golangci-lint run ./...
# The `lintfix` target is intended to lint the
# Go source code with golangci-lint and apply
# any fixes that can be automatically applied.
#
# Usage: `make lintfix`
.PHONY: lintfix
lintfix:
@echo
@echo "### Fixing Go code with linter"
@golangci-lint run ./... --fix
# The `crd-gen` target is intended to create a k8s CRD client
# for the kubernetes runtime using k8s.io/code-generator
#
# Usage: `make crd-gen`
.PHONY: crd-client-gen
crd-client-gen: controller-gen client-gen
$(eval TMP := $(shell mktemp -d))
@echo
@echo "### Generating CRD deepcopy funcs using sig.k8s.io/controller-tools"
$(CONTROLLER_GEN) \
object:headerFile="runtime/kubernetes/codegen/header.go.txt" \
paths="${K8S_CRD_INPUT_PKG}/${K8S_CRD_GROUP}/${K8S_CRD_VERSION}"
@echo "### Generating CRD clientset using k8s.io/code-generator"
@echo "Generating clientset for ${K8S_CRD_GROUP}:${K8S_CRD_VERSION} at ${K8S_CRD_OUTPUT_PKG}/${K8S_CRD_CLIENTSET_PKG_NAME}"
$(CLIENT_GEN) \
--clientset-name "${K8S_CRD_CLIENTSET_NAME_VERSIONED}" \
--input-base "" \
--input \
"${K8S_CRD_INPUT_PKG}/${K8S_CRD_GROUP}/${K8S_CRD_VERSION}" \
--output-package \
"${K8S_CRD_OUTPUT_PKG}/${K8S_CRD_CLIENTSET_PKG_NAME}" \
--output-base "${TMP}" \
--go-header-file runtime/kubernetes/codegen/header.go.txt
@echo "### Copying generated files"
rm -rf runtime/kubernetes/generated/clientset
cp -r ${TMP}/github.com/go-vela/worker/runtime/kubernetes/* runtime/kubernetes/
rm -rf $(TMP)
@echo "### CRD clientset created successfully"
# The `crd-manifest` target will call crd-gen to create a k8s crd
# for the kubernetes runtime.
#
# Usage: `make crd`
.PHONY: crd-manifest
crd-manifest: controller-gen ## Generate CustomResourceDefinition object.
@echo
@echo "### Generating CRD manifest using sig.k8s.io/controller-tools"
@echo "Generating CRD manifest in runtime/kubernetes/generated"
$(CONTROLLER_GEN) crd paths="./..." output:crd:artifacts:config=runtime/kubernetes/generated
@echo "### CRD manifest created successfully"
# The `crd` target will call crd-client-gen and crd-manifest
# to create a k8s CRD for the kubernetes runtime.
#
# Usage: `make crd`
.PHONY: crd
crd: crd-client-gen crd-manifest
CONTROLLER_GEN = $(shell pwd)/bin/controller-gen
.PHONY: controller-gen
controller-gen: ## Download controller-gen locally if necessary.
$(call go-get-tool,$(CONTROLLER_GEN),sigs.k8s.io/controller-tools/cmd/[email protected])
CLIENT_GEN = $(shell pwd)/bin/client-gen
.PHONY: client-gen
client-gen: ## Download client-gen locally if necessary.
$(eval K8S_LIB_VERSION := $(shell go mod graph | grep 'github.com/go-vela/worker k8s.io/client-go@' | sed 's/.*@//'))
$(call go-get-tool,$(CLIENT_GEN),k8s.io/code-generator/cmd/client-gen@$(K8S_LIB_VERSION))
# go-get-tool will 'go get' any package $2 and install it to $1.
PROJECT_DIR := $(shell dirname $(abspath $(lastword $(MAKEFILE_LIST))))
define go-get-tool
@[ -f $(1) ] || { \
set -e ;\
TMP_DIR=$$(mktemp -d) ;\
cd $$TMP_DIR ;\
go mod init tmp ;\
echo "Downloading $(2)" ;\
GOBIN=$(PROJECT_DIR)/bin go get $(2) ;\
rm -rf $$TMP_DIR ;\
}
endef