forked from squat/kilo
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMakefile
341 lines (274 loc) · 11.3 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
export GO111MODULE=on
.PHONY: push container clean container-name container-latest push-latest fmt lint test unit gomodtidy generate crds codegen manifest manfest-latest manifest-annotate manifest manfest-latest manifest-annotate release e2e
OS ?= $(shell go env GOOS)
ARCH ?= $(shell go env GOARCH)
ALL_ARCH := amd64 arm arm64
DOCKER_ARCH := "amd64" "arm v7" "arm64 v8"
ifeq ($(OS),linux)
BINS := bin/$(OS)/$(ARCH)/kg bin/$(OS)/$(ARCH)/kgctl
else
BINS := bin/$(OS)/$(ARCH)/kgctl
endif
RELEASE_BINS := $(addprefix bin/release/kgctl-, $(addprefix linux-, $(ALL_ARCH)) darwin-amd64 darwin-arm64 windows-amd64)
PROJECT := kilo
PKG := github.com/squat/$(PROJECT)
REGISTRY ?= index.docker.io
IMAGE ?= squat/$(PROJECT)
FULLY_QUALIFIED_IMAGE := $(REGISTRY)/$(IMAGE)
TAG := $(shell git describe --abbrev=0 --tags HEAD 2>/dev/null)
COMMIT := $(shell git rev-parse HEAD)
VERSION := $(COMMIT)
ifneq ($(TAG),)
ifeq ($(COMMIT), $(shell git rev-list -n1 $(TAG)))
VERSION := $(TAG)
endif
endif
DIRTY := $(shell test -z "$$(git diff --shortstat 2>/dev/null)" || echo -dirty)
VERSION := $(VERSION)$(DIRTY)
LD_FLAGS := -buildvcs=false -ldflags '-X $(PKG)/pkg/version.Version=$(VERSION)'
SRC := $(shell find . -type f -name '*.go')
GO_FILES ?= $$(find . -name '*.go')
GO_PKGS ?= $$(go list ./...)
CONTROLLER_GEN_BINARY := bin/controller-gen
CLIENT_GEN_BINARY := bin/client-gen
DOCS_GEN_BINARY := bin/docs-gen
DEEPCOPY_GEN_BINARY := bin/deepcopy-gen
INFORMER_GEN_BINARY := bin/informer-gen
LISTER_GEN_BINARY := bin/lister-gen
STATICCHECK_BINARY := bin/staticcheck
EMBEDMD_BINARY := bin/embedmd
KIND_BINARY := $(shell pwd)/bin/kind
KUBECTL_BINARY := $(shell pwd)/bin/kubectl
BASH_UNIT := $(shell pwd)/bin/bash_unit
BASH_UNIT_FLAGS :=
BUILD_IMAGE ?= golang:1.23.0
BASE_IMAGE ?= alpine:3.20
GO_INSTALL = ./hack/go-install.sh
TOOLS_DIR=hack/tools
ROOT_DIR=$(abspath .)
TOOLS_GOBIN_DIR := $(abspath $(TOOLS_DIR))
GOBIN_DIR=$(abspath ./bin)
PATH := $(GOBIN_DIR):$(TOOLS_GOBIN_DIR):$(PATH)
# Detect the path used for the install target
ifeq (,$(shell go env GOBIN))
INSTALL_GOBIN=$(shell go env GOPATH)/bin
else
INSTALL_GOBIN=$(shell go env GOBIN)
endif
CONTROLLER_GEN_VER := v0.16.1
CONTROLLER_GEN_BIN := controller-gen
CONTROLLER_GEN := $(TOOLS_DIR)/$(CONTROLLER_GEN_BIN)-$(CONTROLLER_GEN_VER)
export CONTROLLER_GEN # so hack scripts can use it
YAML_PATCH_VER ?= v0.0.11
YAML_PATCH_BIN := yaml-patch
YAML_PATCH := $(TOOLS_DIR)/$(YAML_PATCH_BIN)-$(YAML_PATCH_VER)
export YAML_PATCH # so hack scripts can use it
OPENSHIFT_GOIMPORTS_VER := c72f1dc2e3aacfa00aece3391d938c9bc734e791
OPENSHIFT_GOIMPORTS_BIN := openshift-goimports
OPENSHIFT_GOIMPORTS := $(TOOLS_DIR)/$(OPENSHIFT_GOIMPORTS_BIN)-$(OPENSHIFT_GOIMPORTS_VER)
export OPENSHIFT_GOIMPORTS # so hack scripts can use i
$(CONTROLLER_GEN):
GOBIN=$(TOOLS_GOBIN_DIR) $(GO_INSTALL) sigs.k8s.io/controller-tools/cmd/controller-gen $(CONTROLLER_GEN_BIN) $(CONTROLLER_GEN_VER)
$(YAML_PATCH):
GOBIN=$(TOOLS_GOBIN_DIR) $(GO_INSTALL) github.com/pivotal-cf/yaml-patch/cmd/yaml-patch $(YAML_PATCH_BIN) $(YAML_PATCH_VER)
$(OPENSHIFT_GOIMPORTS):
GOBIN=$(TOOLS_GOBIN_DIR) $(GO_INSTALL) github.com/openshift-eng/openshift-goimports $(OPENSHIFT_GOIMPORTS_BIN) $(OPENSHIFT_GOIMPORTS_VER)
crds: $(CONTROLLER_GEN) $(YAML_PATCH) $(OPENSHIFT_GOIMPORTS) ## Generate crds
./hack/update-codegen-crds.sh
.PHONY: crds
tools: $(CONTROLLER_GEN) $(YAML_PATCH) ## Install tools
.PHONY: tool
codegen: crds ## Generate all
go mod download
./hack/update-codegen-clients.sh
$(MAKE) imports
.PHONY: codegen
.PHONY: imports
imports: $(OPENSHIFT_GOIMPORTS)
$(OPENSHIFT_GOIMPORTS) -m github.com/squat/kilo
tools: $(CONTROLLER_GEN) $(YAML_PATCH) $(OPENSHIFT_GOIMPORTS) ## Install tools
.PHONY: tools
build: $(BINS)
build-%:
@$(MAKE) --no-print-directory OS=$(word 1,$(subst -, ,$*)) ARCH=$(word 2,$(subst -, ,$*)) build
container-latest-%:
@$(MAKE) --no-print-directory ARCH=$* container-latest
container-%:
@$(MAKE) --no-print-directory ARCH=$* container
push-latest-%:
@$(MAKE) --no-print-directory ARCH=$* push-latest
push-%:
@$(MAKE) --no-print-directory ARCH=$* push
all-build: $(addprefix build-$(OS)-, $(ALL_ARCH))
all-container: $(addprefix container-, $(ALL_ARCH))
all-push: $(addprefix push-, $(ALL_ARCH))
all-container-latest: $(addprefix container-latest-, $(ALL_ARCH))
all-push-latest: $(addprefix push-latest-, $(ALL_ARCH))
generate: codegen crds
$(BINS): $(SRC) go.mod
@mkdir -p bin/$(word 2,$(subst /, ,$@))/$(word 3,$(subst /, ,$@))
@echo "building: $@"
@docker run --rm \
-u $$(id -u):$$(id -g) \
-v $$(pwd):/$(PROJECT) \
-w /$(PROJECT) \
$(BUILD_IMAGE) \
/bin/sh -c " \
GOARCH=$(word 3,$(subst /, ,$@)) \
GOOS=$(word 2,$(subst /, ,$@)) \
GOCACHE=/$(PROJECT)/.cache \
CGO_ENABLED=0 \
go build -o $@ \
$(LD_FLAGS) \
./cmd/$(@F)/... \
"
fmt:
@echo $(GO_PKGS)
gofmt -w -s $(GO_FILES)
lint: $(STATICCHECK_BINARY)
@echo 'go vet $(GO_PKGS)'
@vet_res=$$(GO111MODULE=on go vet $(GO_PKGS) 2>&1); if [ -n "$$vet_res" ]; then \
echo ""; \
echo "Go vet found issues. Please check the reported issues"; \
echo "and fix them if necessary before submitting the code for review:"; \
echo "$$vet_res"; \
exit 1; \
fi
@echo '$(STATICCHECK_BINARY) $(GO_PKGS)'
@lint_res=$$($(STATICCHECK_BINARY) $(GO_PKGS)); if [ -n "$$lint_res" ]; then \
echo ""; \
echo "Staticcheck found style issues. Please check the reported issues"; \
echo "and fix them if necessary before submitting the code for review:"; \
echo "$$lint_res"; \
exit 1; \
fi
@echo 'gofmt -d -s $(GO_FILES)'
@fmt_res=$$(gofmt -d -s $(GO_FILES)); if [ -n "$$fmt_res" ]; then \
echo ""; \
echo "Gofmt found style issues. Please check the reported issues"; \
echo "and fix them if necessary before submitting the code for review:"; \
echo "$$fmt_res"; \
exit 1; \
fi
unit:
go test --race ./...
test: lint unit e2e
$(KIND_BINARY):
curl -Lo $@ https://kind.sigs.k8s.io/dl/v0.25.0/kind-linux-$(ARCH)
chmod +x $@
$(KUBECTL_BINARY):
curl -Lo $@ https://dl.k8s.io/release/v1.31.0/bin/linux/$(ARCH)/kubectl
chmod +x $@
$(BASH_UNIT):
curl -Lo $@ https://raw.githubusercontent.com/pgrange/bash_unit/v2.3.1/bash_unit
chmod +x $@
e2e: container $(KIND_BINARY) $(KUBECTL_BINARY) $(BASH_UNIT) bin/$(OS)/$(ARCH)/kgctl
KILO_IMAGE=$(IMAGE):$(ARCH)-$(VERSION) KIND_BINARY=$(KIND_BINARY) KUBECTL_BINARY=$(KUBECTL_BINARY) KGCTL_BINARY=$(shell pwd)/bin/$(OS)/$(ARCH)/kgctl $(BASH_UNIT) $(BASH_UNIT_FLAGS) ./e2e/setup.sh ./e2e/multi-cluster.sh ./e2e/handlers.sh ./e2e/kgctl.sh ./e2e/teardown.sh
tmp/help.txt: bin/$(OS)/$(ARCH)/kg
mkdir -p tmp
bin//$(OS)/$(ARCH)/kg --help 2>&1 | head -n -1 > $@
docs/kg.md: $(EMBEDMD_BINARY) tmp/help.txt
$(EMBEDMD_BINARY) -w $@
website/docs/README.md: README.md
rm -rf website/static/img/graphs
find docs -type f -name '*.md' | xargs -I{} sh -c 'cat $(@D)/$$(basename {} .md) > website/{}'
find docs -type f -name '*.md' | xargs -I{} sh -c 'cat {} >> website/{}'
cat $(@D)/$$(basename $@ .md) > $@
cat README.md >> $@
cp -r docs/graphs website/static/img/
sed -i 's/\.\/docs\///g' $@
find $(@D) -type f -name '*.md' | xargs -I{} sed -i 's/\.\/\(.\+\.\(svg\|png\)\)/\/img\/\1/g' {}
sed -i 's/graphs\//\/img\/graphs\//g' $@
# The next line is a workaround until mdx, docusaurus' markdown parser, can parse links with preceding brackets.
sed -i 's/\[\]\(\[.*\](.*)\)/\[\]\1/g' website/docs/api.md
website/build/index.html: website/docs/README.md docs/api.md
yarn --cwd website install
yarn --cwd website build
container: .container-$(ARCH)-$(VERSION) container-name
.container-$(ARCH)-$(VERSION): bin/linux/$(ARCH)/kg bin/linux/$(ARCH)/kgctl Dockerfile
@i=0; for a in $(ALL_ARCH); do [ "$$a" = $(ARCH) ] && break; i=$$((i+1)); done; \
ia=""; iv=""; \
j=0; for a in $(DOCKER_ARCH); do \
[ "$$i" -eq "$$j" ] && ia=$$(echo "$$a" | awk '{print $$1}') && iv=$$(echo "$$a" | awk '{print $$2}') && break; j=$$((j+1)); \
done; \
SHA=$$(docker manifest inspect $(BASE_IMAGE) | jq '.manifests[] | select(.platform.architecture == "'$$ia'") | if .platform | has("variant") then select(.platform.variant == "'$$iv'") else . end | .digest' -r); \
docker build -t $(IMAGE):$(ARCH)-$(VERSION) --build-arg FROM=$(BASE_IMAGE)@$$SHA --build-arg GOARCH=$(ARCH) .
@docker images -q $(IMAGE):$(ARCH)-$(VERSION) > $@
container-latest: .container-$(ARCH)-$(VERSION)
@docker tag $(IMAGE):$(ARCH)-$(VERSION) $(FULLY_QUALIFIED_IMAGE):$(ARCH)-latest
@echo "container: $(IMAGE):$(ARCH)-latest"
container-name:
@echo "container: $(IMAGE):$(ARCH)-$(VERSION)"
manifest: .manifest-$(VERSION) manifest-name
.manifest-$(VERSION): Dockerfile $(addprefix push-, $(ALL_ARCH))
@docker manifest create --amend $(FULLY_QUALIFIED_IMAGE):$(VERSION) $(addsuffix -$(VERSION), $(addprefix $(FULLY_QUALIFIED_IMAGE):, $(ALL_ARCH)))
@$(MAKE) --no-print-directory manifest-annotate-$(VERSION)
@docker manifest push $(FULLY_QUALIFIED_IMAGE):$(VERSION) > $@
manifest-latest: Dockerfile $(addprefix push-latest-, $(ALL_ARCH))
@docker manifest rm $(FULLY_QUALIFIED_IMAGE):latest || echo no old manifest
@docker manifest create --amend $(FULLY_QUALIFIED_IMAGE):latest $(addsuffix -latest, $(addprefix $(FULLY_QUALIFIED_IMAGE):, $(ALL_ARCH)))
@$(MAKE) --no-print-directory manifest-annotate-latest
@docker manifest push $(FULLY_QUALIFIED_IMAGE):latest
@echo "manifest: $(IMAGE):latest"
manifest-annotate: manifest-annotate-$(VERSION)
manifest-annotate-%:
@i=0; \
for a in $(ALL_ARCH); do \
annotate=; \
j=0; for da in $(DOCKER_ARCH); do \
if [ "$$j" -eq "$$i" ] && [ -n "$$da" ]; then \
annotate="docker manifest annotate $(FULLY_QUALIFIED_IMAGE):$* $(FULLY_QUALIFIED_IMAGE):$$a-$* --os linux --arch"; \
k=0; for ea in $$da; do \
[ "$$k" = 0 ] && annotate="$$annotate $$ea"; \
[ "$$k" != 0 ] && annotate="$$annotate --variant $$ea"; \
k=$$((k+1)); \
done; \
$$annotate; \
fi; \
j=$$((j+1)); \
done; \
i=$$((i+1)); \
done
manifest-name:
@echo "manifest: $(IMAGE):$(VERSION)"
push: .push-$(ARCH)-$(VERSION) push-name
.push-$(ARCH)-$(VERSION): .container-$(ARCH)-$(VERSION)
ifneq ($(REGISTRY),index.docker.io)
@docker tag $(IMAGE):$(ARCH)-$(VERSION) $(FULLY_QUALIFIED_IMAGE):$(ARCH)-$(VERSION)
endif
@docker push $(FULLY_QUALIFIED_IMAGE):$(ARCH)-$(VERSION)
@docker images -q $(IMAGE):$(ARCH)-$(VERSION) > $@
push-latest: container-latest
@docker push $(FULLY_QUALIFIED_IMAGE):$(ARCH)-latest
@echo "pushed: $(IMAGE):$(ARCH)-latest"
push-name:
@echo "pushed: $(IMAGE):$(ARCH)-$(VERSION)"
release: $(RELEASE_BINS)
$(RELEASE_BINS):
@make OS=$(word 2,$(subst -, ,$(@F))) ARCH=$(word 3,$(subst -, ,$(@F)))
mkdir -p $(@D)
cp bin/$(word 2,$(subst -, ,$(@F)))/$(word 3,$(subst -, ,$(@F)))/kgctl $@
clean: container-clean bin-clean
rm -rf .cache
container-clean:
rm -rf .container-* .manifest-* .push-*
bin-clean:
rm -rf bin
gomodtidy:
go mod tidy
$(CONTROLLER_GEN_BINARY):
go build -o $@ sigs.k8s.io/controller-tools/cmd/controller-gen
$(CLIENT_GEN_BINARY):
go build -o $@ k8s.io/code-generator/cmd/client-gen
$(DEEPCOPY_GEN_BINARY):
go build -o $@ k8s.io/code-generator/cmd/deepcopy-gen
$(INFORMER_GEN_BINARY):
go build -o $@ k8s.io/code-generator/cmd/informer-gen
$(LISTER_GEN_BINARY):
go build -o $@ k8s.io/code-generator/cmd/lister-gen
$(DOCS_GEN_BINARY): cmd/docs-gen/main.go
go build -o $@ ./cmd/docs-gen
$(STATICCHECK_BINARY):
go build -o $@ honnef.co/go/tools/cmd/staticcheck
$(EMBEDMD_BINARY):
go build -o $@ github.com/campoy/embedmd