希望的行为
- 编写检查图像名称是否包含默认最新标签的 OPA 策略。以下是我的 .rego 文件:
package kubernetes.admission
import data.kubernetes.namespaces
deny[msg] {
input.request.kind.kind == "Pod"
input.request.operation == "CREATE"
container := input.request.object.spec.containers[_]
[image_name, image_tag] := split(container.image, ":")
image_tag == "latest"
msg := sprintf("Invalid image tag")
}
- 通过创建 configmap 加载策略。我使用了以下命令:
kubectl create configmap registry-whitelist --from-file image-checker.rego
我当前上下文中的默认命名空间是
opa
.
- 之后,假设我可以通过创建一个带有最新标签的 pod 来执行该策略,并且它必须被拒绝。
实际行为
具有最新标签的 Pod 已成功创建,并且不会被拒绝。
重现问题的步骤
我遵循了这些提示https://www.openpolicyagent.org/docs/latest/kubernetes-debugging/。
因此,预计创建的 configmapregistry-whitelist
具有openpolicyagent.org/policy-status
注释,但它具有<none>
值,我也检查了kube-mgmt
容器的日志,但它们没有帮助我。我得到的唯一有趣的日志是当我尝试删除配置映射时,registry-whitelist
我可以看到以下日志:
level=error msg="删除策略 opa/registry-whitelist/image-checker.rego 失败:code resource_not_found: storage_not_found_error: policy id "opa/registry-whitelist/image-checker.rego""