Repository navigation
Expand file tree
/
Copy pathMakefile
More file actions
315 lines (256 loc) · 9.58 KB
/
Copy pathMakefile
File metadata and controls
315 lines (256 loc) · 9.58 KB
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
# Version
GIT_HEAD_COMMIT ?= $(shell git rev-parse --short HEAD)
VERSION ?= $(or $(shell git describe --abbrev=0 --tags --match "v*" 2>/dev/null),$(GIT_HEAD_COMMIT))
GOOS ?= $(shell go env GOOS)
GOARCH ?= $(shell go env GOARCH)
# Defaults
REGISTRY ?= ghcr.io
REPOSITORY ?= peak-scale/observability-tenancy
GIT_TAG_COMMIT ?= $(shell git rev-parse --short $(VERSION))
GIT_MODIFIED_1 ?= $(shell git diff $(GIT_HEAD_COMMIT) $(GIT_TAG_COMMIT) --quiet && echo "" || echo ".dev")
GIT_MODIFIED_2 ?= $(shell git diff --quiet && echo "" || echo ".dirty")
GIT_MODIFIED ?= $(shell echo "$(GIT_MODIFIED_1)$(GIT_MODIFIED_2)")
GIT_REPO ?= $(shell git config --get remote.origin.url)
BUILD_DATE ?= $(shell git log -1 --format="%at" | xargs -I{} sh -c 'if [ "$(shell uname)" = "Darwin" ]; then date -r {} +%Y-%m-%dT%H:%M:%S; else date -d @{} +%Y-%m-%dT%H:%M:%S; fi')
CORTEX_IMG_BASE ?= $(REPOSITORY)/cortex-proxy
CORTEX_IMG ?= $(CORTEX_IMG_BASE):$(VERSION)
CORTEX_FULL_IMG ?= $(REGISTRY)/$(CORTEX_IMG_BASE)
LOKI_IMG_BASE ?= $(REPOSITORY)/loki-proxy
LOKI_IMG ?= $(LOKI_IMG_BASE):$(VERSION)
LOKI_FULL_IMG ?= $(REGISTRY)/$(LOKI_IMG_BASE)
KIND_K8S_VERSION ?= "v1.37.0"
KIND_K8S_NAME ?= "observability-addon"
## Tool Binaries
KUBECTL ?= kubectl
HELM ?= helm
# Get the currently used golang install path (in GOPATH/bin, unless GOBIN is set)
ifeq (,$(shell go env GOBIN))
GOBIN=$(shell go env GOPATH)/bin
else
GOBIN=$(shell go env GOBIN)
endif
####################
# -- Golang
####################
.PHONY: golint
golint: golangci-lint
$(GOLANGCI_LINT) run -c .golangci.yaml
.PHONY: golint-fix
golint-fix: golangci-lint
$(GOLANGCI_LINT) fmt -c .golangci.yaml
$(GOLANGCI_LINT) run -c .golangci.yaml --fix
all: manager
# Run tests
.PHONY: test
test: test-clean
@GO111MODULE=on go test -v $(shell go list ./... | grep -v "e2e") -coverprofile coverage.out
.PHONY: test-clean
test-clean: ## Clean tests cache
@go clean -testcache
# Build the proxy binaries.
.PHONY: manager
manager: golint
go build -o $(LOCALBIN)/ ./cmd/...
# Run against the configured Kubernetes cluster in ~/.kube/config
run:
go run .
####################
# -- Docker
####################
KO_PLATFORM ?= linux/$(GOARCH)
KOCACHE ?= /tmp/ko-cache
KO_REGISTRY := ko.local
KO_TAGS ?= "latest"
ifdef VERSION
KO_TAGS := $(KO_TAGS),$(VERSION)
endif
LD_FLAGS := "-X main.Version=$(VERSION) \
-X main.GitCommit=$(GIT_HEAD_COMMIT) \
-X main.GitTag=$(VERSION) \
-X main.GitTreeState=$(GIT_MODIFIED) \
-X main.BuildDate=$(BUILD_DATE) \
-X main.GitRepo=$(GIT_REPO)"
# Docker Image Build
# ------------------
.PHONY: ko-build-loki
ko-build-loki: ko
@echo Building Loki-Proxy $(LOKI_FULL_IMG) - $(KO_TAGS) >&2
@LD_FLAGS=$(LD_FLAGS) KOCACHE=$(KOCACHE) KO_DOCKER_REPO=$(LOKI_FULL_IMG) \
$(KO) build ./cmd/loki-proxy/ --bare --tags=$(KO_TAGS) --push=false --local --platform=$(KO_PLATFORM)
.PHONY: ko-build-cortex
ko-build-cortex: ko
@echo Building Cortex-Proxy $(CORTEX_FULL_IMG) - $(KO_TAGS) >&2
@LD_FLAGS=$(LD_FLAGS) KOCACHE=$(KOCACHE) KO_DOCKER_REPO=$(CORTEX_FULL_IMG) \
$(KO) build ./cmd/cortex-proxy/ --bare --tags=$(KO_TAGS) --push=false --local --platform=$(KO_PLATFORM)
.PHONY: ko-build-all
ko-build-all: ko-build-cortex ko-build-loki
# Docker Image Publish
# ------------------
REGISTRY_PASSWORD ?= dummy
REGISTRY_USERNAME ?= dummy
.PHONY: ko-login
ko-login: ko
@$(KO) login $(REGISTRY) --username $(REGISTRY_USERNAME) --password $(REGISTRY_PASSWORD)
.PHONY: ko-publish-cortex
ko-publish-cortex: ko-login
@echo Publishing Cortex-Proxy $(CORTEX_FULL_IMG) - $(KO_TAGS) >&2
@LD_FLAGS=$(LD_FLAGS) KOCACHE=$(KOCACHE) KO_DOCKER_REPO=$(CORTEX_FULL_IMG) \
$(KO) build ./cmd/cortex-proxy/ --bare --tags=$(KO_TAGS) --push=true
.PHONY: ko-publish-loki
ko-publish-loki: ko-login
@echo Publishing Loki-Proxy $(LOKI_FULL_IMG) - $(KO_TAGS) >&2
@LD_FLAGS=$(LD_FLAGS) KOCACHE=$(KOCACHE) KO_DOCKER_REPO=$(LOKI_FULL_IMG) \
$(KO) build ./cmd/loki-proxy/ --bare --tags=$(KO_TAGS) --push=true
.PHONY: ko-publish-all
ko-publish-all: ko-publish-cortex ko-publish-loki
####################
# -- Helm
####################
# Helm
SRC_ROOT = $(shell git rev-parse --show-toplevel)
helm-docs: helm-doc
$(HELM_DOCS) --chart-search-root ./charts
helm-lint: ct
@$(CT) lint --config .github/configs/ct.yaml --validate-yaml=false --all --debug
helm-schema: helm-plugin-schema
cd charts/cortex-proxy && $(HELM) schema
cd charts/loki-proxy && $(HELM) schema
helm-test: kind ct
@$(KIND) create cluster --wait=60s --name $(KIND_K8S_NAME) --image=kindest/node:$(KIND_K8S_VERSION)
@$(MAKE) e2e-install-distro
@$(MAKE) helm-test-exec
@$(KIND) delete cluster --name $(KIND_K8S_NAME)
helm-test-exec: VERSION :=v0.0.0
helm-test-exec: KO_TAGS :=v0.0.0
helm-test-exec: ct ko-build-all
@$(KIND) load docker-image --name $(KIND_K8S_NAME) $(LOKI_FULL_IMG):$(VERSION)
@$(KIND) load docker-image --name $(KIND_K8S_NAME) $(CORTEX_FULL_IMG):$(VERSION)
@$(CT) install --config $(SRC_ROOT)/.github/configs/ct.yaml --all --debug
docker:
@hash docker 2>/dev/null || {\
echo "You need docker" &&\
exit 1;\
}
####################
# -- Install E2E Tools
####################
e2e: e2e-build e2e-exec e2e-destroy
e2e-build: kind
$(KIND) create cluster --wait=60s --name $(KIND_K8S_NAME) --config ./e2e/kind.yaml --image=kindest/node:$(KIND_K8S_VERSION)
$(MAKE) e2e-install
e2e-exec: ginkgo
$(GINKGO) -r -vv ./e2e
e2e-destroy: kind
$(KIND) delete cluster --name $(KIND_K8S_NAME)
e2e-install: e2e-install-distro e2e-load-image-cortex e2e-install-loki
@$(MAKE) wait-for-helmreleases
e2e-install-distro:
@$(KUBECTL) kustomize e2e/manifests/flux/ | kubectl apply -f -
@$(KUBECTL) kustomize e2e/manifests/distro/ | kubectl apply -f -
e2e-install-cortex: VERSION := 0.0.0
e2e-install-cortex: KO_TAGS := 0.0.0
e2e-install-cortex:
$(HELM) upgrade \
--dependency-update \
--debug \
--install \
--namespace monitoring-system \
--create-namespace \
--set args.logLevel=10 \
--set args.pprof=true \
cortex-proxy \
./charts/cortex-proxy
.PHONY: e2e-load-image-cortex
e2e-load-image-cortex:
kind load docker-image --name $(KIND_K8S_NAME) $(CORTEX_FULL_IMG):$(VERSION)
e2e-install-loki: VERSION := 0.0.0
e2e-install-loki: KO_TAGS := 0.0.0
e2e-install-loki:
$(HELM) upgrade \
--dependency-update \
--debug \
--install \
--namespace monitoring-system \
--create-namespace \
--set args.logLevel=10 \
--set args.pprof=true \
loki-proxy \
./charts/loki-proxy
.PHONY: e2e-load-image-loki
e2e-load-image-loki:
kind load docker-image --name $(KIND_K8S_NAME) $(LOKI_FULL_IMG):$(VERSION)
wait-for-helmreleases:
@ echo "Waiting for all HelmReleases to have observedGeneration >= 0..."
@while [ "$$($(KUBECTL) get helmrelease -A -o jsonpath='{range .items[?(@.status.observedGeneration<0)]}{.metadata.namespace}{" "}{.metadata.name}{"\n"}{end}' | wc -l)" -ne 0 ]; do \
sleep 5; \
done
##@ Build Dependencies
## Location to install dependencies to
LOCALBIN ?= $(shell pwd)/bin
$(LOCALBIN):
mkdir -p $(LOCALBIN)
####################
# -- Helm Plugins
####################
HELM_SCHEMA_VERSION := v2.6.0
HELM_SCHEMA_LOOKUP := losisin/helm-values-schema-json
# Helm 4 requires opting out of verification for plugins installed from Git.
HELM_SCHEMA_INSTALL_FLAGS = $(shell $(HELM) version --short | awk '/^v4\./ {print "--verify=false"}')
.PHONY: helm-plugin-schema
helm-plugin-schema:
@set -e; \
if $(HELM) plugin list | awk '$$1 == "schema" {print $$2}' | grep -Fqx '$(patsubst v%,%,$(HELM_SCHEMA_VERSION))'; then \
exit 0; \
fi; \
if $(HELM) plugin list | awk 'NR > 1 {print $$1}' | grep -Fqx schema; then \
$(HELM) plugin uninstall schema; \
fi; \
$(HELM) plugin install https://github.com/$(HELM_SCHEMA_LOOKUP).git --version $(HELM_SCHEMA_VERSION) $(HELM_SCHEMA_INSTALL_FLAGS)
HELM_DOCS := $(LOCALBIN)/helm-docs
HELM_DOCS_VERSION := v1.14.2
HELM_DOCS_LOOKUP := norwoodj/helm-docs
helm-doc:
@$(call go-install-tool,$(HELM_DOCS),github.com/$(HELM_DOCS_LOOKUP)/cmd/helm-docs@$(HELM_DOCS_VERSION))
####################
# -- Tools
####################
GINKGO := $(LOCALBIN)/ginkgo
GINKGO_VERSION := v2.33.0
GINKGO_LOOKUP := onsi/ginkgo
ginkgo:
@$(call go-install-tool,$(GINKGO),github.com/$(GINKGO_LOOKUP)/v2/ginkgo@$(GINKGO_VERSION))
CT := $(LOCALBIN)/ct
CT_VERSION := v3.14.0
CT_LOOKUP := helm/chart-testing
ct:
@$(call go-install-tool,$(CT),github.com/$(CT_LOOKUP)/v3/ct@$(CT_VERSION))
KIND := $(LOCALBIN)/kind
KIND_VERSION := v0.33.0
KIND_LOOKUP := kubernetes-sigs/kind
kind:
@$(call go-install-tool,$(KIND),sigs.k8s.io/kind@$(KIND_VERSION))
KO := $(LOCALBIN)/ko
KO_VERSION := v0.19.1
KO_LOOKUP := ko-build/ko
ko:
@$(call go-install-tool,$(KO),github.com/google/ko@$(KO_VERSION))
NWA := $(LOCALBIN)/nwa
NWA_VERSION := v0.8.0
NWA_LOOKUP := B1NARY-GR0UP/nwa
nwa:
@$(call go-install-tool,$(NWA),github.com/$(NWA_LOOKUP)@$(NWA_VERSION))
GOLANGCI_LINT := $(LOCALBIN)/golangci-lint
GOLANGCI_LINT_VERSION := v2.14.0
GOLANGCI_LINT_LOOKUP := golangci/golangci-lint
golangci-lint: ## Download golangci-lint locally if necessary.
@$(call go-install-tool,$(GOLANGCI_LINT),github.com/$(GOLANGCI_LINT_LOOKUP)/v2/cmd/golangci-lint@$(GOLANGCI_LINT_VERSION))
.PHONY: helm-doc ginkgo ct kind ko nwa golangci-lint
# go-install-tool will 'go install' any package $2 and install it to $1.
PROJECT_DIR := $(shell dirname $(abspath $(lastword $(MAKEFILE_LIST))))
define go-install-tool
test -x $(1) && go version -m $(1) | awk '$$1 == "mod" {print $$3}' | grep -Fqx '$(lastword $(subst @, ,$(2)))' || { \
set -e ;\
mkdir -p $(LOCALBIN) ;\
GOBIN=$(LOCALBIN) go install $(2) ;\
}
endef