Skip to content

Commit 0f05e0c

Browse files
committed
fix: do not add barman-certificates projection if not needed
Closes #347 Signed-off-by: Marco Nenciarini <[email protected]>
1 parent 1097abb commit 0f05e0c

File tree

2 files changed

+84
-32
lines changed

2 files changed

+84
-32
lines changed

internal/cnpgi/operator/lifecycle.go

Lines changed: 43 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,7 @@ func reconcilePodSpec(
333333
Drop: []corev1.Capability{"ALL"},
334334
},
335335
}
336+
sidecarTemplate.RestartPolicy = ptr.To(corev1.ContainerRestartPolicyAlways)
336337
sidecarTemplate.Resources = config.resources
337338

338339
// merge the main container envs if they aren't already set
@@ -368,20 +369,28 @@ func reconcilePodSpec(
368369
}
369370
}
370371

371-
if err := injectPluginSidecarPodSpec(spec, &sidecarTemplate, mainContainerName); err != nil {
372-
return err
373-
}
372+
if len(config.certificates) > 0 {
373+
sidecarTemplate.VolumeMounts = append(
374+
sidecarTemplate.VolumeMounts,
375+
corev1.VolumeMount{
376+
Name: barmanCertificatesVolumeName,
377+
MountPath: metadata.BarmanCertificatesPath,
378+
})
374379

375-
// inject the volume containing the certificates if needed
376-
if !volumeListHasVolume(spec.Volumes, barmanCertificatesVolumeName) {
377-
spec.Volumes = append(spec.Volumes, corev1.Volume{
380+
spec.Volumes = volumeListEnsureVolume(spec.Volumes, corev1.Volume{
378381
Name: barmanCertificatesVolumeName,
379382
VolumeSource: corev1.VolumeSource{
380383
Projected: &corev1.ProjectedVolumeSource{
381384
Sources: config.certificates,
382385
},
383386
},
384387
})
388+
} else {
389+
spec.Volumes = volumeListRemoveVolume(spec.Volumes, barmanCertificatesVolumeName)
390+
}
391+
392+
if err := injectPluginSidecarPodSpec(spec, &sidecarTemplate, mainContainerName); err != nil {
393+
return err
385394
}
386395

387396
return nil
@@ -428,10 +437,6 @@ func InjectPluginVolumePodSpec(spec *corev1.PodSpec, mainContainerName string) {
428437
}
429438

430439
// injectPluginSidecarPodSpec injects a plugin sidecar into a CNPG Pod spec.
431-
//
432-
// If the "injectMainContainerVolumes" flag is true, this will append all the volume
433-
// mounts that are used in the instance manager Pod to the passed sidecar
434-
// container, granting it superuser access to the PostgreSQL instance.
435440
func injectPluginSidecarPodSpec(
436441
spec *corev1.PodSpec,
437442
sidecar *corev1.Container,
@@ -440,12 +445,11 @@ func injectPluginSidecarPodSpec(
440445
sidecar = sidecar.DeepCopy()
441446
InjectPluginVolumePodSpec(spec, mainContainerName)
442447

443-
var volumeMounts []corev1.VolumeMount
444448
sidecarContainerFound := false
445449
mainContainerFound := false
446450
for i := range spec.Containers {
447451
if spec.Containers[i].Name == mainContainerName {
448-
volumeMounts = spec.Containers[i].VolumeMounts
452+
sidecar.VolumeMounts = append(sidecar.VolumeMounts, spec.Containers[i].VolumeMounts...)
449453
mainContainerFound = true
450454
}
451455
}
@@ -457,38 +461,45 @@ func injectPluginSidecarPodSpec(
457461
for i := range spec.InitContainers {
458462
if spec.InitContainers[i].Name == sidecar.Name {
459463
sidecarContainerFound = true
464+
spec.InitContainers[i] = *sidecar
460465
}
461466
}
462467

463-
if sidecarContainerFound {
464-
// The sidecar container was already added
465-
return nil
468+
if !sidecarContainerFound {
469+
spec.InitContainers = append(spec.InitContainers, *sidecar)
466470
}
467471

468-
// Do not modify the passed sidecar definition
469-
sidecar.VolumeMounts = append(
470-
sidecar.VolumeMounts,
471-
corev1.VolumeMount{
472-
Name: barmanCertificatesVolumeName,
473-
MountPath: metadata.BarmanCertificatesPath,
474-
})
475-
sidecar.VolumeMounts = append(sidecar.VolumeMounts, volumeMounts...)
476-
sidecar.RestartPolicy = ptr.To(corev1.ContainerRestartPolicyAlways)
477-
spec.InitContainers = append(spec.InitContainers, *sidecar)
478-
479472
return nil
480473
}
481474

482-
// volumeListHasVolume check if a volume with a known name exists
483-
// in the volume list
484-
func volumeListHasVolume(volumes []corev1.Volume, name string) bool {
475+
// volumeListEnsureVolume makes sure the passed volume is present in the list of volumes.
476+
// If the volume is already present, it is updated.
477+
func volumeListEnsureVolume(volumes []corev1.Volume, volume corev1.Volume) []corev1.Volume {
478+
volumeFound := false
485479
for i := range volumes {
486-
if volumes[i].Name == name {
487-
return true
480+
if volumes[i].Name == volume.Name {
481+
volumeFound = true
482+
volumes[i] = volume
488483
}
489484
}
490485

491-
return false
486+
if !volumeFound {
487+
volumes = append(volumes, volume)
488+
}
489+
490+
return volumes
491+
}
492+
493+
// volumeListRemoveVolume removes a volume with a known name from a list of volumes.
494+
func volumeListRemoveVolume(volumes []corev1.Volume, name string) []corev1.Volume {
495+
j := 0
496+
for _, volume := range volumes {
497+
if volume.Name != name {
498+
volumes[j] = volume
499+
j++
500+
}
501+
}
502+
return volumes[:j]
492503
}
493504

494505
// getCNPGJobRole gets the role associated to a CNPG job

internal/cnpgi/operator/lifecycle_test.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,3 +209,44 @@ var _ = Describe("LifecycleImplementation", func() {
209209
})
210210
})
211211
})
212+
213+
var _ = Describe("Volume utilities", func() {
214+
Describe("volumeListEnsureVolume", func() {
215+
It("adds a new volume if not present", func() {
216+
volumes := []corev1.Volume{{Name: "vol1"}}
217+
newVol := corev1.Volume{Name: "vol2"}
218+
result := volumeListEnsureVolume(volumes, newVol)
219+
Expect(result).To(HaveLen(2))
220+
Expect(result[1]).To(Equal(newVol))
221+
})
222+
223+
It("updates an existing volume", func() {
224+
volumes := []corev1.Volume{{Name: "vol1"}}
225+
updatedVol := corev1.Volume{Name: "vol1", VolumeSource: corev1.VolumeSource{EmptyDir: &corev1.EmptyDirVolumeSource{}}}
226+
result := volumeListEnsureVolume(volumes, updatedVol)
227+
Expect(result).To(HaveLen(1))
228+
Expect(result[0]).To(Equal(updatedVol))
229+
})
230+
})
231+
232+
Describe("volumeListRemoveVolume", func() {
233+
It("removes the specified volume", func() {
234+
volumes := []corev1.Volume{
235+
{Name: "vol1"},
236+
{Name: "vol2"},
237+
{Name: "vol3"},
238+
}
239+
result := volumeListRemoveVolume(volumes, "vol2")
240+
Expect(result).To(HaveLen(2))
241+
for _, v := range result {
242+
Expect(v.Name).NotTo(Equal("vol2"))
243+
}
244+
})
245+
246+
It("returns the same list if the volume is not present", func() {
247+
volumes := []corev1.Volume{{Name: "vol1"}, {Name: "vol2"}}
248+
result := volumeListRemoveVolume(volumes, "vol3")
249+
Expect(result).To(Equal(volumes))
250+
})
251+
})
252+
})

0 commit comments

Comments
 (0)