1+ GO_VERSION ?= 1.20.2
2+ GOLANG_CI_VER ?= v1.52
3+ GOSEC_VER ?= 2.15.0
4+ TEST_COVERAGE_FILE =lcov.info
5+ TEST_COVERAGE_HTML_FILE =coverage_unit.html
6+ TEST_COVERAGE_FUNC_FILE =func_coverage.out
7+
8+ # CONTAINER_RUNNABLE checks if tests and lint check can be run inside container.
9+ PODMAN ?= $(shell podman -v > /dev/null 2>&1; echo $$? )
10+ ifeq ($(PODMAN ) , 0)
11+ CONTAINER_RUNTIME =podman
12+ else
13+ CONTAINER_RUNTIME =docker
14+ endif
15+ CONTAINER_RUNNABLE ?= $(shell $(CONTAINER_RUNTIME ) -v > /dev/null 2>&1; echo $$? )
16+
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+
24+ # Setting SHELL to bash allows bash commands to be executed by recipes.
25+ # This is a requirement for 'setup-envtest.sh' in the test target.
26+ # Options are set to exit when a recipe line exits non-zero or a piped command fails.
27+ SHELL = /usr/bin/env bash -o pipefail
28+ .SHELLFLAGS = -ec
29+
30+ # #@ General
31+
32+ # The help target prints out all targets with their descriptions organized
33+ # beneath their categories. The categories are represented by '##@' and the
34+ # target descriptions by '##'. The awk commands is responsible for reading the
35+ # entire set of makefiles included in this invocation, looking for lines of the
36+ # file as xyz: ## something, and then pretty-format the target and help. Then,
37+ # if there's a line with ##@ something, that gets pretty-printed as a category.
38+ # More info on the usage of ANSI control characters for terminal formatting:
39+ # https://en.wikipedia.org/wiki/ANSI_escape_code#SGR_parameters
40+ # More info on the awk command:
41+ # http://linuxcommand.org/lc3_adv_awk.php
42+
43+ .PHONY : help
44+ help : # # Display this help.
45+ @awk ' BEGIN {FS = ":.*##"; printf "\nUsage:\n make \033[36m<target>\033[0m\n"} /^[a-zA-Z_0-9-]+:.*?##/ { printf " \033[36m%-15s\033[0m %s\n", $$1, $$2 } /^##@/ { printf "\n\033[1m%s\033[0m\n", substr($$0, 5) } ' $(MAKEFILE_LIST )
46+
47+ # #@ Development
48+
49+ .PHONY : manifests
50+ manifests : controller-gen # # Generate WebhookConfiguration, ClusterRole and CustomResourceDefinition objects.
51+ $(CONTROLLER_GEN ) rbac:roleName=manager-role crd webhook paths=" ./..." output:crd:artifacts:config=config/crd/bases
52+
53+ # #@ Build Dependencies
54+
55+ # # Location to install dependencies to
56+ LOCALBIN ?= $(shell pwd) /bin
57+ $(LOCALBIN ) :
58+ mkdir -p $(LOCALBIN )
59+
60+ # # Tool Binaries
61+ CONTROLLER_GEN ?= $(LOCALBIN ) /controller-gen
62+
63+ # # Tool Versions
64+ # ENVTEST_K8S_VERSION refers to the version of kubebuilder assets to be downloaded by envtest binary.
65+ CONTROLLER_TOOLS_VERSION ?= v0.9.2
66+
67+ .PHONY : controller-gen
68+ controller-gen : $(CONTROLLER_GEN ) # # Download controller-gen locally if necessary.
69+ $(CONTROLLER_GEN ) : $(LOCALBIN )
70+ test -s $(LOCALBIN ) /controller-gen || GOBIN=$(LOCALBIN ) go install sigs.k8s.io/controller-tools/cmd/controller-gen@$(CONTROLLER_TOOLS_VERSION )
71+
72+ .PHONY : unit_clean
73+ unit_clean : # # clean up the unit test artifacts created
74+ ifeq ($(CONTAINER_RUNNABLE ) , 0)
75+ $(CONTAINER_RUNTIME) system prune -f
76+ endif
77+ rm ${TEST_COVERAGE_FILE} ${TEST_COVERAGE_HTML_FILE} ${TEST_COVERAGE_FUNC_FILE} > /dev/null 2>&1
78+
79+ .PHONY : unit
80+ unit : # # Run unit tests against code.
81+ ifeq ($(CONTAINER_RUNNABLE ) , 0)
82+ $(CONTAINER_RUNTIME) run -it -v ${PWD}:/go/src -w /go/src docker.io/library/golang:${GO_VERSION}-alpine3.17 \
83+ /bin/sh -c "go test ./... -v -coverprofile ${TEST_COVERAGE_FILE}; \
84+ go tool cover -html=${TEST_COVERAGE_FILE} -o ${TEST_COVERAGE_HTML_FILE}; \
85+ go tool cover -func=${TEST_COVERAGE_FILE} -o ${TEST_COVERAGE_FUNC_FILE}"
86+ else
87+ go test ./... -v -coverprofile ${TEST_COVERAGE_FILE}
88+ go tool cover -html=${TEST_COVERAGE_FILE} -o ${TEST_COVERAGE_HTML_FILE}
89+ go tool cover -func=${TEST_COVERAGE_FILE} -o ${TEST_COVERAGE_FUNC_FILE}
90+ endif
91+
92+ # Install link at https://golangci-lint.run/usage/install/ if not running inside a container
93+ .PHONY : lint
94+ lint : # # Run lint against code.
95+ ifeq ($(CONTAINER_RUNNABLE ) , 0)
96+ $(CONTAINER_RUNTIME) run -it -v ${PWD}:/go/src -w /go/src docker.io/golangci/golangci-lint:${GOLANG_CI_VER}-alpine golangci-lint run ./... -v
97+ else
98+ golangci-lint run ./... -v
99+ endif
100+
101+ # Install link at https://github.com/securego/gosec#install if not running inside a container
102+ .PHONY : gosec
103+ gosec : # # inspects source code for security problem by scanning the Go Abstract Syntax Tree
104+ ifeq ($(CONTAINER_RUNNABLE ) , 0)
105+ $(CONTAINER_RUNTIME) run -it -v ${PWD}:/go/src -w /go/src docker.io/securego/gosec:${GOSEC_VER} ./...
106+ else
107+ gosec ./...
108+ endif
0 commit comments