Skip to content

Commit 35b8c2b

Browse files
committed
Share logic between AddAdditionalVolumesAndMounts and AddVolumeAndMountsToPod
1 parent 2871c31 commit 35b8c2b

File tree

1 file changed

+25
-32
lines changed

1 file changed

+25
-32
lines changed

internal/util/volumes.go

Lines changed: 25 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,34 @@ func AdditionalVolumeMount(name string, readOnly bool) corev1.VolumeMount {
2525
// AddAdditionalVolumesAndMounts adds volumes as [corev1.Volume]s and [corev1.VolumeMount]s in pod.
2626
// Volume names are chosen in [AdditionalVolumeMount].
2727
func AddAdditionalVolumesAndMounts(pod *corev1.PodSpec, volumes []v1beta1.AdditionalVolume) []string {
28+
return addVolumesAndMounts(pod, volumes, AdditionalVolumeMount)
29+
}
30+
31+
// AddVolumeAndMountsToPod takes a Pod spec and a PVC and adds a Volume to the Pod spec with
32+
// the PVC as the VolumeSource and mounts the volume to all containers and init containers
33+
// in the Pod spec.
34+
func AddVolumeAndMountsToPod(podSpec *corev1.PodSpec, volume *corev1.PersistentVolumeClaim) {
35+
additional := []v1beta1.AdditionalVolume{{
36+
ClaimName: volume.Name,
37+
Name: volume.Name,
38+
ReadOnly: false,
39+
}}
40+
41+
addVolumesAndMounts(podSpec, additional, func(string, bool) corev1.VolumeMount {
42+
return corev1.VolumeMount{
43+
// This name has no prefix and differs from [AdditionalVolumeMount].
44+
Name: volume.Name,
45+
MountPath: fmt.Sprintf("/volumes/%s", volume.Name),
46+
ReadOnly: false,
47+
}
48+
})
49+
}
50+
51+
func addVolumesAndMounts(pod *corev1.PodSpec, volumes []v1beta1.AdditionalVolume, namer func(string, bool) corev1.VolumeMount) []string {
2852
missingContainers := []string{}
2953

3054
for _, spec := range volumes {
31-
mount := AdditionalVolumeMount(spec.Name, spec.ReadOnly)
55+
mount := namer(spec.Name, spec.ReadOnly)
3256
pod.Volumes = append(pod.Volumes, spec.AsVolume(mount.Name))
3357

3458
// Create a set of all the requested containers,
@@ -60,34 +84,3 @@ func AddAdditionalVolumesAndMounts(pod *corev1.PodSpec, volumes []v1beta1.Additi
6084

6185
return missingContainers
6286
}
63-
64-
// AddVolumeAndMountsToPod takes a Pod spec and a PVC and adds a Volume to the Pod spec with
65-
// the PVC as the VolumeSource and mounts the volume to all containers and init containers
66-
// in the Pod spec.
67-
func AddVolumeAndMountsToPod(podSpec *corev1.PodSpec, volume *corev1.PersistentVolumeClaim) {
68-
69-
podSpec.Volumes = append(podSpec.Volumes, corev1.Volume{
70-
Name: volume.Name,
71-
VolumeSource: corev1.VolumeSource{
72-
PersistentVolumeClaim: &corev1.PersistentVolumeClaimVolumeSource{
73-
ClaimName: volume.Name,
74-
},
75-
},
76-
})
77-
78-
for i := range podSpec.Containers {
79-
podSpec.Containers[i].VolumeMounts = append(podSpec.Containers[i].VolumeMounts,
80-
corev1.VolumeMount{
81-
Name: volume.Name,
82-
MountPath: fmt.Sprintf("/volumes/%s", volume.Name),
83-
})
84-
}
85-
86-
for i := range podSpec.InitContainers {
87-
podSpec.InitContainers[i].VolumeMounts = append(podSpec.InitContainers[i].VolumeMounts,
88-
corev1.VolumeMount{
89-
Name: volume.Name,
90-
MountPath: fmt.Sprintf("/volumes/%s", volume.Name),
91-
})
92-
}
93-
}

0 commit comments

Comments
 (0)