2021-02-18 10:27:10 +00:00
# set the shell to bash always
2020-12-21 19:31:28 +00:00
SHELL := /bin/bash
2021-02-18 10:27:10 +00:00
# set make and shell flags to exit on errors
MAKEFLAGS += --warn-undefined-variables
2020-12-21 19:31:28 +00:00
.SHELLFLAGS := -euo pipefail -c
2021-02-18 10:27:10 +00:00
# default target is build
2020-12-21 19:31:28 +00:00
.DEFAULT_GOAL := all
2021-02-18 10:27:10 +00:00
.PHONY : all
all : build
# Image registry for build/push image targets
IMAGE_REGISTRY ?= ghcr.io/external-secrets/external-secrets
2020-11-23 14:21:01 +00:00
# Produce CRDs that work back to Kubernetes 1.11 (no version conversion)
CRD_OPTIONS ?= "crd:trivialVersions=true"
2020-12-22 19:12:39 +00:00
CRD_DIR ?= config/crd/bases
2020-11-23 14:21:01 +00:00
2021-02-18 10:27:10 +00:00
HELM_DIR ?= deploy/charts/external-secrets
OUTPUT_DIR ?= bin
2020-11-23 14:21:01 +00:00
# Get the currently used golang install path (in GOPATH/bin, unless GOBIN is set)
i f e q ( , $( shell go env GOBIN ) )
GOBIN = $( shell go env GOPATH) /bin
e l s e
GOBIN = $( shell go env GOBIN)
e n d i f
2021-02-18 10:27:10 +00:00
# check if there are any existing `git tag` values
i f e q ( $( shell git tag ) , )
# no tags found - default to initial tag `v0.0.0`
VERSION := $( shell echo " v0.0.0- $$ (git rev-list HEAD --count)-g $$ (git describe --dirty --always) " | sed 's/-/./2' | sed 's/-/./2' )
e l s e
# use tags
VERSION := $( shell git describe --dirty --always --tags | sed 's/-/./2' | sed 's/-/./2' )
e n d i f
2020-11-23 14:21:01 +00:00
2021-02-18 10:27:10 +00:00
# ====================================================================================
# Colors
2020-11-23 14:21:01 +00:00
2021-02-18 10:27:10 +00:00
BLUE := $( shell printf "\033[34m" )
YELLOW := $( shell printf "\033[33m" )
RED := $( shell printf "\033[31m" )
GREEN := $( shell printf "\033[32m" )
CNone := $( shell printf "\033[0m" )
2020-11-23 14:21:01 +00:00
2021-02-18 10:27:10 +00:00
# ====================================================================================
# Logger
2020-11-23 14:21:01 +00:00
2021-02-18 10:27:10 +00:00
TIME_LONG = ` date +%Y-%m-%d' ' %H:%M:%S`
TIME_SHORT = ` date +%H:%M:%S`
TIME = $( TIME_SHORT)
2020-11-23 14:21:01 +00:00
2021-02-18 10:27:10 +00:00
INFO = echo ${ TIME } ${ BLUE } [ .. ] ${ CNone }
WARN = echo ${ TIME } ${ YELLOW } [ WARN] ${ CNone }
ERR = echo ${ TIME } ${ RED } [ FAIL] ${ CNone }
OK = echo ${ TIME } ${ GREEN } [ OK ] ${ CNone }
FAIL = ( echo ${ TIME } ${ RED } [ FAIL] ${ CNone } && false )
2020-11-23 14:21:01 +00:00
2021-02-18 10:27:10 +00:00
# ====================================================================================
# Conformance
2020-11-23 14:21:01 +00:00
2021-02-18 10:27:10 +00:00
# Ensure a PR is ready for review.
reviewable : generate helm .generate
@go mod tidy
# Ensure branch is clean.
check-diff : reviewable
@$( INFO) checking that branch is clean
@test -z " $$ (git status --porcelain) " || $( FAIL)
@$( OK) branch is clean
# ====================================================================================
# Golang
.PHONY : test
test : generate ## Run tests
@$( INFO) go test unit-tests
go test ./... -coverprofile cover.out
@$( OK) go test unit-tests
.PHONY : build
build : generate ## Build binary
@$( INFO) go build
@CGO_ENABLED= 0 go build -o $( OUTPUT_DIR) /external-secrets main.go
@$( OK) go build
2020-11-23 14:21:01 +00:00
2021-02-18 10:27:10 +00:00
# Check install of golanci-lint
lint.check :
2020-12-21 19:31:28 +00:00
@if ! golangci-lint --version > /dev/null 2>& 1; then \
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" ; \
exit 1; \
fi
2021-02-18 10:27:10 +00:00
# installs golangci-lint to the go bin dir
lint.install :
2020-12-21 19:31:28 +00:00
@if ! golangci-lint --version > /dev/null 2>& 1; then \
echo "Installing golangci-lint" ; \
curl -sfL https://install.goreleaser.com/github.com/golangci/golangci-lint.sh | sh -s -- -b $( GOBIN) v1.33.0; \
fi
2021-02-18 10:27:10 +00:00
lint : lint .check ## run golangci-lint
2020-12-21 19:31:28 +00:00
@if ! golangci-lint run; then \
echo -e "\033[0;33mgolangci-lint failed: some checks can be fixed with \`\033[0;32mmake fmt\033[0m\033[0;33m\`\033[0m" ; \
exit 1; \
fi
2021-02-18 10:27:10 +00:00
fmt : lint .check ## ensure consistent code style
@go mod tidy
@go fmt ./...
@golangci-lint run --fix > /dev/null 2>& 1 || true
@$( OK) Ensured consistent code style
2020-12-22 19:12:39 +00:00
2021-02-18 10:27:10 +00:00
generate : controller -gen ## Generate code, crds, manifests, etc
@$( CONTROLLER_GEN) object:headerFile= "hack/boilerplate.go.txt" paths = "./..."
@$( CONTROLLER_GEN) $( CRD_OPTIONS) rbac:roleName= manager-role webhook paths = "./..." output:crd:artifacts:config= $( CRD_DIR)
# Remove extra header lines in generated CRDs
@for i in $( CRD_DIR) /*.yaml; do \
tail -n +3 <" $$ i " >" $$ i.bkp " && \
cp " $$ i.bkp " " $$ i " && \
rm " $$ i.bkp " ; \
done
@$( OK) Finished generating deepcopy and manifests
2021-01-05 18:11:56 +00:00
2021-02-18 10:27:10 +00:00
# Find or download controller-gen
2020-11-23 14:21:01 +00:00
controller-gen :
i f e q ( , $( shell which controller -gen ) )
@{ \
set -e ; \
CONTROLLER_GEN_TMP_DIR = $$ ( mktemp -d) ; \
cd $$ CONTROLLER_GEN_TMP_DIR ; \
go mod init tmp ; \
2020-11-30 20:29:46 +00:00
go get sigs.k8s.io/controller-tools/cmd/controller-gen@v0.4.1 ; \
2020-11-23 14:21:01 +00:00
rm -rf $$ CONTROLLER_GEN_TMP_DIR ; \
}
CONTROLLER_GEN = $( GOBIN) /controller-gen
e l s e
CONTROLLER_GEN = $( shell which controller-gen)
e n d i f
2020-12-21 19:31:28 +00:00
2021-02-18 10:27:10 +00:00
# ====================================================================================
# Local Utility
# This is for running out-of-cluster locally, and is for convenience.
# For more control, try running the binary directly with different arguments.
run : generate
go run ./main.go
# Install CRDs into a cluster. This is for convenience.
crds.install : generate
kubectl apply -f $( CRD_DIR)
# Uninstall CRDs from a cluster. This is for convenience.
crds.uninstall :
kubectl delete -f $( CRD_DIR)
# ====================================================================================
# Helm Chart
helm.docs : ## Generate helm docs
cd $( HELM_DIR) ; \
docker run --rm -v $( shell pwd ) /$( HELM_DIR) :/helm-docs -u $( shell id -u) jnorwood/helm-docs:latest
helm.build : helm .generate ## Build helm chart
@$( INFO) helm package
@helm package $( HELM_DIR) --dependency-update --destination $( OUTPUT_DIR) /chart
@$( OK) helm package
# Copy crds to helm chart directory
helm.generate :
@cp $( CRD_DIR) /*.yaml $( HELM_DIR) /templates/crds/
# Add helm if statement for controlling the install of CRDs
@for i in $( HELM_DIR) /templates/crds/*.yaml; do \
cp " $$ i " " $$ i.bkp " && \
echo "{{- if .Values.installCRDs }}" > " $$ i " && \
cat " $$ i.bkp " >> " $$ i " && \
echo "{{- end }}" >> " $$ i " && \
rm " $$ i.bkp " ; \
done
@$( OK) Finished generating helm chart files
2021-03-01 07:31:02 +00:00
# ====================================================================================
# Documentation
.PHONY : docs
docs : generate
$( MAKE) -C ./hack/api-docs build
.PHONY : serve -docs
serve-docs :
$( MAKE) -C ./hack/api-docs serve
2021-02-18 10:27:10 +00:00
# ====================================================================================
# Build Artifacts
build.all : docker .build helm .build
docker.build : build ## Build the docker image
@$( INFO) docker build
@docker build . -t $( IMAGE_REGISTRY) :$( VERSION)
@$( OK) docker build
docker.push :
@$( INFO) docker push
@docker push $( IMAGE_REGISTRY) :$( VERSION)
@$( OK) docker push
# RELEASE_TAG is tag to promote. Default is promooting to main branch, but can be overriden
# to promote a tag to a specific version.
RELEASE_TAG ?= main
SOURCE_TAG ?= $( VERSION)
docker.promote :
@$( INFO) docker pull $( SOURCE_TAG)
@docker pull $( IMAGE_REGISTRY) :$( SOURCE_TAG)
@docker tag $( IMAGE_REGISTRY) :$( SOURCE_TAG) $( IMAGE_REGISTRY) :$( RELEASE_TAG)
@docker push $( IMAGE_REGISTRY) :$( RELEASE_TAG)
@$( OK) docker push $( RELEASE_TAG)
# ====================================================================================
# Help
# only comments after make target name are shown as help text
2020-12-21 19:31:28 +00:00
help : ## displays this help message
2021-02-18 10:27:10 +00:00
@echo -e " $$ (grep -hE '^\S+:.*##' $( MAKEFILE_LIST) | sed -e 's/:.*##\s*/:/' -e 's/^\(.\+\):\(.*\)/\\x1b[36m\1\\x1b[m:\2/' | column -c2 -t -s : | sort) "