Makefile 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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
  7. BUILD_ARGS ?=
  8. # default target is build
  9. .DEFAULT_GOAL := all
  10. .PHONY: all
  11. all: $(addprefix build-,$(ARCH))
  12. # Image registry for build/push image targets
  13. export IMAGE_REGISTRY ?= ghcr.io/external-secrets/external-secrets
  14. #Valid licenses for license.check
  15. LICENSES ?= Apache-2.0|MIT|BSD-3-Clause|ISC|MPL-2.0|BSD-2-Clause|Unknown
  16. BUNDLE_DIR ?= deploy/crds
  17. CRD_DIR ?= config/crds
  18. HELM_DIR ?= deploy/charts/external-secrets
  19. TF_DIR ?= terraform
  20. OUTPUT_DIR ?= bin
  21. # Get the currently used golang install path (in GOPATH/bin, unless GOBIN is set)
  22. ifeq (,$(shell go env GOBIN))
  23. GOBIN=$(shell go env GOPATH)/bin
  24. else
  25. GOBIN=$(shell go env GOBIN)
  26. endif
  27. # check if there are any existing `git tag` values
  28. ifeq ($(shell git tag),)
  29. # no tags found - default to initial tag `v0.0.0`
  30. export VERSION := $(shell echo "v0.0.0-$$(git rev-list HEAD --count)-g$$(git describe --dirty --always)" | sed 's/-/./2' | sed 's/-/./2')
  31. else
  32. # use tags
  33. export VERSION := $(shell git describe --dirty --always --tags --exclude 'helm*' | sed 's/-/./2' | sed 's/-/./2')
  34. endif
  35. # ====================================================================================
  36. # Colors
  37. BLUE := $(shell printf "\033[34m")
  38. YELLOW := $(shell printf "\033[33m")
  39. RED := $(shell printf "\033[31m")
  40. GREEN := $(shell printf "\033[32m")
  41. CNone := $(shell printf "\033[0m")
  42. # ====================================================================================
  43. # Logger
  44. TIME_LONG = `date +%Y-%m-%d' '%H:%M:%S`
  45. TIME_SHORT = `date +%H:%M:%S`
  46. TIME = $(TIME_SHORT)
  47. INFO = echo ${TIME} ${BLUE}[ .. ]${CNone}
  48. WARN = echo ${TIME} ${YELLOW}[WARN]${CNone}
  49. ERR = echo ${TIME} ${RED}[FAIL]${CNone}
  50. OK = echo ${TIME} ${GREEN}[ OK ]${CNone}
  51. FAIL = (echo ${TIME} ${RED}[FAIL]${CNone} && false)
  52. # ====================================================================================
  53. # Conformance
  54. reviewable: generate helm.generate lint ## Ensure a PR is ready for review.
  55. @go mod tidy
  56. golicenses.check: ## Check install of go-licenses
  57. @if ! go-licenses >> /dev/null 2>&1; then \
  58. echo -e "\033[0;33mgo-licenses is not installed: run go install github.com/google/go-licenses@latest" ; \
  59. exit 1; \
  60. fi
  61. license.check: golicenses.check
  62. @$(INFO) running dependency license checks
  63. @ok=0; go-licenses csv github.com/external-secrets/external-secrets 2>/dev/null | \
  64. grep -v -E '${LICENSES}' | \
  65. tr "," " " | awk '{print "Invalid License " $$3 " for dependency " $$1 }'|| ok=1; \
  66. if [[ $$ok -eq 1 ]]; then $(OK) dependencies are compliant; else $(FAIL); fi
  67. check-diff: reviewable ## Ensure branch is clean.
  68. @$(INFO) checking that branch is clean
  69. @test -z "$$(git status --porcelain)" || (echo "$$(git status --porcelain)" && $(FAIL))
  70. @$(OK) branch is clean
  71. # ====================================================================================
  72. # Golang
  73. .PHONY: test
  74. test: generate ## Run tests
  75. @$(INFO) go test unit-tests
  76. 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: build
  89. build: $(addprefix build-,$(ARCH)) ## Build binary
  90. .PHONY: build-%
  91. build-%: generate ## Build binary for the specified arch
  92. @$(INFO) go build $*
  93. @CGO_ENABLED=0 GOOS=linux GOARCH=$* \
  94. go build -o '$(OUTPUT_DIR)/external-secrets-linux-$*' main.go
  95. @$(OK) go build $*
  96. lint.check: ## Check install of golanci-lint
  97. @if ! golangci-lint --version > /dev/null 2>&1; then \
  98. echo -e "\033[0;33mgolangci-lint is not installed: run \`\033[0;32mmake lint.install\033[0m\033[0;33m\` or install it from https://golangci-lint.run\033[0m"; \
  99. exit 1; \
  100. fi
  101. lint.install: ## Install golangci-lint to the go bin dir
  102. @if ! golangci-lint --version > /dev/null 2>&1; then \
  103. echo "Installing golangci-lint"; \
  104. curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(GOBIN) v1.42.1; \
  105. fi
  106. lint: lint.check ## 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: lint.check ## Ensure consistent code style
  113. @go mod tidy
  114. @go fmt ./...
  115. @golangci-lint run --fix > /dev/null 2>&1 || true
  116. @$(OK) Ensured consistent code style
  117. generate: ## Generate code and crds
  118. @go run sigs.k8s.io/controller-tools/cmd/controller-gen object:headerFile="hack/boilerplate.go.txt" paths="./..."
  119. @go run sigs.k8s.io/controller-tools/cmd/controller-gen crd paths="./..." output:crd:artifacts:config=$(CRD_DIR)/bases
  120. # Remove extra header lines in generated CRDs
  121. @for i in $(CRD_DIR)/bases/*.yaml; do \
  122. tail -n +2 <"$$i" >"$$i.bkp" && \
  123. cp "$$i.bkp" "$$i" && \
  124. rm "$$i.bkp"; \
  125. done
  126. @yq e '.spec.conversion.strategy = "Webhook" | .spec.conversion.webhook.conversionReviewVersions = ["v1"] | .spec.conversion.webhook.clientConfig.service.name = "kubernetes" | .spec.conversion.webhook.clientConfig.service.namespace = "default" | .spec.conversion.webhook.clientConfig.service.path = "/convert"' $(CRD_DIR)/bases/* > $(BUNDLE_DIR)/bundle.yaml
  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 template external-secrets $(HELM_DIR) -f deploy/manifests/helm-values.yaml > $(OUTPUT_DIR)/deploy/manifests/external-secrets.yaml
  137. crds.install: generate ## Install CRDs into a cluster. This is for convenience
  138. kubectl apply -f $(BUNDLE_DIR)
  139. crds.uninstall: ## Uninstall CRDs from a cluster. This is for convenience
  140. kubectl delete -f $(BUNDLE_DIR)
  141. # ====================================================================================
  142. # Helm Chart
  143. helm.docs: ## Generate helm docs
  144. @cd $(HELM_DIR); \
  145. docker run --rm -v $(shell pwd)/$(HELM_DIR):/helm-docs -u $(shell id -u) jnorwood/helm-docs:v1.5.0
  146. HELM_VERSION ?= $(shell helm show chart $(HELM_DIR) | grep 'version:' | sed 's/version: //g')
  147. helm.build: helm.generate ## Build helm chart
  148. @$(INFO) helm package
  149. @helm package $(HELM_DIR) --dependency-update --destination $(OUTPUT_DIR)/chart
  150. @mv $(OUTPUT_DIR)/chart/external-secrets-$(HELM_VERSION).tgz $(OUTPUT_DIR)/chart/external-secrets.tgz
  151. @$(OK) helm package
  152. helm.generate:
  153. # Split the generated bundle yaml file to inject control flags
  154. @for i in $(BUNDLE_DIR)/*.yaml; do \
  155. yq e -Ns '"$(HELM_DIR)/templates/crds/" + .spec.names.singular' "$$i"; \
  156. done
  157. # Add helm if statement for controlling the install of CRDs
  158. @for i in $(HELM_DIR)/templates/crds/*.yml; do \
  159. export CRDS_FLAG_NAME="create$$(yq e '.spec.names.kind' $$i)"; \
  160. cp "$$i" "$$i.bkp"; \
  161. if [[ "$$CRDS_FLAG_NAME" == *"Cluster"* ]]; then \
  162. echo "{{- if and (.Values.installCRDs) (.Values.crds.$$CRDS_FLAG_NAME) }}" > "$$i"; \
  163. else \
  164. echo "{{- if .Values.installCRDs }}" > "$$i"; \
  165. fi; \
  166. cat "$$i.bkp" >> "$$i" && \
  167. echo "{{- end }}" >> "$$i" && \
  168. rm "$$i.bkp" && \
  169. if [[ "$$OSTYPE" == "darwin"* ]]; then \
  170. SEDPRG="gsed"; \
  171. else \
  172. SEDPRG="sed"; \
  173. fi; \
  174. $$SEDPRG -i 's/name: kubernetes/name: {{ include "external-secrets.fullname" . }}-webhook/g' "$$i" && \
  175. $$SEDPRG -i 's/namespace: default/namespace: {{ .Release.Namespace | quote }}/g' "$$i" && \
  176. mv "$$i" "$${i%.yml}.yaml"; \
  177. done
  178. @$(OK) Finished generating helm chart files
  179. # ====================================================================================
  180. # Documentation
  181. .PHONY: docs
  182. docs: generate ## Generate docs
  183. $(MAKE) -C ./hack/api-docs build
  184. .PHONY: docs.publish
  185. docs.publish: generate ## Generate and deploys docs
  186. $(MAKE) -C ./hack/api-docs build.publish
  187. .PHONY: docs.serve
  188. docs.serve: ## Serve docs
  189. $(MAKE) -C ./hack/api-docs serve
  190. # ====================================================================================
  191. # Build Artifacts
  192. build.all: docker.build helm.build ## Build all artifacts (docker image, helm chart)
  193. docker.build: $(addprefix build-,$(ARCH)) ## Build the docker image
  194. @$(INFO) docker build
  195. @docker build . $(BUILD_ARGS) -t $(IMAGE_REGISTRY):$(VERSION)
  196. @$(OK) docker build
  197. docker.push: ## Push the docker image to the registry
  198. @$(INFO) docker push
  199. @docker push $(IMAGE_REGISTRY):$(VERSION)
  200. @$(OK) docker push
  201. # RELEASE_TAG is tag to promote. Default is promoting to main branch, but can be overriden
  202. # to promote a tag to a specific version.
  203. RELEASE_TAG ?= main
  204. SOURCE_TAG ?= $(VERSION)
  205. docker.promote: ## Promote the docker image to the registry
  206. @$(INFO) promoting $(SOURCE_TAG) to $(RELEASE_TAG)
  207. docker manifest inspect $(IMAGE_REGISTRY):$(SOURCE_TAG) > .tagmanifest
  208. for digest in $$(jq -r '.manifests[].digest' < .tagmanifest); do \
  209. docker pull $(IMAGE_REGISTRY)@$$digest; \
  210. done
  211. docker manifest create $(IMAGE_REGISTRY):$(RELEASE_TAG) \
  212. $$(jq -j '"--amend $(IMAGE_REGISTRY)@" + .manifests[].digest + " "' < .tagmanifest)
  213. docker manifest push $(IMAGE_REGISTRY):$(RELEASE_TAG)
  214. @$(OK) docker push $(RELEASE_TAG) \
  215. docker.sign: ## Sign
  216. @$(INFO) signing $(IMAGE_REGISTRY):$(RELEASE_TAG)
  217. crane digest $(IMAGE_REGISTRY):$(RELEASE_TAG) > .digest
  218. cosign sign $(IMAGE_REGISTRY)@$$(cat .digest)
  219. @$(OK) cosign sign $(IMAGE_REGISTRY):$(RELEASE_TAG)
  220. # ====================================================================================
  221. # Terraform
  222. tf.plan.%: ## Runs terrform plan for a provider
  223. @cd $(TF_DIR)/$*; \
  224. terraform init; \
  225. terraform plan
  226. tf.apply.%: ## Runs terrform apply for a provider
  227. @cd $(TF_DIR)/$*; \
  228. terraform init; \
  229. terraform apply -auto-approve
  230. tf.destroy.%: ## Runs terrform destroy for a provider
  231. @cd $(TF_DIR)/$*; \
  232. terraform init; \
  233. terraform destroy -auto-approve
  234. tf.show.%: ## Runs terrform show for a provider and outputs to a file
  235. @cd $(TF_DIR)/$*; \
  236. terraform init; \
  237. terraform plan -out tfplan.binary; \
  238. terraform show -json tfplan.binary > plan.json
  239. # ====================================================================================
  240. # Help
  241. # only comments after make target name are shown as help text
  242. help: ## Displays this help message
  243. @echo -e "$$(grep -hE '^\S+:.*##' $(MAKEFILE_LIST) | sed -e 's/:.*##\s*/:/' -e 's/^\(.\+\):\(.*\)/\\x1b[36m\1\\x1b[m:\2/' | column -c2 -t -s : | sort)"