Skip to content

Commit 88c974b

Browse files
nthomas91Nivin Thomas
andauthored
INFRA 43544: Add alert linting using mimirtool (#209)
* INFRA-43544: Add alert linting using mimirtool * Update linting logic * Update linting syntax * Fix syntax errors * Fix syntax error * Move mimirtool under k8s * Update the mimirtool targets * Update the output messgaes --------- Co-authored-by: Nivin Thomas <nthomas@mintel.com>
1 parent ca0be85 commit 88c974b

File tree

3 files changed

+52
-1
lines changed

3 files changed

+52
-1
lines changed

modules/k8s/Makefile.mimirtool

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
## Mimirtool helpers
2+
MANIFEST_DIRS:=$(shell find rendered/ -type d -name manifests 2>/dev/null)
3+
TEMP_ALERTS_DIR := /tmp/mimirtool-alerts
4+
5+
.PHONY: k8s/mimirtool/check-env k8s/mimirtool/rules/lint k8s/mimirtool/clean
6+
7+
# Check mimirtool environment is suitable
8+
k8s/mimirtool/check-env:
9+
@if [ ! -d ./rendered ]; then \
10+
echo "rendered manifests directory does not exist, run: make tanka/generate" ;\
11+
exit 1 ;\
12+
fi
13+
14+
# Clean temporary files
15+
k8s/mimirtool/clean:
16+
@rm -rf $(TEMP_ALERTS_DIR)
17+
18+
## Validate PrometheusRule alerts
19+
k8s/mimirtool/rules/lint: satoshi/check-deps k8s/mimirtool/check-env k8s/mimirtool/clean
20+
@mkdir -p $(TEMP_ALERTS_DIR)
21+
@errs=0; \
22+
for dir in $(MANIFEST_DIRS); do \
23+
rulefiles=$$(find $${dir} -type f -name '*PrometheusRule*.yaml'); \
24+
if [ ! -z "$${rulefiles}" ]; then \
25+
echo "Validating PrometheusRules in $${dir}"; \
26+
for rulefile in $${rulefiles}; do \
27+
filename=$$(basename $${rulefile}); \
28+
echo " Checking $${rulefile}..."; \
29+
if ! yq eval '.spec' $${rulefile} > $(TEMP_ALERTS_DIR)/$${filename}.spec.yaml; then \
30+
echo "Failed to extract .spec from $${rulefile}"; \
31+
errs=$$(( $$errs + 1 )); \
32+
continue; \
33+
fi; \
34+
if ! mimirtool rules lint $(TEMP_ALERTS_DIR)/$${filename}.spec.yaml; then \
35+
echo "Validation failed for $${rulefile}"; \
36+
errs=$$(( $$errs + 1 )); \
37+
else \
38+
echo "Validation passed for $${rulefile}"; \
39+
fi; \
40+
done; \
41+
fi; \
42+
done; \
43+
if [ "$${errs:-0}" -gt 0 ]; then \
44+
echo "Validation failed with $${errs} error(s)"; \
45+
exit 1; \
46+
else \
47+
echo "All checks passed"; \
48+
fi

modules/satoshi/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ S_YLW := $(shell tput setaf 3 2> /dev/null || true)
44
S_RST := $(shell tput sgr0 2> /dev/null || true)
55
S_BLD := $(shell tput bold 2> /dev/null || true)
66

7-
REQUIRED_SATOSHI_BINS := conftest helm jb jsonnet kubectl kustomize opa pluto tk yq
7+
REQUIRED_SATOSHI_BINS := conftest helm jb jsonnet kubectl kustomize opa pluto tk yq mimirtool
88

99
.PHONY: satoshi/check-deps satoshi/check-asdf-dep satoshi/update-makefile satoshi/update-tools
1010

modules/satoshi/k8s-tool-versions

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ kustomize 5.0.1
4343
#renovate: depName=kyverno/kyverno
4444
kyverno-cli 1.13.4
4545

46+
#asdf:plugin add mimirtool https://github.com/asdf-community/asdf-mimirtool.git
47+
mimirtool 2.17.2
48+
4649
#asdf:plugin add opa
4750
#renovate: depName=open-policy-agent/opa
4851
opa 0.68.0

0 commit comments

Comments
 (0)