【发布时间】:2020-08-08 02:32:47
【问题描述】:
我想编写一个 Rego 规则,根据模板中声明的标签检查 Kubernetes 部署选择器。如果spec.selector.matchLabels 中存在的每个键/值都存在于spec.template.metadata.labels 中,则该规则应该通过。
如果我用 Javascript 编写它,它看起来像这样:
for(let key of input.spec.selector.matchLabels) {
assert(input.spec.selector.matchLabels[key] === input.spec.template.metadata.labels[key], `${key} doesn't match`)
}
我不知道如何在 Rego 中编写等效项。我想出了一种方法来检查键是否存在,但我不知道如何检查值。这是我目前所拥有的:
selector_match_labels {
# keys in matchLabels are present in the template labels
matchLabels := { label | input.spec.selector.matchLabels[label] }
labels := { label | input.spec.template.metadata.labels[label] }
count(matchLabels - labels) == 0
# How to check the values of each key matches?
}
【问题讨论】: