All Policies

Add Certificates as a Volume

In some cases you would need to trust custom CA certificates for all the containers of a Pod. It makes sense to be in a ConfigMap so that you can automount them by only setting an annotation. This policy adds a volume to all containers in a Pod containing the certificate if the annotation called `inject-certs` with value `enabled` is found.

Policy Definition

/other/add_certificates_volume/add_certificates_volume.yaml

 1apiVersion: kyverno.io/v1
 2kind: ClusterPolicy
 3metadata:
 4  name: add-certificates-volume
 5  annotations:
 6    policies.kyverno.io/title: Add Certificates as a Volume
 7    policies.kyverno.io/category: Sample
 8    policies.kyverno.io/subject: Pod,Volume
 9    kyverno.io/kyverno-version: 1.5.2
10    kyverno.io/kubernetes-version: "1.21"
11    policies.kyverno.io/minversion: 1.5.0
12    pod-policies.kyverno.io/autogen-controllers: DaemonSet,Deployment,Job,StatefulSet
13    policies.kyverno.io/description: >-
14      In some cases you would need to trust custom CA certificates for all the containers of a Pod.
15      It makes sense to be in a ConfigMap so that you can automount them by only setting an annotation.
16      This policy adds a volume to all containers in a Pod containing the certificate if the annotation
17      called `inject-certs` with value `enabled` is found.      
18spec:
19  background: false
20  rules:
21  - name: add-ssl-certs
22    match:
23      resources:
24        kinds:
25        - Pod
26    preconditions:
27      all:
28      - key: '{{request.object.metadata.annotations."inject-certs"}}'
29        operator: Equals
30        value: enabled
31      - key: "{{request.operation || 'BACKGROUND'}}"
32        operator: AnyIn
33        value:
34          - CREATE
35          - UPDATE
36    mutate:
37      foreach:
38      - list: "request.object.spec.containers"
39        patchStrategicMerge:
40          spec:
41            containers:
42            - name: "{{ element.name }}"
43              volumeMounts:
44              - name: etc-ssl-certs
45                mountPath: /etc/ssl/certs
46            volumes:
47            - name: etc-ssl-certs
48              configMap:
49                name: ca-pemstore