Makefile 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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 ?= 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. #Valid licenses for license.check
  19. LICENSES ?= Apache-2.0|MIT|BSD-3-Clause|ISC|MPL-2.0|BSD-2-Clause
  20. BUNDLE_DIR ?= deploy/crds
  21. CRD_DIR ?= config/crds
  22. HELM_DIR ?= deploy/charts/external-secrets
  23. TF_DIR ?= terraform
  24. OUTPUT_DIR ?= bin
  25. # Get the currently used golang install path (in GOPATH/bin, unless GOBIN is set)
  26. ifeq (,$(shell go env GOBIN))
  27. GOBIN=$(shell go env GOPATH)/bin
  28. else
  29. GOBIN=$(shell go env GOBIN)
  30. endif
  31. # check if there are any existing `git tag` values
  32. ifeq ($(shell git tag),)
  33. # no tags found - default to initial tag `v0.0.0`
  34. export VERSION := $(shell echo "v0.0.0-$$(git rev-list HEAD --count)-g$$(git describe --dirty --always)" | sed 's/-/./2' | sed 's/-/./2')
  35. else
  36. # use tags
  37. export VERSION := $(shell git describe --dirty --always --tags --exclude 'helm*' | sed 's/-/./2' | sed 's/-/./2')
  38. endif
  39. TAG_SUFFIX ?=
  40. export IMAGE_TAG ?= $(VERSION)$(TAG_SUFFIX)
  41. # ====================================================================================
  42. # Colors
  43. BLUE := $(shell printf "\033[34m")
  44. YELLOW := $(shell printf "\033[33m")
  45. RED := $(shell printf "\033[31m")
  46. GREEN := $(shell printf "\033[32m")
  47. CNone := $(shell printf "\033[0m")
  48. # ====================================================================================
  49. # Logger
  50. TIME_LONG = `date +%Y-%m-%d' '%H:%M:%S`
  51. TIME_SHORT = `date +%H:%M:%S`
  52. TIME = $(TIME_SHORT)
  53. INFO = echo ${TIME} ${BLUE}[ .. ]${CNone}
  54. WARN = echo ${TIME} ${YELLOW}[WARN]${CNone}
  55. ERR = echo ${TIME} ${RED}[FAIL]${CNone}
  56. OK = echo ${TIME} ${GREEN}[ OK ]${CNone}
  57. FAIL = (echo ${TIME} ${RED}[FAIL]${CNone} && false)
  58. # ====================================================================================
  59. # Conformance
  60. reviewable: generate manifests helm.generate helm.docs lint ## Ensure a PR is ready for review.
  61. @go mod tidy
  62. @cd e2e/ && go mod tidy
  63. golicenses.check: ## Check install of go-licenses
  64. @if ! go-licenses >> /dev/null 2>&1; then \
  65. echo -e "\033[0;33mgo-licenses is not installed: run go install github.com/google/go-licenses@latest" ; \
  66. exit 1; \
  67. fi
  68. license.check: golicenses.check
  69. @$(INFO) running dependency license checks
  70. @ok=0; go-licenses csv github.com/external-secrets/external-secrets 2>/dev/null | \
  71. grep -v -E '${LICENSES}' | \
  72. tr "," " " | awk '{print "Invalid License " $$3 " for dependency " $$1 }'|| ok=1; \
  73. if [[ $$ok -eq 1 ]]; then $(OK) dependencies are compliant; else $(FAIL); fi
  74. check-diff: reviewable ## Ensure branch is clean.
  75. @$(INFO) checking that branch is clean
  76. @test -z "$$(git status --porcelain)" || (echo "$$(git status --porcelain)" && $(FAIL))
  77. @$(OK) branch is clean
  78. # ====================================================================================
  79. # Golang
  80. .PHONY: test
  81. test: generate ## Run tests
  82. @$(INFO) go test unit-tests
  83. go test -race -v $(shell go list ./... | grep -v e2e) -coverprofile cover.out
  84. @$(OK) go test unit-tests
  85. .PHONY: test.e2e
  86. test.e2e: generate ## Run e2e tests
  87. @$(INFO) go test e2e-tests
  88. $(MAKE) -C ./e2e test
  89. @$(OK) go test e2e-tests
  90. .PHONY: test.e2e.managed
  91. test.e2e.managed: generate ## Run e2e tests managed
  92. @$(INFO) go test e2e-tests-managed
  93. $(MAKE) -C ./e2e test.managed
  94. @$(OK) go test e2e-tests-managed
  95. .PHONY: build
  96. build: $(addprefix build-,$(ARCH)) ## Build binary
  97. .PHONY: build-%
  98. build-%: generate ## Build binary for the specified arch
  99. @$(INFO) go build $*
  100. $(BUILD_ARGS) GOOS=linux GOARCH=$* \
  101. go build -o '$(OUTPUT_DIR)/external-secrets-linux-$*' main.go
  102. @$(OK) go build $*
  103. lint.check: ## Check install of golanci-lint
  104. @if ! golangci-lint --version > /dev/null 2>&1; then \
  105. 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"; \
  106. exit 1; \
  107. fi
  108. lint.install: ## Install golangci-lint to the go bin dir
  109. @if ! golangci-lint --version > /dev/null 2>&1; then \
  110. echo "Installing golangci-lint"; \
  111. curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(GOBIN) v1.49.0; \
  112. fi
  113. lint: lint.check ## Run golangci-lint
  114. @if ! golangci-lint run; then \
  115. echo -e "\033[0;33mgolangci-lint failed: some checks can be fixed with \`\033[0;32mmake fmt\033[0m\033[0;33m\`\033[0m"; \
  116. exit 1; \
  117. fi
  118. @$(OK) Finished linting
  119. fmt: lint.check ## Ensure consistent code style
  120. @go mod tidy
  121. @cd e2e/ && go mod tidy
  122. @go fmt ./...
  123. @golangci-lint run --fix > /dev/null 2>&1 || true
  124. @$(OK) Ensured consistent code style
  125. generate: ## Generate code and crds
  126. @./hack/crd.generate.sh $(BUNDLE_DIR) $(CRD_DIR)
  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. ./hack/helm.generate.sh $(BUNDLE_DIR) $(HELM_DIR)
  154. @$(OK) Finished generating helm chart files
  155. # ====================================================================================
  156. # Documentation
  157. .PHONY: docs
  158. docs: generate ## Generate docs
  159. $(MAKE) -C ./hack/api-docs build
  160. .PHONY: docs.publish
  161. docs.publish: generate ## Generate and deploys docs
  162. $(MAKE) -C ./hack/api-docs build.publish
  163. .PHONY: docs.serve
  164. docs.serve: ## Serve docs
  165. $(MAKE) -C ./hack/api-docs serve
  166. # ====================================================================================
  167. # Build Artifacts
  168. build.all: docker.build helm.build ## Build all artifacts (docker image, helm chart)
  169. docker.image:
  170. @echo $(IMAGE_NAME):$(IMAGE_TAG)
  171. docker.tag:
  172. @echo $(IMAGE_TAG)
  173. docker.build: $(addprefix build-,$(ARCH)) ## Build the docker image
  174. @$(INFO) docker build
  175. @docker build -f $(DOCKERFILE) . $(DOCKER_BUILD_ARGS) -t $(IMAGE_NAME):$(IMAGE_TAG)
  176. @$(OK) docker build
  177. docker.push: ## Push the docker image to the registry
  178. @$(INFO) docker push
  179. @docker push $(IMAGE_NAME):$(IMAGE_TAG)
  180. @$(OK) docker push
  181. # RELEASE_TAG is tag to promote. Default is promoting to main branch, but can be overriden
  182. # to promote a tag to a specific version.
  183. RELEASE_TAG ?= $(IMAGE_TAG)
  184. SOURCE_TAG ?= $(VERSION)$(TAG_SUFFIX)
  185. docker.promote: ## Promote the docker image to the registry
  186. @$(INFO) promoting $(SOURCE_TAG) to $(RELEASE_TAG)
  187. docker manifest inspect $(IMAGE_NAME):$(SOURCE_TAG) > .tagmanifest
  188. for digest in $$(jq -r '.manifests[].digest' < .tagmanifest); do \
  189. docker pull $(IMAGE_NAME)@$$digest; \
  190. done
  191. docker manifest create $(IMAGE_NAME):$(RELEASE_TAG) \
  192. $$(jq -j '"--amend $(IMAGE_NAME)@" + .manifests[].digest + " "' < .tagmanifest)
  193. docker manifest push $(IMAGE_NAME):$(RELEASE_TAG)
  194. @$(OK) docker push $(RELEASE_TAG) \
  195. # ====================================================================================
  196. # Terraform
  197. tf.plan.%: ## Runs terrform plan for a provider
  198. @cd $(TF_DIR)/$*; \
  199. terraform init; \
  200. terraform plan
  201. tf.apply.%: ## Runs terrform apply for a provider
  202. @cd $(TF_DIR)/$*; \
  203. terraform init; \
  204. terraform apply -auto-approve
  205. tf.destroy.%: ## Runs terrform destroy for a provider
  206. @cd $(TF_DIR)/$*; \
  207. terraform init; \
  208. terraform destroy -auto-approve
  209. tf.show.%: ## Runs terrform show for a provider and outputs to a file
  210. @cd $(TF_DIR)/$*; \
  211. terraform init; \
  212. terraform plan -out tfplan.binary; \
  213. terraform show -json tfplan.binary > plan.json
  214. # ====================================================================================
  215. # Help
  216. # only comments after make target name are shown as help text
  217. help: ## Displays this help message
  218. @echo -e "$$(grep -hE '^\S+:.*##' $(MAKEFILE_LIST) | sed -e 's/:.*##\s*/:/' -e 's/^\(.\+\):\(.*\)/\\x1b[36m\1\\x1b[m:\2/' | column -c2 -t -s : | sort)"