-
Notifications
You must be signed in to change notification settings - Fork 9
/
Makefile
343 lines (274 loc) · 12 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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
.PHONY: default
default:
$(MAKE) -s $(IMAGES)
.PHONY: all
all: default
help: ## Display this help.
@awk 'BEGIN {FS = ":.*##"; printf "\nUsage:\n make \033[36m<target>\033[0m\n"} /^[a-zA-Z_0-9-]+:.*?##/ { printf " \033[36m%-20s\033[0m %s\n", $$1, $$2 } /^##@/ { printf "\n\033[1m%s\033[0m\n", substr($$0, 5) } ' $(MAKEFILE_LIST)
############################################################################
# Variables
############################################################################
IMAGES ?= base-image stateless-lb proxy tapa ipam nsp example-target frontend operator init
# Versions
VERSION ?= latest
VERSION_STATELESS_LB ?= $(VERSION)
VERSION_PROXY ?= $(VERSION)
VERSION_TAPA ?= $(VERSION)
VERSION_IPAM ?= $(VERSION)
VERSION_NSP ?= $(VERSION)
VERSION_EXAMPLE_TARGET ?= $(VERSION)
VERSION_FRONTEND ?= $(VERSION)
VERSION_BASE_IMAGE ?= $(VERSION)
VERSION_OPERATOR ?= $(VERSION)
LOCAL_VERSION ?= $(VERSION)
VERSION_INIT ?= $(VERSION)
# E2E tests
E2E_FOCUS ?= ""
E2E_SKIP ?= ""
E2E_ENVIRONMENT ?= "kind-helm"
E2E_IP_FAMILY ?= "dualstack"
E2E_PARAMETERS ?= $(shell cat ./test/e2e/environment/$(E2E_ENVIRONMENT)/$(E2E_IP_FAMILY)/config.txt | tr '\n' ' ')
E2E_SEED ?= $(shell shuf -i 1-2147483647 -n1)
# Contrainer Registry
REGISTRY ?= registry.nordix.org/cloud-native/meridio
NSM_REPOSITORY ?= cloud-native/nsm
BASE_IMAGE ?= $(REGISTRY)/base-image:$(VERSION_BASE_IMAGE)
DEBUG_IMAGE ?= $(REGISTRY)/debug:$(VERSION)
# Tools
export PATH := $(shell pwd)/bin:$(PATH)
GOLANGCI_LINT = $(shell pwd)/bin/golangci-lint
GINKGO = $(shell pwd)/bin/ginkgo
MOCKGEN = $(shell pwd)/bin/mockgen
PROTOC_GEN_GO = $(shell pwd)/bin/protoc-gen-go
PROTOC_GEN_GO_GRPC = $(shell pwd)/bin/protoc-gen-go-grpc
NANCY = $(shell pwd)/bin/nancy
CONTROLLER_GEN = $(shell pwd)/bin/controller-gen
KUSTOMIZE = $(shell pwd)/bin/kustomize
PROJECT_DIR := $(shell dirname $(abspath $(lastword $(MAKEFILE_LIST))))
BUILD_DIR ?= build
BUILD_STEPS ?= build tag push
# Security Scan
OUTPUT_DIR ?= _output
SECURITY_SCAN_VOLUME ?= --volume /var/run/docker.sock:/var/run/docker.sock --volume $(HOME)/Library/Caches:/root/.cache/
# Operator
TEMPLATES_HELM_CHART_VALUES_PATH = config/templates/charts/meridio/values.yaml
OPERATOR_NAMESPACE = meridio-operator
ENABLE_MUTATING_WEBHOOK?=true
WEBHOOK_SUPPORT ?= spire # spire or certmanager
#############################################################################
# Container: Build, tag, push
#############################################################################
.PHONY: build
build:
docker build -t $(IMAGE):$(LOCAL_VERSION) --build-arg meridio_version=$(shell git describe --dirty --tags) --build-arg base_image=$(BASE_IMAGE) -f ./$(BUILD_DIR)/$(IMAGE)/Dockerfile .
.PHONY: tag
tag:
docker tag $(IMAGE):$(LOCAL_VERSION) $(REGISTRY)/$(IMAGE):$(VERSION)
.PHONY: push
push:
docker push $(REGISTRY)/$(IMAGE):$(VERSION)
#############################################################################
##@ Component (Build, tag, push): use VERSION to set the version. Use BUILD_STEPS to set the build steps (build, tag, push)
#############################################################################
.PHONY: base-image
base-image: ## Build the base-image.
VERSION=$(VERSION_BASE_IMAGE) IMAGE=base-image $(MAKE) -s $(BUILD_STEPS)
.PHONY: debug-image
debug-image: ## Build the debug-image.
docker build -t $(DEBUG_IMAGE) -f ./build/debug/Dockerfile .
.PHONY: stateless-lb
stateless-lb: ## Build the stateless-lb.
VERSION=$(VERSION_STATELESS_LB) IMAGE=stateless-lb $(MAKE) -s $(BUILD_STEPS)
.PHONY: proxy
proxy: ## Build the proxy.
VERSION=$(VERSION_PROXY) IMAGE=proxy $(MAKE) -s $(BUILD_STEPS)
.PHONY: tapa
tapa: ## Build the tapa.
VERSION=$(VERSION_TAPA) IMAGE=tapa $(MAKE) -s $(BUILD_STEPS)
.PHONY: ipam
ipam: ## Build the ipam.
VERSION=$(VERSION_IPAM) IMAGE=ipam $(MAKE) -s $(BUILD_STEPS)
.PHONY: nsp
nsp: ## Build the nsp.
VERSION=$(VERSION_NSP) IMAGE=nsp $(MAKE) -s $(BUILD_STEPS)
.PHONY: example-target
example-target: ## Build the example target.
VERSION=$(VERSION_EXAMPLE_TARGET) BUILD_DIR=examples/target/build IMAGE=example-target $(MAKE) $(BUILD_STEPS)
.PHONY: frontend
frontend: ## Build the frontend.
VERSION=$(VERSION_FRONTEND) IMAGE=frontend $(MAKE) -s $(BUILD_STEPS)
.PHONY: operator
operator: ## Build the operator.
VERSION=$(VERSION_OPERATOR) IMAGE=operator $(MAKE) -s $(BUILD_STEPS)
.PHONY: init
init: ## Build the init image.
VERSION=$(VERSION_INIT) IMAGE=init $(MAKE) -s $(BUILD_STEPS)
#############################################################################
##@ Testing & Code check
#############################################################################
.PHONY: lint
lint: golangci-lint ## Run linter against code.
$(GOLANGCI_LINT) run ./...
.PHONY: e2e
e2e: ginkgo output-dir ## Run the E2E tests.
$(GINKGO) -v \
--no-color --seed=$(E2E_SEED) \
--repeat=0 --timeout=1h \
--randomize-all \
$(shell $(MAKE) -s print-e2e-skip-focus E2E_FOCUS=$(E2E_FOCUS) E2E_SKIP=$(E2E_SKIP)) \
--json-report=e2e_report.json \
--junit-report=e2e_report_junit.xml \
--output-dir=$(OUTPUT_DIR) \
./test/e2e/... -- $(E2E_PARAMETERS)
.PHONY: test
test: output-dir ## Run the Unit tests (read coverage report: go tool cover -html=_output/cover_unit_test.out -o _output/cover_unit_test.html).
go test -race -cover -short -count=1 -coverprofile $(OUTPUT_DIR)/cover_unit_test.out ./...
.PHONY: check
check: lint test ## Run the linter and the Unit tests.
#############################################################################
##@ Security Scan
#############################################################################
# https://github.com/anchore/grype
.PHONY: grype
grype: ## Run grype scanner on images.
@BUILD_STEPS=grype-scan $(MAKE) -s $(IMAGES)
.PHONY: grype-scan
grype-scan: output-dir
docker run --rm $(SECURITY_SCAN_VOLUME) \
--name Grype anchore/grype:v0.70.0 \
$(REGISTRY)/$(IMAGE):$(VERSION) -o json --add-cpes-if-none > $(OUTPUT_DIR)/grype_$(IMAGE)_$(VERSION).json
# https://github.com/aquasecurity/trivy
.PHONY: trivy
trivy: ## Run trivy scanner on images.
@BUILD_STEPS=trivy-scan $(MAKE) -s $(IMAGES)
.PHONY: trivy-scan
trivy-scan: output-dir
docker run --rm $(SECURITY_SCAN_VOLUME) \
aquasec/trivy:0.45.1 image \
-f json $(REGISTRY)/$(IMAGE):$(VERSION) > $(OUTPUT_DIR)/trivy_$(IMAGE)_$(VERSION).json
# https://github.com/sonatype-nexus-community/nancy
.PHONY: nancy
nancy: nancy-tool output-dir ## Run nancy scanner on dependencies.
go list -json -deps ./... | nancy sleuth -o json-pretty > $(OUTPUT_DIR)/nancy.json || true
#############################################################################
##@ Code generation
#############################################################################
.PHONY: generate
generate: go-generate proto manifests generate-controller ## Generate all.
.PHONY: go-generate
go-generate: mockgen ## Generate the mocks.
go generate ./...
.PHONY: ipam-proto
ipam-proto: proto-compiler
protoc --go_out=. --go_opt=paths=source_relative --go-grpc_out=. --go-grpc_opt=paths=source_relative api/ipam/**/*.proto
.PHONY: nsp-proto
nsp-proto: proto-compiler
protoc --go_out=. --go_opt=paths=source_relative --go-grpc_out=. --go-grpc_opt=paths=source_relative api/nsp/**/*.proto
.PHONY: ambassador-proto
ambassador-proto: proto-compiler
protoc --go_out=. --go_opt=paths=source_relative --go-grpc_out=. --go-grpc_opt=paths=source_relative api/ambassador/**/*.proto
.PHONY: loadbalancer-proto
loadbalancer-proto: proto-compiler
protoc --go_out=. --go_opt=paths=source_relative --go-grpc_out=. --go-grpc_opt=paths=source_relative api/loadbalancer/**/*.proto
.PHONY: proto
proto: ipam-proto nsp-proto ambassador-proto loadbalancer-proto ## Compile the proto.
.PHONY: manifests
manifests: controller-gen ## Generate WebhookConfiguration, ClusterRole and CustomResourceDefinition objects.
$(CONTROLLER_GEN) crd rbac:roleName=operator-role webhook paths="./..." output:crd:artifacts:config=config/crd/bases
.PHONY: generate-controller
generate-controller: controller-gen ## Generate code containing DeepCopy, DeepCopyInto, and DeepCopyObject method implementations.
$(CONTROLLER_GEN) object:headerFile="hack/boilerplate.go.txt" paths="./..."
.PHONY: generate-helm-chart
generate-helm-chart: output-dir ## Generate Meridio, CRDs and target helm charts.
@REGISTRY="$(shell echo ${REGISTRY} | cut -d "/" -f 1)" \
REPOSITORY="$(shell echo ${REGISTRY} | cut -d "/" -f 2-)" \
NSM_REPOSITORY="$(NSM_REPOSITORY)" \
VERSION=$(VERSION) \
./hack/helm-chart/generator.sh
#############################################################################
##@ Operator
#############################################################################
.PHONY: deploy
deploy: manifests kustomize namespace set-templates-values ## Deploy controller to the K8s cluster specified in ~/.kube/config.
cd config/operator && $(KUSTOMIZE) edit set image operator=${REGISTRY}/operator:${VERSION_OPERATOR}
$(KUSTOMIZE) build config/default --enable-helm | kubectl apply -f -
.PHONY: undeploy
undeploy: namespace ## Undeploy controller from the K8s cluster specified in ~/.kube/config.
$(KUSTOMIZE) build config/default --enable-helm | kubectl delete -f - --ignore-not-found=true
.PHONY: set-templates-values
set-templates-values: # Set the values in the templates helm chart
sed -i 's/^version: .*/version: ${VERSION}/' ${TEMPLATES_HELM_CHART_VALUES_PATH} ; \
sed -i 's/^registry: .*/registry: "$(shell echo ${REGISTRY} | cut -d "/" -f 1)"/' ${TEMPLATES_HELM_CHART_VALUES_PATH} ; \
sed -i 's#^repository: .*#repository: "$(shell echo ${REGISTRY} | cut -d "/" -f 2-)"#' ${TEMPLATES_HELM_CHART_VALUES_PATH}
sed -i 's#^ repository: .*# repository: "${NSM_REPOSITORY}"#' ${TEMPLATES_HELM_CHART_VALUES_PATH}
.PHONY: namespace
namespace: # Edit the namespace of operator to be deployed
cd config/default && $(KUSTOMIZE) edit set namespace ${OPERATOR_NAMESPACE}
.PHONY: print-manifests
print-manifests: manifests kustomize namespace set-templates-values # Generate manifests to be deployed in the cluster
cd config/operator && $(KUSTOMIZE) edit set image operator=${REGISTRY}/operator:${VERSION_OPERATOR}
$(KUSTOMIZE) build config/default --enable-helm
#############################################################################
# Tools
#############################################################################
.PHONY: output-dir
output-dir:
@mkdir -p $(OUTPUT_DIR)
.PHONY: golangci-lint
golangci-lint:
$(call go-get-tool,$(GOLANGCI_LINT),github.com/golangci/golangci-lint/cmd/[email protected])
.PHONY: proto-compiler
proto-compiler: protoc protoc-gen-go protoc-gen-go-grpc
.PHONY: protoc
protoc:
@if [ ! $(shell which protoc) ]; then\
echo "Protocol buffer compiler (protoc) must be installed: https://grpc.io/docs/protoc-installation/#install-pre-compiled-binaries-any-os";\
fi
.PHONY: protoc-gen-go
protoc-gen-go:
$(call go-get-tool,$(PROTOC_GEN_GO),google.golang.org/protobuf/cmd/[email protected])
.PHONY: protoc-gen-go-grpc
protoc-gen-go-grpc:
$(call go-get-tool,$(PROTOC_GEN_GO_GRPC),google.golang.org/grpc/cmd/[email protected])
.PHONY: mockgen
mockgen:
$(call go-get-tool,$(MOCKGEN),github.com/golang/mock/[email protected])
.PHONY: ginkgo
ginkgo:
$(call go-get-tool,$(GINKGO),github.com/onsi/ginkgo/v2/[email protected])
.PHONY: nancy-tool
nancy-tool:
$(call go-get-tool,$(NANCY),github.com/sonatype-nexus-community/[email protected])
.PHONY: controller-gen
controller-gen:
$(call go-get-tool,$(CONTROLLER_GEN),sigs.k8s.io/controller-tools/cmd/[email protected])
.PHONY: kustomize
kustomize:
$(call go-get-tool,$(KUSTOMIZE),sigs.k8s.io/kustomize/kustomize/[email protected])
.PHONY: print-e2e-skip-focus
print-e2e-skip-focus:
@focus="" ; \
for f in $(call get_list,$(E2E_FOCUS)); do \
focus="$${focus} --focus $${f}" ; \
done ; \
printf "$${focus}" ; \
skip="" ; \
for f in $(call get_list,$(E2E_SKIP)); do \
skip="$${skip} --skip $${f}" ; \
done ; \
printf "$${skip}"
define get_list
$$(echo "$(1)" | sed -r 's/ //g' | sed -r 's/,/ /g' )
endef
# go-get-tool will 'go get' any package $2 and install it to $1.
define go-get-tool
@[ -f $(1) ] || { \
set -e ;\
TMP_DIR=$$(mktemp -d) ;\
cd $$TMP_DIR ;\
go mod init tmp ;\
echo "Downloading $(2)" ;\
GOBIN=$(PROJECT_DIR)/bin go install $(2) ;\
rm -rf $$TMP_DIR ;\
}
endef