1

希望的行为

  1. 编写检查图像名称是否包含默认最新标签的 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")
}
  1. 通过创建 configmap 加载策略。我使用了以下命令:
kubectl create configmap registry-whitelist --from-file image-checker.rego

我当前上下文中的默认命名空间是opa.

  1. 之后,假设我可以通过创建一个带有最新标签的 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""

4

1 回答 1

1

问题已解决:实际上我在 rego 中编写的策略有一些错误,这就是为什么 kube-mgmt 在将注释添加到新创建的 configmap 之前需要一些时间的原因。过了一会儿,我发现一个注释说我的政策有错误。

于 2020-01-31T22:17:29.160 回答