Makefile 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  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. # 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. # check if there are any existing `git tag` values
  30. ifeq ($(shell git tag),)
  31. # no tags found - default to initial tag `v0.0.0`
  32. export VERSION := $(shell echo "v0.0.0-$$(git rev-list HEAD --count)-g$$(git describe --dirty --always)" | sed 's/-/./2' | sed 's/-/./2')
  33. else
  34. # use tags
  35. export VERSION := $(shell git describe --dirty --always --tags --exclude 'helm*' | sed 's/-/./2' | sed 's/-/./2')
  36. endif
  37. TAG_SUFFIX ?=
  38. export IMAGE_TAG ?= $(VERSION)$(TAG_SUFFIX)
  39. # ====================================================================================
  40. # Colors
  41. BLUE := $(shell printf "\033[34m")
  42. YELLOW := $(shell printf "\033[33m")
  43. RED := $(shell printf "\033[31m")
  44. GREEN := $(shell printf "\033[32m")
  45. CNone := $(shell printf "\033[0m")
  46. # ====================================================================================
  47. # Logger
  48. TIME_LONG = `date +%Y-%m-%d' '%H:%M:%S`
  49. TIME_SHORT = `date +%H:%M:%S`
  50. TIME = $(TIME_SHORT)
  51. INFO = echo ${TIME} ${BLUE}[ .. ]${CNone}
  52. WARN = echo ${TIME} ${YELLOW}[WARN]${CNone}
  53. ERR = echo ${TIME} ${RED}[FAIL]${CNone}
  54. OK = echo ${TIME} ${GREEN}[ OK ]${CNone}
  55. FAIL = (echo ${TIME} ${RED}[FAIL]${CNone} && false)
  56. # ====================================================================================
  57. # Conformance
  58. reviewable: generate docs manifests helm.generate helm.schema.update helm.docs lint ## Ensure a PR is ready for review.
  59. @go mod tidy
  60. @cd e2e/ && go mod tidy
  61. check-diff: reviewable ## Ensure branch is clean.
  62. @$(INFO) checking that branch is clean
  63. @test -z "$$(git status --porcelain)" || (echo "$$(git status --porcelain)" && $(FAIL))
  64. @$(OK) branch is clean
  65. update-deps:
  66. go get -u
  67. cd e2e && go get -u
  68. @go mod tidy
  69. @cd e2e/ && go mod tidy
  70. # ====================================================================================
  71. # Golang
  72. .PHONY: test
  73. test: generate envtest ## Run tests
  74. @$(INFO) go test unit-tests
  75. 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
  76. @$(OK) go test unit-tests
  77. .PHONY: test.e2e
  78. test.e2e: generate ## Run e2e tests
  79. @$(INFO) go test e2e-tests
  80. $(MAKE) -C ./e2e test
  81. @$(OK) go test e2e-tests
  82. .PHONY: test.e2e.managed
  83. test.e2e.managed: generate ## Run e2e tests managed
  84. @$(INFO) go test e2e-tests-managed
  85. $(MAKE) -C ./e2e test.managed
  86. @$(OK) go test e2e-tests-managed
  87. .PHONY: test.crds
  88. test.crds: cty crds.generate.tests ## Test CRDs for modification and backwards compatibility
  89. @$(INFO) $(CTY) test tests
  90. $(CTY) test tests
  91. @$(OK) No breaking CRD changes detected
  92. .PHONY: test.crds.update
  93. test.crds.update: cty crds.generate.tests ## Update the snapshots used by the CRD tests
  94. @$(INFO) $(CTY) test tests -u
  95. $(CTY) test tests -u
  96. @$(OK) Successfully updated all test snapshots
  97. .PHONY: build
  98. build: $(addprefix build-,$(ARCH)) ## Build binary
  99. .PHONY: build-%
  100. build-%: generate ## Build binary for the specified arch
  101. @$(INFO) go build $*
  102. $(BUILD_ARGS) GOOS=linux GOARCH=$* \
  103. go build -o '$(OUTPUT_DIR)/external-secrets-linux-$*' main.go
  104. @$(OK) go build $*
  105. lint: golangci-lint ## Run golangci-lint
  106. @if ! $(GOLANGCI_LINT) run; then \
  107. echo -e "\033[0;33mgolangci-lint failed: some checks can be fixed with \`\033[0;32mmake fmt\033[0m\033[0;33m\`\033[0m"; \
  108. exit 1; \
  109. fi
  110. @$(OK) Finished linting
  111. fmt: golangci-lint ## Ensure consistent code style
  112. @go mod tidy
  113. @cd e2e/ && go mod tidy
  114. @go fmt ./...
  115. @$(GOLANGCI_LINT) run --fix
  116. @$(OK) Ensured consistent code style
  117. generate: ## Generate code and crds
  118. @./hack/crd.generate.sh $(BUNDLE_DIR) $(CRD_DIR)
  119. @$(OK) Finished generating deepcopy and crds
  120. # ====================================================================================
  121. # Local Utility
  122. # This is for running out-of-cluster locally, and is for convenience.
  123. # For more control, try running the binary directly with different arguments.
  124. run: generate ## Run app locally (without a k8s cluster)
  125. go run ./main.go
  126. manifests: helm.generate ## Generate manifests from helm chart
  127. mkdir -p $(OUTPUT_DIR)/deploy/manifests
  128. helm dependency build $(HELM_DIR)
  129. helm template external-secrets $(HELM_DIR) -f deploy/manifests/helm-values.yaml > $(OUTPUT_DIR)/deploy/manifests/external-secrets.yaml
  130. crds.install: generate ## Install CRDs into a cluster. This is for convenience
  131. kubectl apply -f $(BUNDLE_DIR)
  132. crds.uninstall: ## Uninstall CRDs from a cluster. This is for convenience
  133. kubectl delete -f $(BUNDLE_DIR)
  134. crds.generate.tests:
  135. ./hack/test.crds.generate.sh $(BUNDLE_DIR) tests/crds
  136. @$(OK) Finished generating crds for testing
  137. tilt-up: tilt manifests ## Generates the local manifests that tilt will use to deploy the controller's objects.
  138. $(LOCALBIN)/tilt up
  139. # ====================================================================================
  140. # Helm Chart
  141. helm.docs: ## Generate helm docs
  142. @cd $(HELM_DIR); \
  143. docker run --rm -v $(shell pwd)/$(HELM_DIR):/helm-docs -u $(shell id -u) docker.io/jnorwood/helm-docs:v1.7.0
  144. HELM_VERSION ?= $(shell helm show chart $(HELM_DIR) | grep 'version:' | sed 's/version: //g')
  145. helm.build: helm.generate ## Build helm chart
  146. @$(INFO) helm package
  147. @helm package $(HELM_DIR) --dependency-update --destination $(OUTPUT_DIR)/chart
  148. @mv $(OUTPUT_DIR)/chart/external-secrets-$(HELM_VERSION).tgz $(OUTPUT_DIR)/chart/external-secrets.tgz
  149. @$(OK) helm package
  150. helm.schema.plugin:
  151. @$(INFO) Installing helm-values-schema-json plugin
  152. @helm plugin install https://github.com/losisin/helm-values-schema-json.git || true
  153. @$(OK) Installed helm-values-schema-json plugin
  154. helm.schema.update: helm.schema.plugin
  155. @$(INFO) Generating values.schema.json
  156. @helm schema -input $(HELM_DIR)/values.yaml -output $(HELM_DIR)/values.schema.json
  157. @$(OK) Generated values.schema.json
  158. helm.generate:
  159. ./hack/helm.generate.sh $(BUNDLE_DIR) $(HELM_DIR)
  160. @$(OK) Finished generating helm chart files
  161. helm.test: helm.generate
  162. @helm unittest deploy/charts/external-secrets/
  163. helm.test.update: helm.generate
  164. @helm unittest -u deploy/charts/external-secrets/
  165. helm.update.appversion:
  166. @chartversion=$$(yq .version ./deploy/charts/external-secrets/Chart.yaml) ; \
  167. chartappversion=$$(yq .appVersion ./deploy/charts/external-secrets/Chart.yaml) ; \
  168. chartname=$$(yq .name ./deploy/charts/external-secrets/Chart.yaml) ; \
  169. $(INFO) Update chartname and chartversion string in test snapshots.; \
  170. sed -s -i "s/^\([[:space:]]\+helm\.sh\/chart:\).*/\1 $${chartname}-$${chartversion}/" ./deploy/charts/external-secrets/tests/__snapshot__/*.yaml.snap ; \
  171. sed -s -i "s/^\([[:space:]]\+app\.kubernetes\.io\/version:\).*/\1 $${chartappversion}/" ./deploy/charts/external-secrets/tests/__snapshot__/*.yaml.snap ; \
  172. sed -s -i "s/^\([[:space:]]\+image: ghcr\.io\/external-secrets\/external-secrets:\).*/\1$${chartappversion}/" ./deploy/charts/external-secrets/tests/__snapshot__/*.yaml.snap ; \
  173. $(OK) "Version strings updated"
  174. # ====================================================================================
  175. # Documentation
  176. .PHONY: docs
  177. docs: generate ## Generate docs
  178. $(MAKE) -C ./hack/api-docs build
  179. .PHONY: docs.publish
  180. docs.publish: generate ## Generate and deploys docs
  181. $(MAKE) -C ./hack/api-docs build.publish
  182. .PHONY: docs.serve
  183. docs.serve: ## Serve docs
  184. $(MAKE) -C ./hack/api-docs serve
  185. # ====================================================================================
  186. # Build Artifacts
  187. .PHONY: build.all
  188. build.all: docker.build helm.build ## Build all artifacts (docker image, helm chart)
  189. .PHONY: docker.image
  190. docker.image: ## Emit IMAGE_NAME:IMAGE_TAG
  191. @echo $(IMAGE_NAME):$(IMAGE_TAG)
  192. .PHONY: docker.imagename
  193. docker.imagename: ## Emit IMAGE_NAME
  194. @echo $(IMAGE_NAME)
  195. .PHONY: docker.tag
  196. docker.tag: ## Emit IMAGE_TAG
  197. @echo $(IMAGE_TAG)
  198. .PHONY: docker.build
  199. docker.build: $(addprefix build-,$(ARCH)) ## Build the docker image
  200. @$(INFO) docker build
  201. echo docker build -f $(DOCKERFILE) . $(DOCKER_BUILD_ARGS) -t $(IMAGE_NAME):$(IMAGE_TAG)
  202. DOCKER_BUILDKIT=1 docker build -f $(DOCKERFILE) . $(DOCKER_BUILD_ARGS) -t $(IMAGE_NAME):$(IMAGE_TAG)
  203. @$(OK) docker build
  204. .PHONY: docker.push
  205. docker.push: ## Push the docker image to the registry
  206. @$(INFO) docker push
  207. @docker push $(IMAGE_NAME):$(IMAGE_TAG)
  208. @$(OK) docker push
  209. # RELEASE_TAG is tag to promote. Default is promoting to main branch, but can be overriden
  210. # to promote a tag to a specific version.
  211. RELEASE_TAG ?= $(IMAGE_TAG)
  212. SOURCE_TAG ?= $(VERSION)$(TAG_SUFFIX)
  213. .PHONY: docker.promote
  214. docker.promote: ## Promote the docker image to the registry
  215. @$(INFO) promoting $(SOURCE_TAG) to $(RELEASE_TAG)
  216. docker manifest inspect --verbose $(IMAGE_NAME):$(SOURCE_TAG) > .tagmanifest
  217. for digest in $$(jq -r 'if type=="array" then .[].Descriptor.digest else .Descriptor.digest end' < .tagmanifest); do \
  218. docker pull $(IMAGE_NAME)@$$digest; \
  219. done
  220. docker manifest create $(IMAGE_NAME):$(RELEASE_TAG) \
  221. $$(jq -j '"--amend $(IMAGE_NAME)@" + if type=="array" then .[].Descriptor.digest else .Descriptor.digest end + " "' < .tagmanifest)
  222. docker manifest push $(IMAGE_NAME):$(RELEASE_TAG)
  223. @$(OK) docker push $(RELEASE_TAG) \
  224. # ====================================================================================
  225. # Terraform
  226. tf.plan.%: ## Runs terraform plan for a provider
  227. @cd $(TF_DIR)/$*; \
  228. terraform init; \
  229. terraform plan
  230. tf.apply.%: ## Runs terraform apply for a provider
  231. @cd $(TF_DIR)/$*; \
  232. terraform init; \
  233. terraform apply -auto-approve
  234. tf.destroy.%: ## Runs terraform destroy for a provider
  235. @cd $(TF_DIR)/$*; \
  236. terraform init; \
  237. terraform destroy -auto-approve
  238. tf.show.%: ## Runs terraform show for a provider and outputs to a file
  239. @cd $(TF_DIR)/$*; \
  240. terraform init; \
  241. terraform plan -out tfplan.binary; \
  242. terraform show -json tfplan.binary > plan.json
  243. # ====================================================================================
  244. # Help
  245. .PHONY: help
  246. # only comments after make target name are shown as help text
  247. help: ## Displays this help message
  248. @echo -e "$$(grep -hE '^\S+:.*##' $(MAKEFILE_LIST) | sed -e 's/:.*##\s*/|/' -e 's/^\(.\+\):\(.*\)/\\x1b[36m\1\\x1b[m:\2/' | column -c2 -t -s'|' | sort)"
  249. .PHONY: clean
  250. clean: ## Clean bins
  251. @$(INFO) clean
  252. @rm -f $(OUTPUT_DIR)/external-secrets-linux-*
  253. @$(OK) go build $*
  254. # ====================================================================================
  255. # Build Dependencies
  256. ifeq ($(OS),Windows_NT) # is Windows_NT on XP, 2000, 7, Vista, 10...
  257. detected_OS := windows
  258. real_OS := windows
  259. arch := x86_64
  260. else
  261. detected_OS := $(shell uname -s)
  262. real_OS := $(detected_OS)
  263. arch := $(shell uname -m)
  264. ifeq ($(detected_OS),Darwin)
  265. detected_OS := mac
  266. real_OS := darwin
  267. endif
  268. ifeq ($(detected_OS),Linux)
  269. detected_OS := linux
  270. real_OS := linux
  271. endif
  272. endif
  273. ## Location to install dependencies to
  274. LOCALBIN ?= $(shell pwd)/bin
  275. $(LOCALBIN):
  276. mkdir -p $(LOCALBIN)
  277. ## Tool Binaries
  278. TILT ?= $(LOCALBIN)/tilt
  279. CTY ?= $(LOCALBIN)/cty
  280. ENVTEST ?= $(LOCALBIN)/setup-envtest
  281. GOLANGCI_LINT ?= $(LOCALBIN)/golangci-lint
  282. ## Tool Versions
  283. GOLANGCI_VERSION := 1.64.6
  284. KUBERNETES_VERSION := 1.30.x
  285. TILT_VERSION := 0.33.21
  286. CTY_VERSION := 1.1.3
  287. .PHONY: envtest
  288. envtest: $(ENVTEST) ## Download envtest-setup locally if necessary.
  289. $(ENVTEST): $(LOCALBIN)
  290. test -s $(LOCALBIN)/setup-envtest || GOBIN=$(LOCALBIN) go install sigs.k8s.io/controller-runtime/tools/setup-envtest@latest
  291. .PHONY: golangci-lint
  292. .PHONY: $(GOLANGCI_LINT)
  293. golangci-lint: $(GOLANGCI_LINT) ## Download golangci-lint locally if necessary.
  294. $(GOLANGCI_LINT): $(LOCALBIN)
  295. test -s $(LOCALBIN)/golangci-lint && $(LOCALBIN)/golangci-lint version --format short | grep -q $(GOLANGCI_VERSION) || \
  296. curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(LOCALBIN) v$(GOLANGCI_VERSION)
  297. .PHONY: tilt
  298. .PHONY: $(TILT)
  299. tilt: $(TILT) ## Download tilt locally if necessary. Architecture is locked at x86_64.
  300. $(TILT): $(LOCALBIN)
  301. 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
  302. .PHONY: cty
  303. .PHONY: $(CTY)
  304. cty: $(CTY) ## Download cty locally if necessary. Architecture is locked at x86_64.
  305. $(CTY): $(LOCALBIN)
  306. 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