Makefile 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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. # default target is build
  7. .DEFAULT_GOAL := all
  8. .PHONY: all
  9. all: build
  10. # Image registry for build/push image targets
  11. IMAGE_REGISTRY ?= ghcr.io/external-secrets/external-secrets
  12. # Produce CRDs that work back to Kubernetes 1.11 (no version conversion)
  13. CRD_OPTIONS ?= "crd:trivialVersions=true"
  14. CRD_DIR ?= deploy/crds
  15. HELM_DIR ?= deploy/charts/external-secrets
  16. OUTPUT_DIR ?= bin
  17. # Get the currently used golang install path (in GOPATH/bin, unless GOBIN is set)
  18. ifeq (,$(shell go env GOBIN))
  19. GOBIN=$(shell go env GOPATH)/bin
  20. else
  21. GOBIN=$(shell go env GOBIN)
  22. endif
  23. # check if there are any existing `git tag` values
  24. ifeq ($(shell git tag),)
  25. # no tags found - default to initial tag `v0.0.0`
  26. VERSION := $(shell echo "v0.0.0-$$(git rev-list HEAD --count)-g$$(git describe --dirty --always)" | sed 's/-/./2' | sed 's/-/./2')
  27. else
  28. # use tags
  29. VERSION := $(shell git describe --dirty --always --tags | sed 's/-/./2' | sed 's/-/./2')
  30. endif
  31. # ====================================================================================
  32. # Colors
  33. BLUE := $(shell printf "\033[34m")
  34. YELLOW := $(shell printf "\033[33m")
  35. RED := $(shell printf "\033[31m")
  36. GREEN := $(shell printf "\033[32m")
  37. CNone := $(shell printf "\033[0m")
  38. # ====================================================================================
  39. # Logger
  40. TIME_LONG = `date +%Y-%m-%d' '%H:%M:%S`
  41. TIME_SHORT = `date +%H:%M:%S`
  42. TIME = $(TIME_SHORT)
  43. INFO = echo ${TIME} ${BLUE}[ .. ]${CNone}
  44. WARN = echo ${TIME} ${YELLOW}[WARN]${CNone}
  45. ERR = echo ${TIME} ${RED}[FAIL]${CNone}
  46. OK = echo ${TIME} ${GREEN}[ OK ]${CNone}
  47. FAIL = (echo ${TIME} ${RED}[FAIL]${CNone} && false)
  48. # ====================================================================================
  49. # Conformance
  50. # Ensure a PR is ready for review.
  51. reviewable: generate helm.generate
  52. @go mod tidy
  53. # Ensure branch is clean.
  54. check-diff: reviewable
  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 -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 unit-tests
  70. .PHONY: build
  71. build: generate ## Build binary
  72. @$(INFO) go build
  73. @CGO_ENABLED=0 go build -o $(OUTPUT_DIR)/external-secrets main.go
  74. @$(OK) go build
  75. # Check install of golanci-lint
  76. lint.check:
  77. @if ! golangci-lint --version > /dev/null 2>&1; then \
  78. 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"; \
  79. exit 1; \
  80. fi
  81. # installs golangci-lint to the go bin dir
  82. lint.install:
  83. @if ! golangci-lint --version > /dev/null 2>&1; then \
  84. echo "Installing golangci-lint"; \
  85. curl -sfL https://install.goreleaser.com/github.com/golangci/golangci-lint.sh | sh -s -- -b $(GOBIN) v1.33.0; \
  86. fi
  87. lint: lint.check ## run golangci-lint
  88. @if ! golangci-lint run; then \
  89. echo -e "\033[0;33mgolangci-lint failed: some checks can be fixed with \`\033[0;32mmake fmt\033[0m\033[0;33m\`\033[0m"; \
  90. exit 1; \
  91. fi
  92. fmt: lint.check ## ensure consistent code style
  93. @go mod tidy
  94. @go fmt ./...
  95. @golangci-lint run --fix > /dev/null 2>&1 || true
  96. @$(OK) Ensured consistent code style
  97. generate: ## Generate code and crds
  98. @go run sigs.k8s.io/controller-tools/cmd/controller-gen object:headerFile="hack/boilerplate.go.txt" paths="./..."
  99. @go run sigs.k8s.io/controller-tools/cmd/controller-gen $(CRD_OPTIONS) paths="./..." output:crd:artifacts:config=$(CRD_DIR)
  100. # Remove extra header lines in generated CRDs
  101. @for i in $(CRD_DIR)/*.yaml; do \
  102. tail -n +3 <"$$i" >"$$i.bkp" && \
  103. cp "$$i.bkp" "$$i" && \
  104. rm "$$i.bkp"; \
  105. done
  106. @$(OK) Finished generating deepcopy and crds
  107. # ====================================================================================
  108. # Local Utility
  109. # This is for running out-of-cluster locally, and is for convenience.
  110. # For more control, try running the binary directly with different arguments.
  111. run: generate
  112. go run ./main.go
  113. # Generate manifests from helm chart
  114. manifests: helm.generate
  115. mkdir -p $(OUTPUT_DIR)/deploy/manifests
  116. helm template external-secrets $(HELM_DIR) -f deploy/manifests/helm-values.yaml > $(OUTPUT_DIR)/deploy/manifests/external-secrets.yaml
  117. # Install CRDs into a cluster. This is for convenience.
  118. crds.install: generate
  119. kubectl apply -f $(CRD_DIR)
  120. # Uninstall CRDs from a cluster. This is for convenience.
  121. crds.uninstall:
  122. kubectl delete -f $(CRD_DIR)
  123. # ====================================================================================
  124. # Helm Chart
  125. helm.docs: ## Generate helm docs
  126. cd $(HELM_DIR); \
  127. docker run --rm -v $(shell pwd)/$(HELM_DIR):/helm-docs -u $(shell id -u) jnorwood/helm-docs:latest
  128. HELM_VERSION ?= $(shell helm show chart $(HELM_DIR) | grep 'version:' | sed 's/version: //g')
  129. helm.build: helm.generate ## Build helm chart
  130. @$(INFO) helm package
  131. @helm package $(HELM_DIR) --dependency-update --destination $(OUTPUT_DIR)/chart
  132. @mv $(OUTPUT_DIR)/chart/external-secrets-$(HELM_VERSION).tgz $(OUTPUT_DIR)/chart/external-secrets.tgz
  133. @$(OK) helm package
  134. # Copy crds to helm chart directory
  135. helm.generate:
  136. @cp $(CRD_DIR)/*.yaml $(HELM_DIR)/templates/crds/
  137. # Add helm if statement for controlling the install of CRDs
  138. @for i in $(HELM_DIR)/templates/crds/*.yaml; do \
  139. cp "$$i" "$$i.bkp" && \
  140. echo "{{- if .Values.installCRDs }}" > "$$i" && \
  141. cat "$$i.bkp" >> "$$i" && \
  142. echo "{{- end }}" >> "$$i" && \
  143. rm "$$i.bkp"; \
  144. done
  145. @$(OK) Finished generating helm chart files
  146. # ====================================================================================
  147. # Documentation
  148. .PHONY: docs
  149. docs: generate
  150. $(MAKE) -C ./hack/api-docs build
  151. .PHONY: serve-docs
  152. serve-docs:
  153. $(MAKE) -C ./hack/api-docs serve
  154. # ====================================================================================
  155. # Build Artifacts
  156. build.all: docker.build helm.build
  157. docker.build: build ## Build the docker image
  158. @$(INFO) docker build
  159. @docker build . $(BUILD_ARGS) -t $(IMAGE_REGISTRY):$(VERSION)
  160. @$(OK) docker build
  161. docker.push:
  162. @$(INFO) docker push
  163. @docker push $(IMAGE_REGISTRY):$(VERSION)
  164. @$(OK) docker push
  165. # RELEASE_TAG is tag to promote. Default is promooting to main branch, but can be overriden
  166. # to promote a tag to a specific version.
  167. RELEASE_TAG ?= main
  168. SOURCE_TAG ?= $(VERSION)
  169. docker.promote:
  170. @$(INFO) docker pull $(SOURCE_TAG)
  171. @docker pull $(IMAGE_REGISTRY):$(SOURCE_TAG)
  172. @docker tag $(IMAGE_REGISTRY):$(SOURCE_TAG) $(IMAGE_REGISTRY):$(RELEASE_TAG)
  173. @docker push $(IMAGE_REGISTRY):$(RELEASE_TAG)
  174. @$(OK) docker push $(RELEASE_TAG)
  175. # ====================================================================================
  176. # Help
  177. # only comments after make target name are shown as help text
  178. help: ## displays this help message
  179. @echo -e "$$(grep -hE '^\S+:.*##' $(MAKEFILE_LIST) | sed -e 's/:.*##\s*/:/' -e 's/^\(.\+\):\(.*\)/\\x1b[36m\1\\x1b[m:\2/' | column -c2 -t -s : | sort)"