Back to 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.

View on GitHub

Policy Definition

apiVersion: kyverno.io/v1
kind: ClusterPolicy
metadata:
name: add-certificates-volume
annotations:
policies.kyverno.io/title: Add Certificates as a Volume
policies.kyverno.io/category: Sample
policies.kyverno.io/subject: Pod,Volume
kyverno.io/kyverno-version: 1.6.0
kyverno.io/kubernetes-version: "1.21"
policies.kyverno.io/minversion: 1.5.0
pod-policies.kyverno.io/autogen-controllers: DaemonSet,Deployment,Job,StatefulSet
policies.kyverno.io/description: 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.
spec:
background: false
rules:
- name: add-ssl-certs
match:
any:
- resources:
kinds:
- Pod
preconditions:
all:
- key: "{{request.object.metadata.annotations.\"inject-certs\" || \"\"}}"
operator: Equals
value: enabled
- key: "{{request.operation || 'BACKGROUND'}}"
operator: AnyIn
value:
- CREATE
- UPDATE
mutate:
foreach:
- list: request.object.spec.containers
patchStrategicMerge:
spec:
containers:
- name: "{{ element.name }}"
volumeMounts:
- name: etc-ssl-certs
mountPath: /etc/ssl/certs
volumes:
- name: etc-ssl-certs
configMap:
name: ca-pemstore

Related Policies