@@ -113,9 +113,8 @@ func mutationRequired(metadata *metav1.ObjectMeta) bool {
113113 return required
114114}
115115
116- // addContainerTracked adds containers using a boolean to determine if it's the first addition
117- // This fixes the bug where multiple loop iterations would overwrite previous additions
118- func addContainerTracked (added []corev1.Container , basePath string ) (patch []patchOperation ) {
116+ // addContainers simply never add to an empty initContainers array
117+ func addContainer (added []corev1.Container , basePath string ) (patch []patchOperation ) {
119118 for _ , add := range added {
120119 klog .Infof ("container patch with name: %s" , add .Name )
121120 patch = append (patch , patchOperation {
@@ -239,8 +238,9 @@ func createPatch(pod *corev1.Pod, sidecarConfigTemplate *Config, clientset *kube
239238 isFirstVol = false
240239 }
241240
242- // Track whether we have already added a container to know the correct path for the patch (first addition is different than subsequent additions)
243241 // We don't want to overwrite any containers
242+ // We always want to append, so no need to change the isFirst for this one,
243+ // but we do want to check if initContainers exists first because if it doesn't then we need to add it before we can append to it
244244 // If the field is missing entirely, create it as an empty array first.
245245 if pod .Spec .InitContainers == nil {
246246 klog .Infof ("Patch appended" )
@@ -317,7 +317,7 @@ func createPatch(pod *corev1.Pod, sidecarConfigTemplate *Config, clientset *kube
317317
318318 // Add container to initContainers and volume to the patch
319319 // Pass bools to track first addition and handle path change
320- patch = append (patch , addContainerTracked (sidecarConfig .Containers , "/spec/initContainers" )... )
320+ patch = append (patch , addContainer (sidecarConfig .Containers , "/spec/initContainers" )... )
321321
322322 patch = append (patch , addVolumeTracked (isFirstVol , sidecarConfig .Volumes , "/spec/volumes" )... )
323323
0 commit comments