如何解决无法使用opa Gatekeeper拒绝Kubernetes上特定命名空间prod中带有最新标签的Pod
我是OPA政策的新手,需要拒绝在群集中运行带有其映像中带有最新标记的容器的pod,这仅适用于prod名称空间,而我拥有的问题就是所使用的名称空间是什么,如果Pod是在图像中使用最新标签创建的,则会被拒绝!!!!我在这里做错了吗?这是我的约束模板:
apiVersion: templates.gatekeeper.sh/v1beta1
kind: ConstraintTemplate
metadata:
name: k8srequiredtags
spec:
crd:
spec:
names:
kind: k8srequiredtags
validation:
# Schema for the `parameters` field
openAPIV3Schema:
properties:
image:
type: string
targets:
- target: admission.k8s.gatekeeper.sh
rego: |
package k8srequiredtags
violation[{"msg": msg,"details": {"Registry should be": required}}] {
input.review.object.kind == "Pod"
some i
image := input.review.object.spec.containers[i].image
required := input.parameters.registry
contains(image,required)
msg := sprintf("The image tag is not for the production environment,thanks to use specified tags instead of : %v",[image])
}
这是我的约束条件:
apiVersion: constraints.gatekeeper.sh/v1beta1
kind: k8srequiredtags
metadata:
name: deny-latest-tags
spec:
match:
kinds:
- apiGroups: [""]
kinds: ["Pod"]
namespace: ["prod"]
parameters:
registry: "latest"
解决方法
很高兴您尝试使用OPA和网守。
通过快速查看您的代码,我会做出一些更改:
- 由于您将“ Pods”作为一种类型传递,因此在您的约束中,无需在模板中对其进行过滤。您只传递了一种资源:Pod。
- 您的违反规则定义了对象“ input.review.object.spec.containers [i] .image”。该对象引用Pod资源的整个图像属性。因此,它将包含/:。因此,这可能永远不会仅仅是“最新的”。它可能是对docker镜像URL的引用。您可能需要解析标签才能将其用于比较。
- 关于您发布的有关名称空间的问题:我认为这与以下事实有关:匹配过滤器查找名为“ namespaces”而不是“ namespace”的过滤器属性[https://github.com/open-policy -agent / gatekeeper / blob / master / README.md#constraints]。
祝你好运。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。