Makefile 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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. CRD_DIR ?= deploy/crds
  15. HELM_DIR ?= deploy/charts/external-secrets
  16. TF_DIR ?= terraform
  17. OUTPUT_DIR ?= bin
  18. # Get the currently used golang install path (in GOPATH/bin, unless GOBIN is set)
  19. ifeq (,$(shell go env GOBIN))
  20. GOBIN=$(shell go env GOPATH)/bin
  21. else
  22. GOBIN=$(shell go env GOBIN)
  23. endif
  24. # check if there are any existing `git tag` values
  25. ifeq ($(shell git tag),)
  26. # no tags found - default to initial tag `v0.0.0`
  27. export VERSION := $(shell echo "v0.0.0-$$(git rev-list HEAD --count)-g$$(git describe --dirty --always)" | sed 's/-/./2' | sed 's/-/./2')
  28. else
  29. # use tags
  30. export VERSION := $(shell git describe --dirty --always --tags --exclude 'helm*' | sed 's/-/./2' | sed 's/-/./2')
  31. endif
  32. # ====================================================================================
  33. # Colors
  34. BLUE := $(shell printf "\033[34m")
  35. YELLOW := $(shell printf "\033[33m")
  36. RED := $(shell printf "\033[31m")
  37. GREEN := $(shell printf "\033[32m")
  38. CNone := $(shell printf "\033[0m")
  39. # ====================================================================================
  40. # Logger
  41. TIME_LONG = `date +%Y-%m-%d' '%H:%M:%S`
  42. TIME_SHORT = `date +%H:%M:%S`
  43. TIME = $(TIME_SHORT)
  44. INFO = echo ${TIME} ${BLUE}[ .. ]${CNone}
  45. WARN = echo ${TIME} ${YELLOW}[WARN]${CNone}
  46. ERR = echo ${TIME} ${RED}[FAIL]${CNone}
  47. OK = echo ${TIME} ${GREEN}[ OK ]${CNone}
  48. FAIL = (echo ${TIME} ${RED}[FAIL]${CNone} && false)
  49. # ====================================================================================
  50. # Conformance
  51. reviewable: generate helm.generate lint ## Ensure a PR is ready for review.
  52. @go mod tidy
  53. check-diff: reviewable ## Ensure branch is clean.
  54. @$(INFO) checking that branch is clean
  55. @test -z "$$(git status --porcelain)" || (echo "$$(git status --porcelain)" && $(FAIL))
  56. @$(OK) branch is clean
  57. # ====================================================================================
  58. # Golang
  59. .PHONY: test
  60. test: generate ## Run tests
  61. @$(INFO) go test unit-tests
  62. go test -race -v $(shell go list ./... | grep -v e2e) -coverprofile cover.out
  63. @$(OK) go test unit-tests
  64. .PHONY: test.e2e
  65. test.e2e: generate ## Run e2e tests
  66. @$(INFO) go test e2e-tests
  67. $(MAKE) -C ./e2e test
  68. @$(OK) go test e2e-tests
  69. .PHONY: test.e2e.managed
  70. test.e2e.managed: generate ## Run e2e tests managed
  71. @$(INFO) go test e2e-tests-managed
  72. $(MAKE) -C ./e2e test.managed
  73. @$(OK) go test e2e-tests-managed
  74. .PHONY: build
  75. build: $(addprefix build-,$(ARCH)) ## Build binary
  76. .PHONY: build-%
  77. build-%: generate ## Build binary for the specified arch
  78. @$(INFO) go build $*
  79. @CGO_ENABLED=0 GOOS=linux GOARCH=$* \
  80. go build -o '$(OUTPUT_DIR)/external-secrets-linux-$*' main.go
  81. @$(OK) go build $*
  82. lint.check: ## Check install of golanci-lint
  83. @if ! golangci-lint --version > /dev/null 2>&1; then \
  84. 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"; \
  85. exit 1; \
  86. fi
  87. lint.install: ## Install golangci-lint to the go bin dir
  88. @if ! golangci-lint --version > /dev/null 2>&1; then \
  89. echo "Installing golangci-lint"; \
  90. curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(GOBIN) v1.42.1; \
  91. fi
  92. lint: lint.check ## Run golangci-lint
  93. @if ! golangci-lint run; then \
  94. echo -e "\033[0;33mgolangci-lint failed: some checks can be fixed with \`\033[0;32mmake fmt\033[0m\033[0;33m\`\033[0m"; \
  95. exit 1; \
  96. fi
  97. @$(OK) Finished linting
  98. fmt: lint.check ## Ensure consistent code style
  99. @go mod tidy
  100. @go fmt ./...
  101. @golangci-lint run --fix > /dev/null 2>&1 || true
  102. @$(OK) Ensured consistent code style
  103. generate: ## Generate code and crds
  104. @go run sigs.k8s.io/controller-tools/cmd/controller-gen object:headerFile="hack/boilerplate.go.txt" paths="./..."
  105. @go run sigs.k8s.io/controller-tools/cmd/controller-gen crd paths="./..." output:crd:artifacts:config=$(CRD_DIR)
  106. # Remove extra header lines in generated CRDs
  107. @for i in $(CRD_DIR)/*.yaml; do \
  108. tail -n +2 <"$$i" >"$$i.bkp" && \
  109. cp "$$i.bkp" "$$i" && \
  110. rm "$$i.bkp"; \
  111. done
  112. @$(OK) Finished generating deepcopy and crds
  113. # ====================================================================================
  114. # Local Utility
  115. # This is for running out-of-cluster locally, and is for convenience.
  116. # For more control, try running the binary directly with different arguments.
  117. run: generate ## Run app locally (without a k8s cluster)
  118. go run ./main.go
  119. manifests: helm.generate ## Generate manifests from helm chart
  120. mkdir -p $(OUTPUT_DIR)/deploy/manifests
  121. helm template external-secrets $(HELM_DIR) -f deploy/manifests/helm-values.yaml > $(OUTPUT_DIR)/deploy/manifests/external-secrets.yaml
  122. crds.install: generate ## Install CRDs into a cluster. This is for convenience
  123. kubectl apply -f $(CRD_DIR)
  124. crds.uninstall: ## Uninstall CRDs from a cluster. This is for convenience
  125. kubectl delete -f $(CRD_DIR)
  126. # ====================================================================================
  127. # Helm Chart
  128. helm.docs: ## Generate helm docs
  129. @cd $(HELM_DIR); \
  130. docker run --rm -v $(shell pwd)/$(HELM_DIR):/helm-docs -u $(shell id -u) jnorwood/helm-docs:v1.5.0
  131. HELM_VERSION ?= $(shell helm show chart $(HELM_DIR) | grep 'version:' | sed 's/version: //g')
  132. helm.build: helm.generate ## Build helm chart
  133. @$(INFO) helm package
  134. @helm package $(HELM_DIR) --dependency-update --destination $(OUTPUT_DIR)/chart
  135. @mv $(OUTPUT_DIR)/chart/external-secrets-$(HELM_VERSION).tgz $(OUTPUT_DIR)/chart/external-secrets.tgz
  136. @$(OK) helm package
  137. helm.generate: helm.docs ## Copy crds to helm chart directory
  138. @cp $(CRD_DIR)/*.yaml $(HELM_DIR)/templates/crds/
  139. # Add helm if statement for controlling the install of CRDs
  140. @for i in $(HELM_DIR)/templates/crds/*.yaml; do \
  141. cp "$$i" "$$i.bkp" && \
  142. echo "{{- if .Values.installCRDs }}" > "$$i" && \
  143. cat "$$i.bkp" >> "$$i" && \
  144. echo "{{- end }}" >> "$$i" && \
  145. rm "$$i.bkp"; \
  146. done
  147. @$(OK) Finished generating helm chart files
  148. # ====================================================================================
  149. # Documentation
  150. .PHONY: docs
  151. docs: generate ## Generate docs
  152. $(MAKE) -C ./hack/api-docs build
  153. .PHONY: serve-docs
  154. serve-docs: ## Serve docs
  155. $(MAKE) -C ./hack/api-docs serve
  156. # ====================================================================================
  157. # Build Artifacts
  158. build.all: docker.build helm.build ## Build all artifacts (docker image, helm chart)
  159. docker.build: $(addprefix build-,$(ARCH)) ## Build the docker image
  160. @$(INFO) docker build
  161. @docker build . $(BUILD_ARGS) -t $(IMAGE_REGISTRY):$(VERSION)
  162. @$(OK) docker build
  163. docker.push: ## Push the docker image to the registry
  164. @$(INFO) docker push
  165. @docker push $(IMAGE_REGISTRY):$(VERSION)
  166. @$(OK) docker push
  167. # RELEASE_TAG is tag to promote. Default is promoting to main branch, but can be overriden
  168. # to promote a tag to a specific version.
  169. RELEASE_TAG ?= main
  170. SOURCE_TAG ?= $(VERSION)
  171. docker.promote: ## Promote the docker image to the registry
  172. @$(INFO) promoting $(SOURCE_TAG) to $(RELEASE_TAG)
  173. docker manifest inspect $(IMAGE_REGISTRY):$(SOURCE_TAG) > .tagmanifest
  174. for digest in $$(jq -r '.manifests[].digest' < .tagmanifest); do \
  175. docker pull $(IMAGE_REGISTRY)@$$digest; \
  176. done
  177. docker manifest create $(IMAGE_REGISTRY):$(RELEASE_TAG) \
  178. $$(jq -j '"--amend $(IMAGE_REGISTRY)@" + .manifests[].digest + " "' < .tagmanifest)
  179. docker manifest push $(IMAGE_REGISTRY):$(RELEASE_TAG)
  180. @$(OK) docker push $(RELEASE_TAG) \
  181. # ====================================================================================
  182. # Terraform
  183. tf.plan.%: ## Runs terrform plan for a provider
  184. @cd $(TF_DIR)/$*; \
  185. terraform init; \
  186. terraform plan
  187. tf.apply.%: ## Runs terrform apply for a provider
  188. @cd $(TF_DIR)/$*; \
  189. terraform init; \
  190. terraform apply -auto-approve
  191. tf.destroy.%: ## Runs terrform destroy for a provider
  192. @cd $(TF_DIR)/$*; \
  193. terraform init; \
  194. terraform destroy -auto-approve
  195. tf.show.%: ## Runs terrform show for a provider and outputs to a file
  196. @cd $(TF_DIR)/$*; \
  197. terraform init; \
  198. terraform plan -out tfplan.binary; \
  199. terraform show -json tfplan.binary > plan.json
  200. # ====================================================================================
  201. # Help
  202. # only comments after make target name are shown as help text
  203. help: ## Displays this help message
  204. @echo -e "$$(grep -hE '^\S+:.*##' $(MAKEFILE_LIST) | sed -e 's/:.*##\s*/:/' -e 's/^\(.\+\):\(.*\)/\\x1b[36m\1\\x1b[m:\2/' | column -c2 -t -s : | sort)"