Makefile 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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)" || $(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: ## Generate code and crds
  93. @go run sigs.k8s.io/controller-tools/cmd/controller-gen object:headerFile="hack/boilerplate.go.txt" paths="./..."
  94. @go run sigs.k8s.io/controller-tools/cmd/controller-gen $(CRD_OPTIONS) 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 crds
  102. # ====================================================================================
  103. # Local Utility
  104. # This is for running out-of-cluster locally, and is for convenience.
  105. # For more control, try running the binary directly with different arguments.
  106. run: generate
  107. go run ./main.go
  108. # Generate manifests from helm chart
  109. manifests: helm.generate
  110. mkdir -p $(OUTPUT_DIR)/deploy/manifests
  111. helm template external-secrets $(HELM_DIR) -f deploy/manifests/helm-values.yaml > $(OUTPUT_DIR)/deploy/manifests/external-secrets.yaml
  112. # Install CRDs into a cluster. This is for convenience.
  113. crds.install: generate
  114. kubectl apply -f $(CRD_DIR)
  115. # Uninstall CRDs from a cluster. This is for convenience.
  116. crds.uninstall:
  117. kubectl delete -f $(CRD_DIR)
  118. # ====================================================================================
  119. # Helm Chart
  120. helm.docs: ## Generate helm docs
  121. cd $(HELM_DIR); \
  122. docker run --rm -v $(shell pwd)/$(HELM_DIR):/helm-docs -u $(shell id -u) jnorwood/helm-docs:latest
  123. HELM_VERSION ?= $(shell helm show chart $(HELM_DIR) | grep 'version:' | sed 's/version: //g')
  124. helm.build: helm.generate ## Build helm chart
  125. @$(INFO) helm package
  126. @helm package $(HELM_DIR) --dependency-update --destination $(OUTPUT_DIR)/chart
  127. @mv $(OUTPUT_DIR)/chart/external-secrets-$(HELM_VERSION).tgz $(OUTPUT_DIR)/chart/external-secrets.tgz
  128. @$(OK) helm package
  129. # Copy crds to helm chart directory
  130. helm.generate:
  131. @cp $(CRD_DIR)/*.yaml $(HELM_DIR)/templates/crds/
  132. # Add helm if statement for controlling the install of CRDs
  133. @for i in $(HELM_DIR)/templates/crds/*.yaml; do \
  134. cp "$$i" "$$i.bkp" && \
  135. echo "{{- if .Values.installCRDs }}" > "$$i" && \
  136. cat "$$i.bkp" >> "$$i" && \
  137. echo "{{- end }}" >> "$$i" && \
  138. rm "$$i.bkp"; \
  139. done
  140. @$(OK) Finished generating helm chart files
  141. # ====================================================================================
  142. # Documentation
  143. .PHONY: docs
  144. docs: generate
  145. $(MAKE) -C ./hack/api-docs build
  146. .PHONY: serve-docs
  147. serve-docs:
  148. $(MAKE) -C ./hack/api-docs serve
  149. # ====================================================================================
  150. # Build Artifacts
  151. build.all: docker.build helm.build
  152. docker.build: build ## Build the docker image
  153. @$(INFO) docker build
  154. @docker build . $(BUILD_ARGS) -t $(IMAGE_REGISTRY):$(VERSION)
  155. @$(OK) docker build
  156. docker.push:
  157. @$(INFO) docker push
  158. @docker push $(IMAGE_REGISTRY):$(VERSION)
  159. @$(OK) docker push
  160. # RELEASE_TAG is tag to promote. Default is promooting to main branch, but can be overriden
  161. # to promote a tag to a specific version.
  162. RELEASE_TAG ?= main
  163. SOURCE_TAG ?= $(VERSION)
  164. docker.promote:
  165. @$(INFO) docker pull $(SOURCE_TAG)
  166. @docker pull $(IMAGE_REGISTRY):$(SOURCE_TAG)
  167. @docker tag $(IMAGE_REGISTRY):$(SOURCE_TAG) $(IMAGE_REGISTRY):$(RELEASE_TAG)
  168. @docker push $(IMAGE_REGISTRY):$(RELEASE_TAG)
  169. @$(OK) docker push $(RELEASE_TAG)
  170. # ====================================================================================
  171. # Help
  172. # only comments after make target name are shown as help text
  173. help: ## displays this help message
  174. @echo -e "$$(grep -hE '^\S+:.*##' $(MAKEFILE_LIST) | sed -e 's/:.*##\s*/:/' -e 's/^\(.\+\):\(.*\)/\\x1b[36m\1\\x1b[m:\2/' | column -c2 -t -s : | sort)"