Makefile 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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 ?= config/crd/bases
  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)" || $(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 ./... -coverprofile cover.out
  64. @$(OK) go test unit-tests
  65. .PHONY: build
  66. build: generate ## Build binary
  67. @$(INFO) go build
  68. @CGO_ENABLED=0 go build -o $(OUTPUT_DIR)/external-secrets main.go
  69. @$(OK) go build
  70. # Check install of golanci-lint
  71. lint.check:
  72. @if ! golangci-lint --version > /dev/null 2>&1; then \
  73. 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"; \
  74. exit 1; \
  75. fi
  76. # installs golangci-lint to the go bin dir
  77. lint.install:
  78. @if ! golangci-lint --version > /dev/null 2>&1; then \
  79. echo "Installing golangci-lint"; \
  80. curl -sfL https://install.goreleaser.com/github.com/golangci/golangci-lint.sh | sh -s -- -b $(GOBIN) v1.33.0; \
  81. fi
  82. lint: lint.check ## run golangci-lint
  83. @if ! golangci-lint run; then \
  84. echo -e "\033[0;33mgolangci-lint failed: some checks can be fixed with \`\033[0;32mmake fmt\033[0m\033[0;33m\`\033[0m"; \
  85. exit 1; \
  86. fi
  87. fmt: lint.check ## ensure consistent code style
  88. @go mod tidy
  89. @go fmt ./...
  90. @golangci-lint run --fix > /dev/null 2>&1 || true
  91. @$(OK) Ensured consistent code style
  92. generate: controller-gen ## Generate code, crds, manifests, etc
  93. @$(CONTROLLER_GEN) object:headerFile="hack/boilerplate.go.txt" paths="./..."
  94. @$(CONTROLLER_GEN) $(CRD_OPTIONS) rbac:roleName=manager-role webhook paths="./..." output:crd:artifacts:config=$(CRD_DIR)
  95. # Remove extra header lines in generated CRDs
  96. @for i in $(CRD_DIR)/*.yaml; do \
  97. tail -n +3 <"$$i" >"$$i.bkp" && \
  98. cp "$$i.bkp" "$$i" && \
  99. rm "$$i.bkp"; \
  100. done
  101. @$(OK) Finished generating deepcopy and manifests
  102. # Find or download controller-gen
  103. controller-gen:
  104. ifeq (, $(shell which controller-gen))
  105. @{ \
  106. set -e ;\
  107. CONTROLLER_GEN_TMP_DIR=$$(mktemp -d) ;\
  108. cd $$CONTROLLER_GEN_TMP_DIR ;\
  109. go mod init tmp ;\
  110. go get sigs.k8s.io/controller-tools/cmd/controller-gen@v0.4.1 ;\
  111. rm -rf $$CONTROLLER_GEN_TMP_DIR ;\
  112. }
  113. CONTROLLER_GEN=$(GOBIN)/controller-gen
  114. else
  115. CONTROLLER_GEN=$(shell which controller-gen)
  116. endif
  117. # ====================================================================================
  118. # Local Utility
  119. # This is for running out-of-cluster locally, and is for convenience.
  120. # For more control, try running the binary directly with different arguments.
  121. run: generate
  122. go run ./main.go
  123. # Install CRDs into a cluster. This is for convenience.
  124. crds.install: generate
  125. kubectl apply -f $(CRD_DIR)
  126. # Uninstall CRDs from a cluster. This is for convenience.
  127. crds.uninstall:
  128. kubectl delete -f $(CRD_DIR)
  129. # ====================================================================================
  130. # Helm Chart
  131. helm.docs: ## Generate helm docs
  132. cd $(HELM_DIR); \
  133. docker run --rm -v $(shell pwd)/$(HELM_DIR):/helm-docs -u $(shell id -u) jnorwood/helm-docs:latest
  134. helm.build: helm.generate ## Build helm chart
  135. @$(INFO) helm package
  136. @helm package $(HELM_DIR) --dependency-update --destination $(OUTPUT_DIR)/chart
  137. @$(OK) helm package
  138. # Copy crds to helm chart directory
  139. helm.generate:
  140. @cp $(CRD_DIR)/*.yaml $(HELM_DIR)/templates/crds/
  141. # Add helm if statement for controlling the install of CRDs
  142. @for i in $(HELM_DIR)/templates/crds/*.yaml; do \
  143. cp "$$i" "$$i.bkp" && \
  144. echo "{{- if .Values.installCRDs }}" > "$$i" && \
  145. cat "$$i.bkp" >> "$$i" && \
  146. echo "{{- end }}" >> "$$i" && \
  147. rm "$$i.bkp"; \
  148. done
  149. @$(OK) Finished generating helm chart files
  150. # ====================================================================================
  151. # Documentation
  152. .PHONY: docs
  153. docs: generate
  154. $(MAKE) -C ./hack/api-docs build
  155. .PHONY: serve-docs
  156. serve-docs:
  157. $(MAKE) -C ./hack/api-docs serve
  158. # ====================================================================================
  159. # Build Artifacts
  160. build.all: docker.build helm.build
  161. docker.build: build ## Build the docker image
  162. @$(INFO) docker build
  163. @docker build . $(BUILD_ARGS) -t $(IMAGE_REGISTRY):$(VERSION)
  164. @$(OK) docker build
  165. docker.push:
  166. @$(INFO) docker push
  167. @docker push $(IMAGE_REGISTRY):$(VERSION)
  168. @$(OK) docker push
  169. # RELEASE_TAG is tag to promote. Default is promooting to main branch, but can be overriden
  170. # to promote a tag to a specific version.
  171. RELEASE_TAG ?= main
  172. SOURCE_TAG ?= $(VERSION)
  173. docker.promote:
  174. @$(INFO) docker pull $(SOURCE_TAG)
  175. @docker pull $(IMAGE_REGISTRY):$(SOURCE_TAG)
  176. @docker tag $(IMAGE_REGISTRY):$(SOURCE_TAG) $(IMAGE_REGISTRY):$(RELEASE_TAG)
  177. @docker push $(IMAGE_REGISTRY):$(RELEASE_TAG)
  178. @$(OK) docker push $(RELEASE_TAG)
  179. # ====================================================================================
  180. # Help
  181. # only comments after make target name are shown as help text
  182. help: ## displays this help message
  183. @echo -e "$$(grep -hE '^\S+:.*##' $(MAKEFILE_LIST) | sed -e 's/:.*##\s*/:/' -e 's/^\(.\+\):\(.*\)/\\x1b[36m\1\\x1b[m:\2/' | column -c2 -t -s : | sort)"