morvencao / kube-sidecar-injector

A Kubernetes mutating webhook server that implements sidecar injection
Apache License 2.0
628 stars 454 forks source link

Help webhook - func addContainer() #16

Closed HamzaZo closed 4 years ago

HamzaZo commented 4 years ago

Hello, I'm following your fantastic article about mutating webhook and I'm trying to understand what happens underground, unfortunately I'm struggling to understand what add Container func does. Can you please explain to me what you are doing in this block

first := len(target) == 0
for _, add := range added {
        value = add
        path := basePath
        if first {
            first = false
            value = []corev1.Container{add}
        } else {
            path = path + "/-"
        }

original function:

func addContainer(target, added []corev1.Container, basePath string) (patch []patchOperation) {
    first := len(target) == 0
    var value interface{}
    for _, add := range added {
        value = add
        path := basePath
        if first {
            first = false
            value = []corev1.Container{add}
        } else {
            path = path + "/-"
        }
        patch = append(patch, patchOperation{
            Op:    "add",
            Path:  path,
            Value: value,
        })
    }
    return patch
}

Thanks for your help!