我想减少复制/粘贴,并让我的环境(我使用 kustomize)从基础获取警报规则,然后只提供值。
base/prometheus-alerts.yaml
apiVersion: monitoring.coreos.com/v1
kind: PrometheusRule
metadata:
labels:
release: kube-prometheus-stack
role: app-rules
test: $(VAL_A) # This works
name: xx
namespace: monitoring
spec:
groups:
- name: xx
rules:
- alert: xx
annotations:
description: 'xx'
expr: sum(rate(xx[5m])) by (container) < $(VAL_A) # This does not
在我的测试环境中,我有以下内容
kustomization.yml
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
- ../base
vars:
- name: VAL_A
objref:
kind: PrometheusRule
name: connectors-taz-tcp-alerts
apiVersion: monitoring.coreos.com/v1
fieldref:
fieldpath: data.VAL_A
然后是
patch-prometheus-alerts.yaml
apiVersion: monitoring.coreos.com/v1
kind: PrometheusRule
metadata:
name: connectors-taz-tcp-alerts
namespace: monitoring
data:
VAL_A: "hello-world"
每当我运行
kubectl kustomize .
时,我都会得到以下信息:
apiVersion: monitoring.coreos.com/v1
data:
VAL_A: hello-world
kind: PrometheusRule
metadata:
labels:
release: kube-prometheus-stack
role: app-rules
test: hello-world
name: xx
namespace: xx
spec:
groups:
- name: xx
rules:
- alert: xx
annotations:
[..]
expr: sum(rate(xx[5m])) by (container) < $(VAL_A)
为什么标签
test
更改为“hello-world”而不是表达式中的$(VAL_A)
?
字段
vars
自 v5 起已弃用,建议的替代方案是 replacements
1。
更新后的 Kustomize 将为您提供开箱即用的修复。
我使用以下布局重现了您的设置:
.
├── base
│ ├── kustomization.yml
│ └── prometheus-alerts.yaml
└── dev
├── kustomization.yml
└── patch-prometheus-alerts.yaml
base/*
的内容与你的问题中的内容完全相同。
dev/patch-prometheus-alerts.yaml
也是如此。
唯一的变化如下:
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
- ../base
- patch-prometheus-alerts.yaml
replacements:
- source:
fieldPath: data.VAL_A
group: monitoring.coreos.com
kind: PrometheusRule
name: connectors-taz-tcp-alerts
version: v1
targets:
- fieldPaths:
- metadata.labels.test
select:
group: monitoring.coreos.com
kind: PrometheusRule
name: xx
namespace: monitoring
version: v1
- fieldPaths:
- spec.groups.0.rules.0.expr
options:
delimiter: " "
index: 4
select:
group: monitoring.coreos.com
kind: PrometheusRule
name: xx
namespace: monitoring
version: v1
这是在
kustomize edit fix --vars
目录中运行 dev/
的输出。
这是使用
kustomize build .
构建时的结果(在 dev/
目录中)。
apiVersion: monitoring.coreos.com/v1
data:
VAL_A: hello-world
kind: PrometheusRule
metadata:
name: connectors-taz-tcp-alerts
namespace: monitoring
---
apiVersion: monitoring.coreos.com/v1
kind: PrometheusRule
metadata:
labels:
release: kube-prometheus-stack
role: app-rules
test: hello-world
name: xx
namespace: monitoring
spec:
groups:
- name: xx
rules:
- alert: xx
annotations:
description: xx
expr: sum(rate(xx[5m])) by (container) < hello-world