-
Notifications
You must be signed in to change notification settings - Fork 4
/
Makefile
133 lines (109 loc) · 5.77 KB
/
Makefile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
ROOTDIR ?= $(CURDIR)
## Location to install dependencies to
LOCAL_BIN ?= $(ROOTDIR)/bin
$(LOCAL_BIN):
mkdir -p $(LOCAL_BIN)
# Keep an existing GOPATH, make a private one if it is undefined
export GOPATH ?= $(shell go env GOPATH)
ifeq ($(GOPATH),)
GOPATH := $(ROOTDIR)/.go
endif
GOBIN_DEFAULT := $(GOPATH)/bin
export GOBIN ?= $(GOBIN_DEFAULT)
# Set PATH so that locally installed things will be used first
export PATH=$(LOCAL_BIN):$(GOBIN):$(shell echo $$PATH)
# Setting SHELL to bash allows bash commands to be executed by recipes.
# This might be a requirement for envtest, used by the test suites.
# Options are set to exit when a recipe line exits non-zero or a piped command fails.
SHELL = /usr/bin/env bash -o pipefail
.SHELLFLAGS = -ec
# go-install will 'go install' any package $1 to LOCAL_BIN
# Note: this replaces the `go-get-tool` scaffolded by kubebuilder.
go-install = @set -e ; mkdir -p $(LOCAL_BIN) ; GOBIN=$(LOCAL_BIN) go install $(1)
# Local utilities must be defined before other targets so they work correctly.
# Note: this pattern of variables, paths, and target names allows users to
# override the version used, and helps Make by not using PHONY targets.
# To 'refresh' versions, remove the local bin directory.
CONTROLLER_GEN_VERSION ?= v0.15.0 # https://github.com/kubernetes-sigs/controller-tools/releases/latest
CONTROLLER_GEN ?= $(LOCAL_BIN)/controller-gen
$(CONTROLLER_GEN): $(LOCAL_BIN)
$(call go-install,sigs.k8s.io/controller-tools/cmd/controller-gen@$(CONTROLLER_GEN_VERSION))
KUSTOMIZE_VERSION ?= v5.4.1 # https://github.com/kubernetes-sigs/kustomize/releases/latest
KUSTOMIZE ?= $(LOCAL_BIN)/kustomize
$(KUSTOMIZE): $(LOCAL_BIN)
$(call go-install,sigs.k8s.io/kustomize/kustomize/v5@$(KUSTOMIZE_VERSION))
GOFUMPT_VERSION ?= v0.6.0 # https://github.com/mvdan/gofumpt/releases/latest
GOFUMPT ?= $(LOCAL_BIN)/gofumpt
$(GOFUMPT): $(LOCAL_BIN)
$(call go-install,mvdan.cc/gofumpt@$(GOFUMPT_VERSION))
GCI_VERSION ?= v0.13.4 # https://github.com/daixiang0/gci/releases/latest
GCI ?= $(LOCAL_BIN)/gci
$(GCI): $(LOCAL_BIN)
$(call go-install,github.com/daixiang0/gci@$(GCI_VERSION))
GOSEC_VERSION ?= v2.19.0 # https://github.com/securego/gosec/releases/latest
GOSEC ?= $(LOCAL_BIN)/gosec
$(GOSEC): $(LOCAL_BIN)
$(call go-install,github.com/securego/gosec/v2/cmd/gosec@$(GOSEC_VERSION))
GOLANGCI_VERSION ?= v1.58.0 # https://github.com/golangci/golangci-lint/releases/latest
GOLANGCI ?= $(LOCAL_BIN)/golangci-lint
$(GOLANGCI): $(LOCAL_BIN)
$(call go-install,github.com/golangci/golangci-lint/cmd/golangci-lint@$(GOLANGCI_VERSION))
# To change this version, adjust it in the go.mod file
# https://github.com/onsi/ginkgo/releases/latest
GINKGO_VERSION := $(shell awk '/github.com\/onsi\/ginkgo\/v2/ {print $$2}' go.mod)
GINKGO ?= $(LOCAL_BIN)/ginkgo
$(GINKGO): $(LOCAL_BIN)
$(call go-install,github.com/onsi/ginkgo/v2/ginkgo@$(GINKGO_VERSION))
ENVTEST ?= $(LOCAL_BIN)/setup-envtest
$(ENVTEST): $(LOCAL_BIN)
$(call go-install,sigs.k8s.io/controller-runtime/tools/setup-envtest@latest)
.PHONY: manifests
manifests: $(CONTROLLER_GEN) $(KUSTOMIZE) ## Generate WebhookConfiguration, ClusterRole and CustomResourceDefinition objects.
$(CONTROLLER_GEN) rbac:roleName=manager-role crd webhook paths=".;./api/..." \
output:crd:artifacts:config=config/crd/bases
$(CONTROLLER_GEN) rbac:roleName=manager-role crd webhook paths="./test/fakepolicy/..." \
output:crd:artifacts:config=test/fakepolicy/config/crd/bases \
output:rbac:artifacts:config=test/fakepolicy/config/rbac
$(KUSTOMIZE) build ./test/fakepolicy/config/default > ./test/fakepolicy/config/deploy.yaml
cp ./config/samples/* ./test/fakepolicy/test/utils/testdata/
.PHONY: generate
generate: $(CONTROLLER_GEN) ## Generate code containing DeepCopy, DeepCopyInto, and DeepCopyObject method implementations.
$(CONTROLLER_GEN) object:headerFile="config/boilerplate.go.txt" paths="./..."
.PHONY: fmt
fmt: $(GOFUMPT) $(GCI)
go mod tidy
find . -not \( -path "./.go" -prune \) -name "*.go" | xargs $(GOFUMPT) -l -w
find . -not \( -path "./.go" -prune \) -name "*.go" | xargs $(GCI) write --skip-generated \
-s standard -s default -s Prefix\(open-cluster-management.io\)
.PHONY: vet
vet: $(GOSEC)
go vet ./...
$(GOSEC) -fmt sonarqube -out gosec.json -stdout -exclude-dir=.go -exclude-dir=test -exclude-generated ./...
# Note: this target is not used by Github Actions. Instead, each linter is run
# separately to automatically decorate the code with the linting errors.
# Note: this target will fail if yamllint is not installed. Installing yamllint
# is outside the scope of this Makefile.
.PHONY: lint
lint: $(GOLANGCI)
$(GOLANGCI) run
yamllint -c $(ROOTDIR)/.yamllint.yaml .
ENVTEST_K8S_VERSION ?= 1.29
.PHONY: test
test: manifests generate $(GINKGO) $(ENVTEST) ## Run all the tests, except for fuzz tests
KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use $(ENVTEST_K8S_VERSION) -p path)" $(GINKGO) \
--coverpkg=./... --covermode=count --coverprofile=raw-cover.out ./...
.PHONY: test-unit
test-unit: ## Run only the unit tests
go test --coverpkg=./... --covermode=count --coverprofile=cover-unit.out ./api/... ./pkg/...
.PHONY: test-basicsuite
test-basicsuite: manifests generate $(GINKGO) $(ENVTEST) ## Run just the basic suite of tests
KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use $(ENVTEST_K8S_VERSION) -p path)" $(GINKGO) \
--coverpkg=./... --covermode=count --coverprofile=cover-basic.out ./test/fakepolicy/test/basic
.PHONY: test-compsuite
test-compsuite: manifests generate $(GINKGO) $(ENVTEST) ## Run just the compliance event tests
KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use $(ENVTEST_K8S_VERSION) -p path)" $(GINKGO) \
--coverpkg=./... --covermode=count --coverprofile=cover-comp.out ./test/fakepolicy/test/compliance
.PHONY: fuzz-test
fuzz-test:
go test ./api/v1beta1 -fuzz=FuzzMatchesExcludeAll -fuzztime=20s
go test ./api/v1beta1 -fuzz=FuzzMatchesIncludeAll -fuzztime=20s