Makefile 15 KB

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