This repository was archived by the owner on Dec 12, 2025. It is now read-only.
File tree Expand file tree Collapse file tree 2 files changed +49
-0
lines changed
Expand file tree Collapse file tree 2 files changed +49
-0
lines changed Original file line number Diff line number Diff line change @@ -107,6 +107,22 @@ func WithServiceAccount(serviceAccountName string) Modification {
107107 }
108108}
109109
110+ // WithVolumes appends the given volumes to the existing volume, it ensures not
111+ // to override existing volumes
112+ func WithVolumes (volumes []corev1.Volume ) Modification {
113+ return func (template * corev1.PodTemplateSpec ) {
114+ present := make (map [string ]struct {})
115+ for _ , v := range template .Spec .Volumes {
116+ present [v .Name ] = struct {}{}
117+ }
118+ for _ , v := range volumes {
119+ if _ , ok := present [v .Name ]; ! ok {
120+ template .Spec .Volumes = append (template .Spec .Volumes , v )
121+ }
122+ }
123+ }
124+ }
125+
110126// WithVolume ensures the given volume exists
111127func WithVolume (volume corev1.Volume ) Modification {
112128 return func (template * corev1.PodTemplateSpec ) {
Original file line number Diff line number Diff line change @@ -341,6 +341,39 @@ func TestMergeVolumes_DoesNotAddDuplicatesWithSameName(t *testing.T) {
341341 assert .Equal (t , "new-volume-3" , mergedPodSpecTemplate .Spec .Volumes [2 ].Name )
342342}
343343
344+ func TestAddVolumes (t * testing.T ) {
345+ volumeModification := WithVolume (corev1.Volume {
346+ Name : "new-volume" ,
347+ VolumeSource : corev1.VolumeSource {
348+ HostPath : & corev1.HostPathVolumeSource {
349+ Path : "old-host-path" ,
350+ },
351+ }},
352+ )
353+
354+ toAddVolumes := []corev1.Volume {
355+ {
356+ Name : "new-volume" ,
357+ VolumeSource : corev1.VolumeSource {
358+ HostPath : & corev1.HostPathVolumeSource {
359+ Path : "new-host-path" ,
360+ },
361+ },
362+ },
363+ {
364+ Name : "new-volume-2" ,
365+ },
366+ }
367+
368+ volumesModification := WithVolumes (toAddVolumes )
369+
370+ p := New (volumeModification , volumesModification )
371+ assert .Len (t , p .Spec .Volumes , 2 )
372+ assert .Equal (t , p .Spec .Volumes [0 ].Name , "new-volume" )
373+ assert .Equal (t , p .Spec .Volumes [1 ].Name , "new-volume-2" )
374+
375+ }
376+
344377func int64Ref (i int64 ) * int64 {
345378 return & i
346379}
You can’t perform that action at this time.
0 commit comments