forked from kyverno/reports-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Makefile
260 lines (216 loc) · 8.11 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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
.DEFAULT_GOAL := build
##########
# CONFIG #
##########
ORG ?= kyverno
PACKAGE ?= github.com/$(ORG)/reports-server
GIT_SHA := $(shell git rev-parse HEAD)
GOOS ?= $(shell go env GOOS)
GOARCH ?= $(shell go env GOARCH)
REGISTRY ?= ghcr.io
REPO ?= reports-server
REPO_REPORTS_SERVER ?= $(REGISTRY)/$(ORG)/$(REPO)
#########
# TOOLS #
#########
TOOLS_DIR := $(PWD)/.tools
REGISTER_GEN := $(TOOLS_DIR)/register-gen
OPENAPI_GEN := $(TOOLS_DIR)/openapi-gen
CODE_GEN_VERSION := v0.28.0
KIND := $(TOOLS_DIR)/kind
KIND_VERSION := v0.20.0
KO := $(TOOLS_DIR)/ko
KO_VERSION := v0.14.1
HELM := $(TOOLS_DIR)/helm
HELM_VERSION := v3.10.1
TOOLS := $(REGISTER_GEN) $(OPENAPI_GEN) $(KIND) $(KO) $(HELM)
ifeq ($(GOOS), darwin)
SED := gsed
else
SED := sed
endif
$(REGISTER_GEN):
@echo Install register-gen... >&2
@GOBIN=$(TOOLS_DIR) go install k8s.io/code-generator/cmd/register-gen@$(CODE_GEN_VERSION)
$(OPENAPI_GEN):
@echo Install openapi-gen... >&2
@GOBIN=$(TOOLS_DIR) go install k8s.io/code-generator/cmd/openapi-gen@$(CODE_GEN_VERSION)
$(KIND):
@echo Install kind... >&2
@GOBIN=$(TOOLS_DIR) go install sigs.k8s.io/kind@$(KIND_VERSION)
$(KO):
@echo Install ko... >&2
@GOBIN=$(TOOLS_DIR) go install github.com/google/ko@$(KO_VERSION)
$(HELM):
@echo Install helm... >&2
@GOBIN=$(TOOLS_DIR) go install helm.sh/helm/v3/cmd/helm@$(HELM_VERSION)
.PHONY: install-tools
install-tools: $(TOOLS) ## Install tools
.PHONY: clean-tools
clean-tools: ## Remove installed tools
@echo Clean tools... >&2
@rm -rf $(TOOLS_DIR)
#########
# BUILD #
#########
CGO_ENABLED ?= 0
LOCAL_PLATFORM := linux/$(GOARCH)
KO_REGISTRY := ko.local
KO_CACHE ?= /tmp/ko-cache
BIN := reports-server
ifdef VERSION
LD_FLAGS := "-s -w -X $(PACKAGE)/pkg/version.BuildVersion=$(VERSION)"
else
LD_FLAGS := "-s -w"
endif
ifndef VERSION
KO_TAGS := $(GIT_SHA)
else ifeq ($(VERSION),main)
KO_TAGS := $(GIT_SHA),latest
else
KO_TAGS := $(GIT_SHA),$(subst /,-,$(VERSION))
endif
.PHONY: fmt
fmt: ## Run go fmt
@echo Go fmt... >&2
@go fmt ./...
.PHONY: vet
vet: ## Run go vet
@echo Go vet... >&2
@go vet ./...
$(BIN): fmt vet
@echo Build cli binary... >&2
@CGO_ENABLED=$(CGO_ENABLED) GOOS=$(GOOS) go build -o ./$(BIN) -ldflags=$(LD_FLAGS) .
.PHONY: build
build: $(BIN) ## Build
.PHONY: ko-build
ko-build: $(KO) ## Build image (with ko)
@echo Build image with ko... >&2
@LDFLAGS=$(LD_FLAGS) KOCACHE=$(KO_CACHE) KO_DOCKER_REPO=$(KO_REGISTRY) \
$(KO) build . --preserve-import-paths --tags=$(KO_TAGS) --platform=$(LOCAL_PLATFORM)
########
# TEST #
########
.PHONY: tests
tests: build ## Run tests
@echo Running tests... >&2
@go test ./... -race -coverprofile=coverage.out -covermode=atomic
###########
# CODEGEN #
###########
GOPATH_SHIM := ${PWD}/.gopath
PACKAGE_SHIM := $(GOPATH_SHIM)/src/$(PACKAGE)
$(GOPATH_SHIM):
@echo Create gopath shim... >&2
@mkdir -p $(GOPATH_SHIM)
.INTERMEDIATE: $(PACKAGE_SHIM)
$(PACKAGE_SHIM): $(GOPATH_SHIM)
@echo Create package shim... >&2
@mkdir -p $(GOPATH_SHIM)/src/github.com/$(ORG) && ln -s -f ${PWD} $(PACKAGE_SHIM)
.PHONY: codegen-openapi
codegen-openapi: $(PACKAGE_SHIM) $(OPENAPI_GEN) ## Generate openapi
@echo Generate openapi... >&2
@$(OPENAPI_GEN) \
-i k8s.io/apimachinery/pkg/api/resource \
-i k8s.io/apimachinery/pkg/apis/meta/v1 \
-i k8s.io/apimachinery/pkg/version \
-i k8s.io/apimachinery/pkg/runtime \
-i k8s.io/apimachinery/pkg/types \
-i k8s.io/api/core/v1 \
-i sigs.k8s.io/wg-policy-prototypes/policy-report/pkg/api/wgpolicyk8s.io/v1alpha2 \
-i github.com/kyverno/kyverno/api/reports/v1 \
-i github.com/kyverno/kyverno/api/policyreport/v1alpha2 \
-p ./pkg/api/generated/openapi \
-O zz_generated.openapi \
-h ./.hack/boilerplate.go.txt
.PHONY: codegen-helm-docs
codegen-helm-docs: ## Generate helm docs
@echo Generate helm docs... >&2
@docker run -v ${PWD}/charts:/work -w /work jnorwood/helm-docs:v1.11.0 -s file
.PHONY: codegen-install-manifest
codegen-install-manifest: $(HELM) ## Create install manifest
@echo Generate latest install manifest... >&2
@$(HELM) template reports-server --namespace reports-server ./charts/reports-server/ \
--set templating.enabled=true \
| $(SED) -e '/^#.*/d' \
> ./config/install.yaml
codegen-install-manifest-inmemory: $(HELM) ## Create install manifest without postgres
@echo Generate latest install manifest... >&2
@$(HELM) template reports-server --namespace reports-server ./charts/reports-server/ \
--set config.debug=true \
--set postgresql.enabled=false \
--set templating.enabled=true \
| $(SED) -e '/^#.*/d' \
> ./config/install-inmemory.yaml
.PHONY: codegen
codegen: ## Rebuild all generated code and docs
codegen: codegen-helm-docs
codegen: codegen-openapi
codegen: codegen-install-manifest
codegen: codegen-install-manifest-inmemory
.PHONY: verify-codegen
verify-codegen: codegen ## Verify all generated code and docs are up to date
@echo Checking codegen is up to date... >&2
@git --no-pager diff -- .
@echo 'If this test fails, it is because the git diff is non-empty after running "make codegen".' >&2
@echo 'To correct this, locally run "make codegen", commit the changes, and re-run tests.' >&2
@git diff --quiet --exit-code -- .
########
# KIND #
########
KIND_IMAGE ?= kindest/node:v1.28.0
KIND_NAME ?= kind
.PHONY: kind-create
kind-create: $(KIND) ## Create kind cluster
@echo Create kind cluster... >&2
@$(KIND) create cluster --name $(KIND_NAME) --image $(KIND_IMAGE) --wait 1m
.PHONY: kind-delete
kind-delete: $(KIND) ## Delete kind cluster
@echo Delete kind cluster... >&2
@$(KIND) delete cluster --name $(KIND_NAME)
.PHONY: kind-load
kind-load: $(KIND) ko-build ## Build image and load in kind cluster
@echo Load image... >&2
@$(KIND) load docker-image --name $(KIND_NAME) $(KO_REGISTRY)/$(PACKAGE):$(GIT_SHA)
.PHONY: kind-install
kind-install: $(HELM) kind-load ## Build image, load it in kind cluster and deploy helm chart
@echo Install chart... >&2
@$(HELM) upgrade --install reports-server --namespace reports-server --create-namespace --wait ./charts/reports-server \
--set image.registry=$(KO_REGISTRY) \
--set image.repository=$(PACKAGE) \
--set image.tag=$(GIT_SHA)
.PHONY: kind-install-inmemory
kind-install-inmemory: $(HELM) kind-load ## Build image, load it in kind cluster and deploy helm chart
@echo Install chart... >&2
@$(HELM) upgrade --install reports-server --namespace reports-server --create-namespace --wait ./charts/reports-server \
--set image.registry=$(KO_REGISTRY) \
--set config.debug=true \
--set postgresql.enabled=false \
--set image.repository=$(PACKAGE) \
--set image.tag=$(GIT_SHA)
.PHONY: kind-apply
kind-apply: $(HELM) kind-load ## Build image, load it in kind cluster and deploy helm chart
@echo Install chart... >&2
@$(HELM) template reports-server --namespace reports-server ./charts/reports-server \
--set image.registry=$(KO_REGISTRY) \
--set image.repository=$(PACKAGE) \
--set image.tag=$(GIT_SHA) \
| kubectl apply -f -
########
# HELP #
########
.PHONY: help
help: ## Shows the available commands
@grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-40s\033[0m %s\n", $$1, $$2}'
################
# PUBLISH (KO) #
################
REGISTRY_USERNAME ?= dummy
PLATFORMS := all
.PHONY: ko-login
ko-login: $(KO)
@$(KO) login $(REGISTRY) --username $(REGISTRY_USERNAME) --password $(REGISTRY_PASSWORD)
.PHONY: ko-publish-reports-server
ko-publish-reports-server: ko-login ## Build and publish reports-server image (with ko)
@LD_FLAGS=$(LD_FLAGS) KOCACHE=$(KOCACHE) KO_DOCKER_REPO=$(REPO_REPORTS_SERVER) \
$(KO) build . --bare --tags=$(KO_TAGS) --platform=$(PLATFORMS)