@@ -78,28 +78,43 @@ func addBucketsToPodSpec(podspec *corev1.PodSpec, container *corev1.Container, b
7878 container .VolumeMounts = append (container .VolumeMounts , volumeMounts ... )
7979 }
8080
81- modified := len ( volumes ) > 0 || len ( volumeMounts ) > 0
81+ precreatorModified := addOrUpdatePrecreator ( podspec , bucketMounts , precreatorImage )
8282
83- if modified {
84- precreator := corev1.Container {
85- Image : precreatorImage ,
86- Name : precreatorContainerName ,
87- }
88- for mountPoint , bucket := range bucketMounts {
89- volumeName := maxLengthVolumeName (mountPoint )
90- precreator .VolumeMounts = append (precreator .VolumeMounts , corev1.VolumeMount {
91- Name : volumeName ,
92- MountPath : fmt .Sprintf ("/buckets/%s" , bucket ),
93- })
83+ return precreatorModified || len (volumes ) > 0 || len (volumeMounts ) > 0
84+ }
85+
86+ func addOrUpdatePrecreator (podspec * corev1.PodSpec , bucketMounts map [string ]string , precreatorImage string ) (modified bool ) {
87+ precreator := corev1.Container {
88+ Image : precreatorImage ,
89+ Name : precreatorContainerName ,
90+ }
91+ volumeMounts := make (map [string ]corev1.VolumeMount , len (bucketMounts ))
92+ for _ , bucket := range bucketMounts {
93+ volumeName := maxLengthVolumeName (bucket )
94+ volumeMounts [volumeName ] = corev1.VolumeMount {
95+ Name : volumeName ,
96+ MountPath : fmt .Sprintf ("/buckets/%s" , bucket ),
9497 }
95- if ! slices .ContainsFunc (podspec .Containers , func (c corev1.Container ) bool {
96- return c .Name == precreator .Name
97- }) {
98- podspec .Containers = append (podspec .Containers , precreator )
98+ }
99+ for _ , mount := range volumeMounts {
100+ precreator .VolumeMounts = append (precreator .VolumeMounts , mount )
101+ }
102+ for _ , c := range podspec .Containers {
103+ if c .Name == precreator .Name {
104+ if c .Image != precreatorImage ||
105+ ! slices .EqualFunc (precreator .VolumeMounts , c .VolumeMounts , func (a , b corev1.VolumeMount ) bool {
106+ return a .Name == b .Name
107+ }) {
108+ c .Image = precreatorImage
109+ c .VolumeMounts = precreator .VolumeMounts
110+ return true
111+ }
112+ return false
99113 }
100114 }
101115
102- return modified
116+ podspec .Containers = append (podspec .Containers , precreator )
117+ return true
103118}
104119
105120func removeBucketsFromPodSpec (podspec * corev1.PodSpec , container * corev1.Container ) (shouldUpdate bool ) {
0 commit comments