@@ -113,28 +113,22 @@ func mutationRequired(metadata *metav1.ObjectMeta) bool {
113113 return required
114114}
115115
116- func addContainer (target , added []corev1.Container , basePath string ) (patch []patchOperation ) {
117- first := len (target ) == 0
118- var value interface {}
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 {
120- value = add
121- path := basePath
122- if first {
123- first = false
124- value = []corev1.Container {add }
125- } else {
126- path = path + "/-"
127- }
119+ klog .Infof ("container patch with name: %s" , add .Name )
128120 patch = append (patch , patchOperation {
129121 Op : "add" ,
130- Path : path ,
131- Value : value ,
122+ Path : basePath + "/-" ,
123+ Value : add ,
132124 })
133125 }
134126 return patch
135127}
136128
137- func addVolume (target , added []corev1.Volume , basePath string ) (patch []patchOperation ) {
129+ // addVolumeTracked adds volumes using a boolean to determine if it's the first addition
130+ // This fixes the bug where multiple loop iterations would overwrite previous additions
131+ func addVolumeTracked (target , added []corev1.Volume , basePath string ) (patch []patchOperation ) {
138132 first := len (target ) == 0
139133 var value interface {}
140134 for _ , add := range added {
@@ -244,6 +238,19 @@ func createPatch(pod *corev1.Pod, sidecarConfigTemplate *Config, clientset *kube
244238 isFirstVol = false
245239 }
246240
241+ // 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
244+ // If the field is missing entirely, create it as an empty array first.
245+ if pod .Spec .InitContainers == nil {
246+ klog .Infof ("Patch appended" )
247+ patch = append (patch , patchOperation {
248+ Op : "add" ,
249+ Path : "/spec/initContainers" ,
250+ Value : []corev1.Container {},
251+ })
252+ }
253+
247254 // shareList.Data is a map[string]string
248255 // https://goplay.tools/snippet/zUiIt23ZYVK
249256 var shareList []string
@@ -253,8 +260,7 @@ func createPatch(pod *corev1.Pod, sidecarConfigTemplate *Config, clientset *kube
253260 secret , err := clientset .CoreV1 ().Secrets (pod .Namespace ).Get (context .Background (),
254261 svmSecretName , metav1.GetOptions {})
255262 if k8serrors .IsNotFound (err ) {
256- klog .Infof ("Error, secret for svm:" + svmName + " was not found for ns:" + pod .Namespace +
257- " so mounting will be skipped" )
263+ klog .Infof ("Error, secret for svm: %s was not found for ns: %s so mounting will be skipped" , svmName , pod .Namespace )
258264 continue
259265 }
260266 s3Url := string (svmInfoMap [svmName ].Url )
@@ -263,8 +269,7 @@ func createPatch(pod *corev1.Pod, sidecarConfigTemplate *Config, clientset *kube
263269 // Unmarshal to get list of wanted shares to mount
264270 err = json .Unmarshal ([]byte (svmShareList .Data [svmName ]), & shareList )
265271 if err != nil {
266- klog .Infof ("Error unmarshalling the share list for svm:" + svmName +
267- " for ns:" + pod .Namespace + " so mounting will be skipped" )
272+ klog .Infof ("Error unmarshalling the share list for svm: %s for ns: %s so mounting will be skipped" , svmName , pod .Namespace )
268273 continue
269274 }
270275 // must set ACCESS and SECRET keys as well as svm url in the patch
@@ -311,9 +316,11 @@ func createPatch(pod *corev1.Pod, sidecarConfigTemplate *Config, clientset *kube
311316 sidecarConfig .Volumes [1 ].CSI .VolumeAttributes ["fdPassingEmptyDirName" ] = fdPassingvolumeMountName
312317
313318 // Add container to initContainers and volume to the patch
314- patch = append (patch , addContainer (pod .Spec .InitContainers , sidecarConfig .Containers , "/spec/initContainers" )... )
315- // Add restartPolicy: Always to allow sidecar to terminate when main container completes
316- patch = append (patch , addVolume (pod .Spec .Volumes , sidecarConfig .Volumes , "/spec/volumes" )... )
319+ // Pass bools to track first addition and handle path change
320+ patch = append (patch , addContainer (sidecarConfig .Containers , "/spec/initContainers" )... )
321+
322+ patch = append (patch , addVolumeTracked (pod .Spec .Volumes , sidecarConfig .Volumes , "/spec/volumes" )... )
323+
317324 patch = append (patch , updateAnnotation (pod .Annotations )... )
318325 patch = append (patch , updateWorkingVolumeMounts (pod .Spec .Containers , csiEphemeralVolumeountName , bucketMount , svmName , isFirstVol )... )
319326 // Add the environment variables
0 commit comments