-
Notifications
You must be signed in to change notification settings - Fork 7
/
Makefile
344 lines (259 loc) · 11.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
# Build parameters.
CGO_ENABLED=0
LD_FLAGS="-extldflags '-static'"
# Go parameters.
GOCMD=env GO111MODULE=on CGO_ENABLED=$(CGO_ENABLED) go
GOTEST=$(GOCMD) test -covermode=atomic
GOGET=$(GOCMD) get
GOMOD=$(GOCMD) mod
GORUN=$(GOCMD) run
GOBUILD=$(GOCMD) build -v -ldflags $(LD_FLAGS) -trimpath
CC_TEST_REPORTER_ID=6e107e510c5479f40b0ce9166a254f3f1ee0bc547b3e48281bada1a5a32bb56d
GOLANGCI_LINT_VERSION=v1.61.0
BIN_PATH=$$HOME/bin
GO_PACKAGES=./...
GO_TESTS=^.*$
INTEGRATION_IMAGE=flexkube/libflexkube-integration
INTEGRATION_CMD=docker run -it --rm -v /tmp:/tmp -v /run:/run -v /home/core/libflexkube:/usr/src/libflexkube -v /home/core/go:/go -v /home/core/.ssh:/home/core/.ssh -v /home/core/.cache:/root/.cache -w /usr/src/libflexkube --net host $(INTEGRATION_IMAGE)
E2E_IMAGE=flexkube/libflexkube-e2e
E2E_CMD=docker run -it --rm -v /home/core/libflexkube:/root/libflexkube -v /home/core/.ssh:/root/.ssh -v /home/core/go:/go -w /root/libflexkube --net host --entrypoint /bin/bash -e TF_VAR_flatcar_channel=$(FLATCAR_CHANNEL) -e TF_VAR_controllers_count=$(CONTROLLERS) -e TF_VAR_workers_count=$(WORKERS) -e TF_VAR_nodes_cidr=$(NODES_CIDR) $(E2E_IMAGE)
BUILD_CMD=docker run -it --rm -v /home/core/libflexkube:/usr/src/libflexkube -v /home/core/go:/go -v /home/core/.cache:/root/.cache -v /tmp:/tmp -v /run:/run -w /usr/src/libflexkube $(INTEGRATION_IMAGE)
BINARY_IMAGE=flexkube/libflexkube
TERRAFORM_BIN=$(TERRAFORM_ENV) /usr/bin/terraform
CONTROLLERS=$(shell (grep CONTROLLERS .env 2>/dev/null || echo "1") | cut -d= -f2 2>/dev/null)
WORKERS=$(shell (grep WORKERS .env 2>/dev/null || echo "2") | cut -d= -f2 2>/dev/null)
NODES_CIDR=$(shell (grep NODES_CIDR .env 2>/dev/null || echo "192.168.50.0/24") | cut -d= -f2 2>/dev/null)
FLATCAR_CHANNEL=$(shell (grep FLATCAR_CHANNEL .env 2>/dev/null || echo "stable") | cut -d= -f2 2>/dev/null)
TERRAFORM_ENV=TF_VAR_flatcar_channel=$(FLATCAR_CHANNEL) TF_VAR_controllers_count=$(CONTROLLERS) TF_VAR_workers_count=$(WORKERS) TF_VAR_nodes_cidr=$(NODES_CIDR)
VAGRANTCMD=$(TERRAFORM_ENV) vagrant
COVERPROFILE=c.out
.PHONY: all
all: build build-test test lint semgrep
.PHONY: all-cover
all-cover: build build-test test-cover lint semgrep
.PHONY: build
build:
$(GOBUILD) ./cmd/...
.PHONY: build-bin
build-bin:
mkdir -p ./bin
cd bin && for i in $$(ls ../cmd); do $(GOBUILD) ../cmd/$$i; done
.PHONY: build-docker
build-docker:
docker build -t $(BINARY_IMAGE) .
.PHONY: build-integration
build-integration:
docker build -t $(INTEGRATION_IMAGE) integration
.PHONY: build-e2e
build-e2e:
docker build -t $(E2E_IMAGE) e2e
.PHONY: build-test
build-test:
$(GOTEST) -run=nope -tags integration,e2e $(GO_PACKAGES)
.PHONY: clean
clean:
rm -r ./bin $(COVERPROFILE) coverage.txt kubeconfig local-testing/resources local-testing/values local-testing/state.yaml 2>/dev/null || true
make vagrant-destroy || true
.PHONY: test
test: build-test
$(GOTEST) -run $(GO_TESTS) $(GO_PACKAGES)
.PHONY: download
download:
$(GOMOD) download
.PHONY: test-race
test-race: build-test
$(GOTEST) -run $(GO_TESTS) -race $(GO_PACKAGES)
.PHONY: test-integration
test-integration: build-test
$(GOTEST) -run $(GO_TESTS) -tags=integration $(GO_PACKAGES)
.PHONY: test-cover
test-cover: build-test
$(GOTEST) -run $(GO_TESTS) -coverprofile=$(COVERPROFILE) $(GO_PACKAGES)
.PHONY: test-integration-cover
test-integration-cover: build-test
$(GOTEST) -run $(GO_TESTS) -tags=integration -coverprofile=$(COVERPROFILE) $(GO_PACKAGES)
.PHONY: test-mutate
test-mutate: install-go-mutesting
go-mutesting --verbose $(GO_PACKAGES)
.PHONY: test-vagrant
test-vagrant:
vagrant validate --ignore-provider
.PHONY: test-terraform
test-terraform:
terraform -chdir=libvirt validate
.PHONY: test-working-tree-clean
test-working-tree-clean:
@test -z "$$(git status --porcelain)" || (echo "Commit all changes before running this target"; exit 1)
.PHONY: test-tidy
test-tidy: test-working-tree-clean
go mod tidy
@test -z "$$(git status --porcelain)" || (echo "Please run 'go mod tidy' and commit generated changes."; git diff; exit 1)
.PHONY: test-changelog
test-changelog: test-working-tree-clean
make format-changelog
@test -z "$$(git status --porcelain)" || (echo "Please run 'make format-changelog' and commit generated changes."; git diff; exit 1)
.PHONY: format-changelog
format-changelog:
changelog fmt -o CHANGELOG.md.fmt
mv CHANGELOG.md.fmt CHANGELOG.md
.PHONY: cover-browse
cover-browse:
go tool cover -html=$(COVERPROFILE)
.PHONY: test-cover-browse
test-cover-browse: test-cover cover-browse
.PHONY: test-e2e-run
test-e2e-run:
helm repo update
env $(TERRAFORM_ENV) $(GOTEST) -v -tags e2e -count 1 ./e2e/
.PHONY: test-e2e
test-e2e: test-e2e-run
.PHONY: test-local-apply
test-local-apply:
env $(TERRAFORM_ENV) $(GOTEST) -v -tags e2e -count 1 ./local-testing/
.PHONY: test-conformance
test-conformance:SHELL=/bin/bash
test-conformance:
until kubectl get nodes >/dev/null; do sleep 1; done
sonobuoy run --mode=certified-conformance || true
until sonobuoy status | grep e2e | grep complete; do timeout --foreground 10m sonobuoy logs -f || true; sleep 1; done
sonobuoy results $$(sonobuoy retrieve)
.PHONY: test-conformance-clean
test-conformance-clean:
sonobuoy delete
.PHONY: lint
lint:
golangci-lint run $(GO_PACKAGES)
.PHONY: update
update:
$(GOGET) -u $(GO_PACKAGES)
$(GOMOD) tidy
.PHONY: update-linters
update-linters:
# Remove all enabled linters.
sed -i '/^ enable:/q0' .golangci.yml
# Then add all possible linters to config.
golangci-lint linters | grep -E '^\S+:' | cut -d: -f1 | sort | sed 's/^/ - /g' | grep -v -E "($$(grep '^ disable:' -A 100 .golangci.yml | grep -E ' - \S+$$' | awk '{print $$2}' | tr \\n '|' | sed 's/|$$//g'))" >> .golangci.yml
.PHONY: test-update-linters
test-update-linters: test-working-tree-clean
make update-linters
@test -z "$$(git status --porcelain)" || (echo "Linter configuration outdated. Run 'make update-linters' and commit generated changes to fix."; exit 1)
.PHONY: codespell
codespell:
codespell -S .git,state.yaml,go.sum,terraform.tfstate,terraform.tfstate.backup,./local-testing/resources -L uptodate,decorder,complies
.PHONY: codespell-pr
codespell-pr:
git diff master..HEAD | grep -v ^- | codespell -
git log master..HEAD | codespell -
.PHONY: test-cover-upload-codecov
test-cover-upload-codecov: SHELL=/bin/bash
test-cover-upload-codecov: test-cover
test-cover-upload-codecov:
bash <(curl -s https://codecov.io/bash) -f $(COVERPROFILE)
.PHONY: test-cover-upload-codeclimate
test-cover-upload-codeclimate: test-cover
test-cover-upload-codeclimate:
env CC_TEST_REPORTER_ID=$(CC_TEST_REPORTER_ID) cc-test-reporter after-build -t gocov -p $$(go list -m)
.PHONY: test-cover-upload
test-cover-upload: test-cover-upload-codecov test-cover-upload-codeclimate
.PHONY: test-integration-cover-upload-codecov
test-integration-cover-upload-codecov: SHELL=/bin/bash
test-integration-cover-upload-codecov: test-integration-cover
test-integration-cover-upload-codecov:
bash <(curl -s https://codecov.io/bash) -f $(COVERPROFILE)
.PHONY: test-integration-cover-upload-codeclimate
test-integration-cover-upload-codeclimate: test-integration-cover
test-integration-cover-upload-codeclimate:
env CC_TEST_REPORTER_ID=$(CC_TEST_REPORTER_ID) cc-test-reporter after-build -t gocov -p $$(go list -m)
.PHONY: test-integration-cover-upload
test-integration-cover-upload: test-integration-cover-upload-codecov test-integration-cover-upload-codeclimate
.PHONY: install-golangci-lint
install-golangci-lint:
curl -sfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(BIN_PATH) $(GOLANGCI_LINT_VERSION)
.PHONY: install-cc-test-reporter
install-cc-test-reporter:
curl -L https://codeclimate.com/downloads/test-reporter/test-reporter-latest-linux-amd64 > $(BIN_PATH)/cc-test-reporter
chmod +x $(BIN_PATH)/cc-test-reporter
.PHONY: install-ci
install-ci: install-golangci-lint install-cc-test-reporter
.PHONY: install-go-mutesting
install-go-mutesting:
go install github.com/Stebalien/go-mutesting/cmd/go-mutesting@master
.PHONY: install-changelog
install-changelog:
go install github.com/rcmachado/changelog@0.7.0
.PHONY: vagrant-up
vagrant-up:
$(VAGRANTCMD) up
.PHONY: vagrant-rsync
vagrant-rsync:
$(VAGRANTCMD) rsync
.PHONY: vagrant-destroy
vagrant-destroy:
$(VAGRANTCMD) destroy --force
.PHONY: vagrant
vagrant: SHELL=/bin/bash
vagrant:
alias vagrant='$(VAGRANTCMD)'
.PHONY: vagrant-integration-build
vagrant-integration-build:
$(VAGRANTCMD) ssh -c "docker build -t $(INTEGRATION_IMAGE) libflexkube/integration"
.PHONY: vagrant-integration-run
vagrant-integration-run:
$(VAGRANTCMD) ssh -c "$(INTEGRATION_CMD) make test-integration GO_PACKAGES=$(GO_PACKAGES)"
.PHONY: vagrant-integration-shell
vagrant-integration-shell:
$(VAGRANTCMD) ssh -c "$(INTEGRATION_CMD) bash"
.PHONY: vagrant-integration
vagrant-integration: CONTROLLERS=1
vagrant-integration: WORKERS=0
vagrant-integration: vagrant-up vagrant-rsync vagrant-integration-build vagrant-integration-run
.PHONY: vagrant-build-bin
vagrant-build-bin: vagrant-integration-build
$(VAGRANTCMD) ssh -c "$(BUILD_CMD) make build-bin"
.PHONY: vagrant-e2e-build
vagrant-e2e-build:
$(VAGRANTCMD) ssh -c "$(BUILD_CMD) make build-e2e"
.PHONY: vagrant-e2e-kubeconfig
vagrant-e2e-kubeconfig:
ssh -p 2222 -i ~/.vagrant.d/insecure_private_key -o StrictHostKeyChecking=no core@127.0.0.1 sudo cat /home/core/libflexkube/e2e/kubeconfig > ./e2e/kubeconfig
chmod 0600 ./e2e/kubeconfig
.PHONY: vagrant-e2e-run
vagrant-e2e-run: vagrant-up vagrant-rsync vagrant-build-bin vagrant-e2e-build
$(VAGRANTCMD) ssh -c "$(E2E_CMD) -c 'make test-e2e-run'"
make vagrant-e2e-kubeconfig
.PHONY: vagrant-e2e-shell
vagrant-e2e-shell:
$(VAGRANTCMD) ssh -c "$(E2E_CMD)"
.PHONY: vagrant-e2e
vagrant-e2e: vagrant-e2e-run vagrant-destroy
.PHONY: vagrant-conformance-run
vagrant-conformance-run:
# Make sure static controlplane is shut down.
$(VAGRANTCMD) ssh -c "docker stop kube-apiserver kube-scheduler kube-controller-manager"
# Wait leaseDurationSeconds to make sure self-hosted kube-scheduler and kube-controller-manager takes over.
sleep 15
$(VAGRANTCMD) ssh -c "$(E2E_CMD) -c 'make test-conformance'"
.PHONY: vagrant-conformance
vagrant-conformance: vagrant-e2e-run vagrant-conformance-run vagrant-conformance-copy-results
.PHONY: vagrant-conformance-copy-results
vagrant-conformance-copy-results:
ssh core@127.0.0.1 -o StrictHostKeyChecking=no -p 2222 -i ~/.vagrant.d/insecure_private_key sudo chmod +w /home/core/libflexkube/*.tar.gz
scp -P 2222 -o StrictHostKeyChecking=no -i ~/.vagrant.d/insecure_private_key core@127.0.0.1:/home/core/libflexkube/*.tar.gz ./
.PHONY: libvirt-apply
libvirt-apply: libvirt-download-image
cd libvirt && $(TERRAFORM_BIN) init && $(TERRAFORM_BIN) apply -auto-approve
.PHONY: libvirt-destroy
libvirt-destroy:
cd libvirt && $(TERRAFORM_BIN) init && $(TERRAFORM_BIN) destroy -auto-approve
.PHONY: libvirt-download-image
libvirt-download-image:
((test -f libvirt/flatcar_production_qemu_image.img.bz2 || test -f libvirt/flatcar_production_qemu_image.img) || wget https://$(FLATCAR_CHANNEL).release.flatcar-linux.net/amd64-usr/current/flatcar_production_qemu_image.img.bz2 -O libvirt/flatcar_production_qemu_image.img.bz2) || true
(test -f libvirt/flatcar_production_qemu_image.img.bz2 && bunzip2 libvirt/flatcar_production_qemu_image.img.bz2 && rm libvirt/flatcar_production_qemu_image.img.bz2) || true
qemu-img resize libvirt/flatcar_production_qemu_image.img +5G
.PHONY: terraform-fmt
terraform-fmt:
for j in $$(for i in $$(find -name *.tf 2>/dev/null | grep -v .terraform); do dirname $$i; done | sort | uniq); do terraform fmt -check $$j; done
.PHONY: semgrep
semgrep: SEMGREP_BIN=semgrep
semgrep:
@if ! which $(SEMGREP_BIN) >/dev/null 2>&1; then echo "$(SEMGREP_BIN) binary not found, skipping extra linting"; else $(SEMGREP_BIN) --error; fi