apiVersion: kyverno.io/v1
kind: Policy
metadata:
  name: allow-add-capabilities
  annotations:
    policies.kyverno.io/category: Pod Security Standards
    policies.kyverno.io/severity: medium
    policies.kyverno.io/subject: Pod
    policies.kyverno.io/description: >-
      Provides a list of capabilities that are allowed to be added to a container.
spec:
  validationFailureAction: audit
  background: true
  rules:
    - name: capabilities
      match:
        resources:
          kinds:
            - Pod
          selector:
            matchLabels:
              # applicable for experiments which uses container runtime apis
              app.kubernetes.io/runtime-api-usage: "true"
      validate:
        message: >-
          The provided capabilities are not allowed at securityContext.capabilities.add     
        pattern:
          spec:
            containers:
              - =(securityContext):
                  =(capabilities):
                    # allow only NET_ADMIN and SYS_ADMIN capabilities
                    =(add):
                      - "NET_ADMIN | SYS_ADMIN"
