Last active
November 28, 2022 23:56
-
-
Save mohllal/b61a4a5e6239776ca7ff484a768f8eff to your computer and use it in GitHub Desktop.
An example of the injection mutation function
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import * as jsonpatch from 'fast-json-patch'; | |
const mutate = (admissionReviewRequest: V1AdmissionRequest<V1Pod>): V1AdmissionResponse => { | |
const admissionReviewResponse: V1AdmissionResponse = { | |
allowed: true, | |
uid: admissionReviewRequest.uid, | |
}; | |
// get the pod object and clone it | |
const originalPod = admissionReviewRequest.object as V1Pod; | |
const mutatedPod = JSON.parse(JSON.stringify(originalPod)) as V1Pod; | |
// update the mutated pod spec with the new containers array | |
const mutatedPodContainers = injectContainer(originalPod.spec?.containers); | |
mutatedPod.spec = { ...mutatedPod.spec, containers: mutatedPodContainers }; | |
// generate json patch string | |
const patchArray = jsonpatch.compare(originalPod, mutatedPod); | |
const patchArrayJsonStr = JSON.stringify(patchArray); | |
admissionReviewResponse.patchType = "JSONPatch"; | |
admissionReviewResponse.patch = Buffer.from(patchArrayJsonStr).toString('base64'); | |
return admissionReviewResponse; | |
} | |
const injectContainer = (containers: V1Container[] = []): V1Container[] => { | |
const sidecarContainer: V1Container = { | |
name: 'curl', | |
image: 'yauritux/busybox-curl:latest' | |
}; | |
return [...containers, sidecarContainer]; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment