@@ -17,6 +17,7 @@ import (
1717 "k8s.io/utils/ptr"
1818 "sigs.k8s.io/controller-runtime/pkg/client"
1919
20+ barmancloudv1 "github.com/cloudnative-pg/plugin-barman-cloud/api/v1"
2021 "github.com/cloudnative-pg/plugin-barman-cloud/internal/cnpgi/metadata"
2122 "github.com/cloudnative-pg/plugin-barman-cloud/internal/cnpgi/operator/config"
2223)
@@ -123,23 +124,36 @@ func (impl LifecycleImplementation) reconcileJob(
123124 return nil , err
124125 }
125126
126- return reconcileJob (ctx , cluster , request , env , certificates )
127+ resources , err := impl .collectSidecarResources (ctx , pluginConfiguration )
128+ if err != nil {
129+ return nil , err
130+ }
131+
132+ return reconcileJob (ctx , cluster , request , sidecarConfiguration {
133+ env : env ,
134+ certificates : certificates ,
135+ resources : resources ,
136+ })
137+ }
138+
139+ type sidecarConfiguration struct {
140+ env []corev1.EnvVar
141+ certificates []corev1.VolumeProjection
142+ resources corev1.ResourceRequirements
127143}
128144
129145func reconcileJob (
130146 ctx context.Context ,
131147 cluster * cnpgv1.Cluster ,
132148 request * lifecycle.OperatorLifecycleRequest ,
133- env []corev1.EnvVar ,
134- certificates []corev1.VolumeProjection ,
149+ config sidecarConfiguration ,
135150) (* lifecycle.OperatorLifecycleResponse , error ) {
136151 contextLogger := log .FromContext (ctx ).WithName ("lifecycle" )
137152 if pluginConfig := cluster .GetRecoverySourcePlugin (); pluginConfig == nil || pluginConfig .Name != metadata .PluginName {
138153 contextLogger .Debug ("cluster does not use the this plugin for recovery, skipping" )
139154 return nil , nil
140155 }
141156
142- var barmanObjectStore barmancloudv1.ObjectStore
143157 var job batchv1.Job
144158 if err := decoder .DecodeObjectStrict (
145159 request .GetObjectDefinition (),
@@ -170,9 +184,7 @@ func reconcileJob(
170184 corev1.Container {
171185 Args : []string {"restore" },
172186 },
173- env ,
174- certificates ,
175- barmanObjectStore ,
187+ config ,
176188 ); err != nil {
177189 return nil , fmt .Errorf ("while reconciling pod spec for job: %w" , err )
178190 }
@@ -204,23 +216,36 @@ func (impl LifecycleImplementation) reconcilePod(
204216 return nil , err
205217 }
206218
219+ resources , err := impl .collectSidecarResources (ctx , pluginConfiguration )
220+ if err != nil {
221+ return nil , err
222+ }
223+
224+ return reconcilePod (ctx , cluster , request , pluginConfiguration , sidecarConfiguration {
225+ env : env ,
226+ certificates : certificates ,
227+ resources : resources ,
228+ })
229+ }
230+
231+ func (impl LifecycleImplementation ) collectSidecarResources (
232+ ctx context.Context ,
233+ configuration * config.PluginConfiguration ,
234+ ) (corev1.ResourceRequirements , error ) {
207235 var barmanObjectStore barmancloudv1.ObjectStore
208- configuration := config .NewFromCluster (cluster )
209- if err := impl .Client .Get (ctx , configuration .GetBarmanObjectKey (), & barmanObjectStore ); err != nil {
210- return nil , err
211- }
236+ if err := impl .Client .Get (ctx , configuration .GetBarmanObjectKey (), & barmanObjectStore ); err != nil {
237+ return corev1.ResourceRequirements {}, err
238+ }
212239
213- return reconcilePod ( ctx , cluster , request , pluginConfiguration , env , certificates , barmanObjectStore )
240+ return barmanObjectStore . Spec . InstanceSidecarConfiguration . Resources , nil
214241}
215242
216243func reconcilePod (
217244 ctx context.Context ,
218245 cluster * cnpgv1.Cluster ,
219246 request * lifecycle.OperatorLifecycleRequest ,
220247 pluginConfiguration * config.PluginConfiguration ,
221- env []corev1.EnvVar ,
222- certificates []corev1.VolumeProjection ,
223- barmanObjectStore barmancloudv1.ObjectStore ,
248+ config sidecarConfiguration ,
224249) (* lifecycle.OperatorLifecycleResponse , error ) {
225250 pod , err := decoder .DecodePodJSON (request .GetObjectDefinition ())
226251 if err != nil {
@@ -240,9 +265,7 @@ func reconcilePod(
240265 corev1.Container {
241266 Args : []string {"instance" },
242267 },
243- env ,
244- certificates ,
245- barmanObjectStore ,
268+ config ,
246269 ); err != nil {
247270 return nil , fmt .Errorf ("while reconciling pod spec for pod: %w" , err )
248271 }
@@ -265,10 +288,8 @@ func reconcilePodSpec(
265288 cluster * cnpgv1.Cluster ,
266289 spec * corev1.PodSpec ,
267290 mainContainerName string ,
268- sidecarConfig corev1.Container ,
269- additionalEnvs []corev1.EnvVar ,
270- certificates []corev1.VolumeProjection ,
271- barmanObjectStore barmancloudv1.ObjectStore ,
291+ sidecarTemplate corev1.Container ,
292+ config sidecarConfiguration ,
272293) error {
273294 envs := []corev1.EnvVar {
274295 {
@@ -295,7 +316,7 @@ func reconcilePodSpec(
295316 },
296317 }
297318
298- envs = append (envs , additionalEnvs ... )
319+ envs = append (envs , config . env ... )
299320
300321 baseProbe := & corev1.Probe {
301322 FailureThreshold : 10 ,
@@ -308,11 +329,11 @@ func reconcilePodSpec(
308329 }
309330
310331 // fixed values
311- sidecarConfig .Name = "plugin-barman-cloud"
312- sidecarConfig .Image = viper .GetString ("sidecar-image" )
313- sidecarConfig .ImagePullPolicy = cluster .Spec .ImagePullPolicy
314- sidecarConfig .StartupProbe = baseProbe .DeepCopy ()
315- sidecarConfig .SecurityContext = & corev1.SecurityContext {
332+ sidecarTemplate .Name = "plugin-barman-cloud"
333+ sidecarTemplate .Image = viper .GetString ("sidecar-image" )
334+ sidecarTemplate .ImagePullPolicy = cluster .Spec .ImagePullPolicy
335+ sidecarTemplate .StartupProbe = baseProbe .DeepCopy ()
336+ sidecarTemplate .SecurityContext = & corev1.SecurityContext {
316337 AllowPrivilegeEscalation : ptr .To (false ),
317338 RunAsNonRoot : ptr .To (true ),
318339 Privileged : ptr .To (false ),
@@ -324,21 +345,21 @@ func reconcilePodSpec(
324345 Drop : []corev1.Capability {"ALL" },
325346 },
326347 }
327- sidecarConfig .Resources = barmanObjectStore . Spec . InstanceSidecarConfiguration . Resources
348+ sidecarTemplate .Resources = config . resources
328349
329350 // merge the main container envs if they aren't already set
330351 for _ , container := range spec .Containers {
331352 if container .Name == mainContainerName {
332353 for _ , env := range container .Env {
333354 found := false
334- for _ , existingEnv := range sidecarConfig .Env {
355+ for _ , existingEnv := range sidecarTemplate .Env {
335356 if existingEnv .Name == env .Name {
336357 found = true
337358 break
338359 }
339360 }
340361 if ! found {
341- sidecarConfig .Env = append (sidecarConfig .Env , env )
362+ sidecarTemplate .Env = append (sidecarTemplate .Env , env )
342363 }
343364 }
344365 break
@@ -348,18 +369,18 @@ func reconcilePodSpec(
348369 // merge the default envs if they aren't already set
349370 for _ , env := range envs {
350371 found := false
351- for _ , existingEnv := range sidecarConfig .Env {
372+ for _ , existingEnv := range sidecarTemplate .Env {
352373 if existingEnv .Name == env .Name {
353374 found = true
354375 break
355376 }
356377 }
357378 if ! found {
358- sidecarConfig .Env = append (sidecarConfig .Env , env )
379+ sidecarTemplate .Env = append (sidecarTemplate .Env , env )
359380 }
360381 }
361382
362- if err := injectPluginSidecarPodSpec (spec , & sidecarConfig , mainContainerName ); err != nil {
383+ if err := injectPluginSidecarPodSpec (spec , & sidecarTemplate , mainContainerName ); err != nil {
363384 return err
364385 }
365386
@@ -369,7 +390,7 @@ func reconcilePodSpec(
369390 Name : barmanCertificatesVolumeName ,
370391 VolumeSource : corev1.VolumeSource {
371392 Projected : & corev1.ProjectedVolumeSource {
372- Sources : certificates ,
393+ Sources : config . certificates ,
373394 },
374395 },
375396 })
0 commit comments