@@ -20,23 +20,17 @@ import (
2020
2121 barmancloudv1 "github.com/cloudnative-pg/plugin-barman-cloud/api/v1"
2222 "github.com/cloudnative-pg/plugin-barman-cloud/internal/cnpgi/metadata"
23+ "github.com/cloudnative-pg/plugin-barman-cloud/internal/cnpgi/operator/config"
2324)
2425
2526// WALServiceImplementation is the implementation of the WAL Service
2627type WALServiceImplementation struct {
2728 wal.UnimplementedWALServer
28- ClusterObjectKey client.ObjectKey
29- Client client.Client
30- InstanceName string
31- SpoolDirectory string
32- PGDataPath string
33- PGWALPath string
34-
35- BarmanObjectKey client.ObjectKey
36- ServerName string
37-
38- RecoveryBarmanObjectKey client.ObjectKey
39- RecoveryServerName string
29+ Client client.Client
30+ InstanceName string
31+ SpoolDirectory string
32+ PGDataPath string
33+ PGWALPath string
4034}
4135
4236// GetCapabilities implements the WALService interface
@@ -72,13 +66,13 @@ func (w WALServiceImplementation) Archive(
7266 contextLogger := log .FromContext (ctx )
7367 contextLogger .Debug ("starting wal archive" )
7468
75- var cluster cnpgv1. Cluster
76- if err := w . Client . Get ( ctx , w . ClusterObjectKey , & cluster ); err != nil {
69+ configuration , err := config . NewFromClusterJSON ( request . ClusterDefinition )
70+ if err != nil {
7771 return nil , err
7872 }
7973
8074 var objectStore barmancloudv1.ObjectStore
81- if err := w .Client .Get (ctx , w . BarmanObjectKey , & objectStore ); err != nil {
75+ if err := w .Client .Get (ctx , configuration . GetBarmanObjectKey () , & objectStore ); err != nil {
8276 return nil , err
8377 }
8478
@@ -106,7 +100,7 @@ func (w WALServiceImplementation) Archive(
106100 return nil , err
107101 }
108102
109- options , err := arch .BarmanCloudWalArchiveOptions (ctx , & objectStore .Spec .Configuration , w .ServerName )
103+ options , err := arch .BarmanCloudWalArchiveOptions (ctx , & objectStore .Spec .Configuration , configuration .ServerName )
110104 if err != nil {
111105 return nil , err
112106 }
@@ -127,43 +121,49 @@ func (w WALServiceImplementation) Restore(
127121 ctx context.Context ,
128122 request * wal.WALRestoreRequest ,
129123) (* wal.WALRestoreResult , error ) {
130- // TODO: build full paths
124+ contextLogger := log .FromContext (ctx )
125+
131126 walName := request .GetSourceWalName ()
132127 destinationPath := request .GetDestinationFileName ()
133128
134- var cluster cnpgv1. Cluster
135- if err := w . Client . Get ( ctx , w . ClusterObjectKey , & cluster ); err != nil {
129+ configuration , err := config . NewFromClusterJSON ( request . ClusterDefinition )
130+ if err != nil {
136131 return nil , err
137132 }
138133
139134 var objectStore barmancloudv1.ObjectStore
140135 var serverName string
141136
142137 switch {
143- case cluster . IsReplica () && cluster .Status .CurrentPrimary == w .InstanceName :
144- // Designated primary on replica cluster, using recovery object store
145- serverName = w . RecoveryServerName
146- if err := w .Client .Get (ctx , w . RecoveryBarmanObjectKey , & objectStore ); err != nil {
138+ case configuration . Cluster . IsReplica () && configuration . Cluster .Status .CurrentPrimary == w .InstanceName :
139+ // Designated primary on replica cluster, using replica source object store
140+ serverName = configuration . ReplicaSourceServerName
141+ if err := w .Client .Get (ctx , configuration . GetReplicaSourceBarmanObjectKey () , & objectStore ); err != nil {
147142 return nil , err
148143 }
149144
150- case cluster .Status .CurrentPrimary == "" :
145+ case configuration . Cluster .Status .CurrentPrimary == "" :
151146 // Recovery from object store, using recovery object store
152- serverName = w .RecoveryServerName
153- if err := w .Client .Get (ctx , w . RecoveryBarmanObjectKey , & objectStore ); err != nil {
147+ serverName = configuration .RecoveryServerName
148+ if err := w .Client .Get (ctx , configuration . GetRecoveryBarmanObjectKey () , & objectStore ); err != nil {
154149 return nil , err
155150 }
156151
157152 default :
158153 // Using cluster object store
159- serverName = w .ServerName
160- if err := w .Client .Get (ctx , w . BarmanObjectKey , & objectStore ); err != nil {
154+ serverName = configuration .ServerName
155+ if err := w .Client .Get (ctx , configuration . GetBarmanObjectKey () , & objectStore ); err != nil {
161156 return nil , err
162157 }
163158 }
164159
160+ contextLogger .Info (
161+ "Restoring WAL file" ,
162+ "objectStore" , objectStore .Name ,
163+ "serverName" , serverName ,
164+ "walName" , walName )
165165 return & wal.WALRestoreResult {}, w .restoreFromBarmanObjectStore (
166- ctx , & cluster , & objectStore , serverName , walName , destinationPath )
166+ ctx , configuration . Cluster , & objectStore , serverName , walName , destinationPath )
167167}
168168
169169func (w WALServiceImplementation ) restoreFromBarmanObjectStore (
@@ -246,6 +246,10 @@ func (w WALServiceImplementation) restoreFromBarmanObjectStore(
246246 // is the one that PostgreSQL has requested to restore.
247247 // The failure has already been logged in walRestorer.RestoreList method
248248 if walStatus [0 ].Err != nil {
249+ if errors .Is (walStatus [0 ].Err , barmanRestorer .ErrWALNotFound ) {
250+ return newWALNotFoundError ()
251+ }
252+
249253 return walStatus [0 ].Err
250254 }
251255
0 commit comments