All Policies

Restrict Pod Count per Node

Sometimes Kubernetes Nodes may have a maximum number of Pods they can accommodate due to resources outside CPU and memory such as licensing, or in some development cases. This policy restricts Pod count on a Node named `minikube` to be no more than 10.

Policy Definition

/other/restrict_pod_count_per_node/restrict_pod_count_per_node.yaml

 1apiVersion: kyverno.io/v1
 2kind: ClusterPolicy
 3metadata:
 4  name: restrict-pod-count
 5  annotations:
 6    policies.kyverno.io/title: Restrict Pod Count per Node
 7    policies.kyverno.io/category: Sample
 8    policies.kyverno.io/severity: medium
 9    policies.kyverno.io/subject: Pod
10    policies.kyverno.io/minversion: 1.3.2
11    policies.kyverno.io/description: >-
12      Sometimes Kubernetes Nodes may have a maximum number of Pods they can accommodate due to
13      resources outside CPU and memory such as licensing, or in some
14      development cases. This policy restricts Pod count on a Node named `minikube` to be no more than 10.      
15    # pod-policies.kyverno.io/autogen-controllers: None
16spec:
17  validationFailureAction: audit
18  background: false
19  rules:
20    - name: restrict-pod-count
21      match:
22        resources:
23          kinds:
24            - Pod
25      context:
26        - name: podcounts
27          apiCall:
28            urlPath: "/api/v1/pods"
29            jmesPath: "items[?spec.nodeName=='minikube'] | length(@)"
30      preconditions:
31        any:
32        - key: "{{ request.operation || 'BACKGROUND' }}"
33          operator: Equals
34          value: "CREATE"
35      validate:
36        message: "A maximum of 10 Pods are allowed on the Node `minikube`"
37        deny:
38          conditions:
39            any:
40            - key: "{{ podcounts }}"
41              operator: GreaterThan
42              value: 10