Makefile 9.9 KB

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