Skip to content

Commit 0bd3d21

Browse files
committed
fix(webhook.go): Consider 0th index
1 parent ef3f31e commit 0bd3d21

File tree

1 file changed

+21
-7
lines changed

1 file changed

+21
-7
lines changed

cmd/webhook.go

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -113,8 +113,10 @@ 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
116+
// addContainerTracked adds containers using a counter to determine if it's the first addition
117+
// This fixes the bug where multiple loop iterations would overwrite previous additions
118+
func addContainerTracked(alreadyAdded int, added []corev1.Container, basePath string) (patch []patchOperation) {
119+
first := alreadyAdded == 0
118120
var value interface{}
119121
for _, add := range added {
120122
value = add
@@ -134,8 +136,10 @@ func addContainer(target, added []corev1.Container, basePath string) (patch []pa
134136
return patch
135137
}
136138

137-
func addVolume(target, added []corev1.Volume, basePath string) (patch []patchOperation) {
138-
first := len(target) == 0
139+
// addVolumeTracked adds volumes using a counter to determine if it's the first addition
140+
// This fixes the bug where multiple loop iterations would overwrite previous additions
141+
func addVolumeTracked(alreadyAdded int, added []corev1.Volume, basePath string) (patch []patchOperation) {
142+
first := alreadyAdded == 0
139143
var value interface{}
140144
for _, add := range added {
141145
value = add
@@ -244,6 +248,11 @@ func createPatch(pod *corev1.Pod, sidecarConfigTemplate *Config, clientset *kube
244248
isFirstVol = false
245249
}
246250

251+
// Track how many containers and volumes we've added to the patch
252+
// Start with the count of what already exists in the pod
253+
containersAdded := len(pod.Spec.InitContainers)
254+
volumesAdded := len(pod.Spec.Volumes)
255+
247256
// shareList.Data is a map[string]string
248257
// https://goplay.tools/snippet/zUiIt23ZYVK
249258
var shareList []string
@@ -311,9 +320,14 @@ func createPatch(pod *corev1.Pod, sidecarConfigTemplate *Config, clientset *kube
311320
sidecarConfig.Volumes[1].CSI.VolumeAttributes["fdPassingEmptyDirName"] = fdPassingvolumeMountName
312321

313322
// 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")...)
323+
// Pass containersAdded to determine if this is the first container
324+
patch = append(patch, addContainerTracked(containersAdded, sidecarConfig.Containers, "/spec/initContainers")...)
325+
containersAdded++
326+
327+
// Add volumes - pass volumesAdded count
328+
patch = append(patch, addVolumeTracked(volumesAdded, sidecarConfig.Volumes, "/spec/volumes")...)
329+
volumesAdded += 2 // We add 2 volumes per iteration (fuse-fd-passing and fuse-csi-ephemeral)
330+
317331
patch = append(patch, updateAnnotation(pod.Annotations)...)
318332
patch = append(patch, updateWorkingVolumeMounts(pod.Spec.Containers, csiEphemeralVolumeountName, bucketMount, svmName, isFirstVol)...)
319333
// Add the environment variables

0 commit comments

Comments
 (0)