@@ -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,29 @@ 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 = ensureVolumeMount (
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 = ensureVolume (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+ sidecarTemplate .VolumeMounts = removeVolumeMount (sidecarTemplate .VolumeMounts , barmanCertificatesVolumeName )
390+ spec .Volumes = removeVolume (spec .Volumes , barmanCertificatesVolumeName )
391+ }
392+
393+ if err := injectPluginSidecarPodSpec (spec , & sidecarTemplate , mainContainerName ); err != nil {
394+ return err
385395 }
386396
387397 return nil
@@ -407,7 +417,7 @@ func InjectPluginVolumePodSpec(spec *corev1.PodSpec, mainContainerName string) {
407417 return
408418 }
409419
410- spec .Volumes = append (spec .Volumes , corev1.Volume {
420+ spec .Volumes = ensureVolume (spec .Volumes , corev1.Volume {
411421 Name : pluginVolumeName ,
412422 VolumeSource : corev1.VolumeSource {
413423 EmptyDir : & corev1.EmptyDirVolumeSource {},
@@ -416,7 +426,7 @@ func InjectPluginVolumePodSpec(spec *corev1.PodSpec, mainContainerName string) {
416426
417427 for i := range spec .Containers {
418428 if spec .Containers [i ].Name == mainContainerName {
419- spec .Containers [i ].VolumeMounts = append (
429+ spec .Containers [i ].VolumeMounts = ensureVolumeMount (
420430 spec .Containers [i ].VolumeMounts ,
421431 corev1.VolumeMount {
422432 Name : pluginVolumeName ,
@@ -428,10 +438,6 @@ func InjectPluginVolumePodSpec(spec *corev1.PodSpec, mainContainerName string) {
428438}
429439
430440// 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.
435441func injectPluginSidecarPodSpec (
436442 spec * corev1.PodSpec ,
437443 sidecar * corev1.Container ,
@@ -440,12 +446,11 @@ func injectPluginSidecarPodSpec(
440446 sidecar = sidecar .DeepCopy ()
441447 InjectPluginVolumePodSpec (spec , mainContainerName )
442448
443- var volumeMounts []corev1.VolumeMount
444449 sidecarContainerFound := false
445450 mainContainerFound := false
446451 for i := range spec .Containers {
447452 if spec .Containers [i ].Name == mainContainerName {
448- volumeMounts = spec .Containers [i ].VolumeMounts
453+ sidecar . VolumeMounts = ensureVolumeMount ( sidecar . VolumeMounts , spec .Containers [i ].VolumeMounts ... )
449454 mainContainerFound = true
450455 }
451456 }
@@ -457,38 +462,75 @@ func injectPluginSidecarPodSpec(
457462 for i := range spec .InitContainers {
458463 if spec .InitContainers [i ].Name == sidecar .Name {
459464 sidecarContainerFound = true
465+ spec .InitContainers [i ] = * sidecar
460466 }
461467 }
462468
463- if sidecarContainerFound {
464- // The sidecar container was already added
465- return nil
469+ if ! sidecarContainerFound {
470+ spec .InitContainers = append (spec .InitContainers , * sidecar )
466471 }
467472
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-
479473 return nil
480474}
481475
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 {
476+ // ensureVolume makes sure the passed volume is present in the list of volumes.
477+ // If the volume is already present, it is updated.
478+ func ensureVolume (volumes []corev1.Volume , volume corev1.Volume ) []corev1.Volume {
479+ volumeFound := false
485480 for i := range volumes {
486- if volumes [i ].Name == name {
487- return true
481+ if volumes [i ].Name == volume .Name {
482+ volumeFound = true
483+ volumes [i ] = volume
488484 }
489485 }
490486
491- return false
487+ if ! volumeFound {
488+ volumes = append (volumes , volume )
489+ }
490+
491+ return volumes
492+ }
493+
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+
515+ // removeVolume removes a volume with a known name from a list of volumes.
516+ func removeVolume (volumes []corev1.Volume , name string ) []corev1.Volume {
517+ var filteredVolumes []corev1.Volume
518+ for _ , volume := range volumes {
519+ if volume .Name != name {
520+ filteredVolumes = append (filteredVolumes , volume )
521+ }
522+ }
523+ return filteredVolumes
524+ }
525+
526+ func removeVolumeMount (mounts []corev1.VolumeMount , name string ) []corev1.VolumeMount {
527+ var filteredMounts []corev1.VolumeMount
528+ for _ , mount := range mounts {
529+ if mount .Name != name {
530+ filteredMounts = append (filteredMounts , mount )
531+ }
532+ }
533+ return filteredMounts
492534}
493535
494536// getCNPGJobRole gets the role associated to a CNPG job
0 commit comments