Checkov -创建基本的自定义策略,以确保在Kubernetes Deployment上设置注解

xzlaal3s  于 2023-06-21  发布在  Kubernetes
关注(0)|答案(1)|浏览(144)

我一直在查看checkov,看看它是否可以标记任何Kubernetes Deployments缺少注解kubectl.kubernetes.io/default-container
我似乎无法让它工作。这看起来像是checkov的一个非常简单的用例。
我目前有以下政策文件:

  1. ---
  2. metadata:
  3. id: "CKV2_KCDC_1"
  4. name: "Ensure all Deployments have default-container annotation"
  5. category: "KUBERNETES"
  6. definition:
  7. and:
  8. - cond_type: filter
  9. value:
  10. - Deployment
  11. operator: within
  12. attribute: kind
  13. - cond_type: attribute
  14. resource_types:
  15. - Deployment
  16. attribute: "metadata.annotations.kubectl.kubernetes.io/default-container"
  17. operator: exists

我对此的解释是“过滤部署,并确保每个部署都有注解”
当我运行这个程序时,我会遇到很多失败,但是当我将注解添加到失败的清单时,这些失败没有得到解决。

ih99xse1

ih99xse11#

我最后和达特里一起去了。我的组织已经在使用它,我发现为我的场景编写一个带有自定义规则的策略非常容易。该策略看起来像这样:

  1. apiVersion: v1
  2. policies:
  3. - name: Custom
  4. isDefault: true
  5. rules:
  6. - identifier: ENSURE_DEFAULT_CONTAINER_ANNOTATION_IS_SET
  7. messageOnFailure: Every workload must set the kubectl.kubernetes.io/default-container annotation so that multi-container workloads have sensible defaults for kubctl exec and kubectl log commands.
  8. customRules:
  9. - identifier: ENSURE_DEFAULT_CONTAINER_ANNOTATION_IS_SET
  10. name: Ensure workload has default container annotation set
  11. defaultMessageOnFailure: Every workload must set the kubectl.kubernetes.io/default-container annotation so that multi-container workloads have sensible defaults for kubctl exec and kubectl log commands.
  12. schema:
  13. if:
  14. properties:
  15. kind:
  16. enum:
  17. - Deployment
  18. - StatefulSet
  19. then:
  20. properties:
  21. spec:
  22. properties:
  23. template:
  24. properties:
  25. metadata:
  26. properties:
  27. annotations:
  28. required:
  29. - kubectl.kubernetes.io/default-container
  30. required:
  31. - annotations
展开查看全部

相关问题