forked from vmware/cluster-api-provider-cloud-director
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Makefile
313 lines (232 loc) · 10.1 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
CONTROLLER_GEN_VERSION := 0.14.0
CONVERSION_GEN_VERSION := 0.29.1
CONTROLLER_RUNTIME_VERSION := 0.8.3
LINT_VERSION := 1.51.2
GOSEC_VERSION := "v2.16.0"
KUSTOMIZE_VERSION := 4.5.7
GOLANGCI_EXIT_CODE ?= 1
# Produce CRDs that work back to Kubernetes 1.11 (no version conversion)
CRD_OPTIONS ?= crd
# Set PATH to pick up cached tools. The additional 'sed' is required for cross-platform support of quoting the args to 'env'
SHELL := /usr/bin/env PATH=$(shell echo $(GITROOT)/bin:${PATH} | sed 's/ /\\ /g') bash
# 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
GITCOMMIT ?= $(shell git rev-parse --short HEAD 2>/dev/null)
GITROOT ?= $(shell git rev-parse --show-toplevel)
CAPVCD_IMG := cluster-api-provider-cloud-director
ARTIFACT_IMG := capvcd-manifest-airgapped
VERSION ?= $(shell cat $(GITROOT)/release/version)
PLATFORM ?= linux/amd64
OS ?= linux
ARCH ?= amd64
CGO ?= 0
KUSTOMIZE ?= bin/kustomize
CONTROLLER_GEN ?= bin/controller-gen
CONVERSION_GEN ?= bin/conversion-gen
GOLANGCI_LINT ?= bin/golangci-lint
GOSEC ?= bin/gosec
SHELLCHECK ?= bin/shellcheck
TEST_PACKAGES := ./...
.PHONY: all
all: vendor lint dev
.PHONY: capvcd
capvcd: vendor lint docker-build-capvcd ## Run checks, and build capvcd docker image.
##@ General
# The help target prints out all targets with their descriptions organized
# beneath their categories. The categories are represented by '##@' and the
# target descriptions by '##'. The awk commands is responsible for reading the
# entire set of makefiles included in this invocation, looking for lines of the
# file as xyz: ## something, and then pretty-format the target and help. Then,
# if there's a line with ##@ something, that gets pretty-printed as a category.
# More info on the usage of ANSI control characters for terminal formatting:
# https://en.wikipedia.org/wiki/ANSI_escape_code#SGR_parameters
# More info on the awk command:
# http://linuxcommand.org/lc3_adv_awk.php
.PHONY: help
help: ## Display this help.
@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)
##@ Development
.PHONY: vendor
vendor: ## Update go mod dependencies.
go mod edit -go=1.22
go mod tidy -compat=1.22
go mod vendor
.PHONY: fmt
fmt: ## Run go fmt against code.
go fmt ./...
.PHONY: vet
vet: ## Run go vet against code.
go vet ./...
.PHONY: golangci-lint
golangci-lint: $(GOLANGCI_LINT) ## Run golangci-lint against code.
$(GOLANGCI_LINT) run --issues-exit-code $(GOLANGCI_EXIT_CODE)
.PHONY: gosec
gosec: $(GOSEC) ## Run gosec against code.
$(GOSEC) -conf .gosec.json ./...
.PHONY: shellcheck
shellcheck: $(SHELLCHECK) ## Run shellcheck against code.
find . -name '*.*sh' -not -path '*/vendor/*' | xargs $(SHELLCHECK) --color
.PHONY: lint
lint: lint-deps golangci-lint gosec shellcheck ## Run golangci-lint, gosec, shellcheck.
.PHONY: lint-fix
lint-fix: $(GOLANGCI_LINT)
$(GOLANGCI_LINT) run --fix
.PHONY: manifests
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
.PHONY: generate
generate: controller-gen ## Run controller-gen.
$(CONTROLLER_GEN) object:headerFile="hack/boilerplate.go.txt" paths="./..."
.PHONY: conversion
conversion: conversion-gen ## Run conversion-gen.
rm -f $(GITROOT)/api/*/zz_generated.conversion.*
$(CONVERSION_GEN) \
--input-dirs=./api/v1alpha4,./api/v1beta1,./api/v1beta2 \
--build-tag=ignore_autogenerated_conversions \
--output-file-base=zz_generated.conversion \
--go-header-file=./boilerplate.go.txt
.PHONY: autogen-files
autogen-files: manifests generate conversion release-manifests
##@ Build
.PHONY: build
build: ## Build CAPVCD binary. To be used from within a Dockerfile
@mkdir -p bin
GOOS=$(OS) GOARCH=$(ARCH) CGO_ENABLED=$(CGO) go build -ldflags "-s -w -X github.com/vmware/cluster-api-provider-cloud-director/release.Version=$(VERSION)" -o bin/cluster-api-provider-cloud-director main.go
.PHONY: test
test: manifests generate ## Run tests.
@mkdir -p bin/testbin
test -f bin/testbin/setup-envtest.sh || \
curl -sSLo bin/testbin/setup-envtest.sh https://raw.githubusercontent.com/kubernetes-sigs/controller-runtime/v$(CONTROLLER_RUNTIME_VERSION)/hack/setup-envtest.sh; \
source bin/testbin/setup-envtest.sh; \
fetch_envtest_tools bin/testbin; \
setup_envtest_env "$(shell pwd)/bin/testbin"; \
go test $(TEST_PACKAGES) -coverprofile cover.out
.PHONY: manager
manager: generate ## Build manager binary.
@mkdir -p bin
go build -o bin/manager main.go
.PHONY: run
run: manifests generate ## Run a controller from your host.
go run ./main.go
.PHONY: docker-build-capvcd
docker-build-capvcd: generate release-manifests build
docker build \
--platform $(PLATFORM) \
--file Dockerfile \
--tag $(CAPVCD_IMG):$(VERSION) \
--build-arg CAPVCD_BUILD_DIR=bin \
.
.PHONY: docker-build-artifacts
docker-build-artifacts: release-prep
docker build \
--platform $(PLATFORM) \
--file artifacts/Dockerfile \
--tag $(ARTIFACT_IMG):$(VERSION) \
.
.PHONY: docker-build
docker-build: docker-build-capvcd docker-build-artifacts ## Build CAPVCD docker image and artifact image.
##@ Deploymet
.PHONY: install
install: manifests kustomize ## Install CRDs into the K8s cluster specified in ~/.kube/config.
$(KUSTOMIZE) build config/crd | kubectl apply -f -
.PHONY: uninstall
uninstall: manifests kustomize ## Uninstall CRDs from the K8s cluster specified in ~/.kube/config.
$(KUSTOMIZE) build config/crd | kubectl delete -f -
.PHONY: deploy
deploy: manifests kustomize ## Deploy controller to the K8s cluster specified in ~/.kube/config.
cd config/manager && kustomize edit set image controller=${CAPVCD_IMG}
$(KUSTOMIZE) build config/default | kubectl apply -f -
.PHONY: teardown
teardown: manifests kustomize ## Teardown controller from the K8s cluster specified in ~/.kube/config.
$(KUSTOMIZE) build config/default | kubectl delete -f -
##@ Gobuild
.PHONY: dev-build
dev-build: VERSION := $(VERSION)-${BUILD_TAG}-$(GITCOMMIT)
dev-build: gobuild
.PHONY: rc-build
rc-build: gobuild
.PHONY: gobuild
gobuild: vendor generate release-manifests release-prep build docker-build docker-archive publish
.PHONY: docker-archive
docker-archive:
mkdir -p build/docker
docker save -o build/docker/$(CAPVCD_IMG)_$(VERSION).tar $(CAPVCD_IMG):$(VERSION)
docker save -o build/docker/$(ARTIFACT_IMG)_$(VERSION).tar $(ARTIFACT_IMG):$(VERSION)
gzip build/docker/$(CAPVCD_IMG)_$(VERSION).tar
gzip build/docker/$(ARTIFACT_IMG)_$(VERSION).tar
.PHONY: publish
publish:
cp -R build/docker ${PUBLISH_DIR}
##@ Publish
.PHONY: dev
dev: VERSION := $(VERSION)-$(GITCOMMIT)
dev: git-check release ## Build development images and push to registry.
.PHONY: release
release: docker-build docker-push ## Build release images and push to registry.
.PHONY: release-manifests
release-manifests: kustomize ## Generate release manifests e.g. CRD, RBAC etc.
sed -e "s/__VERSION__/$(VERSION)/g" config/manager/manager.yaml.template > config/manager/manager.yaml
$(KUSTOMIZE) build config/default > templates/infrastructure-components.yaml
.PHONY: release-prep
release-prep: ## Generate BOM and dependencies files.
sed -e "s/__VERSION__/$(VERSION)/g" artifacts/bom.json.template > artifacts/bom.json
sed -e "s/__VERSION__/$(VERSION)/g" artifacts/dependencies.txt.template > artifacts/dependencies.txt
.PHONY: docker-push-capvcd
docker-push-capvcd: # Push capvcd image to registry.
docker tag $(CAPVCD_IMG):$(VERSION) projects-stg.registry.vmware.com/vmware-cloud-director/$(CAPVCD_IMG):$(VERSION)
docker push projects-stg.registry.vmware.com/vmware-cloud-director/$(CAPVCD_IMG):$(VERSION)
.PHONY: docker-push-artifacts
docker-push-artifacts: # Push artifacts image to registry
docker tag $(ARTIFACT_IMG):$(VERSION) projects-stg.registry.vmware.com/vmware-cloud-director/$(ARTIFACT_IMG):$(VERSION)
docker push projects-stg.registry.vmware.com/vmware-cloud-director/$(ARTIFACT_IMG):$(VERSION)
.PHONY: docker-push
docker-push: docker-push-capvcd docker-push-artifacts ## Push images to container registry.
##@ Dependencies
.PHONY: deps
deps: lint-deps kustomize controller-gen conversion-gen ## Download all dependencies locally.
.PHONY: lint-deps
lint-deps: $(GOLANGCI_LINT) $(GOSEC) $(SHELLCHECK) ## Download lint dependencies locally.
.PHONY: kustomize
kustomize: $(KUSTOMIZE) ## Download kustomize binary locally.
.PHONY: controller-gen
controller-gen: $(CONTROLLER_GEN) ## Download controller-gen binary locally.
.PHONY: conversion-gen
conversion-gen: $(CONVERSION_GEN) ## Download conversion-gen binary locally.
.PHONY: clean
clean:
rm -rf bin
rm artifacts/bom.json artifacts/dependencies.txt
$(KUSTOMIZE):
@mkdir -p bin
@cd bin && \
set -ex -o pipefail && \
wget "https://raw.githubusercontent.com/kubernetes-sigs/kustomize/master/hack/install_kustomize.sh"; \
chmod +x ./install_kustomize.sh; \
./install_kustomize.sh $(KUSTOMIZE_VERSION) .; \
rm -f ./install_kustomize.sh;
$(CONTROLLER_GEN):
@mkdir -p bin
@GOBIN=$(GITROOT)/bin go install sigs.k8s.io/controller-tools/cmd/controller-gen@v${CONTROLLER_GEN_VERSION}
$(CONVERSION_GEN):
@mkdir -p bin
@GOBIN=$(GITROOT)/bin go install k8s.io/code-generator/cmd/conversion-gen@v${CONVERSION_GEN_VERSION}
$(GOLANGCI_LINT):
@mkdir -p bin
@set -o pipefail && \
wget -q -O - https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(GITROOT)/bin v$(LINT_VERSION);
$(GOSEC):
@mkdir -p bin
@GOBIN=$(GITROOT)/bin go install github.com/securego/gosec/v2/cmd/gosec@${GOSEC_VERSION}
$(SHELLCHECK):
@mkdir -p bin
@cd bin && \
set -o pipefail && \
wget -q -O - https://github.com/koalaman/shellcheck/releases/download/stable/shellcheck-stable.$$(uname).x86_64.tar.xz | tar -xJv --strip-components=1 shellcheck-stable/shellcheck && \
chmod +x $(GITROOT)/bin/shellcheck
.PHONY: git-check
git-check:
@git diff --exit-code --quiet api/ artifacts/ config/ controllers/ pkg/ main.go Dockerfile || (echo 'Uncommitted changes found. Please commit your changes before proceeding.'; exit 1)