@@ -14,15 +14,19 @@ 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"
1718 "k8s.io/utils/ptr"
19+ "sigs.k8s.io/controller-runtime/pkg/client"
1820
21+ barmancloudv1 "github.com/cloudnative-pg/plugin-barman-cloud/api/v1"
1922 "github.com/cloudnative-pg/plugin-barman-cloud/internal/cnpgi/metadata"
2023 "github.com/cloudnative-pg/plugin-barman-cloud/internal/cnpgi/operator/config"
2124)
2225
2326// LifecycleImplementation is the implementation of the lifecycle handler
2427type LifecycleImplementation struct {
2528 lifecycle.UnimplementedOperatorLifecycleServer
29+ Client client.Client
2630}
2731
2832// GetCapabilities exposes the lifecycle capabilities
@@ -94,20 +98,85 @@ func (impl LifecycleImplementation) LifecycleHook(
9498 switch kind {
9599 case "Pod" :
96100 contextLogger .Info ("Reconciling pod" )
97- return reconcilePod (ctx , & cluster , request , pluginConfiguration )
101+ return impl . reconcilePod (ctx , & cluster , request , pluginConfiguration )
98102 case "Job" :
99103 contextLogger .Info ("Reconciling job" )
100- return reconcileJob (ctx , & cluster , request , pluginConfiguration )
104+ return impl . reconcileJob (ctx , & cluster , request , pluginConfiguration )
101105 default :
102106 return nil , fmt .Errorf ("unsupported kind: %s" , kind )
103107 }
104108}
105109
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+
160+ func (impl LifecycleImplementation ) reconcileJob (
161+ ctx context.Context ,
162+ cluster * cnpgv1.Cluster ,
163+ request * lifecycle.OperatorLifecycleRequest ,
164+ pluginConfiguration * config.PluginConfiguration ,
165+ ) (* lifecycle.OperatorLifecycleResponse , error ) {
166+ env , err := impl .collectAdditionalEnvs (ctx , cluster .Namespace , pluginConfiguration )
167+ if err != nil {
168+ return nil , nil
169+ }
170+
171+ return reconcileJob (ctx , cluster , request , pluginConfiguration , env )
172+ }
173+
106174func reconcileJob (
107175 ctx context.Context ,
108176 cluster * cnpgv1.Cluster ,
109177 request * lifecycle.OperatorLifecycleRequest ,
110178 pluginConfiguration * config.PluginConfiguration ,
179+ env []corev1.EnvVar ,
111180) (* lifecycle.OperatorLifecycleResponse , error ) {
112181 contextLogger := log .FromContext (ctx ).WithName ("lifecycle" )
113182 if pluginConfig := cluster .GetRecoverySourcePlugin (); pluginConfig == nil || pluginConfig .Name != metadata .PluginName {
@@ -144,6 +213,7 @@ func reconcileJob(
144213 corev1.Container {
145214 Args : []string {"restore" },
146215 },
216+ env ,
147217 ); err != nil {
148218 return nil , fmt .Errorf ("while reconciling pod spec for job: %w" , err )
149219 }
@@ -159,11 +229,26 @@ func reconcileJob(
159229 }, nil
160230}
161231
232+ func (impl LifecycleImplementation ) reconcilePod (
233+ ctx context.Context ,
234+ cluster * cnpgv1.Cluster ,
235+ request * lifecycle.OperatorLifecycleRequest ,
236+ pluginConfiguration * config.PluginConfiguration ,
237+ ) (* lifecycle.OperatorLifecycleResponse , error ) {
238+ env , err := impl .collectAdditionalEnvs (ctx , cluster .Namespace , pluginConfiguration )
239+ if err != nil {
240+ return nil , nil
241+ }
242+
243+ return reconcilePod (ctx , cluster , request , pluginConfiguration , env )
244+ }
245+
162246func reconcilePod (
163247 ctx context.Context ,
164248 cluster * cnpgv1.Cluster ,
165249 request * lifecycle.OperatorLifecycleRequest ,
166250 pluginConfiguration * config.PluginConfiguration ,
251+ env []corev1.EnvVar ,
167252) (* lifecycle.OperatorLifecycleResponse , error ) {
168253 pod , err := decoder .DecodePodJSON (request .GetObjectDefinition ())
169254 if err != nil {
@@ -176,9 +261,16 @@ func reconcilePod(
176261 mutatedPod := pod .DeepCopy ()
177262
178263 if len (pluginConfiguration .BarmanObjectName ) != 0 {
179- if err := reconcilePodSpec (pluginConfiguration , cluster , & mutatedPod .Spec , "postgres" , corev1.Container {
180- Args : []string {"instance" },
181- }); err != nil {
264+ if err := reconcilePodSpec (
265+ pluginConfiguration ,
266+ cluster ,
267+ & mutatedPod .Spec ,
268+ "postgres" ,
269+ corev1.Container {
270+ Args : []string {"instance" },
271+ },
272+ env ,
273+ ); err != nil {
182274 return nil , fmt .Errorf ("while reconciling pod spec for pod: %w" , err )
183275 }
184276 } else {
@@ -202,6 +294,7 @@ func reconcilePodSpec(
202294 spec * corev1.PodSpec ,
203295 mainContainerName string ,
204296 sidecarConfig corev1.Container ,
297+ additionalEnvs []corev1.EnvVar ,
205298) error {
206299 envs := []corev1.EnvVar {
207300 {
@@ -246,6 +339,8 @@ func reconcilePodSpec(
246339 )
247340 }
248341
342+ envs = append (envs , additionalEnvs ... )
343+
249344 baseProbe := & corev1.Probe {
250345 FailureThreshold : 3 ,
251346 ProbeHandler : corev1.ProbeHandler {
0 commit comments