Makefile 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  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 ppc64le
  7. BUILD_ARGS ?= CGO_ENABLED=0
  8. DOCKER_BUILD_ARGS ?=
  9. DOCKERFILE ?= Dockerfile
  10. DOCKER ?= docker
  11. # default target is build
  12. .DEFAULT_GOAL := all
  13. .PHONY: all
  14. all: $(addprefix build-,$(ARCH))
  15. # Image registry for build/push image targets
  16. export IMAGE_REGISTRY ?= ghcr.io
  17. export IMAGE_REPO ?= external-secrets/external-secrets
  18. export IMAGE_NAME ?= $(IMAGE_REGISTRY)/$(IMAGE_REPO)
  19. BUNDLE_DIR ?= deploy/crds
  20. CRD_DIR ?= config/crds
  21. HELM_DIR ?= deploy/charts/external-secrets
  22. TF_DIR ?= terraform
  23. OUTPUT_DIR ?= bin
  24. # Get the currently used golang install path (in GOPATH/bin, unless GOBIN is set)
  25. ifeq (,$(shell go env GOBIN))
  26. GOBIN=$(shell go env GOPATH)/bin
  27. else
  28. GOBIN=$(shell go env GOBIN)
  29. endif
  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.schema.update helm.docs lint license.check helm.test.update test.crds.update tf.fmt ## Ensure a PR is ready for review.
  60. @go mod tidy
  61. @cd e2e/ && go mod tidy
  62. @cd apis/ && go mod tidy
  63. @cd runtime/ && go mod tidy
  64. @for provider in providers/v1/*/; do (cd $$provider && go mod tidy); done
  65. @for generator in generators/v1/*/; do (cd $$generator && go mod tidy); done
  66. check-diff: reviewable ## Ensure branch is clean.
  67. @$(INFO) checking that branch is clean
  68. @test -z "$$(git status --porcelain)" || (echo "$$(git status --porcelain)" && $(FAIL))
  69. @$(OK) branch is clean
  70. update-deps: ## Update dependencies across all modules (root, apis, runtime, e2e, providers, generators)
  71. @./hack/update-deps.sh
  72. .PHONY: license.check
  73. license.check:
  74. $(DOCKER) run --rm -u $(shell id -u) -v $(shell pwd):/github/workspace apache/skywalking-eyes:0.6.0 header check
  75. # ====================================================================================
  76. # Golang
  77. .PHONY: go-work ## Creates go workspace and syncs it
  78. go-work:
  79. @$(INFO) creating go workspace
  80. @rm -rf go.work go.work.sum
  81. @go work init
  82. @go work use -r .
  83. @go work edit -dropuse ./e2e
  84. @go work sync
  85. @$(OK) created go workspace
  86. .PHONY: test
  87. test: generate envtest go-work ## Run tests
  88. @$(INFO) go test unit-tests
  89. KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use $(KUBERNETES_VERSION) -p path --bin-dir $(LOCALBIN))" go test work -v -race -coverprofile cover.out
  90. @$(OK) go test unit-tests
  91. .PHONY: test.e2e
  92. test.e2e: generate ## Run e2e tests
  93. @$(INFO) go test e2e-tests
  94. $(MAKE) -C ./e2e test
  95. @$(OK) go test e2e-tests
  96. .PHONY: test.e2e.managed
  97. test.e2e.managed: generate ## Run e2e tests managed
  98. @$(INFO) go test e2e-tests-managed
  99. $(MAKE) -C ./e2e test.managed
  100. @$(OK) go test e2e-tests-managed
  101. .PHONY: test.crds
  102. test.crds: cty crds.generate.tests ## Test CRDs for modification and backwards compatibility
  103. @$(INFO) $(CTY) test tests
  104. $(CTY) test tests
  105. @$(OK) No breaking CRD changes detected
  106. .PHONY: test.crds.update
  107. test.crds.update: cty crds.generate.tests ## Update the snapshots used by the CRD tests
  108. @$(INFO) $(CTY) test tests -u
  109. $(CTY) test tests -u
  110. @$(OK) Successfully updated all test snapshots
  111. .PHONY: build
  112. build: $(addprefix build-,$(ARCH)) ## Build binary
  113. .PHONY: build-%
  114. build-%: generate ## Build binary for the specified arch
  115. @$(INFO) go build $*
  116. $(BUILD_ARGS) GOOS=linux GOARCH=$* \
  117. go build -o '$(OUTPUT_DIR)/external-secrets-linux-$*' main.go
  118. @$(OK) go build $*
  119. lint: golangci-lint ## Run golangci-lint (set LINT_TARGET to run on specific module)
  120. @if [ -n "$(LINT_TARGET)" ]; then \
  121. $(INFO) Running golangci-lint on $(LINT_TARGET); \
  122. (cd $(LINT_TARGET) && $(GOLANGCI_LINT) run ./...) || exit 1; \
  123. $(OK) Finished linting $(LINT_TARGET); \
  124. else \
  125. $(INFO) Running golangci-lint on all modules; \
  126. FAILED=0; \
  127. MODULES=$$(find . -name go.mod -not -path "*/vendor/*" -not -path "*/e2e/*" -not -path "*/node_modules/*" -exec dirname {} \;); \
  128. for module in $$MODULES; do \
  129. echo "Linting $$module"; \
  130. (cd $$module && $(GOLANGCI_LINT) run ./...) || FAILED=$$((FAILED + 1)); \
  131. done; \
  132. if [ $$FAILED -ne 0 ]; then \
  133. $(ERR) Linting failed in $$FAILED module\(s\); \
  134. exit 1; \
  135. fi; \
  136. $(OK) Finished linting; \
  137. fi
  138. fmt: golangci-lint ## Ensure consistent code style (set LINT_TARGET to run on specific module)
  139. @go mod tidy
  140. @cd e2e/ && go mod tidy
  141. @go fmt ./...
  142. @if [ -n "$(LINT_TARGET)" ]; then \
  143. $(INFO) Running golangci-lint --fix on $(LINT_TARGET); \
  144. (cd $(LINT_TARGET) && $(GOLANGCI_LINT) run --fix ./...); \
  145. $(OK) Finished fixing $(LINT_TARGET); \
  146. else \
  147. $(INFO) Running golangci-lint --fix on all modules; \
  148. FAILED=0; \
  149. MODULES=$$(find . -name go.mod -not -path "*/vendor/*" -not -path "*/e2e/*" -not -path "*/node_modules/*" -exec dirname {} \;); \
  150. for module in $$MODULES; do \
  151. echo "Fixing $$module"; \
  152. (cd $$module && $(GOLANGCI_LINT) run --fix ./...) || FAILED=$$((FAILED + 1)); \
  153. done; \
  154. if [ $$FAILED -ne 0 ]; then \
  155. $(ERR) Fixing failed in $$FAILED module\(s\); \
  156. exit 1; \
  157. fi; \
  158. $(OK) Ensured consistent code style; \
  159. fi
  160. generate: ## Generate code and crds
  161. @./hack/crd.generate.sh $(BUNDLE_DIR) $(CRD_DIR)
  162. @$(OK) Finished generating deepcopy and crds
  163. # ====================================================================================
  164. # Local Utility
  165. # This is for running out-of-cluster locally, and is for convenience.
  166. # For more control, try running the binary directly with different arguments.
  167. run: generate ## Run app locally (without a k8s cluster)
  168. go run ./main.go
  169. manifests: helm.generate ## Generate manifests from helm chart
  170. mkdir -p $(OUTPUT_DIR)/deploy/manifests
  171. helm dependency build $(HELM_DIR)
  172. helm template external-secrets $(HELM_DIR) -f deploy/manifests/helm-values.yaml > $(OUTPUT_DIR)/deploy/manifests/external-secrets.yaml
  173. crds.install: generate ## Install CRDs into a cluster. This is for convenience
  174. kubectl apply -f $(BUNDLE_DIR) --server-side
  175. crds.uninstall: ## Uninstall CRDs from a cluster. This is for convenience
  176. kubectl delete -f $(BUNDLE_DIR)
  177. crds.generate.tests:
  178. ./hack/test.crds.generate.sh $(BUNDLE_DIR) tests/crds
  179. @$(OK) Finished generating crds for testing
  180. tilt-up: tilt manifests ## Generates the local manifests that tilt will use to deploy the controller's objects.
  181. $(LOCALBIN)/tilt up
  182. # ====================================================================================
  183. # Helm Chart
  184. helm.docs: ## Generate helm docs
  185. @cd $(HELM_DIR); \
  186. $(DOCKER) run --rm -v $(shell pwd)/$(HELM_DIR):/helm-docs -u $(shell id -u) docker.io/jnorwood/helm-docs:v1.7.0
  187. HELM_VERSION ?= $(shell helm show chart $(HELM_DIR) | grep '^version:' | sed 's/version: //g')
  188. helm.build: helm.generate ## Build helm chart
  189. @$(INFO) helm package
  190. @helm package $(HELM_DIR) --dependency-update --destination $(OUTPUT_DIR)/chart
  191. @mv $(OUTPUT_DIR)/chart/external-secrets-$(HELM_VERSION).tgz $(OUTPUT_DIR)/chart/external-secrets.tgz
  192. @$(OK) helm package
  193. # install_helm_plugin is for installing the provided plugin, if it doesn't exist
  194. # $1 - plugin name
  195. # $2 - plugin version
  196. # $3 - plugin url
  197. define install_helm_plugin
  198. @v=$$(helm plugin list | awk '$$1=="$(1)"{print $$2}'); \
  199. if [ -z "$$v" ]; then \
  200. $(INFO) "Installing $(1) v$(2)"; \
  201. helm plugin install --version $(2) $(3); \
  202. $(OK) "Installed $(1) v$(2)"; \
  203. elif [ "$$v" != "$(2)" ]; then \
  204. $(INFO) "Found $(1) $$v. Reinstalling v$(2)"; \
  205. helm plugin remove $(1); \
  206. helm plugin install --version $(2) $(3); \
  207. $(OK) "Reinstalled $(1) v$(2)"; \
  208. else \
  209. $(OK) "$(1) already at v$(2)"; \
  210. fi
  211. endef
  212. HELM_SCHEMA_NAME := schema
  213. HELM_SCHEMA_VER := 2.2.1
  214. HELM_SCHEMA_URL := https://github.com/losisin/helm-values-schema-json.git
  215. helm.schema.plugin:
  216. $(call install_helm_plugin,$(HELM_SCHEMA_NAME),$(HELM_SCHEMA_VER), $(HELM_SCHEMA_URL))
  217. HELM_UNITTEST_PLUGIN_NAME := unittest
  218. HELM_UNITTEST_PLUGIN_VER := 1.0.0
  219. HELM_UNITTEST_PLUGIN_URL := https://github.com/helm-unittest/helm-unittest.git
  220. helm.unittest.plugin:
  221. $(call install_helm_plugin,$(HELM_UNITTEST_PLUGIN_NAME),$(HELM_UNITTEST_PLUGIN_VER), $(HELM_UNITTEST_PLUGIN_URL))
  222. helm.schema.update: helm.schema.plugin
  223. @$(INFO) Generating values.schema.json
  224. @helm schema -f $(HELM_DIR)/values.yaml -o $(HELM_DIR)/values.schema.json
  225. @$(OK) Generated values.schema.json
  226. helm.generate:
  227. ./hack/helm.generate.sh $(BUNDLE_DIR) $(HELM_DIR)
  228. @$(OK) Finished generating helm chart files
  229. helm.test: helm.unittest.plugin helm.generate
  230. @helm unittest deploy/charts/external-secrets/
  231. helm.test.update: helm.unittest.plugin helm.generate
  232. @helm unittest -u deploy/charts/external-secrets/
  233. helm.update.appversion:
  234. @chartversion=$$(yq .version ./deploy/charts/external-secrets/Chart.yaml) ; \
  235. chartappversion=$$(yq .appVersion ./deploy/charts/external-secrets/Chart.yaml) ; \
  236. chartname=$$(yq .name ./deploy/charts/external-secrets/Chart.yaml) ; \
  237. $(INFO) Update chartname and chartversion string in test snapshots.; \
  238. sed -s -i "s/^\([[:space:]]\+helm\.sh\/chart:\).*/\1 $${chartname}-$${chartversion}/" ./deploy/charts/external-secrets/tests/__snapshot__/*.yaml.snap ; \
  239. sed -s -i "s/^\([[:space:]]\+app\.kubernetes\.io\/version:\).*/\1 $${chartappversion}/" ./deploy/charts/external-secrets/tests/__snapshot__/*.yaml.snap ; \
  240. sed -s -i "s/^\([[:space:]]\+image: ghcr\.io\/external-secrets\/external-secrets:\).*/\1$${chartappversion}/" ./deploy/charts/external-secrets/tests/__snapshot__/*.yaml.snap ; \
  241. $(OK) "Version strings updated"
  242. # ====================================================================================
  243. # Documentation
  244. .PHONY: docs
  245. docs: generate ## Generate docs
  246. $(MAKE) -C ./hack/api-docs build
  247. .PHONY: docs.publish
  248. docs.publish: generate ## Generate and deploys docs
  249. $(MAKE) -C ./hack/api-docs build.publish
  250. .PHONY: docs.serve
  251. docs.serve: ## Serve docs
  252. $(MAKE) -C ./hack/api-docs serve
  253. DOCS_VERSION ?= $(VERSION)
  254. .PHONY: docs.check
  255. docs.check: ## Check docs
  256. $(MAKE) -C ./hack/api-docs check DOCS_VERSION=$(DOCS_VERSION)
  257. .PHONY: docs.update
  258. docs.update: ## Update docs
  259. $(MAKE) -C ./hack/api-docs stability-support.update DOCS_VERSION=$(DOCS_VERSION)
  260. # ====================================================================================
  261. # Build Artifacts
  262. .PHONY: build.all
  263. build.all: docker.build helm.build ## Build all artifacts (docker image, helm chart)
  264. .PHONY: docker.image
  265. docker.image: ## Emit IMAGE_NAME:IMAGE_TAG
  266. @echo $(IMAGE_NAME):$(IMAGE_TAG)
  267. .PHONY: docker.imagename
  268. docker.imagename: ## Emit IMAGE_NAME
  269. @echo $(IMAGE_NAME)
  270. .PHONY: docker.tag
  271. docker.tag: ## Emit IMAGE_TAG
  272. @echo $(IMAGE_TAG)
  273. .PHONY: docker.build
  274. docker.build: $(addprefix build-,$(ARCH)) ## Build the docker image
  275. @$(INFO) $(DOCKER) build
  276. echo $(DOCKER) build -f $(DOCKERFILE) . $(DOCKER_BUILD_ARGS) -t $(IMAGE_NAME):$(IMAGE_TAG)
  277. DOCKER_BUILDKIT=1 $(DOCKER) build -f $(DOCKERFILE) . $(DOCKER_BUILD_ARGS) -t $(IMAGE_NAME):$(IMAGE_TAG)
  278. @$(OK) $(DOCKER) build
  279. .PHONY: docker.push
  280. docker.push: ## Push the docker image to the registry
  281. @$(INFO) $(DOCKER) push
  282. @$(DOCKER) push $(IMAGE_NAME):$(IMAGE_TAG)
  283. @$(OK) $(DOCKER) push
  284. # RELEASE_TAG is tag to promote. Default is promoting to main branch, but can be overriden
  285. # to promote a tag to a specific version.
  286. RELEASE_TAG ?= $(IMAGE_TAG)
  287. SOURCE_TAG ?= $(VERSION)$(TAG_SUFFIX)
  288. .PHONY: docker.promote
  289. docker.promote: ## Promote the docker image to the registry
  290. @$(INFO) promoting $(SOURCE_TAG) to $(RELEASE_TAG)
  291. $(DOCKER) manifest inspect --verbose $(IMAGE_NAME):$(SOURCE_TAG) > .tagmanifest
  292. for digest in $$(jq -r 'if type=="array" then .[].Descriptor.digest else .Descriptor.digest end' < .tagmanifest); do \
  293. $(DOCKER) pull $(IMAGE_NAME)@$$digest; \
  294. done
  295. $(DOCKER) manifest create $(IMAGE_NAME):$(RELEASE_TAG) \
  296. $$(jq -j '"--amend $(IMAGE_NAME)@" + if type=="array" then .[].Descriptor.digest else .Descriptor.digest end + " "' < .tagmanifest)
  297. $(DOCKER) manifest push $(IMAGE_NAME):$(RELEASE_TAG)
  298. @$(OK) $(DOCKER) push $(RELEASE_TAG) \
  299. # ====================================================================================
  300. # Terraform
  301. define run_terraform
  302. @cd $(TF_DIR)/$1/infrastructure && \
  303. terraform init && \
  304. $2 && \
  305. cd ../kubernetes && \
  306. terraform init && \
  307. $3
  308. endef
  309. tf.plan.%:
  310. $(call run_terraform,$*,terraform plan,terraform plan)
  311. tf.apply.%:
  312. $(call run_terraform,$*,terraform apply -auto-approve,terraform apply -auto-approve)
  313. tf.destroy.%:
  314. @cd $(TF_DIR)/$*/kubernetes && \
  315. terraform init && \
  316. terraform destroy -auto-approve && \
  317. cd ../infrastructure && \
  318. terraform init && \
  319. terraform destroy -auto-approve
  320. tf.fmt:
  321. @cd $(TF_DIR) && \
  322. terraform fmt -recursive
  323. # ====================================================================================
  324. # Help
  325. .PHONY: help
  326. # only comments after make target name are shown as help text
  327. help: ## Displays this help message
  328. @echo -e "$$(grep -hE '^\S+:.*##' $(MAKEFILE_LIST) | sed -e 's/:.*##\s*/|/' -e 's/^\(.\+\):\(.*\)/\\x1b[36m\1\\x1b[m:\2/' | column -c2 -t -s'|' | sort)"
  329. .PHONY: clean
  330. clean: ## Clean bins
  331. @$(INFO) clean
  332. @rm -f $(OUTPUT_DIR)/external-secrets-linux-*
  333. @$(OK) go build $*
  334. # ====================================================================================
  335. # Build Dependencies
  336. ifeq ($(OS),Windows_NT) # is Windows_NT on XP, 2000, 7, Vista, 10...
  337. detected_OS := windows
  338. real_OS := windows
  339. arch := x86_64
  340. else
  341. detected_OS := $(shell uname -s)
  342. real_OS := $(detected_OS)
  343. arch := $(shell uname -m)
  344. ifeq ($(detected_OS),Darwin)
  345. detected_OS := mac
  346. real_OS := darwin
  347. endif
  348. ifeq ($(detected_OS),Linux)
  349. detected_OS := linux
  350. real_OS := linux
  351. endif
  352. endif
  353. ## Location to install dependencies to
  354. LOCALBIN ?= $(shell pwd)/bin
  355. $(LOCALBIN):
  356. mkdir -p $(LOCALBIN)
  357. ## Tool Binaries
  358. TILT ?= $(LOCALBIN)/tilt
  359. CTY ?= $(LOCALBIN)/cty
  360. ENVTEST ?= $(LOCALBIN)/setup-envtest
  361. GOLANGCI_LINT ?= $(LOCALBIN)/golangci-lint
  362. ## Tool Versions
  363. GOLANGCI_VERSION := 2.4.0
  364. KUBERNETES_VERSION := 1.33.x
  365. TILT_VERSION := 0.33.21
  366. CTY_VERSION := 1.1.3
  367. .PHONY: envtest
  368. envtest: $(ENVTEST) ## Download envtest-setup locally if necessary.
  369. $(ENVTEST): $(LOCALBIN)
  370. test -s $(LOCALBIN)/setup-envtest || GOBIN=$(LOCALBIN) go install sigs.k8s.io/controller-runtime/tools/setup-envtest@latest
  371. .PHONY: golangci-lint
  372. .PHONY: $(GOLANGCI_LINT)
  373. golangci-lint: $(GOLANGCI_LINT) ## Download golangci-lint locally if necessary.
  374. $(GOLANGCI_LINT): $(LOCALBIN)
  375. test -s $(LOCALBIN)/golangci-lint && $(LOCALBIN)/golangci-lint version | grep -q $(GOLANGCI_VERSION) || \
  376. curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(LOCALBIN) v$(GOLANGCI_VERSION)
  377. .PHONY: tilt
  378. .PHONY: $(TILT)
  379. tilt: $(TILT) ## Download tilt locally if necessary. Architecture is locked at x86_64.
  380. $(TILT): $(LOCALBIN)
  381. test -s $(LOCALBIN)/tilt || curl -fsSL https://github.com/tilt-dev/tilt/releases/download/v$(TILT_VERSION)/tilt.$(TILT_VERSION).$(detected_OS).$(arch).tar.gz | tar -xz -C $(LOCALBIN) tilt
  382. .PHONY: cty
  383. .PHONY: $(CTY)
  384. cty: $(CTY) ## Download cty locally if necessary. Architecture is locked at x86_64.
  385. $(CTY): $(LOCALBIN)
  386. test -s $(LOCALBIN)/cty || curl -fsSL https://github.com/Skarlso/crd-to-sample-yaml/releases/download/v$(CTY_VERSION)/cty_$(real_OS)_amd64.tar.gz | tar -xz -C $(LOCALBIN) cty