Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ci: Add CI #14

Merged
merged 3 commits into from
Jun 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 83 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
---
name: CI
on:
push:
branches:
- main
pull_request:
branches:
- main
env:
golang-version: '1.17'
jobs:
generate:
runs-on: ubuntu-latest
name: Generate yaml
steps:
- uses: actions/checkout@v2
with:
persist-credentials: false
- uses: actions/setup-go@v2
with:
go-version: ${{ env.golang-version }}
- run: make --always-make generate && git diff --exit-code

jsonnet-lint:
runs-on: ubuntu-latest
name: Jsonnet linter
steps:
- uses: actions/checkout@v2
with:
persist-credentials: false
- uses: actions/setup-go@v2
with:
go-version: ${{ env.golang-version }}
- run: make --always-make jsonnet-lint

dashboards-lint:
runs-on: ubuntu-latest
name: Grafana dashboard linter
steps:
- uses: actions/checkout@v2
with:
persist-credentials: false
- uses: actions/setup-go@v2
with:
go-version: ${{ env.golang-version }}
- run: make --always-make dashboards-lint

alerts-lint:
runs-on: ubuntu-latest
name: Alerts linter
steps:
- uses: actions/checkout@v2
with:
persist-credentials: false
- uses: actions/setup-go@v2
with:
go-version: ${{ env.golang-version }}
- run: make --always-make alerts-lint

fmt:
runs-on: ubuntu-latest
name: Jsonnet formatter
steps:
- uses: actions/checkout@v2
with:
persist-credentials: false
- uses: actions/setup-go@v2
with:
go-version: ${{ env.golang-version }}
- run: make --always-make fmt && git diff --exit-code

unit-tests:
runs-on: ubuntu-latest
name: Unit tests
steps:
- uses: actions/checkout@v2
with:
persist-credentials: false
- uses: actions/setup-go@v2
with:
go-version: ${{ env.golang-version }}
- run: make --always-make test
6 changes: 4 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
*.yaml
dashboards-out
prometheus_alerts.yaml
prometheus_rules.yaml
dashboards_out
vendor
jsonnetfile.lock.json
tmp
4 changes: 4 additions & 0 deletions .lint
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
exclusions:
template-job-rule:
panel-job-instance-rule:
target-rate-interval-rule:
100 changes: 70 additions & 30 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,39 +1,79 @@
JSONNET_ARGS := -n 2 --max-blank-lines 2 --string-style s --comment-style s
ifneq (,$(shell which jsonnetfmt))
JSONNET_FMT_CMD := jsonnetfmt
else
JSONNET_FMT_CMD := jsonnet
JSONNET_FMT_ARGS := fmt $(JSONNET_ARGS)
endif
JSONNET_FMT := $(JSONNET_FMT_CMD) $(JSONNET_FMT_ARGS)

all: fmt prometheus-alerts.yaml prometheus-rules.yaml dashboards-out lint test

fmt:
find . -name 'vendor' -prune -o -name '*.libsonnet' -print -o -name '*.jsonnet' -print | \
xargs -n 1 -- $(JSONNET_FMT) -i
BIN_DIR ?= $(shell pwd)/tmp/bin

JSONNET_VENDOR=vendor
GRAFANA_DASHBOARD_LINTER_BIN=$(BIN_DIR)/dashboard-linter
JB_BIN=$(BIN_DIR)/jb
JSONNET_BIN=$(BIN_DIR)/jsonnet
JSONNETLINT_BIN=$(BIN_DIR)/jsonnet-lint
JSONNETFMT_BIN=$(BIN_DIR)/jsonnetfmt
PROMTOOL_BIN=$(BIN_DIR)/promtool
TOOLING=$(JB_BIN) $(JSONNETLINT_BIN) $(JSONNET_BIN) $(JSONNETFMT_BIN) $(PROMTOOL_BIN) $(GRAFANA_DASHBOARD_LINTER_BIN)
JSONNETFMT_ARGS=-n 2 --max-blank-lines 2 --string-style s --comment-style s
SRC_DIR ?=dashboards
OUT_DIR ?=dashboards_out

prometheus-alerts.yaml: mixin.libsonnet alerts.jsonnet alerts/*.libsonnet
jsonnet -J vendor -S alerts.jsonnet > $@
.PHONY: all
all: fmt generate lint test

prometheus-rules.yaml: mixin.libsonnet rules.jsonnet rules/*.libsonnet
jsonnet -J vendor -S rules.jsonnet > $@
.PHONY: generate
generate: prometheus_alerts.yaml $(OUT_DIR) # prometheus_rules.yaml

dashboards-out: mixin.libsonnet dashboards.jsonnet dashboards/*.libsonnet
@mkdir -p dashboards-out
jsonnet -J vendor -m dashboards-out dashboards.jsonnet
$(JSONNET_VENDOR): $(JB_BIN) jsonnetfile.json
$(JB_BIN) install

lint: prometheus-alerts.yaml prometheus-rules.yaml
.PHONY: fmt
fmt: $(JSONNETFMT_BIN)
find . -name 'vendor' -prune -o -name '*.libsonnet' -print -o -name '*.jsonnet' -print | \
while read f; do \
$(JSONNET_FMT) "$$f" | diff -u "$$f" -; \
done
xargs -n 1 -- $(JSONNETFMT_BIN) $(JSONNETFMT_ARGS) -i

prometheus_alerts.yaml: $(JSONNET_BIN) mixin.libsonnet lib/alerts.jsonnet alerts/*.libsonnet
@$(JSONNET_BIN) -J vendor -S lib/alerts.jsonnet > $@

prometheus_rules.yaml: $(JSONNET_BIN) mixin.libsonnet lib/rules.jsonnet rules/*.libsonnet
@$(JSONNET_BIN) -J vendor -S lib/rules.jsonnet > $@

$(OUT_DIR): $(JSONNET_BIN) $(JSONNET_VENDOR) mixin.libsonnet lib/dashboards.jsonnet $(SRC_DIR)/*.libsonnet
@mkdir -p $(OUT_DIR)
@$(JSONNET_BIN) -J vendor -m $(OUT_DIR) lib/dashboards.jsonnet

.PHONY: lint
lint: jsonnet-lint alerts-lint dashboards-lint

.PHONY: jsonnet-lint
jsonnet-lint: $(JSONNETLINT_BIN) $(JSONNET_VENDOR)
@find . -name 'vendor' -prune -o -name '*.libsonnet' -print -o -name '*.jsonnet' -print | \
xargs -n 1 -- $(JSONNETLINT_BIN) -J vendor

promtool check rules prometheus-rules.yaml
promtool check rules prometheus-alerts.yaml

.PHONY: alerts-lint
alerts-lint: $(PROMTOOL_BIN) prometheus_alerts.yaml # prometheus_rules.yaml
# @$(PROMTOOL_BIN) check rules prometheus_rules.yaml
@$(PROMTOOL_BIN) check rules prometheus_alerts.yaml

$(OUT_DIR)/.lint: $(OUT_DIR)
@cp .lint $@

.PHONY: dashboards-lint
dashboards-lint: $(GRAFANA_DASHBOARD_LINTER_BIN) $(OUT_DIR)/.lint
# Replace rates var with $$__rate_interval to make dashboard-linter happy.
@sed -i -e 's/1w/$$__rate_interval/g' $(OUT_DIR)/*.json
@sed -i -e 's/\[$$error_codes\]\.\*/500/g' $(OUT_DIR)/*.json
@find $(OUT_DIR) -name '*.json' -print0 | xargs -n 1 -0 $(GRAFANA_DASHBOARD_LINTER_BIN) lint --strict


.PHONY: clean
clean:
rm -rf dashboards-out prometheus-alerts.yaml prometheus-rules.yaml
# Remove all files and directories ignored by git.
git clean -Xfd .

.PHONY: test
test: $(PROMTOOL_BIN) prometheus_alerts.yaml # prometheus_rules.yaml
@$(PROMTOOL_BIN) test rules tests.yaml

$(BIN_DIR):
mkdir -p $(BIN_DIR)

$(TOOLING): $(BIN_DIR)
@echo Installing tools from hack/tools.go
@cd scripts && go list -mod=mod -tags tools -f '{{ range .Imports }}{{ printf "%s\n" .}}{{end}}' ./ | xargs -tI % go build -mod=mod -o $(BIN_DIR) %

test: prometheus-alerts.yaml prometheus-rules.yaml
promtool test rules tests.yaml
Loading