-
Notifications
You must be signed in to change notification settings - Fork 365
/
Makefile
274 lines (215 loc) · 9.03 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
SHELL=/usr/bin/env bash -o pipefail
GO_PKG=github.com/openshift/cluster-monitoring-operator
REPO?=quay.io/openshift/cluster-monitoring-operator
TAG?=$(shell git rev-parse --short HEAD)
VERSION=$(shell cat VERSION | tr -d " \t\n\r")
GOOS?=$(shell go env GOOS)
GOARCH?=$(shell go env GOARCH)
GO111MODULE?=on
GOPROXY?=http://proxy.golang.org
export GO111MODULE
export GOPROXY
# go pakages for unit tests, excluding e2e tests
PKGS=$(shell go list ./... | grep -v /test/e2e)
GOLANG_FILES:=$(shell find . -name \*.go -print)
# NOTE: grep -v %.yaml is needed because "%s-policy.yaml" is used
# in manifest.go and that isn't a valid asset.
# NOTE: Certain paths included in the manifest.go file are not valid
# asset paths and should be excluded from the list of assets. These
# paths are:
# - /etc/
ASSETS=$(shell grep -oh '[^"]*/.*\.yaml' pkg/manifests/manifests.go \
| grep -v '^/etc' \
| grep -v '%.*yaml' | sed 's/^/assets\//')
BIN_DIR ?= $(shell pwd)/tmp/bin
# Docgen related variables
TYPES_TARGET=pkg/manifests/types.go
K8S_VERSION=$(shell echo -n v1. && cat go.mod | grep -w "k8s.io/api" | awk '{ print $$2 }' | cut -d "." -f 2)
PO_VERSION=$(shell cat go.mod | grep "github.com/prometheus-operator/prometheus-operator[^=>]\+$$" | awk '{ print $$2 }' | sort -u)
EMBEDMD_BIN=$(BIN_DIR)/embedmd
JB_BIN=$(BIN_DIR)/jb
GOJSONTOYAML_BIN=$(BIN_DIR)/gojsontoyaml
JSONNET_BIN=$(BIN_DIR)/jsonnet
JSONNETFMT_BIN=$(BIN_DIR)/jsonnetfmt
GOLANGCI_LINT_BIN=$(BIN_DIR)/golangci-lint
GOLANGCI_LINT_VERSION=v1.55.2
PROMTOOL_BIN=$(BIN_DIR)/promtool
DOCGEN_BIN=$(BIN_DIR)/docgen
MISSPELL_BIN=$(BIN_DIR)/misspell
TOOLING=$(EMBEDMD_BIN) $(JB_BIN) $(GOJSONTOYAML_BIN) $(JSONNET_BIN) $(JSONNETFMT_BIN) $(PROMTOOL_BIN) $(DOCGEN_BIN) $(GOLANGCI_LINT_BIN)
MANIFESTS_DIR ?= $(shell pwd)/manifests
JSON_MANIFESTS_DIR ?= $(shell pwd)/tmp/json-manifests/manifests
MANIFESTS ?= $(wildcard $(MANIFESTS_DIR)/*.yaml)
JSON_MANIFESTS ?= $(patsubst $(MANIFESTS_DIR)%,$(JSON_MANIFESTS_DIR)%,$(patsubst %.yaml,%.json,$(MANIFESTS)))
JSONNET_SRC=$(shell find ./jsonnet -type f -not -path "./jsonnet/vendor*")
JSONNET_VENDOR=jsonnet/vendor
GO_BUILD_RECIPE=GOOS=$(GOOS) GOARCH=$(GOARCH) CGO_ENABLED=0 go build --ldflags="-s -X $(GO_PKG)/pkg/operator.Version=$(VERSION)"
MARKDOWN_DOCS=$(shell find . -type f -name '*.md' ! -path '*/vendor/*' ! -path './git/*' \
! -name 'data-collection.md' ! -name 'sample-metrics.md' | sort)
.PHONY: all
all: clean format generate build test
.PHONY: clean
clean:
rm -rf $(JSONNET_VENDOR) operator .hack-operator-image tmp/
############
# Building #
############
# run-local builds and runs operator out of cluster.
# use make run-local SWITCH_TO_CMO=false to not switch the login to CMO
# service-account. E.g. when logged in kube:admin and what to run operator
# as kube:admin
.PHONY: run-local
run-local: build
@if $${SWITCH_TO_CMO:-true} ; then \
PATH="$(PATH):$(BIN_DIR)" \
KUBECONFIG=$(KUBECONFIG) ./hack/local-cmo.sh ;\
else \
PATH="$(PATH):$(BIN_DIR)" \
KUBECONFIG=$(KUBECONFIG) ./hack/local-cmo.sh --no-cmo-login ;\
fi
.PHONY: build
build: operator
.PHONY: operator
operator: $(GOLANG_FILES)
$(GO_BUILD_RECIPE) -o operator $(GO_PKG)/cmd/operator
# We need this Make target so that we can build the operator depending
# only on what is checked into the repo, without calling to the internet.
.PHONY: operator-no-deps
operator-no-deps:
$(GO_BUILD_RECIPE) -o operator $(GO_PKG)/cmd/operator
.PHONY: image
image: .hack-operator-image
.hack-operator-image: Dockerfile operator
# Create empty target file, for the sole purpose of recording when this target
# was last executed via the last-modification timestamp on the file. See
# https://www.gnu.org/software/make/manual/make.html#Empty-Targets
docker build -t $(REPO):$(TAG) .
touch $@
##############
# Generating #
##############
.PHONY: vendor
vendor:
go mod tidy
go mod vendor
go mod verify
.PHONY: update-go-deps
update-go-deps:
for m in $$(go list -mod=readonly -m -f '{{ if and (not .Indirect) (not .Main)}}{{.Path}}{{end}}' all); do \
go get $$m; \
done
@echo "Don't forget to run 'make vendor'"
.PHONY: update
update: $(JB_BIN)
cd jsonnet && $(JB_BIN) update $(COMPONENTS)
.PHONY: generate
generate: build-jsonnet docs
.PHONY: verify
verify: check-assets check-rules check-runbooks
# TODO(paulfantom): generate-in-docker can be completely removed after OpenShift 4.7 is EOL
.PHONY: generate-in-docker
generate-in-docker:
echo -e "FROM golang:1.14 \n RUN apt update && apt install python-yaml jq -y \n RUN mkdir /.cache && chown $(shell id -u):$(shell id -g) /.cache" | docker build -t cmo-tooling -
docker run -it --user $(shell id -u):$(shell id -g) \
-w /go/src/github.com/openshift/cluster-monitoring-operator \
-v ${PWD}:/go/src/github.com/openshift/cluster-monitoring-operator \
cmo-tooling make generate
$(JSONNET_VENDOR): $(JB_BIN) jsonnet/jsonnetfile.json jsonnet/jsonnetfile.lock.json
cd jsonnet && $(JB_BIN) install
$(ASSETS): build-jsonnet
# Check if files were properly generated
[ -f "$@" ] || exit 1
.PHONY: build-jsonnet
build-jsonnet: $(JSONNET_BIN) $(GOJSONTOYAML_BIN) $(JSONNET_SRC) $(JSONNET_VENDOR) json-manifests json-crds
./hack/build-jsonnet.sh
$(JSON_MANIFESTS): $(MANIFESTS)
cat $(MANIFESTS_DIR)/$(patsubst %.json,%.yaml,$(@F)) | $(GOJSONTOYAML_BIN) -yamltojson > $@
.PHONY: json-manifests
json-manifests: $(JSON_MANIFESTS_DIR) $(JSON_MANIFESTS)
.PHONY: json-crds
json-crds: jsonnet/crds/alertingrules-custom-resource-definition.json jsonnet/crds/alertrelabelconfigs-custom-resource-definition.json
jsonnet/crds/alertingrules-custom-resource-definition.json: vendor/github.com/openshift/api/monitoring/v1/zz_generated.crd-manifests/0000_50_monitoring_01_alertingrules.crd.yaml
$(GOJSONTOYAML_BIN) -yamltojson < $< > $@
jsonnet/crds/alertrelabelconfigs-custom-resource-definition.json: vendor/github.com/openshift/api/monitoring/v1/zz_generated.crd-manifests/0000_50_monitoring_02_alertrelabelconfigs.crd.yaml
$(GOJSONTOYAML_BIN) -yamltojson < $< > $@
.PHONY: versions
versions:
@cd ./hack/go && go mod tidy && go mod download && go run -mod=mod generate_versions.go
.PHONY: check-versions
check-versions: VERSION_FILE=jsonnet/versions.yaml
check-versions:
export VERSION_FILE=$(VERSION_FILE) && $(MAKE) versions && git diff --exit-code -- ${VERSION_FILE}
.PHONY: docs
docs: $(EMBEDMD_BIN) $(DOCGEN_BIN) Documentation/telemetry/telemeter_query
$(EMBEDMD_BIN) -w `find Documentation -name "*.md"`
$(DOCGEN_BIN) api markdown $(K8S_VERSION) $(PO_VERSION) $(TYPES_TARGET) > Documentation/api.md
$(DOCGEN_BIN) api asciidocs $(K8S_VERSION) $(PO_VERSION) $(TYPES_TARGET)
$(DOCGEN_BIN) resources markdown > Documentation/resources.md
$(DOCGEN_BIN) resources asciidocs > Documentation/resources.adoc
Documentation/telemetry/telemeter_query: manifests/0000_50_cluster-monitoring-operator_04-config.yaml hack/telemeter_query.go
go generate ./hack/telemeter_query.go > Documentation/telemetry/telemeter_query
##############
# Formatting #
##############
.PHONY: format
format: go-fmt golangci-lint shellcheck jsonnet-fmt misspell
.PHONY: go-fmt
go-fmt:
go fmt ./...
.PHONY: golangci-lint
golangci-lint: $(GOLANGCI_LINT_BIN)
$(GOLANGCI_LINT_BIN) run --verbose --print-resources-usage
.PHONY: golangci-lint-fix
golangci-lint-fix: $(GOLANGCI_LINT_BIN)
$(GOLANGCI_LINT_BIN) run --verbose --print-resources-usage --fix
.PHONY:
misspell:
$(MISSPELL_BIN) -error $(MARKDOWN_DOCS)
.PHONY:
misspell-fix:
$(MISSPELL_BIN) -w $(MARKDOWN_DOCS)
.PHONY: jsonnet-fmt
jsonnet-fmt: $(JSONNETFMT_BIN)
find jsonnet/ -name 'vendor' -prune -o -name '*.libsonnet' -print -o -name '*.jsonnet' -print | xargs -n 1 -- $(JSONNETFMT_BIN) -i
.PHONY: shellcheck
shellcheck:
hack/shellcheck.sh
tmp/rules.yaml: $(GOJSONTOYAML_BIN) $(ASSETS)
mkdir -p tmp/rules
hack/find-rules.sh | $(GOJSONTOYAML_BIN) > tmp/rules.yaml
.PHONY: check-rules
check-rules: $(PROMTOOL_BIN) tmp/rules.yaml
rm -f tmp/"$@".out
@$(PROMTOOL_BIN) check rules tmp/rules.yaml | tee "tmp/[email protected]"
.PHONY: test-rules
test-rules: check-rules
hack/test-rules.sh | tee "tmp/[email protected]"
.PHONY: check-assets
check-assets:
hack/check-assets.sh
.PHONY: check-runbooks
check-runbooks:
# Get runbook urls from the alerts annotations and test if a link is broken
# with wget. It also make sure that the command succeed when there are no urls.
@grep -rho 'runbook_url.*' assets || true | cut -f2- -d: | wget --spider -nv -i -
###########
# Testing #
###########
.PHONY: test
test: test-unit test-rules test-e2e
.PHONY: test-unit
test-unit:
go test -race -short $(PKGS) -count=1
.PHONY: test-e2e
test-e2e: KUBECONFIG?=$(HOME)/.kube/config
test-e2e:
go test -v -timeout=150m ./test/e2e/ --kubeconfig $(KUBECONFIG)
$(BIN_DIR):
mkdir -p $(BIN_DIR)
$(JSON_MANIFESTS_DIR):
mkdir -p $(JSON_MANIFESTS_DIR)
$(TOOLING): $(BIN_DIR)
@echo Installing tools from hack/tools/tools.go
@cd hack/tools && go list -mod=mod -tags tools -e -f '{{ range .Imports }}{{ printf "%s\n" .}}{{end}}' ./ | xargs -tI % go build -mod=mod -o $(BIN_DIR) %
@GOBIN=$(BIN_DIR) go install $(GO_PKG)/hack/docgen
curl -sfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh| sh -s -- -b $(BIN_DIR) $(GOLANGCI_LINT_VERSION)