Skip to content

Commit d247858

Browse files
committed
chore: consistency
Signed-off-by: Armando Ruocco <[email protected]>
1 parent 25f4dc3 commit d247858

File tree

2 files changed

+78
-4
lines changed

2 files changed

+78
-4
lines changed

internal/cnpgi/operator/lifecycle.go

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,7 @@ func reconcilePodSpec(
370370
}
371371

372372
if len(config.certificates) > 0 {
373-
sidecarTemplate.VolumeMounts = append(
373+
sidecarTemplate.VolumeMounts = ensureVolumeMount(
374374
sidecarTemplate.VolumeMounts,
375375
corev1.VolumeMount{
376376
Name: barmanCertificatesVolumeName,
@@ -417,7 +417,7 @@ func InjectPluginVolumePodSpec(spec *corev1.PodSpec, mainContainerName string) {
417417
return
418418
}
419419

420-
spec.Volumes = append(spec.Volumes, corev1.Volume{
420+
spec.Volumes = ensureVolume(spec.Volumes, corev1.Volume{
421421
Name: pluginVolumeName,
422422
VolumeSource: corev1.VolumeSource{
423423
EmptyDir: &corev1.EmptyDirVolumeSource{},
@@ -426,7 +426,7 @@ func InjectPluginVolumePodSpec(spec *corev1.PodSpec, mainContainerName string) {
426426

427427
for i := range spec.Containers {
428428
if spec.Containers[i].Name == mainContainerName {
429-
spec.Containers[i].VolumeMounts = append(
429+
spec.Containers[i].VolumeMounts = ensureVolumeMount(
430430
spec.Containers[i].VolumeMounts,
431431
corev1.VolumeMount{
432432
Name: pluginVolumeName,
@@ -450,7 +450,7 @@ func injectPluginSidecarPodSpec(
450450
mainContainerFound := false
451451
for i := range spec.Containers {
452452
if spec.Containers[i].Name == mainContainerName {
453-
sidecar.VolumeMounts = append(sidecar.VolumeMounts, spec.Containers[i].VolumeMounts...)
453+
sidecar.VolumeMounts = ensureVolumeMount(sidecar.VolumeMounts, spec.Containers[i].VolumeMounts...)
454454
mainContainerFound = true
455455
}
456456
}
@@ -491,6 +491,27 @@ func ensureVolume(volumes []corev1.Volume, volume corev1.Volume) []corev1.Volume
491491
return volumes
492492
}
493493

494+
// ensureVolumeMount makes sure the passed volume mounts are present in the list of volume mounts.
495+
// If a volume mount is already present, it is updated.
496+
func ensureVolumeMount(mounts []corev1.VolumeMount, volumeMounts ...corev1.VolumeMount) []corev1.VolumeMount {
497+
for _, mount := range volumeMounts {
498+
mountFound := false
499+
for i := range mounts {
500+
if mounts[i].Name == mount.Name {
501+
mountFound = true
502+
mounts[i] = mount
503+
break
504+
}
505+
}
506+
507+
if !mountFound {
508+
mounts = append(mounts, mount)
509+
}
510+
}
511+
512+
return mounts
513+
}
514+
494515
// removeVolume removes a volume with a known name from a list of volumes.
495516
func removeVolume(volumes []corev1.Volume, name string) []corev1.Volume {
496517
var filteredVolumes []corev1.Volume

internal/cnpgi/operator/lifecycle_test.go

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,3 +279,56 @@ var _ = Describe("removeVolumeMount", func() {
279279
Expect(result).To(BeEmpty())
280280
})
281281
})
282+
283+
var _ = Describe("ensureVolumeMount", func() {
284+
It("adds a new volume mount to an empty list", func() {
285+
var mounts []corev1.VolumeMount
286+
newMount := corev1.VolumeMount{Name: "mount1", MountPath: "/path1"}
287+
result := ensureVolumeMount(mounts, newMount)
288+
Expect(result).To(HaveLen(1))
289+
Expect(result[0]).To(Equal(newMount))
290+
})
291+
292+
It("adds a new volume mount to a non-empty list", func() {
293+
mounts := []corev1.VolumeMount{{Name: "mount1", MountPath: "/path1"}}
294+
newMount := corev1.VolumeMount{Name: "mount2", MountPath: "/path2"}
295+
result := ensureVolumeMount(mounts, newMount)
296+
Expect(result).To(HaveLen(2))
297+
Expect(result[0].Name).To(Equal("mount1"))
298+
Expect(result[1].Name).To(Equal("mount2"))
299+
})
300+
301+
It("updates an existing volume mount", func() {
302+
mounts := []corev1.VolumeMount{{Name: "mount1", MountPath: "/path1"}}
303+
updatedMount := corev1.VolumeMount{Name: "mount1", MountPath: "/new-path"}
304+
result := ensureVolumeMount(mounts, updatedMount)
305+
Expect(result).To(HaveLen(1))
306+
Expect(result[0].MountPath).To(Equal("/new-path"))
307+
})
308+
309+
It("adds multiple new volume mounts", func() {
310+
mounts := []corev1.VolumeMount{{Name: "mount1", MountPath: "/path1"}}
311+
newMount1 := corev1.VolumeMount{Name: "mount2", MountPath: "/path2"}
312+
newMount2 := corev1.VolumeMount{Name: "mount3", MountPath: "/path3"}
313+
result := ensureVolumeMount(mounts, newMount1, newMount2)
314+
Expect(result).To(HaveLen(3))
315+
Expect(result[0].Name).To(Equal("mount1"))
316+
Expect(result[1].Name).To(Equal("mount2"))
317+
Expect(result[2].Name).To(Equal("mount3"))
318+
})
319+
320+
It("handles a mix of new and existing volume mounts", func() {
321+
mounts := []corev1.VolumeMount{
322+
{Name: "mount1", MountPath: "/path1"},
323+
{Name: "mount2", MountPath: "/path2"},
324+
}
325+
updatedMount := corev1.VolumeMount{Name: "mount1", MountPath: "/new-path"}
326+
newMount := corev1.VolumeMount{Name: "mount3", MountPath: "/path3"}
327+
result := ensureVolumeMount(mounts, updatedMount, newMount)
328+
Expect(result).To(HaveLen(3))
329+
Expect(result[0].Name).To(Equal("mount1"))
330+
Expect(result[0].MountPath).To(Equal("/new-path"))
331+
Expect(result[1].Name).To(Equal("mount2"))
332+
Expect(result[2].Name).To(Equal("mount3"))
333+
})
334+
})

0 commit comments

Comments
 (0)