@intentius/chant-lexicon-k8s 0.105.1 → 0.106.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/crd/crd-sources.d.ts.map +1 -1
- package/dist/generated/index.d.ts +24 -0
- package/dist/generated/index.d.ts.map +1 -1
- package/dist/import/embedded.d.ts +7 -1
- package/dist/import/embedded.d.ts.map +1 -1
- package/dist/integrity.json +4 -4
- package/dist/manifest.json +1 -1
- package/dist/meta.json +153 -0
- package/dist/okf/index.md +6 -0
- package/dist/okf/types/GrafanaAlertRuleGroup.md +14 -0
- package/dist/okf/types/GrafanaContactPoint.md +14 -0
- package/dist/okf/types/GrafanaMuteTiming.md +14 -0
- package/dist/okf/types/GrafanaNotificationPolicy.md +14 -0
- package/dist/okf/types/GrafanaNotificationPolicyRoute.md +14 -0
- package/dist/okf/types/GrafanaNotificationTemplate.md +14 -0
- package/dist/types/index.d.ts +78 -0
- package/package.json +3 -3
- package/src/crd/crd-sources.ts +11 -2
- package/src/generated/index.d.ts +78 -0
- package/src/generated/index.ts +24 -0
- package/src/generated/lexicon-k8s.json +153 -0
- package/src/generated/operations.json +96 -0
- package/src/import/embedded.ts +33 -1
- package/src/import/operator-alerting-roundtrip.test.ts +135 -0
- package/src/import/testdata/embedded/agent-observability.yaml +14 -14
package/src/import/embedded.ts
CHANGED
|
@@ -9,7 +9,13 @@
|
|
|
9
9
|
* JSON);
|
|
10
10
|
* - a `monitoring.coreos.com` PrometheusRule's `spec.groups`, as the rule file
|
|
11
11
|
* `{ groups }` those groups would make;
|
|
12
|
-
* - a Grafana Operator `GrafanaDashboard`'s `spec.json`, as text (#3015)
|
|
12
|
+
* - a Grafana Operator `GrafanaDashboard`'s `spec.json`, as text (#3015);
|
|
13
|
+
* - the alerting content of the Grafana Operator's `GrafanaAlertRuleGroup`
|
|
14
|
+
* (`spec.rules`), `GrafanaContactPoint` (`spec.receivers`),
|
|
15
|
+
* `GrafanaNotificationPolicy` (`spec.route`), `GrafanaMuteTiming`
|
|
16
|
+
* (`spec.time_intervals`) and `GrafanaNotificationTemplate`
|
|
17
|
+
* (`spec.template`), each with the whole `spec` as the document so the
|
|
18
|
+
* owner can read the fields beside it (#3538).
|
|
13
19
|
*
|
|
14
20
|
* Which lexicon imports the content is decided by core at run time, from the
|
|
15
21
|
* lexicons that register an `embeddedImporters()`; this module names none of
|
|
@@ -27,6 +33,15 @@ function isObject(v: unknown): v is Record<string, unknown> {
|
|
|
27
33
|
/** The `grafana_dashboard` label the Grafana dashboard sidecar looks for (kube-prometheus-stack, the Grafana Helm chart). */
|
|
28
34
|
export const GRAFANA_DASHBOARD_LABEL = "grafana_dashboard";
|
|
29
35
|
|
|
36
|
+
/** The Grafana Operator alerting kinds, and the `spec` field holding what the grafana lexicon imports. */
|
|
37
|
+
const GRAFANA_ALERTING_FIELDS: Record<string, { field: string; what: string }> = {
|
|
38
|
+
"K8s::Grafana::GrafanaAlertRuleGroup": { field: "rules", what: "Grafana alert rules" },
|
|
39
|
+
"K8s::Grafana::GrafanaContactPoint": { field: "receivers", what: "Grafana contact point receivers" },
|
|
40
|
+
"K8s::Grafana::GrafanaNotificationPolicy": { field: "route", what: "a Grafana notification policy" },
|
|
41
|
+
"K8s::Grafana::GrafanaMuteTiming": { field: "time_intervals", what: "Grafana mute timing intervals" },
|
|
42
|
+
"K8s::Grafana::GrafanaNotificationTemplate": { field: "template", what: "a Grafana notification template" },
|
|
43
|
+
};
|
|
44
|
+
|
|
30
45
|
function looksLikeRuleGroups(groups: unknown): boolean {
|
|
31
46
|
return Array.isArray(groups) && groups.length > 0 && groups.every((g) => isObject(g) && typeof g.name === "string" && Array.isArray(g.rules));
|
|
32
47
|
}
|
|
@@ -116,6 +131,23 @@ export function delegateEmbedded(
|
|
|
116
131
|
return;
|
|
117
132
|
}
|
|
118
133
|
|
|
134
|
+
const alerting = GRAFANA_ALERTING_FIELDS[type];
|
|
135
|
+
if (alerting && isObject(properties.spec) && properties.spec[alerting.field] !== undefined) {
|
|
136
|
+
const spec = properties.spec;
|
|
137
|
+
const ref = embedded.resolve({
|
|
138
|
+
host: "k8s",
|
|
139
|
+
hostType: type,
|
|
140
|
+
location: `${kind} ${name} spec.${alerting.field}`,
|
|
141
|
+
directory: name,
|
|
142
|
+
document: spec,
|
|
143
|
+
select: alerting.field,
|
|
144
|
+
labels,
|
|
145
|
+
expectedOwner: { lexicon: "grafana", what: alerting.what },
|
|
146
|
+
});
|
|
147
|
+
if (ref) properties.spec = { ...spec, [alerting.field]: ref };
|
|
148
|
+
return;
|
|
149
|
+
}
|
|
150
|
+
|
|
119
151
|
if (type === "K8s::Monitoring::PrometheusRule" && isObject(properties.spec) && Array.isArray(properties.spec.groups)) {
|
|
120
152
|
const groups = properties.spec.groups;
|
|
121
153
|
const ref = embedded.resolve({
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Grafana Operator alerting through `chant import` (#3538): the output of
|
|
3
|
+
* `GrafanaOperatorResources` (#3156) for a set of alerting declarations,
|
|
4
|
+
* imported as grafana declarations, builds back to the same resources, and
|
|
5
|
+
* the declarations build the same alerting provisioning file.
|
|
6
|
+
*
|
|
7
|
+
* The manifest is written by `GrafanaOperatorResources` itself, so the test
|
|
8
|
+
* holds the writer and the reader to each other. A rule group's folder is
|
|
9
|
+
* the one thing that changes on the way: the resource names its
|
|
10
|
+
* GrafanaFolder and the declaration wants the folder's title, so the
|
|
11
|
+
* provisioning comparison puts the folder name in the source's `folder`.
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
import { describe, expect, test } from "vitest";
|
|
15
|
+
import { load, loadAll } from "js-yaml";
|
|
16
|
+
import { build } from "@intentius/chant/build";
|
|
17
|
+
import { expandComposite } from "@intentius/chant/composite";
|
|
18
|
+
import type { SerializerResult } from "@intentius/chant/serializer";
|
|
19
|
+
import {
|
|
20
|
+
AlertRule,
|
|
21
|
+
AlertRuleGroup,
|
|
22
|
+
ALERTING_FILE,
|
|
23
|
+
ContactPoint,
|
|
24
|
+
Datasource,
|
|
25
|
+
MuteTiming,
|
|
26
|
+
NotificationPolicy,
|
|
27
|
+
NotificationTemplate,
|
|
28
|
+
PromQuery,
|
|
29
|
+
grafanaSerializer,
|
|
30
|
+
} from "@intentius/chant-lexicon-grafana";
|
|
31
|
+
import { buildGrafana } from "@intentius/chant-lexicon-grafana/build";
|
|
32
|
+
import { normalizeAlerting } from "@intentius/chant-lexicon-grafana/import/normalize-alerting";
|
|
33
|
+
import { GrafanaOperatorResources } from "@intentius/chant-lexicon-grafana/k8s";
|
|
34
|
+
import { k8sSerializer } from "../serializer";
|
|
35
|
+
import { importManifest, removeDir } from "./testdata/embedded/fixtures";
|
|
36
|
+
|
|
37
|
+
type Json = Record<string, unknown>;
|
|
38
|
+
|
|
39
|
+
const prometheus = new Datasource({ name: "Prometheus", type: "prometheus", url: "http://prometheus:9090" });
|
|
40
|
+
const oncall = new ContactPoint({
|
|
41
|
+
name: "oncall",
|
|
42
|
+
receivers: [
|
|
43
|
+
{ uid: "oncall-slack", type: "slack", settings: { url: "${SLACK_URL}", title: "chant" } },
|
|
44
|
+
{ uid: "oncall-mail", type: "email", settings: { addresses: "a@example.com" }, disableResolveMessage: true },
|
|
45
|
+
],
|
|
46
|
+
});
|
|
47
|
+
const weekends = new MuteTiming({ name: "weekends", time_intervals: [{ weekdays: ["saturday", "sunday"] }] });
|
|
48
|
+
const template = new NotificationTemplate({ name: "t", template: '{{ define "t" }}x{{ end }}' });
|
|
49
|
+
const policy = new NotificationPolicy({
|
|
50
|
+
receiver: oncall,
|
|
51
|
+
group_by: ["alertname"],
|
|
52
|
+
routes: [{ receiver: "tickets", matchers: ['severity="ticket"', 'team=~"a|b"'], mute_time_intervals: [weekends], routes: [{ matchers: ['env!="dev"'] }] }],
|
|
53
|
+
});
|
|
54
|
+
const errors = new AlertRule({
|
|
55
|
+
title: "High errors",
|
|
56
|
+
data: [new PromQuery({ datasource: prometheus, expr: "up" })],
|
|
57
|
+
dashboardUid: "dash1",
|
|
58
|
+
panelId: 2,
|
|
59
|
+
for: "5m",
|
|
60
|
+
labels: { severity: "page" },
|
|
61
|
+
});
|
|
62
|
+
const fewSeries = new AlertRule({
|
|
63
|
+
title: "Few series",
|
|
64
|
+
data: [new PromQuery({ datasource: prometheus, expr: "count(up)" })],
|
|
65
|
+
missing_series_evals_to_resolve: 3,
|
|
66
|
+
notification_settings: { receiver: oncall, group_by: ["alertname"] },
|
|
67
|
+
});
|
|
68
|
+
const group = new AlertRuleGroup({ name: "errors", folder: "Alerts", interval: "30s", rules: [errors, fewSeries] });
|
|
69
|
+
const alerting = [oncall, weekends, template, policy, group];
|
|
70
|
+
const entities = [prometheus, ...alerting];
|
|
71
|
+
|
|
72
|
+
const KINDS = ["GrafanaAlertRuleGroup", "GrafanaContactPoint", "GrafanaNotificationPolicy", "GrafanaMuteTiming", "GrafanaNotificationTemplate"];
|
|
73
|
+
|
|
74
|
+
function primary(out: string | SerializerResult | undefined): string {
|
|
75
|
+
if (out === undefined) return "";
|
|
76
|
+
return typeof out === "string" ? out : out.primary;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
function manifest(): string {
|
|
80
|
+
const instance = GrafanaOperatorResources({
|
|
81
|
+
entities: entities as never,
|
|
82
|
+
instanceSelector: { matchLabels: { dashboards: "grafana" } },
|
|
83
|
+
secretName: "grafana-secrets",
|
|
84
|
+
namespace: "monitoring",
|
|
85
|
+
});
|
|
86
|
+
return primary(k8sSerializer.serialize(expandComposite("grafana", instance)));
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
function find(docs: Json[], kind: string): Json {
|
|
90
|
+
const doc = docs.find((d) => d.kind === kind);
|
|
91
|
+
if (!doc) throw new Error(`no ${kind}`);
|
|
92
|
+
return doc;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
describe("GrafanaOperatorResources -> chant import -> chant build", () => {
|
|
96
|
+
test("the five alerting kinds are imported as grafana declarations and build back to the same resources", async () => {
|
|
97
|
+
const yaml = manifest();
|
|
98
|
+
const input = (loadAll(yaml) as Json[]).filter((d) => d);
|
|
99
|
+
expect(KINDS.every((k) => input.some((d) => d.kind === k))).toBe(true);
|
|
100
|
+
|
|
101
|
+
const imported = await importManifest(yaml);
|
|
102
|
+
try {
|
|
103
|
+
expect(imported.result.error).toBeUndefined();
|
|
104
|
+
expect(imported.result.success).toBe(true);
|
|
105
|
+
|
|
106
|
+
// Each field the grafana lexicon reads is a call over its declarations.
|
|
107
|
+
const main = imported.files["other.ts"];
|
|
108
|
+
expect(main).toContain('from "@intentius/chant-lexicon-grafana/k8s"');
|
|
109
|
+
for (const fn of ["operatorRules", "operatorPolicy", "operatorTimeIntervals", "operatorTemplate"]) expect(main).toContain(`${fn}(`);
|
|
110
|
+
// The contact point reads a Secret, so its receivers come from a module naming it.
|
|
111
|
+
const receivers = imported.files["grafana-contact-point-oncall/receivers.ts"];
|
|
112
|
+
expect(receivers).toContain("operatorReceivers(");
|
|
113
|
+
expect(receivers).toContain('"grafana-secrets"');
|
|
114
|
+
expect(imported.files["grafana-contact-point-oncall/notifications.ts"]).toContain("${SLACK_URL}");
|
|
115
|
+
expect(imported.files["grafana-rule-group-alerts-errors/alert-rules.ts"]).toContain("new AlertRuleGroup(");
|
|
116
|
+
expect(imported.files["grafana-notification-policy/notifications.ts"]).toContain("new NotificationPolicy(");
|
|
117
|
+
expect(imported.result.warnings.join("\n")).toContain('rule group "errors"');
|
|
118
|
+
|
|
119
|
+
const result = await build(imported.srcDir, [k8sSerializer, grafanaSerializer]);
|
|
120
|
+
expect(result.errors).toEqual([]);
|
|
121
|
+
|
|
122
|
+
const output = (loadAll(primary(result.outputs.get("k8s"))) as Json[]).filter((d) => d);
|
|
123
|
+
for (const kind of KINDS) expect(find(output, kind)).toEqual(find(input, kind));
|
|
124
|
+
|
|
125
|
+
// The declarations build the alerting file the source declares.
|
|
126
|
+
const expected = buildGrafana(entities as never).alerting!;
|
|
127
|
+
const groups = (expected.groups ?? []).map((g) => ({ ...g, folder: "grafana-folder-alerts" }));
|
|
128
|
+
const text = (result.outputs.get("grafana") as SerializerResult | undefined)?.files?.[ALERTING_FILE];
|
|
129
|
+
expect(text).toBeDefined();
|
|
130
|
+
expect(normalizeAlerting(load(text!) as Json)).toEqual(normalizeAlerting({ ...expected, groups } as unknown as Json));
|
|
131
|
+
} finally {
|
|
132
|
+
removeDir(imported.dir);
|
|
133
|
+
}
|
|
134
|
+
});
|
|
135
|
+
});
|
|
@@ -2645,51 +2645,51 @@ data:
|
|
|
2645
2645
|
rules:
|
|
2646
2646
|
- record: slo:sli_error:ratio_rate5m
|
|
2647
2647
|
expr: |-
|
|
2648
|
-
(sum(rate(traces_span_metrics_calls_total{service_name="support-agent",span_name="invoke_agent support",status_code="STATUS_CODE_ERROR"}[5m])))
|
|
2648
|
+
((sum(rate(traces_span_metrics_calls_total{service_name="support-agent",span_name="invoke_agent support",status_code="STATUS_CODE_ERROR"}[5m]))) or 0 * ((sum(rate(traces_span_metrics_calls_total{service_name="support-agent",span_name="invoke_agent support"}[5m]))) > 0))
|
|
2649
2649
|
/
|
|
2650
|
-
(sum(rate(traces_span_metrics_calls_total{service_name="support-agent",span_name="invoke_agent support"}[5m])))
|
|
2650
|
+
((sum(rate(traces_span_metrics_calls_total{service_name="support-agent",span_name="invoke_agent support"}[5m]))) > 0)
|
|
2651
2651
|
labels:
|
|
2652
2652
|
slo: support-agent-runs
|
|
2653
2653
|
- record: slo:sli_error:ratio_rate30m
|
|
2654
2654
|
expr: |-
|
|
2655
|
-
(sum(rate(traces_span_metrics_calls_total{service_name="support-agent",span_name="invoke_agent support",status_code="STATUS_CODE_ERROR"}[30m])))
|
|
2655
|
+
((sum(rate(traces_span_metrics_calls_total{service_name="support-agent",span_name="invoke_agent support",status_code="STATUS_CODE_ERROR"}[30m]))) or 0 * ((sum(rate(traces_span_metrics_calls_total{service_name="support-agent",span_name="invoke_agent support"}[30m]))) > 0))
|
|
2656
2656
|
/
|
|
2657
|
-
(sum(rate(traces_span_metrics_calls_total{service_name="support-agent",span_name="invoke_agent support"}[30m])))
|
|
2657
|
+
((sum(rate(traces_span_metrics_calls_total{service_name="support-agent",span_name="invoke_agent support"}[30m]))) > 0)
|
|
2658
2658
|
labels:
|
|
2659
2659
|
slo: support-agent-runs
|
|
2660
2660
|
- record: slo:sli_error:ratio_rate1h
|
|
2661
2661
|
expr: |-
|
|
2662
|
-
(sum(rate(traces_span_metrics_calls_total{service_name="support-agent",span_name="invoke_agent support",status_code="STATUS_CODE_ERROR"}[1h])))
|
|
2662
|
+
((sum(rate(traces_span_metrics_calls_total{service_name="support-agent",span_name="invoke_agent support",status_code="STATUS_CODE_ERROR"}[1h]))) or 0 * ((sum(rate(traces_span_metrics_calls_total{service_name="support-agent",span_name="invoke_agent support"}[1h]))) > 0))
|
|
2663
2663
|
/
|
|
2664
|
-
(sum(rate(traces_span_metrics_calls_total{service_name="support-agent",span_name="invoke_agent support"}[1h])))
|
|
2664
|
+
((sum(rate(traces_span_metrics_calls_total{service_name="support-agent",span_name="invoke_agent support"}[1h]))) > 0)
|
|
2665
2665
|
labels:
|
|
2666
2666
|
slo: support-agent-runs
|
|
2667
2667
|
- record: slo:sli_error:ratio_rate2h
|
|
2668
2668
|
expr: |-
|
|
2669
|
-
(sum(rate(traces_span_metrics_calls_total{service_name="support-agent",span_name="invoke_agent support",status_code="STATUS_CODE_ERROR"}[2h])))
|
|
2669
|
+
((sum(rate(traces_span_metrics_calls_total{service_name="support-agent",span_name="invoke_agent support",status_code="STATUS_CODE_ERROR"}[2h]))) or 0 * ((sum(rate(traces_span_metrics_calls_total{service_name="support-agent",span_name="invoke_agent support"}[2h]))) > 0))
|
|
2670
2670
|
/
|
|
2671
|
-
(sum(rate(traces_span_metrics_calls_total{service_name="support-agent",span_name="invoke_agent support"}[2h])))
|
|
2671
|
+
((sum(rate(traces_span_metrics_calls_total{service_name="support-agent",span_name="invoke_agent support"}[2h]))) > 0)
|
|
2672
2672
|
labels:
|
|
2673
2673
|
slo: support-agent-runs
|
|
2674
2674
|
- record: slo:sli_error:ratio_rate6h
|
|
2675
2675
|
expr: |-
|
|
2676
|
-
(sum(rate(traces_span_metrics_calls_total{service_name="support-agent",span_name="invoke_agent support",status_code="STATUS_CODE_ERROR"}[6h])))
|
|
2676
|
+
((sum(rate(traces_span_metrics_calls_total{service_name="support-agent",span_name="invoke_agent support",status_code="STATUS_CODE_ERROR"}[6h]))) or 0 * ((sum(rate(traces_span_metrics_calls_total{service_name="support-agent",span_name="invoke_agent support"}[6h]))) > 0))
|
|
2677
2677
|
/
|
|
2678
|
-
(sum(rate(traces_span_metrics_calls_total{service_name="support-agent",span_name="invoke_agent support"}[6h])))
|
|
2678
|
+
((sum(rate(traces_span_metrics_calls_total{service_name="support-agent",span_name="invoke_agent support"}[6h]))) > 0)
|
|
2679
2679
|
labels:
|
|
2680
2680
|
slo: support-agent-runs
|
|
2681
2681
|
- record: slo:sli_error:ratio_rate1d
|
|
2682
2682
|
expr: |-
|
|
2683
|
-
(sum(rate(traces_span_metrics_calls_total{service_name="support-agent",span_name="invoke_agent support",status_code="STATUS_CODE_ERROR"}[1d])))
|
|
2683
|
+
((sum(rate(traces_span_metrics_calls_total{service_name="support-agent",span_name="invoke_agent support",status_code="STATUS_CODE_ERROR"}[1d]))) or 0 * ((sum(rate(traces_span_metrics_calls_total{service_name="support-agent",span_name="invoke_agent support"}[1d]))) > 0))
|
|
2684
2684
|
/
|
|
2685
|
-
(sum(rate(traces_span_metrics_calls_total{service_name="support-agent",span_name="invoke_agent support"}[1d])))
|
|
2685
|
+
((sum(rate(traces_span_metrics_calls_total{service_name="support-agent",span_name="invoke_agent support"}[1d]))) > 0)
|
|
2686
2686
|
labels:
|
|
2687
2687
|
slo: support-agent-runs
|
|
2688
2688
|
- record: slo:sli_error:ratio_rate3d
|
|
2689
2689
|
expr: |-
|
|
2690
|
-
(sum(rate(traces_span_metrics_calls_total{service_name="support-agent",span_name="invoke_agent support",status_code="STATUS_CODE_ERROR"}[3d])))
|
|
2690
|
+
((sum(rate(traces_span_metrics_calls_total{service_name="support-agent",span_name="invoke_agent support",status_code="STATUS_CODE_ERROR"}[3d]))) or 0 * ((sum(rate(traces_span_metrics_calls_total{service_name="support-agent",span_name="invoke_agent support"}[3d]))) > 0))
|
|
2691
2691
|
/
|
|
2692
|
-
(sum(rate(traces_span_metrics_calls_total{service_name="support-agent",span_name="invoke_agent support"}[3d])))
|
|
2692
|
+
((sum(rate(traces_span_metrics_calls_total{service_name="support-agent",span_name="invoke_agent support"}[3d]))) > 0)
|
|
2693
2693
|
labels:
|
|
2694
2694
|
slo: support-agent-runs
|
|
2695
2695
|
- record: slo:sli_error:ratio_rate28d
|