@@ -14,11 +14,9 @@ import (
1414 "github.com/spf13/viper"
1515 batchv1 "k8s.io/api/batch/v1"
1616 corev1 "k8s.io/api/core/v1"
17- "k8s.io/apimachinery/pkg/types"
1817 "k8s.io/utils/ptr"
1918 "sigs.k8s.io/controller-runtime/pkg/client"
2019
21- barmancloudv1 "github.com/cloudnative-pg/plugin-barman-cloud/api/v1"
2220 "github.com/cloudnative-pg/plugin-barman-cloud/internal/cnpgi/metadata"
2321 "github.com/cloudnative-pg/plugin-barman-cloud/internal/cnpgi/operator/config"
2422)
@@ -107,56 +105,6 @@ func (impl LifecycleImplementation) LifecycleHook(
107105 }
108106}
109107
110- func (impl LifecycleImplementation ) collectAdditionalEnvs (
111- ctx context.Context ,
112- namespace string ,
113- pluginConfiguration * config.PluginConfiguration ,
114- ) ([]corev1.EnvVar , error ) {
115- var result []corev1.EnvVar
116-
117- if len (pluginConfiguration .BarmanObjectName ) > 0 {
118- envs , err := impl .collectObjectStoreEnvs (
119- ctx ,
120- types.NamespacedName {
121- Name : pluginConfiguration .BarmanObjectName ,
122- Namespace : namespace ,
123- },
124- )
125- if err != nil {
126- return nil , err
127- }
128- result = append (result , envs ... )
129- }
130-
131- if len (pluginConfiguration .RecoveryBarmanObjectName ) > 0 {
132- envs , err := impl .collectObjectStoreEnvs (
133- ctx ,
134- types.NamespacedName {
135- Name : pluginConfiguration .RecoveryBarmanObjectName ,
136- Namespace : namespace ,
137- },
138- )
139- if err != nil {
140- return nil , err
141- }
142- result = append (result , envs ... )
143- }
144-
145- return result , nil
146- }
147-
148- func (impl LifecycleImplementation ) collectObjectStoreEnvs (
149- ctx context.Context ,
150- barmanObjectKey types.NamespacedName ,
151- ) ([]corev1.EnvVar , error ) {
152- var objectStore barmancloudv1.ObjectStore
153- if err := impl .Client .Get (ctx , barmanObjectKey , & objectStore ); err != nil {
154- return nil , err
155- }
156-
157- return objectStore .Spec .InstanceSidecarConfiguration .Env , nil
158- }
159-
160108func (impl LifecycleImplementation ) reconcileJob (
161109 ctx context.Context ,
162110 cluster * cnpgv1.Cluster ,
@@ -165,17 +113,23 @@ func (impl LifecycleImplementation) reconcileJob(
165113) (* lifecycle.OperatorLifecycleResponse , error ) {
166114 env , err := impl .collectAdditionalEnvs (ctx , cluster .Namespace , pluginConfiguration )
167115 if err != nil {
168- return nil , nil
116+ return nil , err
169117 }
170118
171- return reconcileJob (ctx , cluster , request , env )
119+ certificates , err := impl .collectAdditionalCertificates (ctx , cluster .Namespace , pluginConfiguration )
120+ if err != nil {
121+ return nil , err
122+ }
123+
124+ return reconcileJob (ctx , cluster , request , env , certificates )
172125}
173126
174127func reconcileJob (
175128 ctx context.Context ,
176129 cluster * cnpgv1.Cluster ,
177130 request * lifecycle.OperatorLifecycleRequest ,
178131 env []corev1.EnvVar ,
132+ certificates []corev1.VolumeProjection ,
179133) (* lifecycle.OperatorLifecycleResponse , error ) {
180134 contextLogger := log .FromContext (ctx ).WithName ("lifecycle" )
181135 if pluginConfig := cluster .GetRecoverySourcePlugin (); pluginConfig == nil || pluginConfig .Name != metadata .PluginName {
@@ -212,6 +166,7 @@ func reconcileJob(
212166 Args : []string {"restore" },
213167 },
214168 env ,
169+ certificates ,
215170 ); err != nil {
216171 return nil , fmt .Errorf ("while reconciling pod spec for job: %w" , err )
217172 }
@@ -235,10 +190,15 @@ func (impl LifecycleImplementation) reconcilePod(
235190) (* lifecycle.OperatorLifecycleResponse , error ) {
236191 env , err := impl .collectAdditionalEnvs (ctx , cluster .Namespace , pluginConfiguration )
237192 if err != nil {
238- return nil , nil
193+ return nil , err
194+ }
195+
196+ certificates , err := impl .collectAdditionalCertificates (ctx , cluster .Namespace , pluginConfiguration )
197+ if err != nil {
198+ return nil , err
239199 }
240200
241- return reconcilePod (ctx , cluster , request , pluginConfiguration , env )
201+ return reconcilePod (ctx , cluster , request , pluginConfiguration , env , certificates )
242202}
243203
244204func reconcilePod (
@@ -247,6 +207,7 @@ func reconcilePod(
247207 request * lifecycle.OperatorLifecycleRequest ,
248208 pluginConfiguration * config.PluginConfiguration ,
249209 env []corev1.EnvVar ,
210+ certificates []corev1.VolumeProjection ,
250211) (* lifecycle.OperatorLifecycleResponse , error ) {
251212 pod , err := decoder .DecodePodJSON (request .GetObjectDefinition ())
252213 if err != nil {
@@ -267,6 +228,7 @@ func reconcilePod(
267228 Args : []string {"instance" },
268229 },
269230 env ,
231+ certificates ,
270232 ); err != nil {
271233 return nil , fmt .Errorf ("while reconciling pod spec for pod: %w" , err )
272234 }
@@ -291,6 +253,7 @@ func reconcilePodSpec(
291253 mainContainerName string ,
292254 sidecarConfig corev1.Container ,
293255 additionalEnvs []corev1.EnvVar ,
256+ certificates []corev1.VolumeProjection ,
294257) error {
295258 envs := []corev1.EnvVar {
296259 {
@@ -360,10 +323,22 @@ func reconcilePodSpec(
360323 }
361324 }
362325
363- if err := InjectPluginSidecarPodSpec (spec , & sidecarConfig , mainContainerName , true ); err != nil {
326+ if err := injectPluginSidecarPodSpec (spec , & sidecarConfig , mainContainerName ); err != nil {
364327 return err
365328 }
366329
330+ // inject the volume containing the certificates if needed
331+ if ! volumeListHasVolume (spec .Volumes , barmanCertificatesVolumeName ) {
332+ spec .Volumes = append (spec .Volumes , corev1.Volume {
333+ Name : barmanCertificatesVolumeName ,
334+ VolumeSource : corev1.VolumeSource {
335+ Projected : & corev1.ProjectedVolumeSource {
336+ Sources : certificates ,
337+ },
338+ },
339+ })
340+ }
341+
367342 return nil
368343}
369344
@@ -407,16 +382,15 @@ func InjectPluginVolumePodSpec(spec *corev1.PodSpec, mainContainerName string) {
407382 }
408383}
409384
410- // InjectPluginSidecarPodSpec injects a plugin sidecar into a CNPG Pod spec.
385+ // injectPluginSidecarPodSpec injects a plugin sidecar into a CNPG Pod spec.
411386//
412387// If the "injectMainContainerVolumes" flag is true, this will append all the volume
413388// mounts that are used in the instance manager Pod to the passed sidecar
414389// container, granting it superuser access to the PostgreSQL instance.
415- func InjectPluginSidecarPodSpec (
390+ func injectPluginSidecarPodSpec (
416391 spec * corev1.PodSpec ,
417392 sidecar * corev1.Container ,
418393 mainContainerName string ,
419- injectMainContainerVolumes bool ,
420394) error {
421395 sidecar = sidecar .DeepCopy ()
422396 InjectPluginVolumePodSpec (spec , mainContainerName )
@@ -447,11 +421,27 @@ func InjectPluginSidecarPodSpec(
447421 }
448422
449423 // Do not modify the passed sidecar definition
450- if injectMainContainerVolumes {
451- sidecar .VolumeMounts = append (sidecar .VolumeMounts , volumeMounts ... )
452- }
424+ sidecar .VolumeMounts = append (
425+ sidecar .VolumeMounts ,
426+ corev1.VolumeMount {
427+ Name : barmanCertificatesVolumeName ,
428+ MountPath : metadata .BarmanCertificatesPath ,
429+ })
430+ sidecar .VolumeMounts = append (sidecar .VolumeMounts , volumeMounts ... )
453431 sidecar .RestartPolicy = ptr .To (corev1 .ContainerRestartPolicyAlways )
454432 spec .InitContainers = append (spec .InitContainers , * sidecar )
455433
456434 return nil
457435}
436+
437+ // volumeListHasVolume check if a volume with a known name exists
438+ // in the volume list
439+ func volumeListHasVolume (volumes []corev1.Volume , name string ) bool {
440+ for i := range volumes {
441+ if volumes [i ].Name == name {
442+ return true
443+ }
444+ }
445+
446+ return false
447+ }
0 commit comments