Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save Nathanael-Mtd/9290faef3163acb653c34a556b8f6d5d to your computer and use it in GitHub Desktop.

Select an option

Save Nathanael-Mtd/9290faef3163acb653c34a556b8f6d5d to your computer and use it in GitHub Desktop.
Kyverno policy : Mutate pods to redirect bitnami to bitnamilegacy image repository
apiVersion: kyverno.io/v1
kind: ClusterPolicy
metadata:
name: replace-bitnami-image-repository-with-bitnamilegacy
annotations:
policies.kyverno.io/title: Replace Bitnami Image Repository With Bitnami Legacy
pod-policies.kyverno.io/autogen-controllers: none
policies.kyverno.io/subject: Pod
spec:
rules:
- name: redirect-bitnami-to-bitnamilegacy
match:
any:
- resources:
kinds:
- Pod
operations:
- CREATE
- UPDATE
mutate:
foreach:
- list: request.object.spec.initContainers[]
context:
- name: imageData
imageRegistry:
reference: "{{ element.image }}"
preconditions:
any:
- key: "{{ regex_match('^(docker\\.io)/(bitnami)/(.*)$', '{{element.image}}') }}"
operator: Equals
value: true
patchStrategicMerge:
spec:
initContainers:
- name: "{{ element.name }}"
image: "{{ regex_replace_all('^(docker\\.io)/(bitnami)/(.*)$', '{{element.image}}', '$1/bitnamilegacy/$3' )}}"
- list: request.object.spec.containers[]
context:
- name: imageData
imageRegistry:
reference: "{{ element.image }}"
preconditions:
any:
- key: "{{ regex_match('^(docker\\.io)/(bitnami)/(.*)$', '{{element.image}}') }}"
operator: Equals
value: true
patchStrategicMerge:
spec:
containers:
- name: "{{ element.name }}"
image: "{{ regex_replace_all('^(docker\\.io)/(bitnami)/(.*)$', '{{element.image}}', '$1/bitnamilegacy/$3' )}}"
@Nathanael-Mtd

Copy link
Copy Markdown
Author

Probably not perfect, it only handle image source in docker.io/bitnami/xxx format.
It only apply on new or updated pods, not on existing ones. A restart on StatefulSet/Deploy, or a change on Pod object is needed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment