-
Notifications
You must be signed in to change notification settings - Fork 49
/
Makefile
409 lines (343 loc) · 13.6 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
SHELL := /bin/bash
GIT_VERSION ?= v0.1.0
GIT_COMMIT ?= $(shell git rev-parse HEAD)
BUILD_DATE ?= $(shell date -u +'%Y-%m-%dT%H:%M:%SZ')
GIT_MODULE ?= opendev.org/airship/airshipctl/pkg/version
LDFLAGS += -X ${GIT_MODULE}.gitVersion=${GIT_VERSION}
LDFLAGS += -X ${GIT_MODULE}.gitCommit=${GIT_COMMIT}
LDFLAGS += -X ${GIT_MODULE}.buildDate=${BUILD_DATE}
GO_FLAGS := -ldflags '-extldflags "-static"' -tags=netgo -trimpath
GO_FLAGS += -ldflags '$(LDFLAGS)'
# Get the currently used golang install path (in GOPATH/bin, unless GOBIN is set)
ifeq (,$(shell go env GOBIN 2> /dev/null))
GOBIN = $(shell go env GOPATH 2> /dev/null)/bin
else
GOBIN = $(shell go env GOBIN 2> /dev/null)
endif
# Produce CRDs that work back to Kubernetes 1.21.2
CRD_OPTIONS ?= crd:crdVersions=v1
TOOLBINDIR := tools/bin
# linting
LINTER := $(TOOLBINDIR)/golangci-lint
LINTER_CONFIG := .golangci.yaml
# docker
DOCKER_MAKE_TARGET := build
DOCKER_CMD_FLAGS :=
# docker image options
DOCKER_REGISTRY ?= quay.io
DOCKER_FORCE_CLEAN ?= true
DOCKER_IMAGE_NAME ?= airshipctl
DOCKER_IMAGE_PREFIX ?= airshipit
DOCKER_IMAGE_TAG ?= latest
DOCKER_IMAGE ?= $(DOCKER_REGISTRY)/$(DOCKER_IMAGE_PREFIX)/$(DOCKER_IMAGE_NAME):$(DOCKER_IMAGE_TAG)
DOCKER_TARGET_STAGE ?= release
PUBLISH ?= false
# use this variable for image labels added in internal build process
COMMIT ?= $(shell git rev-parse HEAD)
LABEL ?= org.airshipit.build=community
LABEL += --label "org.opencontainers.image.revision=$(COMMIT)"
LABEL += --label "org.opencontainers.image.created=$(shell date --rfc-3339=seconds --utc)"
LABEL += --label "org.opencontainers.image.title=$(DOCKER_IMAGE_NAME)"
# go options
PKG ?= ./...
TESTS ?= .
TEST_FLAGS ?=
COVER_FLAGS ?=
COVER_PROFILE ?= cover.out
COVER_EXCLUDE ?= (zz_generated|errors)
# proxy options
PROXY ?= http://proxy.foo.com:8000
NO_PROXY ?= localhost,127.0.0.1,.svc.cluster.local
USE_PROXY ?= false
# docker build flags
DOCKER_CMD_FLAGS += --network=host
DOCKER_CMD_FLAGS += --force-rm=$(DOCKER_FORCE_CLEAN)
ifeq ($(USE_PROXY), true)
DOCKER_CMD_FLAGS += --build-arg http_proxy=$(PROXY)
DOCKER_CMD_FLAGS += --build-arg https_proxy=$(PROXY)
DOCKER_CMD_FLAGS += --build-arg HTTP_PROXY=$(PROXY)
DOCKER_CMD_FLAGS += --build-arg HTTPS_PROXY=$(PROXY)
DOCKER_CMD_FLAGS += --build-arg no_proxy=$(NO_PROXY)
DOCKER_CMD_FLAGS += --build-arg NO_PROXY=$(NO_PROXY)
endif
ifneq ($(strip $(GOPROXY)),)
DOCKER_CMD_FLAGS += --build-arg GOPROXY=$(strip $(GOPROXY))
endif
# Godoc server options
GD_PORT ?= 8080
# Documentation location
DOCS_DIR ?= docs
# document validation options
UNAME != uname
export KIND_URL ?= https://kind.sigs.k8s.io/dl/v0.8.1/kind-$(UNAME)-amd64
KUBECTL_VERSION ?= v1.21.2
export KUBECTL_URL ?= https://storage.googleapis.com/kubernetes-release/release/${KUBECTL_VERSION}/bin/linux/amd64/kubectl
.PHONY: depend
depend:
@go mod download
.PHONY: build
.PHONY: install
install: depend
install:
@CGO_ENABLED=0 go install .
# Core of build logic
BIN_DIR := bin
BIN_SRC_DIR := krm-functions
BINS := airshipctl $(subst $(BIN_SRC_DIR)/,,$(wildcard $(BIN_SRC_DIR)/*))
IMGS := $(BINS)
# This section sets the settings for different subcomponents
# airshipctl is a special case - we need to override it manually:
# its makefile target for image is 'docker-image' - others have
# docker-image-<name of component> targets
airshipctl_IMG_TGT_NAME:=docker-image
# its main.go is in the root of repo - others have main.go in
# $(BIN_SRC_DIR)/<name of component>/main.go
airshipctl_FROM_PATH:=.
# and its Dockerfile is also in the root of repo - others have Dockerfile in
# $(BIN_SRC_DIR)/<name of component>/Dockerfile
docker-image_DOCKERFILE:=Dockerfile
# kubeval-validator, toolbox and toolbox-virsh don't depend on
# airshipctl repo. Their Dockerfiles don't
# need to be called from the root of the repo.
applier_IS_INDEPENDED:=true
kubeval-validator_IS_INDEPENDED:=true
clusterctl_IS_INDEPENDED:=true
clusterctl-v0.3_IS_INDEPENDED:=true
toolbox-virsh_IS_INDEPENDED:=true
# in addition toolbox-virsh docker image needs toolbox docker image to be built first
docker-image-clusterctl-v0.3_DEPENDENCY:=docker-image-clusterctl
docker-image-toolbox-virsh_DEPENDENCY:=docker-image-toolbox
# The template that generates targets for creating binaries per component:
# Targets will be generated only for components that depend on airshipctl repo (part of that go module)
# Note: expressions with ?= won't be executed if the values of that variable was already set to it.
# Using that syntax it's possible to build values overrides for components.
# Note 2: $$ is needed to instruct make-engine that variable should be used after template rendering.
# When template is rendered all $ will be rendered in the template and $$ will be converted to $, e.g.
# if we call map_binary_defaults_tmpl for airshipctl $1 will be converted to 'airshipctl' and we'll get
# ifneq ($(airshipctl_IS_INDEPENDED),true)
# airshipctl_FROM_PATH?=$(BIN_SRC_DIR)/airshipctl/main.go
# ...
# since we defining airshipctl_FROM_PATH above, and ?= is used in the 2nd line
# airshipctl_FROM_PATH will stay the same as it was defined above.
define map_binary_defaults_tmpl
ifneq ($$($1_IS_INDEPENDED),true)
$1_FROM_PATH?=$$(BIN_SRC_DIR)/$1/main.go
$$(warning Adding dynamic target $$(BIN_DIR)/$1)
$$(BIN_DIR)/$1: $$($1_FROM_PATH) depend
@CGO_ENABLED=0 go build -o $$@ $$(GO_FLAGS) $$<
$$(warning Adding dynamic target $1)
.PHONY: $1
$1: $$(BIN_DIR)/$1
build: $1
endif
endef
map_binary_defaults = $(eval $(call map_binary_defaults_tmpl,$1))
# Go through all components and generate binary targets for each of them
$(foreach bin,$(BINS),$(call map_binary_defaults,$(bin)))
.PHONY: images
.PHONY: images-publish
# The template that generates targets for creating images per components
# There is a special logic to handle per-components overrides
# 2 targets will be generated per component: docker-image-<component name> (possible to override)
# and docker-image-<component name>-publish
define map_image_defaults_tmpl
$1_IMG_TGT_NAME?=docker-image-$1
$$($1_IMG_TGT_NAME)_IMG_TITLE?=$1
$$($1_IMG_TGT_NAME)_IMG_TAG?=$$(DOCKER_IMAGE_TAG)
$$($1_IMG_TGT_NAME)_DOCKERTGT?=$$(DOCKER_TARGET_STAGE)
$$($1_IMG_TGT_NAME)_DOCKERFILE?=$$(BIN_SRC_DIR)/$1/Dockerfile
$$($1_IMG_TGT_NAME)_MAKETGT?=$$(BIN_DIR)/$1
ifeq ($$($1_IS_INDEPENDED),true)
$$($1_IMG_TGT_NAME)_DOCKERROOT?=$$(BIN_SRC_DIR)/$1
else
$$($1_IMG_TGT_NAME)_DOCKERROOT?=.
endif
ifneq ($1,airshipctl)
ifneq ($$(origin DOCKER_BASE_PLUGINS_GO_IMAGE), undefined)
$$($1_IMG_TGT_NAME)_BASE_GO_IMAGE?=$$(DOCKER_BASE_PLUGINS_GO_IMAGE)
endif
endif
$$($1_IMG_TGT_NAME)_BASE_GO_IMAGE?=$$(DOCKER_BASE_GO_IMAGE)
ifneq ($$(strip $$($$($1_IMG_TGT_NAME)_BASE_GO_IMAGE)),)
$$($1_IMG_TGT_NAME)_BUILD_ARG += GO_IMAGE=$$($$($1_IMG_TGT_NAME)_BASE_GO_IMAGE)
endif
ifneq ($1,airshipctl)
ifneq ($$(origin DOCKER_BASE_PLUGINS_BUILD_IMAGE), undefined)
$$($1_IMG_TGT_NAME)_BASE_BUILD_IMAGE?=$$(DOCKER_BASE_PLUGINS_BUILD_IMAGE)
endif
endif
$$($1_IMG_TGT_NAME)_BASE_BUILD_IMAGE?=$$(DOCKER_BASE_BUILD_IMAGE)
ifneq ($$(strip $$($$($1_IMG_TGT_NAME)_BASE_BUILD_IMAGE)),)
$$($1_IMG_TGT_NAME)_BUILD_ARG += BUILD_IMAGE=$$($$($1_IMG_TGT_NAME)_BASE_BUILD_IMAGE)
endif
ifneq ($1,airshipctl)
ifneq ($$(origin DOCKER_BASE_PLUGINS_RELEASE_IMAGE), undefined)
$$($1_IMG_TGT_NAME)_BASE_RELEASE_IMAGE?=$$(DOCKER_BASE_PLUGINS_RELEASE_IMAGE)
endif
endif
$$($1_IMG_TGT_NAME)_BASE_RELEASE_IMAGE?=$$(DOCKER_BASE_RELEASE_IMAGE)
ifneq ($$(strip $$($$($1_IMG_TGT_NAME)_BASE_RELEASE_IMAGE)),)
$$($1_IMG_TGT_NAME)_BUILD_ARG += RELEASE_IMAGE=$$($$($1_IMG_TGT_NAME)_BASE_RELEASE_IMAGE)
endif
ifeq ($1,clusterctl-v0.3)
$$($1_IMG_TGT_NAME)_IMG_TAG=v0.3
$$($1_IMG_TGT_NAME)_IMG_TITLE=clusterctl
endif
$$(warning Adding dynamic target $$($1_IMG_TGT_NAME))
.PHONY: $$($1_IMG_TGT_NAME)
$$($1_IMG_TGT_NAME): $$($$($1_IMG_TGT_NAME)_DEPENDENCY)
docker build $$($$($1_IMG_TGT_NAME)_DOCKERROOT) $$(DOCKER_CMD_FLAGS)\
--file $$($$($1_IMG_TGT_NAME)_DOCKERFILE) \
--label $$(LABEL) \
--label "org.opencontainers.image.revision=$$(COMMIT)" \
--label "org.opencontainers.image.created=$$(shell date --rfc-3339=seconds --utc)" \
--label "org.opencontainers.image.title=$$($$($1_IMG_TGT_NAME)_IMG_TITLE)" \
--target $$($$($1_IMG_TGT_NAME)_DOCKERTGT) \
$$(addprefix --build-arg ,$$($$($1_IMG_TGT_NAME)_BUILD_ARG)) \
--build-arg MAKE_TARGET=$$($$($1_IMG_TGT_NAME)_MAKETGT) \
--tag $$(DOCKER_REGISTRY)/$$(DOCKER_IMAGE_PREFIX)/$$($$($1_IMG_TGT_NAME)_IMG_TITLE):$$($$($1_IMG_TGT_NAME)_IMG_TAG) \
$$(foreach tag,$$(DOCKER_IMAGE_EXTRA_TAGS),--tag $$(DOCKER_REGISTRY)/$$(DOCKER_IMAGE_PREFIX)/$1:$$(tag) )
ifeq ($$(PUBLISH), true)
@docker push $$(DOCKER_REGISTRY)/$$(DOCKER_IMAGE_PREFIX)/$$($$($1_IMG_TGT_NAME)_IMG_TITLE):$$($$($1_IMG_TGT_NAME)_IMG_TAG)
endif
images: $$($1_IMG_TGT_NAME)
$$(warning Adding dynamic target $$($1_IMG_TGT_NAME)-publish)
.PHONY: $$($1_IMG_TGT_NAME)-publish
$$($1_IMG_TGT_NAME)-publish: $$($1_IMG_TGT_NAME)
@docker push $$(DOCKER_REGISTRY)/$$(DOCKER_IMAGE_PREFIX)/$1:$$(DOCKER_IMAGE_TAG)
images-publish: $$($1_IMG_TGT_NAME)-publish
endef
map_image_defaults = $(eval $(call map_image_defaults_tmpl,$1))
# go through components and render the template
$(foreach img,$(IMGS),$(call map_image_defaults,$(img)))
.PHONY: test
test: lint
test: cover
test: check-copyright
.PHONY: unit-tests
unit-tests: TESTFLAGS += -race -v
unit-tests:
@echo "Performing unit test step..."
@go test -run $(TESTS) $(PKG) $(TESTFLAGS) $(COVER_FLAGS)
@echo "All unit tests passed"
.PHONY: cover
cover: COVER_FLAGS = -covermode=atomic -coverprofile=fullcover.out
cover: unit-tests
@grep -vE "$(COVER_EXCLUDE)" fullcover.out > $(COVER_PROFILE)
@./tools/coverage_check $(COVER_PROFILE)
.PHONY: fmt
fmt: lint
.PHONY: lint
lint: tidy
lint: $(LINTER)
@echo "Performing linting step..."
@./tools/whitespace_linter
@./$(LINTER) run --config $(LINTER_CONFIG)
@echo "Linting completed successfully"
.PHONY: tidy
tidy:
@echo "Checking that go.mod is up to date..."
@./tools/gomod_check
@echo "go.mod is up to date"
.PHONY: golint
golint:
@./tools/golint
.PHONY: print-docker-image-tag
print-docker-image-tag:
@echo "$(DOCKER_IMAGE)"
.PHONY: docker-image-test-suite
docker-image-test-suite: docker-image_MAKETGT = "cover update-golden generate check-git-diff"
docker-image-test-suite: docker-image_DOCKERTGT = builder
docker-image-test-suite: docker-image
.PHONY: docker-image-unit-tests
docker-image-unit-tests: docker-image_MAKETGT = cover
docker-image-unit-tests: docker-image_DOCKERTGT = builder
docker-image-unit-tests: docker-image
.PHONY: docker-image-lint
docker-image-lint: docker-image_MAKETGT = "lint check-copyright"
docker-image-lint: docker-image_DOCKERTGT = builder
docker-image-lint: docker-image
.PHONY: docker-image-golint
docker-image-golint: docker-image_MAKETGT = golint
docker-image-golint: docker-image_DOCKERTGT = builder
docker-image-golint: docker-image
.PHONY: docker-image-check-manifests
docker-image-check-manifests: docker-image_MAKETGT = "generate manifests check-git-diff"
docker-image-check-manifests: docker-image_DOCKERTGT = builder
docker-image-check-manifests: docker-image
.PHONY: clean
clean:
@rm -fr $(BIN_DIR)
@rm -fr $(COVER_PROFILE)
.PHONY: docs
docs:
tox
.PHONY: godoc
godoc:
@go install golang.org/x/tools/cmd/godoc
@echo "Follow this link to package documentation: http://localhost:${GD_PORT}/pkg/opendev.org/airship/airshipctl/"
@godoc -http=":${GD_PORT}"
.PHONY: cli-docs
cli-docs:
@echo "Generating CLI documentation..."
@go run $(DOCS_DIR)/tools/generate_cli_docs.go
@echo "CLI documentation generated"
.PHONY: releasenotes
releasenotes:
@echo "TODO"
$(TOOLBINDIR):
mkdir -p $(TOOLBINDIR)
$(LINTER): $(TOOLBINDIR)
./tools/install_linter
.PHONY: update-golden
update-golden: delete-golden
update-golden: TESTFLAGS += -update
update-golden: PKG = opendev.org/airship/airshipctl/cmd/...
update-golden: unit-tests
update-golden: cli-docs
# The delete-golden target is a utility for update-golden
.PHONY: delete-golden
delete-golden:
@find . -type f -name "*.golden" -delete
# Used by gates after unit-tests and update-golden targets to ensure no files are deleted.
.PHONY: check-git-diff
check-git-diff:
@./tools/git_diff_check
# add-copyright is a utility to add copyright header to missing files
.PHONY: add-copyright
add-copyright:
@./tools/add_license.sh
# check-copyright is a utility to check if copyright header is present on all files
.PHONY: check-copyright
check-copyright:
@./tools/check_copyright
# Validate YAMLs for all sites
.PHONY: validate-docs
validate-docs:
@./tools/validate_docs
# Validate all URL references in documentation work
.PHONY: dead-link-linter
dead-link-linter:
@./tools/dead-link-linter
# Generate code
generate: controller-gen
$(CONTROLLER_GEN) object:headerFile="tools/license_go.txt" paths="./..."
# 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
# Generate manifests e.g. CRD, RBAC etc.
manifests: controller-gen
find manifests/function/airshipctl-schemas/ -type f -not -name 'kustomization.yaml' -delete && $(CONTROLLER_GEN) $(CRD_OPTIONS) rbac:roleName=manager-role webhook paths="./..." output:crd:artifacts:config=manifests/function/airshipctl-schemas