@@ -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.
435440func 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
0 commit comments