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