Makefile 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. # set the shell to bash always
  2. SHELL := /bin/bash
  3. # set make and shell flags to exit on errors
  4. MAKEFLAGS += --warn-undefined-variables
  5. .SHELLFLAGS := -euo pipefail -c
  6. ARCH ?= amd64 arm64
  7. BUILD_ARGS ?= CGO_ENABLED=0
  8. DOCKER_BUILD_ARGS ?=
  9. DOCKERFILE ?= Dockerfile
  10. # default target is build
  11. .DEFAULT_GOAL := all
  12. .PHONY: all
  13. all: $(addprefix build-,$(ARCH))
  14. # Image registry for build/push image targets
  15. export IMAGE_REGISTRY ?= ghcr.io
  16. export IMAGE_REPO ?= external-secrets/external-secrets
  17. export IMAGE_NAME ?= $(IMAGE_REGISTRY)/$(IMAGE_REPO)
  18. BUNDLE_DIR ?= deploy/crds
  19. CRD_DIR ?= config/crds
  20. HELM_DIR ?= deploy/charts/external-secrets
  21. TF_DIR ?= terraform
  22. OUTPUT_DIR ?= bin
  23. # Get the currently used golang install path (in GOPATH/bin, unless GOBIN is set)
  24. ifeq (,$(shell go env GOBIN))
  25. GOBIN=$(shell go env GOPATH)/bin
  26. else
  27. GOBIN=$(shell go env GOBIN)
  28. endif
  29. KUBERNETES_VERSION := '1.24.x'
  30. # check if there are any existing `git tag` values
  31. ifeq ($(shell git tag),)
  32. # no tags found - default to initial tag `v0.0.0`
  33. export VERSION := $(shell echo "v0.0.0-$$(git rev-list HEAD --count)-g$$(git describe --dirty --always)" | sed 's/-/./2' | sed 's/-/./2')
  34. else
  35. # use tags
  36. export VERSION := $(shell git describe --dirty --always --tags --exclude 'helm*' | sed 's/-/./2' | sed 's/-/./2')
  37. endif
  38. TAG_SUFFIX ?=
  39. export IMAGE_TAG ?= $(VERSION)$(TAG_SUFFIX)
  40. # ====================================================================================
  41. # Colors
  42. BLUE := $(shell printf "\033[34m")
  43. YELLOW := $(shell printf "\033[33m")
  44. RED := $(shell printf "\033[31m")
  45. GREEN := $(shell printf "\033[32m")
  46. CNone := $(shell printf "\033[0m")
  47. # ====================================================================================
  48. # Logger
  49. TIME_LONG = `date +%Y-%m-%d' '%H:%M:%S`
  50. TIME_SHORT = `date +%H:%M:%S`
  51. TIME = $(TIME_SHORT)
  52. INFO = echo ${TIME} ${BLUE}[ .. ]${CNone}
  53. WARN = echo ${TIME} ${YELLOW}[WARN]${CNone}
  54. ERR = echo ${TIME} ${RED}[FAIL]${CNone}
  55. OK = echo ${TIME} ${GREEN}[ OK ]${CNone}
  56. FAIL = (echo ${TIME} ${RED}[FAIL]${CNone} && false)
  57. # ====================================================================================
  58. # Conformance
  59. reviewable: generate docs manifests helm.generate helm.docs lint ## Ensure a PR is ready for review.
  60. @go mod tidy
  61. @cd e2e/ && go mod tidy
  62. check-diff: reviewable ## Ensure branch is clean.
  63. @$(INFO) checking that branch is clean
  64. @test -z "$$(git status --porcelain)" || (echo "$$(git status --porcelain)" && $(FAIL))
  65. @$(OK) branch is clean
  66. update-deps:
  67. go get -u
  68. cd e2e && go get -u
  69. @go mod tidy
  70. @cd e2e/ && go mod tidy
  71. # ====================================================================================
  72. # Golang
  73. .PHONY: test
  74. test: export KUBEBUILDER_ASSETS := $(shell setup-envtest use $(KUBERNETES_VERSION) -p path --os $(shell go env GOOS) --arch $(shell go env GOARCH))
  75. test: generate ## Run tests
  76. @$(INFO) go test unit-tests
  77. go test -race -v $(shell go list ./... | grep -v e2e) -coverprofile cover.out
  78. @$(OK) go test unit-tests
  79. .PHONY: test.e2e
  80. test.e2e: generate ## Run e2e tests
  81. @$(INFO) go test e2e-tests
  82. $(MAKE) -C ./e2e test
  83. @$(OK) go test e2e-tests
  84. .PHONY: test.e2e.managed
  85. test.e2e.managed: generate ## Run e2e tests managed
  86. @$(INFO) go test e2e-tests-managed
  87. $(MAKE) -C ./e2e test.managed
  88. @$(OK) go test e2e-tests-managed
  89. .PHONY: build
  90. build: $(addprefix build-,$(ARCH)) ## Build binary
  91. .PHONY: build-%
  92. build-%: generate ## Build binary for the specified arch
  93. @$(INFO) go build $*
  94. $(BUILD_ARGS) GOOS=linux GOARCH=$* \
  95. go build -o '$(OUTPUT_DIR)/external-secrets-linux-$*' main.go
  96. @$(OK) go build $*
  97. lint.check: ## Check install of golanci-lint
  98. @if ! golangci-lint --version > /dev/null 2>&1; then \
  99. echo -e "\033[0;33mgolangci-lint is not installed: run \`\033[0;32mmake lint.install\033[0m\033[0;33m\` or install it from https://golangci-lint.run\033[0m"; \
  100. exit 1; \
  101. fi
  102. lint.install: ## Install golangci-lint to the go bin dir
  103. @if ! golangci-lint --version > /dev/null 2>&1; then \
  104. echo "Installing golangci-lint"; \
  105. curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(GOBIN) v1.49.0; \
  106. fi
  107. lint: lint.check ## Run golangci-lint
  108. @if ! golangci-lint run; then \
  109. echo -e "\033[0;33mgolangci-lint failed: some checks can be fixed with \`\033[0;32mmake fmt\033[0m\033[0;33m\`\033[0m"; \
  110. exit 1; \
  111. fi
  112. @$(OK) Finished linting
  113. fmt: lint.check ## Ensure consistent code style
  114. @go mod tidy
  115. @cd e2e/ && go mod tidy
  116. @go fmt ./...
  117. @golangci-lint run --fix
  118. @$(OK) Ensured consistent code style
  119. generate: ## Generate code and crds
  120. @./hack/crd.generate.sh $(BUNDLE_DIR) $(CRD_DIR)
  121. @$(OK) Finished generating deepcopy and crds
  122. # ====================================================================================
  123. # Local Utility
  124. # This is for running out-of-cluster locally, and is for convenience.
  125. # For more control, try running the binary directly with different arguments.
  126. run: generate ## Run app locally (without a k8s cluster)
  127. go run ./main.go
  128. manifests: helm.generate ## Generate manifests from helm chart
  129. mkdir -p $(OUTPUT_DIR)/deploy/manifests
  130. helm template external-secrets $(HELM_DIR) -f deploy/manifests/helm-values.yaml > $(OUTPUT_DIR)/deploy/manifests/external-secrets.yaml
  131. crds.install: generate ## Install CRDs into a cluster. This is for convenience
  132. kubectl apply -f $(BUNDLE_DIR)
  133. crds.uninstall: ## Uninstall CRDs from a cluster. This is for convenience
  134. kubectl delete -f $(BUNDLE_DIR)
  135. # ====================================================================================
  136. # Helm Chart
  137. helm.docs: ## Generate helm docs
  138. @cd $(HELM_DIR); \
  139. docker run --rm -v $(shell pwd)/$(HELM_DIR):/helm-docs -u $(shell id -u) jnorwood/helm-docs:v1.5.0
  140. HELM_VERSION ?= $(shell helm show chart $(HELM_DIR) | grep 'version:' | sed 's/version: //g')
  141. helm.build: helm.generate ## Build helm chart
  142. @$(INFO) helm package
  143. @helm package $(HELM_DIR) --dependency-update --destination $(OUTPUT_DIR)/chart
  144. @mv $(OUTPUT_DIR)/chart/external-secrets-$(HELM_VERSION).tgz $(OUTPUT_DIR)/chart/external-secrets.tgz
  145. @$(OK) helm package
  146. helm.generate:
  147. ./hack/helm.generate.sh $(BUNDLE_DIR) $(HELM_DIR)
  148. @$(OK) Finished generating helm chart files
  149. helm.test: helm.generate
  150. @helm unittest --file tests/*.yaml --file 'tests/**/*.yaml' deploy/charts/external-secrets/
  151. helm.test.update: helm.generate
  152. @helm unittest -u --file tests/*.yaml --file 'tests/**/*.yaml' deploy/charts/external-secrets/
  153. helm.update.appversion:
  154. @chartversion=$$(yq .version ./deploy/charts/external-secrets/Chart.yaml) ; \
  155. chartappversion=$$(yq .appVersion ./deploy/charts/external-secrets/Chart.yaml) ; \
  156. chartname=$$(yq .name ./deploy/charts/external-secrets/Chart.yaml) ; \
  157. $(INFO) Update chartname and chartversion string in test snapshots.; \
  158. sed -s -i "s/^\([[:space:]]\+helm\.sh\/chart:\).*/\1 $${chartname}-$${chartversion}/" ./deploy/charts/external-secrets/tests/__snapshot__/*.yaml.snap ; \
  159. sed -s -i "s/^\([[:space:]]\+app\.kubernetes\.io\/version:\).*/\1 $${chartappversion}/" ./deploy/charts/external-secrets/tests/__snapshot__/*.yaml.snap ; \
  160. sed -s -i "s/^\([[:space:]]\+image: ghcr\.io\/external-secrets\/external-secrets:\).*/\1$${chartappversion}/" ./deploy/charts/external-secrets/tests/__snapshot__/*.yaml.snap ; \
  161. $(OK) "Version strings updated"
  162. # ====================================================================================
  163. # Documentation
  164. .PHONY: docs
  165. docs: generate ## Generate docs
  166. $(MAKE) -C ./hack/api-docs build
  167. .PHONY: docs.publish
  168. docs.publish: generate ## Generate and deploys docs
  169. $(MAKE) -C ./hack/api-docs build.publish
  170. .PHONY: docs.serve
  171. docs.serve: ## Serve docs
  172. $(MAKE) -C ./hack/api-docs serve
  173. # ====================================================================================
  174. # Build Artifacts
  175. .PHONY: build.all
  176. build.all: docker.build helm.build ## Build all artifacts (docker image, helm chart)
  177. .PHONY: docker.image
  178. docker.image: ## Emit IMAGE_NAME:IMAGE_TAG
  179. @echo $(IMAGE_NAME):$(IMAGE_TAG)
  180. .PHONY: docker.imagename
  181. docker.imagename: ## Emit IMAGE_NAME
  182. @echo $(IMAGE_NAME)
  183. .PHONY: docker.tag
  184. docker.tag: ## Emit IMAGE_TAG
  185. @echo $(IMAGE_TAG)
  186. .PHONY: docker.build
  187. docker.build: $(addprefix build-,$(ARCH)) ## Build the docker image
  188. @$(INFO) docker build
  189. echo docker build -f $(DOCKERFILE) . $(DOCKER_BUILD_ARGS) -t $(IMAGE_NAME):$(IMAGE_TAG)
  190. docker build -f $(DOCKERFILE) . $(DOCKER_BUILD_ARGS) -t $(IMAGE_NAME):$(IMAGE_TAG)
  191. @$(OK) docker build
  192. .PHONY: docker.push
  193. docker.push: ## Push the docker image to the registry
  194. @$(INFO) docker push
  195. @docker push $(IMAGE_NAME):$(IMAGE_TAG)
  196. @$(OK) docker push
  197. # RELEASE_TAG is tag to promote. Default is promoting to main branch, but can be overriden
  198. # to promote a tag to a specific version.
  199. RELEASE_TAG ?= $(IMAGE_TAG)
  200. SOURCE_TAG ?= $(VERSION)$(TAG_SUFFIX)
  201. .PHONY: docker.promote
  202. docker.promote: ## Promote the docker image to the registry
  203. @$(INFO) promoting $(SOURCE_TAG) to $(RELEASE_TAG)
  204. docker manifest inspect --verbose $(IMAGE_NAME):$(SOURCE_TAG) > .tagmanifest
  205. for digest in $$(jq -r 'if type=="array" then .[].Descriptor.digest else .Descriptor.digest end' < .tagmanifest); do \
  206. docker pull $(IMAGE_NAME)@$$digest; \
  207. done
  208. docker manifest create $(IMAGE_NAME):$(RELEASE_TAG) \
  209. $$(jq -j '"--amend $(IMAGE_NAME)@" + if type=="array" then .[].Descriptor.digest else .Descriptor.digest end + " "' < .tagmanifest)
  210. docker manifest push $(IMAGE_NAME):$(RELEASE_TAG)
  211. @$(OK) docker push $(RELEASE_TAG) \
  212. # ====================================================================================
  213. # Terraform
  214. tf.plan.%: ## Runs terrform plan for a provider
  215. @cd $(TF_DIR)/$*; \
  216. terraform init; \
  217. terraform plan
  218. tf.apply.%: ## Runs terrform apply for a provider
  219. @cd $(TF_DIR)/$*; \
  220. terraform init; \
  221. terraform apply -auto-approve
  222. tf.destroy.%: ## Runs terrform destroy for a provider
  223. @cd $(TF_DIR)/$*; \
  224. terraform init; \
  225. terraform destroy -auto-approve
  226. tf.show.%: ## Runs terrform show for a provider and outputs to a file
  227. @cd $(TF_DIR)/$*; \
  228. terraform init; \
  229. terraform plan -out tfplan.binary; \
  230. terraform show -json tfplan.binary > plan.json
  231. # ====================================================================================
  232. # Help
  233. .PHONY: help
  234. # only comments after make target name are shown as help text
  235. help: ## Displays this help message
  236. @echo -e "$$(grep -hE '^\S+:.*##' $(MAKEFILE_LIST) | sed -e 's/:.*##\s*/|/' -e 's/^\(.\+\):\(.*\)/\\x1b[36m\1\\x1b[m:\2/' | column -c2 -t -s'|' | sort)"
  237. .PHONY: clean
  238. clean: ## Clean bins
  239. @$(INFO) clean
  240. @rm -f $(OUTPUT_DIR)/external-secrets-linux-*
  241. @$(OK) go build $*