apiVersion: v1
kind: Namespace
metadata:
  name: secure-workloads
  labels:
    pod-security.kubernetes.io/enforce: restricted
    pod-security.kubernetes.io/enforce-version: latest
    pod-security.kubernetes.io/audit: restricted
    pod-security.kubernetes.io/audit-version: latest
    pod-security.kubernetes.io/warn: restricted
    pod-security.kubernetes.io/warn-version: latest
---
apiVersion: kyverno.io/v1
kind: ClusterPolicy
metadata:
  name: require-basic-pod-hardening
spec:
  validationFailureAction: Enforce
  background: true
  rules:
    - name: require-non-root-and-readonly-rootfs
      match:
        any:
          - resources:
              kinds:
                - Pod
      validate:
        message: "Pods must avoid root and use a read-only root filesystem unless an approved exception exists."
        pattern:
          spec:
            =(securityContext):
              =(runAsNonRoot): true
            containers:
              - securityContext:
                  =(allowPrivilegeEscalation): false
                  =(readOnlyRootFilesystem): true
                  capabilities:
                    drop:
                      - ALL
