All Policies

Restrict control plane scheduling in CEL expressions

Scheduling non-system Pods to control plane nodes (which run kubelet) is often undesirable because it takes away resources from the control plane components and can represent a possible security threat vector. This policy prevents users from setting a toleration in a Pod spec which allows running on control plane nodes with the taint key `node-role.kubernetes.io/master`.

Policy Definition

/other-cel/restrict-controlplane-scheduling/restrict-controlplane-scheduling.yaml

 1apiVersion: kyverno.io/v1
 2kind: ClusterPolicy
 3metadata:
 4  name: restrict-controlplane-scheduling
 5  annotations:
 6    policies.kyverno.io/title: Restrict control plane scheduling in CEL expressions
 7    policies.kyverno.io/category: Sample in CEL 
 8    policies.kyverno.io/subject: Pod
 9    policies.kyverno.io/minversion: 1.11.0
10    kyverno.io/kubernetes-version: "1.26-1.27"
11    policies.kyverno.io/description: >-
12      Scheduling non-system Pods to control plane nodes (which run kubelet) is often undesirable
13      because it takes away resources from the control plane components and can represent
14      a possible security threat vector. This policy prevents users from setting a toleration
15      in a Pod spec which allows running on control plane nodes
16      with the taint key `node-role.kubernetes.io/master`.
17spec:
18  validationFailureAction: Audit
19  background: true
20  rules:
21  - name: restrict-controlplane-scheduling-master
22    match:
23      any:
24      - resources:
25          kinds:
26          - Pod
27          operations:
28          - CREATE
29          - UPDATE
30    validate:
31      cel:
32        expressions:
33          - expression: >-
34              !has(object.spec.tolerations) || 
35              !object.spec.tolerations.exists(toleration, toleration.?key.orValue('') in ['node-role.kubernetes.io/master', 'node-role.kubernetes.io/control-plane'])
36            message: Pods may not use tolerations which schedule on control plane nodes.