Makefile 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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. DOCKERFILE ?= Dockerfile
  9. # default target is build
  10. .DEFAULT_GOAL := all
  11. .PHONY: all
  12. all: $(addprefix build-,$(ARCH))
  13. # Image registry for build/push image targets
  14. export IMAGE_REGISTRY ?= ghcr.io
  15. export IMAGE_REPO ?= external-secrets/external-secrets
  16. export IMAGE_NAME ?= $(IMAGE_REGISTRY)/$(IMAGE_REPO)
  17. #Valid licenses for license.check
  18. LICENSES ?= Apache-2.0|MIT|BSD-3-Clause|ISC|MPL-2.0|BSD-2-Clause
  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 helm.generate helm.docs lint ## Ensure a PR is ready for review.
  60. @go mod tidy
  61. @cd e2e/ && go mod tidy
  62. golicenses.check: ## Check install of go-licenses
  63. @if ! go-licenses >> /dev/null 2>&1; then \
  64. echo -e "\033[0;33mgo-licenses is not installed: run go install github.com/google/go-licenses@latest" ; \
  65. exit 1; \
  66. fi
  67. license.check: golicenses.check
  68. @$(INFO) running dependency license checks
  69. @ok=0; go-licenses csv github.com/external-secrets/external-secrets 2>/dev/null | \
  70. grep -v -E '${LICENSES}' | \
  71. tr "," " " | awk '{print "Invalid License " $$3 " for dependency " $$1 }'|| ok=1; \
  72. if [[ $$ok -eq 1 ]]; then $(OK) dependencies are compliant; else $(FAIL); fi
  73. check-diff: reviewable ## Ensure branch is clean.
  74. @$(INFO) checking that branch is clean
  75. @test -z "$$(git status --porcelain)" || (echo "$$(git status --porcelain)" && $(FAIL))
  76. @$(OK) branch is clean
  77. # ====================================================================================
  78. # Golang
  79. .PHONY: test
  80. test: generate ## Run tests
  81. @$(INFO) go test unit-tests
  82. go test -race -v $(shell go list ./... | grep -v e2e) -coverprofile cover.out
  83. @$(OK) go test unit-tests
  84. .PHONY: test.e2e
  85. test.e2e: generate ## Run e2e tests
  86. @$(INFO) go test e2e-tests
  87. $(MAKE) -C ./e2e test
  88. @$(OK) go test e2e-tests
  89. .PHONY: test.e2e.managed
  90. test.e2e.managed: generate ## Run e2e tests managed
  91. @$(INFO) go test e2e-tests-managed
  92. $(MAKE) -C ./e2e test.managed
  93. @$(OK) go test e2e-tests-managed
  94. .PHONY: build
  95. build: $(addprefix build-,$(ARCH)) ## Build binary
  96. .PHONY: build-%
  97. build-%: generate ## Build binary for the specified arch
  98. @$(INFO) go build $*
  99. @CGO_ENABLED=0 GOOS=linux GOARCH=$* \
  100. go build -o '$(OUTPUT_DIR)/external-secrets-linux-$*' main.go
  101. @$(OK) go build $*
  102. lint.check: ## Check install of golanci-lint
  103. @if ! golangci-lint --version > /dev/null 2>&1; then \
  104. 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"; \
  105. exit 1; \
  106. fi
  107. lint.install: ## Install golangci-lint to the go bin dir
  108. @if ! golangci-lint --version > /dev/null 2>&1; then \
  109. echo "Installing golangci-lint"; \
  110. curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(GOBIN) v1.49.0; \
  111. fi
  112. lint: lint.check ## Run golangci-lint
  113. @if ! golangci-lint run; then \
  114. echo -e "\033[0;33mgolangci-lint failed: some checks can be fixed with \`\033[0;32mmake fmt\033[0m\033[0;33m\`\033[0m"; \
  115. exit 1; \
  116. fi
  117. @$(OK) Finished linting
  118. fmt: lint.check ## Ensure consistent code style
  119. @go mod tidy
  120. @cd e2e/ && go mod tidy
  121. @go fmt ./...
  122. @golangci-lint run --fix > /dev/null 2>&1 || true
  123. @$(OK) Ensured consistent code style
  124. generate: ## Generate code and crds
  125. @./hack/crd.generate.sh $(BUNDLE_DIR) $(CRD_DIR)
  126. @$(OK) Finished generating deepcopy and crds
  127. # ====================================================================================
  128. # Local Utility
  129. # This is for running out-of-cluster locally, and is for convenience.
  130. # For more control, try running the binary directly with different arguments.
  131. run: generate ## Run app locally (without a k8s cluster)
  132. go run ./main.go
  133. manifests: helm.generate ## Generate manifests from helm chart
  134. mkdir -p $(OUTPUT_DIR)/deploy/manifests
  135. helm template external-secrets $(HELM_DIR) -f deploy/manifests/helm-values.yaml > $(OUTPUT_DIR)/deploy/manifests/external-secrets.yaml
  136. crds.install: generate ## Install CRDs into a cluster. This is for convenience
  137. kubectl apply -f $(BUNDLE_DIR)
  138. crds.uninstall: ## Uninstall CRDs from a cluster. This is for convenience
  139. kubectl delete -f $(BUNDLE_DIR)
  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) jnorwood/helm-docs:v1.5.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.generate:
  152. ./hack/helm.generate.sh $(BUNDLE_DIR) $(HELM_DIR)
  153. @$(OK) Finished generating helm chart files
  154. # ====================================================================================
  155. # Documentation
  156. .PHONY: docs
  157. docs: generate ## Generate docs
  158. $(MAKE) -C ./hack/api-docs build
  159. .PHONY: docs.publish
  160. docs.publish: generate ## Generate and deploys docs
  161. $(MAKE) -C ./hack/api-docs build.publish
  162. .PHONY: docs.serve
  163. docs.serve: ## Serve docs
  164. $(MAKE) -C ./hack/api-docs serve
  165. # ====================================================================================
  166. # Build Artifacts
  167. build.all: docker.build helm.build ## Build all artifacts (docker image, helm chart)
  168. docker.image:
  169. @echo $(IMAGE_NAME):$(IMAGE_TAG)
  170. docker.tag:
  171. @echo $(IMAGE_TAG)
  172. docker.build: $(addprefix build-,$(ARCH)) ## Build the docker image
  173. @$(INFO) docker build
  174. @docker build -f $(DOCKERFILE) . $(BUILD_ARGS) -t $(IMAGE_NAME):$(IMAGE_TAG)
  175. @$(OK) docker build
  176. docker.push: ## Push the docker image to the registry
  177. @$(INFO) docker push
  178. @docker push $(IMAGE_NAME):$(IMAGE_TAG)
  179. @$(OK) docker push
  180. # RELEASE_TAG is tag to promote. Default is promoting to main branch, but can be overriden
  181. # to promote a tag to a specific version.
  182. RELEASE_TAG ?= $(IMAGE_TAG)
  183. SOURCE_TAG ?= $(VERSION)$(TAG_SUFFIX)
  184. docker.promote: ## Promote the docker image to the registry
  185. @$(INFO) promoting $(SOURCE_TAG) to $(RELEASE_TAG)
  186. docker manifest inspect $(IMAGE_NAME):$(SOURCE_TAG) > .tagmanifest
  187. for digest in $$(jq -r '.manifests[].digest' < .tagmanifest); do \
  188. docker pull $(IMAGE_NAME)@$$digest; \
  189. done
  190. docker manifest create $(IMAGE_NAME):$(RELEASE_TAG) \
  191. $$(jq -j '"--amend $(IMAGE_NAME)@" + .manifests[].digest + " "' < .tagmanifest)
  192. docker manifest push $(IMAGE_NAME):$(RELEASE_TAG)
  193. @$(OK) docker push $(RELEASE_TAG) \
  194. # ====================================================================================
  195. # Terraform
  196. tf.plan.%: ## Runs terrform plan for a provider
  197. @cd $(TF_DIR)/$*; \
  198. terraform init; \
  199. terraform plan
  200. tf.apply.%: ## Runs terrform apply for a provider
  201. @cd $(TF_DIR)/$*; \
  202. terraform init; \
  203. terraform apply -auto-approve
  204. tf.destroy.%: ## Runs terrform destroy for a provider
  205. @cd $(TF_DIR)/$*; \
  206. terraform init; \
  207. terraform destroy -auto-approve
  208. tf.show.%: ## Runs terrform show for a provider and outputs to a file
  209. @cd $(TF_DIR)/$*; \
  210. terraform init; \
  211. terraform plan -out tfplan.binary; \
  212. terraform show -json tfplan.binary > plan.json
  213. # ====================================================================================
  214. # Help
  215. # only comments after make target name are shown as help text
  216. help: ## Displays this help message
  217. @echo -e "$$(grep -hE '^\S+:.*##' $(MAKEFILE_LIST) | sed -e 's/:.*##\s*/:/' -e 's/^\(.\+\):\(.*\)/\\x1b[36m\1\\x1b[m:\2/' | column -c2 -t -s : | sort)"