我们验证有名称空间和没有名称空间的对象,我总是想打印当前名称空间是什么……但是当没有设置名称空间时,后退到"“或一些其他默认值。
使用朴素的input.review.object.metadata.namespace直接违反了规则,所以我求助于
namespace := [input.review.object.metadata.namespace | ""]它打印了一个数组,有点难看,但它很有效...有没有更好的解决方案?
发布于 2020-01-11 04:32:30
它没有一个很好的操作符,尽管正在讨论添加一个操作符。
通常,模式是使用具有访问器的帮助器规则/函数和具有默认值的负规则/函数。例如:
https://play.openpolicyagent.org/p/RhZVyJjqOJ使用..
# If the namespace exists, use it
input_namespace = ns {
ns := input.review.object.metadata.namespace
}
# or if it doesn't, use the string defined here
input_namespace = "whatever-default-value-i-want" {
not input.review.object.metadata.namespace
}在野外也可以看到这种相同的模式,例如在网守库中:https://github.com/open-policy-agent/gatekeeper/blob/master/library/general/requiredlabels/src.rego#L3-L10
https://stackoverflow.com/questions/59688239
复制相似问题