Makefile 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  1. # set the shell to bash always
  2. SHELL := /usr/bin/env 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 ## Run tests
  88. @$(INFO) go test unit-tests
  89. @set -e; \
  90. snap=$$(mktemp); \
  91. ./hack/modfiles.sh snapshot "$$snap"; \
  92. trap "./hack/modfiles.sh restore $$snap" EXIT INT TERM; \
  93. $(MAKE) go-work; \
  94. KUBEBUILDER_ASSETS="$$($(ENVTEST) use $(KUBERNETES_VERSION) -p path --bin-dir $(LOCALBIN))" \
  95. go test -tags $(PROVIDER) work -v -race -coverprofile cover.out
  96. @$(OK) go test unit-tests
  97. .PHONY: test.e2e
  98. test.e2e: generate ## Run e2e tests
  99. @$(INFO) go test e2e-tests
  100. $(MAKE) -C ./e2e test
  101. @$(OK) go test e2e-tests
  102. .PHONY: test.e2e.managed
  103. test.e2e.managed: generate ## Run e2e tests managed
  104. @$(INFO) go test e2e-tests-managed
  105. $(MAKE) -C ./e2e test.managed
  106. @$(OK) go test e2e-tests-managed
  107. .PHONY: test.crds
  108. test.crds: cty crds.generate.tests ## Test CRDs for modification and backwards compatibility
  109. @$(INFO) $(CTY) test tests
  110. $(CTY) test tests
  111. @$(OK) No breaking CRD changes detected
  112. .PHONY: test.crds.update
  113. test.crds.update: cty crds.generate.tests ## Update the snapshots used by the CRD tests
  114. @$(INFO) $(CTY) test tests -u
  115. $(CTY) test tests -u
  116. @$(OK) Successfully updated all test snapshots
  117. .PHONY: build
  118. build: $(addprefix build-,$(ARCH)) ## Build binary
  119. PROVIDER ?= all_providers
  120. .PHONY: build-%
  121. build-%: generate ## Build binary for the specified arch
  122. @$(INFO) go build $*
  123. $(BUILD_ARGS) GOOS=linux GOARCH=$* \
  124. go build -tags $(PROVIDER) -o '$(OUTPUT_DIR)/external-secrets-linux-$*' main.go
  125. @$(OK) go build $*
  126. lint: golangci-lint ## Run golangci-lint (set LINT_TARGET to run on specific module, LINT_JOBS for parallel jobs)
  127. @if [ -n "$(LINT_TARGET)" ]; then \
  128. $(INFO) Running golangci-lint on $(LINT_TARGET); \
  129. (cd $(LINT_TARGET) && $(GOLANGCI_LINT) run ./...) || exit 1; \
  130. $(OK) Finished linting $(LINT_TARGET); \
  131. else \
  132. $(INFO) Running golangci-lint on all modules in parallel; \
  133. JOBS=$${LINT_JOBS:-1}; \
  134. TMPDIR=$$(mktemp -d); \
  135. GOLANGCI=$(GOLANGCI_LINT); \
  136. trap "rm -rf $$TMPDIR" EXIT; \
  137. export TMPDIR GOLANGCI; \
  138. find . -name go.mod -not -path "*/vendor/*" -not -path "*/e2e/*" -not -path "*/node_modules/*" -exec dirname {} \; | \
  139. xargs -n 1 -P $$JOBS sh -c ' \
  140. module="$$0"; \
  141. name=$$(echo "$$module" | sed "s/[\/\.]/_/g"); \
  142. echo "Linting $$module"; \
  143. if (cd "$$module" && $$GOLANGCI run ./... 2>&1); then \
  144. echo "✓ $$module" > "$$TMPDIR/$$name.success"; \
  145. else \
  146. echo "✗ $$module" > "$$TMPDIR/$$name.failed"; \
  147. exit 1; \
  148. fi \
  149. '; \
  150. FAILED=$$(find $$TMPDIR -name "*.failed" 2>/dev/null | wc -l | tr -d " "); \
  151. SUCCESS=$$(find $$TMPDIR -name "*.success" 2>/dev/null | wc -l | tr -d " "); \
  152. echo "Results: $$SUCCESS passed, $$FAILED failed"; \
  153. if [ $$FAILED -ne 0 ]; then \
  154. echo "Failed modules:"; \
  155. cat $$TMPDIR/*.failed 2>/dev/null || true; \
  156. $(ERR) Linting failed in $$FAILED module\(s\); \
  157. exit 1; \
  158. fi; \
  159. $(OK) Finished linting all modules; \
  160. fi
  161. generate: ## Generate code and crds
  162. @./hack/crd.generate.sh $(BUNDLE_DIR) $(CRD_DIR)
  163. @$(OK) Finished generating deepcopy and crds
  164. # ====================================================================================
  165. # Local Utility
  166. # This is for running out-of-cluster locally, and is for convenience.
  167. # For more control, try running the binary directly with different arguments.
  168. run: generate ## Run app locally (without a k8s cluster)
  169. go run -tags $(PROVIDER) ./main.go
  170. manifests: helm.generate ## Generate manifests from helm chart
  171. mkdir -p $(OUTPUT_DIR)/deploy/manifests
  172. helm dependency build $(HELM_DIR)
  173. helm template external-secrets $(HELM_DIR) -f deploy/manifests/helm-values.yaml > $(OUTPUT_DIR)/deploy/manifests/external-secrets.yaml
  174. crds.install: generate ## Install CRDs into a cluster. This is for convenience
  175. kubectl apply -f $(BUNDLE_DIR) --server-side
  176. crds.uninstall: ## Uninstall CRDs from a cluster. This is for convenience
  177. kubectl delete -f $(BUNDLE_DIR)
  178. crds.generate.tests:
  179. ./hack/test.crds.generate.sh $(BUNDLE_DIR) tests/crds
  180. @$(OK) Finished generating crds for testing
  181. tilt-up: tilt manifests ## Generates the local manifests that tilt will use to deploy the controller's objects.
  182. $(LOCALBIN)/tilt up
  183. # ====================================================================================
  184. # Helm Chart
  185. helm.docs: ## Generate helm docs
  186. @cd $(HELM_DIR); \
  187. $(DOCKER) run --rm -v $(shell pwd)/$(HELM_DIR):/helm-docs -u $(shell id -u) docker.io/jnorwood/helm-docs:v1.14.2
  188. HELM_VERSION ?= $(shell helm show chart $(HELM_DIR) | grep '^version:' | sed 's/version: //g')
  189. helm.build: helm.generate ## Build helm chart
  190. @$(INFO) helm package
  191. @helm package $(HELM_DIR) --dependency-update --destination $(OUTPUT_DIR)/chart
  192. @mv $(OUTPUT_DIR)/chart/external-secrets-$(HELM_VERSION).tgz $(OUTPUT_DIR)/chart/external-secrets.tgz
  193. @$(OK) helm package
  194. # install_helm_plugin is for installing the provided plugin, if it doesn't exist
  195. # $1 - plugin name
  196. # $2 - plugin version
  197. # $3 - plugin url
  198. define install_helm_plugin
  199. @v=$$(helm plugin list | awk '$$1=="$(1)"{print $$2}'); \
  200. if [ -z "$$v" ]; then \
  201. $(INFO) "Installing $(1) v$(2)"; \
  202. helm plugin install --version $(2) $(3); \
  203. $(OK) "Installed $(1) v$(2)"; \
  204. elif [ "$$v" != "$(2)" ]; then \
  205. $(INFO) "Found $(1) $$v. Reinstalling v$(2)"; \
  206. helm plugin remove $(1); \
  207. helm plugin install --version $(2) $(3); \
  208. $(OK) "Reinstalled $(1) v$(2)"; \
  209. else \
  210. $(OK) "$(1) already at v$(2)"; \
  211. fi
  212. endef
  213. HELM_SCHEMA_NAME := schema
  214. HELM_SCHEMA_VER := 2.2.1
  215. HELM_SCHEMA_URL := https://github.com/losisin/helm-values-schema-json.git
  216. helm.schema.plugin:
  217. $(call install_helm_plugin,$(HELM_SCHEMA_NAME),$(HELM_SCHEMA_VER), $(HELM_SCHEMA_URL))
  218. HELM_UNITTEST_PLUGIN_NAME := unittest
  219. HELM_UNITTEST_PLUGIN_VER := 1.0.0
  220. HELM_UNITTEST_PLUGIN_URL := https://github.com/helm-unittest/helm-unittest.git
  221. helm.unittest.plugin:
  222. $(call install_helm_plugin,$(HELM_UNITTEST_PLUGIN_NAME),$(HELM_UNITTEST_PLUGIN_VER), $(HELM_UNITTEST_PLUGIN_URL))
  223. helm.schema.update: helm.schema.plugin
  224. @$(INFO) Generating values.schema.json
  225. @helm schema -f $(HELM_DIR)/values.yaml -o $(HELM_DIR)/values.schema.json
  226. @$(OK) Generated values.schema.json
  227. helm.generate:
  228. ./hack/helm.generate.sh $(BUNDLE_DIR) $(HELM_DIR)
  229. @$(OK) Finished generating helm chart files
  230. helm.test: helm.unittest.plugin helm.generate
  231. @helm unittest deploy/charts/external-secrets/
  232. helm.test.update: helm.unittest.plugin helm.generate
  233. @helm unittest -u deploy/charts/external-secrets/
  234. helm.update.appversion:
  235. @chartversion=$$(yq .version ./deploy/charts/external-secrets/Chart.yaml) ; \
  236. chartappversion=$$(yq .appVersion ./deploy/charts/external-secrets/Chart.yaml) ; \
  237. chartname=$$(yq .name ./deploy/charts/external-secrets/Chart.yaml) ; \
  238. $(INFO) Update chartname and chartversion string in test snapshots.; \
  239. sed -s -i "s/^\([[:space:]]\+helm\.sh\/chart:\).*/\1 $${chartname}-$${chartversion}/" ./deploy/charts/external-secrets/tests/__snapshot__/*.yaml.snap ; \
  240. sed -s -i "s/^\([[:space:]]\+app\.kubernetes\.io\/version:\).*/\1 $${chartappversion}/" ./deploy/charts/external-secrets/tests/__snapshot__/*.yaml.snap ; \
  241. sed -s -i "s/^\([[:space:]]\+image: ghcr\.io\/external-secrets\/external-secrets:\).*/\1$${chartappversion}/" ./deploy/charts/external-secrets/tests/__snapshot__/*.yaml.snap ; \
  242. $(OK) "Version strings updated"
  243. # ====================================================================================
  244. # Documentation
  245. .PHONY: docs
  246. docs: generate ## Generate docs
  247. $(MAKE) -C ./hack/api-docs build
  248. .PHONY: docs.publish
  249. docs.publish: generate ## Generate and deploys docs
  250. $(MAKE) -C ./hack/api-docs build.publish
  251. .PHONY: docs.serve
  252. docs.serve: ## Serve docs
  253. $(MAKE) -C ./hack/api-docs serve
  254. DOCS_VERSION ?= $(VERSION)
  255. .PHONY: docs.update
  256. docs.update: ## Update docs
  257. $(MAKE) -C ./hack/api-docs stability-support.update DOCS_VERSION=$(DOCS_VERSION)
  258. # ====================================================================================
  259. # Build Artifacts
  260. .PHONY: build.all
  261. build.all: docker.build helm.build ## Build all artifacts (docker image, helm chart)
  262. .PHONY: docker.image
  263. docker.image: ## Emit IMAGE_NAME:IMAGE_TAG
  264. @echo $(IMAGE_NAME):$(IMAGE_TAG)
  265. .PHONY: docker.imagename
  266. docker.imagename: ## Emit IMAGE_NAME
  267. @echo $(IMAGE_NAME)
  268. .PHONY: docker.tag
  269. docker.tag: ## Emit IMAGE_TAG
  270. @echo $(IMAGE_TAG)
  271. .PHONY: docker.build
  272. docker.build: $(addprefix build-,$(ARCH)) ## Build the docker image
  273. @$(INFO) $(DOCKER) build
  274. echo $(DOCKER) buildx build -f $(DOCKERFILE) . $(DOCKER_BUILD_ARGS) -t $(IMAGE_NAME):$(IMAGE_TAG)
  275. $(DOCKER) buildx build -f $(DOCKERFILE) . $(DOCKER_BUILD_ARGS) -t $(IMAGE_NAME):$(IMAGE_TAG)
  276. @$(OK) $(DOCKER) build
  277. .PHONY: docker.push
  278. docker.push: ## Push the docker image to the registry
  279. @$(INFO) $(DOCKER) push
  280. @$(DOCKER) push $(IMAGE_NAME):$(IMAGE_TAG)
  281. @$(OK) $(DOCKER) push
  282. # RELEASE_TAG is tag to promote. Default is promoting to main branch, but can be overriden
  283. # to promote a tag to a specific version.
  284. RELEASE_TAG ?= $(IMAGE_TAG)
  285. SOURCE_TAG ?= $(VERSION)$(TAG_SUFFIX)
  286. .PHONY: docker.promote
  287. docker.promote: ## Promote the docker image to the registry
  288. @$(INFO) promoting $(SOURCE_TAG) to $(RELEASE_TAG)
  289. $(DOCKER) manifest inspect --verbose $(IMAGE_NAME):$(SOURCE_TAG) > .tagmanifest
  290. for digest in $$(jq -r 'if type=="array" then .[] | select(.Descriptor.platform.architecture != "unknown") | .Descriptor.digest else .Descriptor.digest end' < .tagmanifest); do \
  291. $(DOCKER) pull $(IMAGE_NAME)@$$digest; \
  292. done
  293. $(DOCKER) manifest create $(IMAGE_NAME):$(RELEASE_TAG) \
  294. $$(jq -j 'if type=="array" then [.[] | select(.Descriptor.platform.architecture != "unknown")] | map("--amend $(IMAGE_NAME)@" + .Descriptor.digest) | join(" ") else "--amend $(IMAGE_NAME)@" + .Descriptor.digest end' < .tagmanifest)
  295. $(DOCKER) manifest push $(IMAGE_NAME):$(RELEASE_TAG)
  296. @$(OK) $(DOCKER) push $(RELEASE_TAG) \
  297. # ====================================================================================
  298. # Terraform
  299. define run_terraform
  300. @cd $(TF_DIR)/$1/infrastructure && \
  301. terraform init && \
  302. $2 && \
  303. cd ../kubernetes && \
  304. terraform init && \
  305. $3
  306. endef
  307. tf.plan.%:
  308. $(call run_terraform,$*,terraform plan,terraform plan)
  309. tf.apply.%:
  310. $(call run_terraform,$*,terraform apply -auto-approve,terraform apply -auto-approve)
  311. tf.destroy.%:
  312. @cd $(TF_DIR)/$*/kubernetes && \
  313. terraform init && \
  314. terraform destroy -auto-approve && \
  315. cd ../infrastructure && \
  316. terraform init && \
  317. terraform destroy -auto-approve
  318. tf.fmt:
  319. @cd $(TF_DIR) && \
  320. terraform fmt -recursive
  321. # ====================================================================================
  322. # Help
  323. .PHONY: help
  324. # only comments after make target name are shown as help text
  325. help: ## Displays this help message
  326. @echo -e "$$(grep -hE '^\S+:.*##' $(MAKEFILE_LIST) | sed -e 's/:.*##\s*/|/' -e 's/^\(.\+\):\(.*\)/\\x1b[36m\1\\x1b[m:\2/' | column -c2 -t -s'|' | sort)"
  327. .PHONY: clean
  328. clean: ## Clean bins
  329. @$(INFO) clean
  330. @rm -f $(OUTPUT_DIR)/external-secrets-linux-*
  331. @$(OK) go build $*
  332. # ====================================================================================
  333. # Build Dependencies
  334. detected_OS := $(shell uname -s)
  335. real_OS := $(detected_OS)
  336. arch := $(shell uname -m)
  337. ifeq ($(detected_OS),Darwin)
  338. detected_OS := mac
  339. real_OS := darwin
  340. endif
  341. ifeq ($(detected_OS),Linux)
  342. detected_OS := linux
  343. real_OS := linux
  344. endif
  345. ## Location to install dependencies to
  346. LOCALBIN ?= $(shell pwd)/bin
  347. $(LOCALBIN):
  348. mkdir -p $(LOCALBIN)
  349. ## Tool Binaries
  350. TILT ?= $(LOCALBIN)/tilt
  351. CTY ?= $(LOCALBIN)/cty
  352. ENVTEST ?= $(LOCALBIN)/setup-envtest
  353. GOLANGCI_LINT ?= $(LOCALBIN)/golangci-lint
  354. LINT_TARGET ?= ""
  355. ## Tool Versions
  356. GOLANGCI_VERSION := 2.11.3
  357. KUBERNETES_VERSION := 1.33.x
  358. TILT_VERSION := 0.33.21
  359. CTY_VERSION := 1.1.3
  360. .PHONY: envtest
  361. envtest: $(ENVTEST) ## Download envtest-setup locally if necessary.
  362. $(ENVTEST): $(LOCALBIN)
  363. test -s $(LOCALBIN)/setup-envtest || GOBIN=$(LOCALBIN) go install sigs.k8s.io/controller-runtime/tools/setup-envtest@latest
  364. .PHONY: golangci-lint
  365. .PHONY: $(GOLANGCI_LINT)
  366. golangci-lint: $(GOLANGCI_LINT) ## Download golangci-lint locally if necessary.
  367. $(GOLANGCI_LINT): $(LOCALBIN)
  368. test -s $(LOCALBIN)/golangci-lint && $(LOCALBIN)/golangci-lint version | grep -q $(GOLANGCI_VERSION) || \
  369. curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(LOCALBIN) v$(GOLANGCI_VERSION)
  370. .PHONY: tilt
  371. .PHONY: $(TILT)
  372. tilt: $(TILT) ## Download tilt locally if necessary. Architecture is locked at x86_64.
  373. $(TILT): $(LOCALBIN)
  374. 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
  375. .PHONY: cty
  376. .PHONY: $(CTY)
  377. cty: $(CTY) ## Download cty locally if necessary. Architecture is locked at x86_64.
  378. $(CTY): $(LOCALBIN)
  379. 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