Makefile 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. export GO111MODULE=on
  2. .PHONY: push container clean container-name container-latest push-latest fmt lint test unit vendor header generate client deepcopy informer lister openapi manifest manfest-latest manifest-annotate manifest manfest-latest manifest-annotate release gen-docs e2e
  3. OS ?= $(shell go env GOOS)
  4. ARCH ?= $(shell go env GOARCH)
  5. ALL_OS := linux darwin windows
  6. ALL_ARCH := amd64 arm arm64
  7. DOCKER_ARCH := "amd64" "arm v7" "arm64 v8"
  8. ifeq ($(OS),linux)
  9. BINS := bin/$(OS)/$(ARCH)/kg bin/$(OS)/$(ARCH)/kgctl
  10. else
  11. BINS := bin/$(OS)/$(ARCH)/kgctl
  12. endif
  13. RELEASE_BINS := $(addprefix bin/release/kgctl-, $(addprefix linux-, $(ALL_ARCH)) darwin-amd64 windows-amd64)
  14. PROJECT := kilo
  15. PKG := github.com/squat/$(PROJECT)
  16. REGISTRY ?= index.docker.io
  17. IMAGE ?= squat/$(PROJECT)
  18. TAG := $(shell git describe --abbrev=0 --tags HEAD 2>/dev/null)
  19. COMMIT := $(shell git rev-parse HEAD)
  20. VERSION := $(COMMIT)
  21. ifneq ($(TAG),)
  22. ifeq ($(COMMIT), $(shell git rev-list -n1 $(TAG)))
  23. VERSION := $(TAG)
  24. endif
  25. endif
  26. DIRTY := $(shell test -z "$$(git diff --shortstat 2>/dev/null)" || echo -dirty)
  27. VERSION := $(VERSION)$(DIRTY)
  28. LD_FLAGS := -ldflags '-X $(PKG)/pkg/version.Version=$(VERSION)'
  29. SRC := $(shell find . -type f -name '*.go' -not -path "./vendor/*")
  30. GO_FILES ?= $$(find . -name '*.go' -not -path './vendor/*')
  31. GO_PKGS ?= $$(go list ./... | grep -v "$(PKG)/vendor")
  32. CLIENT_GEN_BINARY := bin/client-gen
  33. DOCS_GEN_BINARY := bin/docs-gen
  34. DEEPCOPY_GEN_BINARY := bin/deepcopy-gen
  35. INFORMER_GEN_BINARY := bin/informer-gen
  36. LISTER_GEN_BINARY := bin/lister-gen
  37. OPENAPI_GEN_BINARY := bin/openapi-gen
  38. GOLINT_BINARY := bin/golint
  39. EMBEDMD_BINARY := bin/embedmd
  40. KIND_BINARY := $(shell pwd)/bin/kind
  41. KUBECTL_BINARY := $(shell pwd)/bin/kubectl
  42. BASH_UNIT := $(shell pwd)/bin/bash_unit
  43. BUILD_IMAGE ?= golang:1.15.7-alpine
  44. BASE_IMAGE ?= alpine:3.12
  45. build: $(BINS)
  46. build-%:
  47. @$(MAKE) --no-print-directory OS=$(word 1,$(subst -, ,$*)) ARCH=$(word 2,$(subst -, ,$*)) build
  48. container-latest-%:
  49. @$(MAKE) --no-print-directory ARCH=$* container-latest
  50. container-%:
  51. @$(MAKE) --no-print-directory ARCH=$* container
  52. push-latest-%:
  53. @$(MAKE) --no-print-directory ARCH=$* push-latest
  54. push-%:
  55. @$(MAKE) --no-print-directory ARCH=$* push
  56. all-build: $(addprefix build-$(OS)-, $(ALL_ARCH))
  57. all-container: $(addprefix container-, $(ALL_ARCH))
  58. all-push: $(addprefix push-, $(ALL_ARCH))
  59. all-container-latest: $(addprefix container-latest-, $(ALL_ARCH))
  60. all-push-latest: $(addprefix push-latest-, $(ALL_ARCH))
  61. generate: client deepcopy informer lister openapi
  62. client: pkg/k8s/clientset/versioned/typed/kilo/v1alpha1/peer.go
  63. pkg/k8s/clientset/versioned/typed/kilo/v1alpha1/peer.go: .header pkg/k8s/apis/kilo/v1alpha1/types.go $(CLIENT_GEN_BINARY)
  64. $(CLIENT_GEN_BINARY) \
  65. --clientset-name versioned \
  66. --input-base "" \
  67. --input $(PKG)/pkg/k8s/apis/kilo/v1alpha1 \
  68. --output-base $(CURDIR) \
  69. --output-package $(PKG)/pkg/k8s/clientset \
  70. --go-header-file=.header \
  71. --logtostderr
  72. rm -r pkg/k8s/clientset || true
  73. mv $(PKG)/pkg/k8s/clientset pkg/k8s
  74. rm -r github.com || true
  75. go fmt ./pkg/k8s/clientset/...
  76. deepcopy: pkg/k8s/apis/kilo/v1alpha1/zz_generated.deepcopy.go
  77. pkg/k8s/apis/kilo/v1alpha1/zz_generated.deepcopy.go: .header pkg/k8s/apis/kilo/v1alpha1/types.go $(DEEPCOPY_GEN_BINARY)
  78. $(DEEPCOPY_GEN_BINARY) \
  79. --input-dirs ./$(@D) \
  80. --go-header-file=.header \
  81. --logtostderr \
  82. --output-base $(CURDIR) \
  83. --output-file-base zz_generated.deepcopy
  84. mv $(PKG)/$@ $@ || true
  85. rm -r github.com || true
  86. go fmt $@
  87. informer: pkg/k8s/informers/kilo/v1alpha1/peer.go
  88. pkg/k8s/informers/kilo/v1alpha1/peer.go: .header pkg/k8s/apis/kilo/v1alpha1/types.go $(INFORMER_GEN_BINARY)
  89. $(INFORMER_GEN_BINARY) \
  90. --input-dirs $(PKG)/pkg/k8s/apis/kilo/v1alpha1 \
  91. --go-header-file=.header \
  92. --logtostderr \
  93. --versioned-clientset-package $(PKG)/pkg/k8s/clientset/versioned \
  94. --listers-package $(PKG)/pkg/k8s/listers \
  95. --output-base $(CURDIR) \
  96. --output-package $(PKG)/pkg/k8s/informers \
  97. --single-directory
  98. rm -r pkg/k8s/informers || true
  99. mv $(PKG)/pkg/k8s/informers pkg/k8s
  100. rm -r github.com || true
  101. go fmt ./pkg/k8s/informers/...
  102. lister: pkg/k8s/listers/kilo/v1alpha1/peer.go
  103. pkg/k8s/listers/kilo/v1alpha1/peer.go: .header pkg/k8s/apis/kilo/v1alpha1/types.go $(LISTER_GEN_BINARY)
  104. $(LISTER_GEN_BINARY) \
  105. --input-dirs $(PKG)/pkg/k8s/apis/kilo/v1alpha1 \
  106. --go-header-file=.header \
  107. --logtostderr \
  108. --output-base $(CURDIR) \
  109. --output-package $(PKG)/pkg/k8s/listers
  110. rm -r pkg/k8s/listers || true
  111. mv $(PKG)/pkg/k8s/listers pkg/k8s
  112. rm -r github.com || true
  113. go fmt ./pkg/k8s/listers/...
  114. openapi: pkg/k8s/apis/kilo/v1alpha1/openapi_generated.go
  115. pkg/k8s/apis/kilo/v1alpha1/openapi_generated.go: pkg/k8s/apis/kilo/v1alpha1/types.go $(OPENAPI_GEN_BINARY)
  116. $(OPENAPI_GEN_BINARY) \
  117. --input-dirs $(PKG)/$(@D),k8s.io/apimachinery/pkg/apis/meta/v1,k8s.io/api/core/v1 \
  118. --output-base $(CURDIR) \
  119. --output-package ./$(@D) \
  120. --logtostderr \
  121. --report-filename /dev/null \
  122. --go-header-file=.header
  123. go fmt $@
  124. gen-docs: generate docs/api.md
  125. docs/api.md: pkg/k8s/apis/kilo/v1alpha1/types.go $(DOCS_GEN_BINARY)
  126. $(DOCS_GEN_BINARY) $< > $@
  127. $(BINS): $(SRC) go.mod
  128. @mkdir -p bin/$(word 2,$(subst /, ,$@))/$(word 3,$(subst /, ,$@))
  129. @echo "building: $@"
  130. @docker run --rm \
  131. -u $$(id -u):$$(id -g) \
  132. -v $$(pwd):/$(PROJECT) \
  133. -w /$(PROJECT) \
  134. $(BUILD_IMAGE) \
  135. /bin/sh -c " \
  136. GOARCH=$(word 3,$(subst /, ,$@)) \
  137. GOOS=$(word 2,$(subst /, ,$@)) \
  138. GOCACHE=/$(PROJECT)/.cache \
  139. CGO_ENABLED=0 \
  140. go build -mod=vendor -o $@ \
  141. $(LD_FLAGS) \
  142. ./cmd/$(@F)/... \
  143. "
  144. fmt:
  145. @echo $(GO_PKGS)
  146. gofmt -w -s $(GO_FILES)
  147. lint: header $(GOLINT_BINARY)
  148. @echo 'go vet $(GO_PKGS)'
  149. @vet_res=$$(GO111MODULE=on go vet -mod=vendor $(GO_PKGS) 2>&1); if [ -n "$$vet_res" ]; then \
  150. echo ""; \
  151. echo "Go vet found issues. Please check the reported issues"; \
  152. echo "and fix them if necessary before submitting the code for review:"; \
  153. echo "$$vet_res"; \
  154. exit 1; \
  155. fi
  156. @echo '$(GOLINT_BINARY) $(GO_PKGS)'
  157. @lint_res=$$($(GOLINT_BINARY) $(GO_PKGS)); if [ -n "$$lint_res" ]; then \
  158. echo ""; \
  159. echo "Golint found style issues. Please check the reported issues"; \
  160. echo "and fix them if necessary before submitting the code for review:"; \
  161. echo "$$lint_res"; \
  162. exit 1; \
  163. fi
  164. @echo 'gofmt -d -s $(GO_FILES)'
  165. @fmt_res=$$(gofmt -d -s $(GO_FILES)); if [ -n "$$fmt_res" ]; then \
  166. echo ""; \
  167. echo "Gofmt found style issues. Please check the reported issues"; \
  168. echo "and fix them if necessary before submitting the code for review:"; \
  169. echo "$$fmt_res"; \
  170. exit 1; \
  171. fi
  172. unit:
  173. go test -mod=vendor --race ./...
  174. test: lint unit e2e
  175. $(KIND_BINARY):
  176. curl -Lo $@ https://kind.sigs.k8s.io/dl/v0.10.0/kind-linux-$(ARCH)
  177. chmod +x $@
  178. $(KUBECTL_BINARY):
  179. curl -Lo $@ https://dl.k8s.io/release/v1.21.0/bin/linux/$(ARCH)/kubectl
  180. chmod +x $@
  181. $(BASH_UNIT):
  182. curl -Lo $@ https://raw.githubusercontent.com/pgrange/bash_unit/v1.6.0/bash_unit
  183. chmod +x $@
  184. e2e: container ${KIND_BINARY} ${KUBECTL_BINARY} $(BASH_UNIT)
  185. KILO_IMAGE=${IMAGE}:${ARCH}-${VERSION} KIND_BINARY=${KIND_BINARY} $(BASH_UNIT) ./e2e/kind.sh
  186. header: .header
  187. @HEADER=$$(cat .header); \
  188. HEADER_LEN=$$(wc -l .header | awk '{print $$1}'); \
  189. FILES=; \
  190. for f in $(GO_FILES); do \
  191. for i in 0 1 2 3 4 5; do \
  192. FILE=$$(t=$$(mktemp) && tail -n +$$i $$f > $$t && head -n $$HEADER_LEN $$t | sed "s/[0-9]\{4\}/YEAR/"); \
  193. [ "$$FILE" = "$$HEADER" ] && continue 2; \
  194. done; \
  195. FILES="$$FILES$$f "; \
  196. done; \
  197. if [ -n "$$FILES" ]; then \
  198. printf 'the following files are missing the license header: %s\n' "$$FILES"; \
  199. exit 1; \
  200. fi
  201. tmp/help.txt: bin/$(OS)/$(ARCH)/kg
  202. mkdir -p tmp
  203. bin//$(OS)/$(ARCH)/kg --help 2>&1 | head -n -1 > $@
  204. docs/kg.md: $(EMBEDMD_BINARY) tmp/help.txt
  205. $(EMBEDMD_BINARY) -w $@
  206. website/docs/README.md: README.md
  207. rm -rf website/static/img/graphs
  208. find docs -type f -name '*.md' | xargs -I{} sh -c 'cat $(@D)/$$(basename {} .md) > website/{}'
  209. find docs -type f -name '*.md' | xargs -I{} sh -c 'cat {} >> website/{}'
  210. cat $(@D)/$$(basename $@ .md) > $@
  211. cat README.md >> $@
  212. cp -r docs/graphs website/static/img/
  213. sed -i 's/\.\/docs\///g' $@
  214. find $(@D) -type f -name '*.md' | xargs -I{} sed -i 's/\.\/\(.\+\.svg\)/\/img\/\1/g' {}
  215. sed -i 's/graphs\//\/img\/graphs\//g' $@
  216. website/build/index.html: website/docs/README.md docs/api.md
  217. yarn --cwd website install
  218. yarn --cwd website build
  219. container: .container-$(ARCH)-$(VERSION) container-name
  220. .container-$(ARCH)-$(VERSION): bin/linux/$(ARCH)/kg Dockerfile
  221. @i=0; for a in $(ALL_ARCH); do [ "$$a" = $(ARCH) ] && break; i=$$((i+1)); done; \
  222. ia=""; iv=""; \
  223. j=0; for a in $(DOCKER_ARCH); do \
  224. [ "$$i" -eq "$$j" ] && ia=$$(echo "$$a" | awk '{print $$1}') && iv=$$(echo "$$a" | awk '{print $$2}') && break; j=$$((j+1)); \
  225. done; \
  226. 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); \
  227. docker build -t $(IMAGE):$(ARCH)-$(VERSION) --build-arg FROM=$(BASE_IMAGE)@$$SHA --build-arg GOARCH=$(ARCH) .
  228. @docker images -q $(IMAGE):$(ARCH)-$(VERSION) > $@
  229. container-latest: .container-$(ARCH)-$(VERSION)
  230. @docker tag $(IMAGE):$(ARCH)-$(VERSION) $(IMAGE):$(ARCH)-latest
  231. @echo "container: $(IMAGE):$(ARCH)-latest"
  232. container-name:
  233. @echo "container: $(IMAGE):$(ARCH)-$(VERSION)"
  234. manifest: .manifest-$(VERSION) manifest-name
  235. .manifest-$(VERSION): Dockerfile $(addprefix push-, $(ALL_ARCH))
  236. @docker manifest create --amend $(IMAGE):$(VERSION) $(addsuffix -$(VERSION), $(addprefix squat/$(PROJECT):, $(ALL_ARCH)))
  237. @$(MAKE) --no-print-directory manifest-annotate-$(VERSION)
  238. @docker manifest push $(IMAGE):$(VERSION) > $@
  239. manifest-latest: Dockerfile $(addprefix push-latest-, $(ALL_ARCH))
  240. @docker manifest create --amend $(IMAGE):latest $(addsuffix -latest, $(addprefix squat/$(PROJECT):, $(ALL_ARCH)))
  241. @$(MAKE) --no-print-directory manifest-annotate-latest
  242. @docker manifest push $(IMAGE):latest
  243. @echo "manifest: $(IMAGE):latest"
  244. manifest-annotate: manifest-annotate-$(VERSION)
  245. manifest-annotate-%:
  246. @i=0; \
  247. for a in $(ALL_ARCH); do \
  248. annotate=; \
  249. j=0; for da in $(DOCKER_ARCH); do \
  250. if [ "$$j" -eq "$$i" ] && [ -n "$$da" ]; then \
  251. annotate="docker manifest annotate $(IMAGE):$* $(IMAGE):$$a-$* --os linux --arch"; \
  252. k=0; for ea in $$da; do \
  253. [ "$$k" = 0 ] && annotate="$$annotate $$ea"; \
  254. [ "$$k" != 0 ] && annotate="$$annotate --variant $$ea"; \
  255. k=$$((k+1)); \
  256. done; \
  257. $$annotate; \
  258. fi; \
  259. j=$$((j+1)); \
  260. done; \
  261. i=$$((i+1)); \
  262. done
  263. manifest-name:
  264. @echo "manifest: $(IMAGE_ROOT):$(VERSION)"
  265. push: .push-$(ARCH)-$(VERSION) push-name
  266. .push-$(ARCH)-$(VERSION): .container-$(ARCH)-$(VERSION)
  267. @docker push $(REGISTRY)/$(IMAGE):$(ARCH)-$(VERSION)
  268. @docker images -q $(IMAGE):$(ARCH)-$(VERSION) > $@
  269. push-latest: container-latest
  270. @docker push $(REGISTRY)/$(IMAGE):$(ARCH)-latest
  271. @echo "pushed: $(IMAGE):$(ARCH)-latest"
  272. push-name:
  273. @echo "pushed: $(IMAGE):$(ARCH)-$(VERSION)"
  274. release: $(RELEASE_BINS)
  275. $(RELEASE_BINS):
  276. @make OS=$(word 2,$(subst -, ,$(@F))) ARCH=$(word 3,$(subst -, ,$(@F)))
  277. mkdir -p $(@D)
  278. cp bin/$(word 2,$(subst -, ,$(@F)))/$(word 3,$(subst -, ,$(@F)))/kgctl $@
  279. clean: container-clean bin-clean
  280. rm -rf .cache
  281. container-clean:
  282. rm -rf .container-* .manifest-* .push-*
  283. bin-clean:
  284. rm -rf bin
  285. vendor:
  286. go mod tidy
  287. go mod vendor
  288. $(CLIENT_GEN_BINARY):
  289. go build -mod=vendor -o $@ k8s.io/code-generator/cmd/client-gen
  290. $(DEEPCOPY_GEN_BINARY):
  291. go build -mod=vendor -o $@ k8s.io/code-generator/cmd/deepcopy-gen
  292. $(INFORMER_GEN_BINARY):
  293. go build -mod=vendor -o $@ k8s.io/code-generator/cmd/informer-gen
  294. $(LISTER_GEN_BINARY):
  295. go build -mod=vendor -o $@ k8s.io/code-generator/cmd/lister-gen
  296. $(OPENAPI_GEN_BINARY):
  297. go build -mod=vendor -o $@ k8s.io/kube-openapi/cmd/openapi-gen
  298. $(DOCS_GEN_BINARY): cmd/docs-gen/main.go
  299. go build -mod=vendor -o $@ ./cmd/docs-gen
  300. $(GOLINT_BINARY):
  301. go build -mod=vendor -o $@ golang.org/x/lint/golint
  302. $(EMBEDMD_BINARY):
  303. go build -mod=vendor -o $@ github.com/campoy/embedmd