11package operator
22
33import (
4+ "context"
45 "encoding/json"
56
67 cnpgv1 "github.com/cloudnative-pg/cloudnative-pg/api/v1"
78 "github.com/cloudnative-pg/cloudnative-pg/pkg/utils"
89 "github.com/cloudnative-pg/cnpg-i/pkg/lifecycle"
10+ barmancloudv1 "github.com/cloudnative-pg/plugin-barman-cloud/api/v1"
11+ "github.com/cloudnative-pg/plugin-barman-cloud/internal/cnpgi/operator/config"
912 batchv1 "k8s.io/api/batch/v1"
1013 corev1 "k8s.io/api/core/v1"
1114 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
12-
13- "github.com/cloudnative-pg/plugin-barman-cloud/internal/cnpgi/operator/config "
15+ "k8s.io/apimachinery/pkg/runtime"
16+ "sigs.k8s.io/controller-runtime/pkg/client/fake "
1417
1518 . "github.com/onsi/ginkgo/v2"
1619 . "github.com/onsi/gomega"
1720)
1821
1922var _ = Describe ("LifecycleImplementation" , func () {
2023 var (
21- lifecycleImpl LifecycleImplementation
2224 pluginConfiguration * config.PluginConfiguration
2325 cluster * cnpgv1.Cluster
2426 jobTypeMeta = metav1.TypeMeta {
@@ -31,6 +33,26 @@ var _ = Describe("LifecycleImplementation", func() {
3133 }
3234 )
3335
36+ // helper to build a fake client with our scheme and optional objects
37+ buildClientFunc := func (objs ... runtime.Object ) * fake.ClientBuilder {
38+ s := runtime .NewScheme ()
39+ _ = barmancloudv1 .AddToScheme (s )
40+ return fake .NewClientBuilder ().WithScheme (s ).WithRuntimeObjects (objs ... )
41+ }
42+
43+ // helper to create an ObjectStore with given args
44+ makeStoreFunc := func (ns , name string , args []string ) * barmancloudv1.ObjectStore {
45+ return & barmancloudv1.ObjectStore {
46+ TypeMeta : metav1.TypeMeta {Kind : "ObjectStore" , APIVersion : barmancloudv1 .GroupVersion .String ()},
47+ ObjectMeta : metav1.ObjectMeta {Name : name , Namespace : ns },
48+ Spec : barmancloudv1.ObjectStoreSpec {
49+ InstanceSidecarConfiguration : barmancloudv1.InstanceSidecarConfiguration {
50+ AdditionalContainerArgs : args ,
51+ },
52+ },
53+ }
54+ }
55+
3456 BeforeEach (func () {
3557 pluginConfiguration = & config.PluginConfiguration {
3658 BarmanObjectName : "minio-store-dest" ,
@@ -67,6 +89,7 @@ var _ = Describe("LifecycleImplementation", func() {
6789
6890 Describe ("GetCapabilities" , func () {
6991 It ("returns the correct capabilities" , func (ctx SpecContext ) {
92+ var lifecycleImpl LifecycleImplementation
7093 response , err := lifecycleImpl .GetCapabilities (ctx , & lifecycle.OperatorLifecycleCapabilitiesRequest {})
7194 Expect (err ).NotTo (HaveOccurred ())
7295 Expect (response ).NotTo (BeNil ())
@@ -76,6 +99,7 @@ var _ = Describe("LifecycleImplementation", func() {
7699
77100 Describe ("LifecycleHook" , func () {
78101 It ("returns an error if object definition is invalid" , func (ctx SpecContext ) {
102+ var lifecycleImpl LifecycleImplementation
79103 request := & lifecycle.OperatorLifecycleRequest {
80104 ObjectDefinition : []byte ("invalid-json" ),
81105 }
@@ -171,7 +195,7 @@ var _ = Describe("LifecycleImplementation", func() {
171195 })
172196 })
173197
174- Describe ("reconcilePod " , func () {
198+ Describe ("reconcileInstancePod " , func () {
175199 It ("returns a patch for a valid pod" , func (ctx SpecContext ) {
176200 pod := & corev1.Pod {
177201 TypeMeta : podTypeMeta ,
@@ -185,7 +209,7 @@ var _ = Describe("LifecycleImplementation", func() {
185209 ObjectDefinition : podJSON ,
186210 }
187211
188- response , err := reconcilePod (ctx , cluster , request , pluginConfiguration , sidecarConfiguration {})
212+ response , err := reconcileInstancePod (ctx , cluster , request , pluginConfiguration , sidecarConfiguration {})
189213 Expect (err ).NotTo (HaveOccurred ())
190214 Expect (response ).NotTo (BeNil ())
191215 Expect (response .JsonPatch ).NotTo (BeEmpty ())
@@ -203,11 +227,93 @@ var _ = Describe("LifecycleImplementation", func() {
203227 ObjectDefinition : []byte ("invalid-json" ),
204228 }
205229
206- response , err := reconcilePod (ctx , cluster , request , pluginConfiguration , sidecarConfiguration {})
230+ response , err := reconcileInstancePod (ctx , cluster , request , pluginConfiguration , sidecarConfiguration {})
207231 Expect (err ).To (HaveOccurred ())
208232 Expect (response ).To (BeNil ())
209233 })
210234 })
235+
236+ Describe ("collectAdditionalInstanceArgs" , func () {
237+ It ("prefers cluster object store when both are configured" , func () {
238+ ns := "test-ns"
239+ cluster := & cnpgv1.Cluster {ObjectMeta : metav1.ObjectMeta {Name : "c" , Namespace : ns }}
240+ pc := & config.PluginConfiguration {
241+ Cluster : cluster ,
242+ BarmanObjectName : "primary-store" ,
243+ RecoveryBarmanObjectName : "recovery-store" ,
244+ }
245+ primaryArgs := []string {"--primary-a" , "--primary-b" }
246+ recoveryArgs := []string {"--reco-a" }
247+ cli := buildClientFunc (
248+ makeStoreFunc (ns , pc .BarmanObjectName , primaryArgs ),
249+ makeStoreFunc (ns , pc .RecoveryBarmanObjectName , recoveryArgs ),
250+ ).Build ()
251+
252+ impl := LifecycleImplementation {Client : cli }
253+ args , err := impl .collectAdditionalInstanceArgs (context .Background (), pc )
254+ Expect (err ).NotTo (HaveOccurred ())
255+ Expect (args ).To (Equal (primaryArgs ))
256+ })
257+
258+ It ("falls back to recovery object store when primary not set" , func () {
259+ ns := "test-ns"
260+ cluster := & cnpgv1.Cluster {ObjectMeta : metav1.ObjectMeta {Name : "c" , Namespace : ns }}
261+ pc := & config.PluginConfiguration {
262+ Cluster : cluster ,
263+ BarmanObjectName : "" ,
264+ RecoveryBarmanObjectName : "recovery-store" ,
265+ }
266+ recoveryArgs := []string {"--reco-x" , "--reco-y" }
267+ cli := buildClientFunc (
268+ makeStoreFunc (ns , pc .RecoveryBarmanObjectName , recoveryArgs ),
269+ ).Build ()
270+
271+ impl := LifecycleImplementation {Client : cli }
272+ args , err := impl .collectAdditionalInstanceArgs (context .Background (), pc )
273+ Expect (err ).NotTo (HaveOccurred ())
274+ Expect (args ).To (Equal (recoveryArgs ))
275+ })
276+
277+ It ("returns nil when neither object name is configured" , func () {
278+ ns := "test-ns"
279+ cluster := & cnpgv1.Cluster {ObjectMeta : metav1.ObjectMeta {Name : "c" , Namespace : ns }}
280+ pc := & config.PluginConfiguration {Cluster : cluster }
281+ cli := buildClientFunc ().Build ()
282+
283+ impl := LifecycleImplementation {Client : cli }
284+ args , err := impl .collectAdditionalInstanceArgs (context .Background (), pc )
285+ Expect (err ).NotTo (HaveOccurred ())
286+ Expect (args ).To (BeNil ())
287+ })
288+
289+ It ("returns error if primary object store cannot be retrieved" , func () {
290+ ns := "test-ns"
291+ cluster := & cnpgv1.Cluster {ObjectMeta : metav1.ObjectMeta {Name : "c" , Namespace : ns }}
292+ pc := & config.PluginConfiguration {Cluster : cluster , BarmanObjectName : "missing-store" }
293+ cli := buildClientFunc ().Build ()
294+
295+ impl := LifecycleImplementation {Client : cli }
296+ args , err := impl .collectAdditionalInstanceArgs (context .Background (), pc )
297+ Expect (err ).To (HaveOccurred ())
298+ Expect (err .Error ()).To (ContainSubstring ("while getting barman object store" ))
299+ Expect (err .Error ()).To (ContainSubstring (ns + "/" + pc .BarmanObjectName ))
300+ Expect (args ).To (BeNil ())
301+ })
302+
303+ It ("returns error if recovery object store cannot be retrieved" , func () {
304+ ns := "test-ns"
305+ cluster := & cnpgv1.Cluster {ObjectMeta : metav1.ObjectMeta {Name : "c" , Namespace : ns }}
306+ pc := & config.PluginConfiguration {Cluster : cluster , RecoveryBarmanObjectName : "missing-reco" }
307+ cli := buildClientFunc ().Build ()
308+
309+ impl := LifecycleImplementation {Client : cli }
310+ args , err := impl .collectAdditionalInstanceArgs (context .Background (), pc )
311+ Expect (err ).To (HaveOccurred ())
312+ Expect (err .Error ()).To (ContainSubstring ("while getting recovery barman object store" ))
313+ Expect (err .Error ()).To (ContainSubstring (ns + "/" + pc .RecoveryBarmanObjectName ))
314+ Expect (args ).To (BeNil ())
315+ })
316+ })
211317})
212318
213319var _ = Describe ("Volume utilities" , func () {
0 commit comments