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.6.0
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      any:
24      - resources:
25          kinds:
26          - Pod
27    preconditions:
28      all:
29      - key: '{{request.object.metadata.annotations."inject-certs" || ""}}'
30        operator: Equals
31        value: enabled
32      - key: "{{request.operation || 'BACKGROUND'}}"
33        operator: AnyIn
34        value:
35          - CREATE
36          - UPDATE
37    mutate:
38      foreach:
39      - list: "request.object.spec.containers"
40        patchStrategicMerge:
41          spec:
42            containers:
43            - name: "{{ element.name }}"
44              volumeMounts:
45              - name: etc-ssl-certs
46                mountPath: /etc/ssl/certs
47            volumes:
48            - name: etc-ssl-certs
49              configMap:
50                name: ca-pemstore