I have multiple environments where we define prometheus alerts, and these alerts have the same labels and description among environments, they just differ in the expression and priority.
I am currently using Kustomize with the following structure:
- base/prometheus/ It contains 2 files, kustomization and servicemonitor.
- overlays/environment/ It contains a kustomization and the current alerting rules
This is the kustomation file in the overlay folder:
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
...
bases:
...
- ../../base/prometheus/
resources:
- alerting-rules.yaml
patchesStrategicMerge:
...
And the one in the base:
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
- service-monitor.yaml
I tried to add the rules in the base:
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
- service-monitor.yaml
- prometheus-rules.yaml
And then add it in the overlays, adding this new resource to the patchesStrategicMerge, but when I run kubectl kustomize, the resulting rule is always the one that I put in the overlay, without the information (labels and description) from the base.
Edit: These are my files:
base/prometheus-alerts/kustomization.yaml
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
namespace: my-workspace
resources:
- alerting-rules-test.yaml
base/prometheus-alerts/alerting-rules-test.yaml
apiVersion: monitoring.coreos.com/v1
kind: PrometheusRule
metadata:
name: alerting-rules-test
namespace: my-namespace
labels:
app-id: MY-APP-ID
ms-id: my-ms-id
spec:
groups:
- name: alerting-rules-test
interval: 60s
rules:
- alert: alert-testing
expr: absent(application:health_microservice{service="my-service"})
for: 10m
labels:
priority: "MAJOR"
mc_id: ""
event_id: "100"
namespace: "my-namespace"
annotations:
summary: >
Summary of test alert
description: >
Description of test alert
overlays/env/kustomization.yaml
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
namespace: my-namespace
commonAnnotations: #use by dynatrace
... # some labels
bases:
... # other bases
- ../../base/prometheus-alerts
resources:
... # some resources from the same folder
patches:
...
- alerting-rules-test.yaml
overlays/env/alerting-rules-test.yaml
apiVersion: monitoring.coreos.com/v1
kind: PrometheusRule
metadata:
name: alerting-rules-test
namespace: my-namespace
spec:
groups:
- name: alerting-rules-test
interval: 60s
rules:
- alert: alert-testing
expr: absent(application:health_microservice{service="my-service"})
labels:
priority: "MINOR"
When I run kubectl kustomize, this is the resulting yaml:
apiVersion: monitoring.coreos.com/v1
kind: PrometheusRule
metadata:
annotations:
... #annotations from env/kustomization.yaml
labels:
app-id: MY-APP-ID
ms-id: my-ms-id
name: alerting-rules-test
namespace: my-namespace
spec:
groups:
- interval: 60s
name: alerting-rules-test
rules:
- alert: alert-testing
expr: absent(application:health_microservice{service="my-service"})
labels:
priority: MINOR
The problem is that the complete rule gets replaced by the one defined en env/alerting-rules-test.yaml. The resulting PrometheusRule should have the other fields defined in the base, like the summary and the description.
I have tried with JsonPatches6902 but it does not replace the value.
In the overlays/env/kustomization.yaml, I added
patchesJson6902:
- target:
group: monitoring.coreos.com
version: v1
kind: PrometheusRule
name: alerting-rules-test
path: alerting-rules-test-patch.yaml
And the contents of the new file:
- op: replace
path: "/spec/groups/0/rules/0/labels/priority"
value: "MINOR"
It works after properly adding the group and version to the target. Is it possible to specify in the path the rule by its name instead of the index?