我是 OPA 策略的新手,需要拒绝在我的集群中运行的容器在其映像中具有最新标签的容器,这必须仅针对 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"