Skip to content

Commit afd4603

Browse files
armruleonardocefcanovai
authored
fix: ensure restore configuration points to manager wal-restore (#68)
Signed-off-by: Armando Ruocco <[email protected]> Signed-off-by: Leonardo Cecchi <[email protected]> Signed-off-by: Francesco Canovai <[email protected]> Co-authored-by: Leonardo Cecchi <[email protected]> Co-authored-by: Francesco Canovai <[email protected]>
1 parent 74d4f5d commit afd4603

File tree

13 files changed

+104
-72
lines changed

13 files changed

+104
-72
lines changed

internal/cmd/instance/main.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ func NewCmd() *cobra.Command {
2222
"cluster-name",
2323
"pod-name",
2424
"spool-directory",
25+
"server-name",
2526
}
2627

2728
for _, k := range requiredSettings {
@@ -40,6 +41,7 @@ func NewCmd() *cobra.Command {
4041
_ = viper.BindEnv("pod-name", "POD_NAME")
4142
_ = viper.BindEnv("pgdata", "PGDATA")
4243
_ = viper.BindEnv("spool-directory", "SPOOL_DIRECTORY")
44+
_ = viper.BindEnv("server-name", "SERVER_NAME")
4345

4446
return cmd
4547
}

internal/cmd/restore/main.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ func NewCmd() *cobra.Command {
2323
"cluster-name",
2424
"pod-name",
2525
"spool-directory",
26+
"barman-object-name",
27+
"server-name",
2628
}
2729

2830
for _, k := range requiredSettings {
@@ -40,6 +42,8 @@ func NewCmd() *cobra.Command {
4042
_ = viper.BindEnv("pod-name", "POD_NAME")
4143
_ = viper.BindEnv("pgdata", "PGDATA")
4244
_ = viper.BindEnv("spool-directory", "SPOOL_DIRECTORY")
45+
_ = viper.BindEnv("barman-object-name", "BARMAN_OBJECT_NAME")
46+
_ = viper.BindEnv("server-name", "SERVER_NAME")
4347

4448
return cmd
4549
}
Lines changed: 6 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package instance
1+
package common
22

33
import (
44
"context"
@@ -19,12 +19,12 @@ import (
1919
"sigs.k8s.io/controller-runtime/pkg/client"
2020

2121
barmancloudv1 "github.com/cloudnative-pg/plugin-barman-cloud/api/v1"
22-
"github.com/cloudnative-pg/plugin-barman-cloud/internal/cnpgi/common"
2322
"github.com/cloudnative-pg/plugin-barman-cloud/internal/cnpgi/metadata"
2423
)
2524

2625
// WALServiceImplementation is the implementation of the WAL Service
2726
type WALServiceImplementation struct {
27+
ServerName string
2828
BarmanObjectKey client.ObjectKey
2929
ClusterObjectKey client.ObjectKey
3030
Client client.Client
@@ -73,16 +73,6 @@ func (w WALServiceImplementation) Archive(
7373
return nil, err
7474
}
7575

76-
// TODO: refactor this code elsewhere
77-
serverName := cluster.Name
78-
for _, plugin := range cluster.Spec.Plugins {
79-
if plugin.IsEnabled() && plugin.Name == metadata.PluginName {
80-
if pluginServerName, ok := plugin.Parameters["serverName"]; ok {
81-
serverName = pluginServerName
82-
}
83-
}
84-
}
85-
8676
var objectStore barmancloudv1.ObjectStore
8777
if err := w.Client.Get(ctx, w.BarmanObjectKey, &objectStore); err != nil {
8878
return nil, err
@@ -112,7 +102,7 @@ func (w WALServiceImplementation) Archive(
112102
return nil, err
113103
}
114104

115-
options, err := arch.BarmanCloudWalArchiveOptions(ctx, &objectStore.Spec.Configuration, serverName)
105+
options, err := arch.BarmanCloudWalArchiveOptions(ctx, &objectStore.Spec.Configuration, w.ServerName)
116106
if err != nil {
117107
return nil, err
118108
}
@@ -152,7 +142,7 @@ func (w WALServiceImplementation) Restore(
152142

153143
barmanConfiguration := &objectStore.Spec.Configuration
154144

155-
env := common.GetRestoreCABundleEnv(barmanConfiguration)
145+
env := GetRestoreCABundleEnv(barmanConfiguration)
156146
credentialsEnv, err := barmanCredentials.EnvSetBackupCloudCredentials(
157147
ctx,
158148
w.Client,
@@ -163,19 +153,9 @@ func (w WALServiceImplementation) Restore(
163153
if err != nil {
164154
return nil, fmt.Errorf("while getting recover credentials: %w", err)
165155
}
166-
env = common.MergeEnv(env, credentialsEnv)
167-
168-
// TODO: refactor this code elsewhere
169-
serverName := cluster.Name
170-
for _, plugin := range cluster.Spec.Plugins {
171-
if plugin.IsEnabled() && plugin.Name == metadata.PluginName {
172-
if pluginServerName, ok := plugin.Parameters["serverName"]; ok {
173-
serverName = pluginServerName
174-
}
175-
}
176-
}
156+
env = MergeEnv(env, credentialsEnv)
177157

178-
options, err := barmanCommand.CloudWalRestoreOptions(ctx, barmanConfiguration, serverName)
158+
options, err := barmanCommand.CloudWalRestoreOptions(ctx, barmanConfiguration, w.ServerName)
179159
if err != nil {
180160
return nil, fmt.Errorf("while getting barman-cloud-wal-restore options: %w", err)
181161
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
1414
limitations under the License.
1515
*/
1616

17-
package instance
17+
package common
1818

1919
import (
2020
"errors"

internal/cnpgi/instance/backup.go

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ type BackupServiceImplementation struct {
3131
ClusterObjectKey client.ObjectKey
3232
Client client.Client
3333
InstanceName string
34+
ServerName string
3435
backup.UnimplementedBackupServer
3536
}
3637

@@ -111,21 +112,12 @@ func (b BackupServiceImplementation) Backup(
111112
return nil, err
112113
}
113114

114-
serverName := cluster.Name
115-
for _, plugin := range cluster.Spec.Plugins {
116-
if plugin.IsEnabled() && plugin.Name == metadata.PluginName {
117-
if pluginServerName, ok := plugin.Parameters["serverName"]; ok {
118-
serverName = pluginServerName
119-
}
120-
}
121-
}
122-
123115
backupName := fmt.Sprintf("backup-%v", pgTime.ToCompactISO8601(time.Now()))
124116

125117
if err = backupCmd.Take(
126118
ctx,
127119
backupName,
128-
serverName,
120+
b.ServerName,
129121
env,
130122
barmanCloudExecutor{},
131123
postgres.BackupTemporaryDirectory,
@@ -137,7 +129,7 @@ func (b BackupServiceImplementation) Backup(
137129
executedBackupInfo, err := backupCmd.GetExecutedBackupInfo(
138130
ctx,
139131
backupName,
140-
serverName,
132+
b.ServerName,
141133
barmanCloudExecutor{},
142134
env)
143135
if err != nil {

internal/cnpgi/instance/manager.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ func Start(ctx context.Context) error {
8181
Name: clusterName,
8282
},
8383
BarmanObjectKey: barmanObjectKey,
84+
ServerName: viper.GetString("server-name"),
8485
InstanceName: podName,
8586
// TODO: improve
8687
PGDataPath: viper.GetString("pgdata"),

internal/cnpgi/instance/start.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,15 @@ import (
88
"github.com/cloudnative-pg/cnpg-i/pkg/wal"
99
"google.golang.org/grpc"
1010
"sigs.k8s.io/controller-runtime/pkg/client"
11+
12+
"github.com/cloudnative-pg/plugin-barman-cloud/internal/cnpgi/common"
1113
)
1214

1315
// CNPGI is the implementation of the PostgreSQL sidecar
1416
type CNPGI struct {
1517
Client client.Client
1618
BarmanObjectKey client.ObjectKey
19+
ServerName string
1720
ClusterObjectKey client.ObjectKey
1821
PGDataPath string
1922
PGWALPath string
@@ -26,9 +29,10 @@ type CNPGI struct {
2629
// Start starts the GRPC service
2730
func (c *CNPGI) Start(ctx context.Context) error {
2831
enrich := func(server *grpc.Server) error {
29-
wal.RegisterWALServer(server, WALServiceImplementation{
32+
wal.RegisterWALServer(server, common.WALServiceImplementation{
3033
BarmanObjectKey: c.BarmanObjectKey,
3134
ClusterObjectKey: c.ClusterObjectKey,
35+
ServerName: c.ServerName,
3236
InstanceName: c.InstanceName,
3337
Client: c.Client,
3438
SpoolDirectory: c.SpoolDirectory,
@@ -38,6 +42,7 @@ func (c *CNPGI) Start(ctx context.Context) error {
3842
backup.RegisterBackupServer(server, BackupServiceImplementation{
3943
Client: c.Client,
4044
BarmanObjectKey: c.BarmanObjectKey,
45+
ServerName: c.ServerName,
4146
ClusterObjectKey: c.ClusterObjectKey,
4247
InstanceName: c.InstanceName,
4348
})

internal/cnpgi/operator/config/config.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ func (e *ConfigurationError) IsEmpty() bool {
4545
// PluginConfiguration is the configuration of the plugin
4646
type PluginConfiguration struct {
4747
BarmanObjectName string
48+
ServerName string
4849
RecoveryBarmanObjectName string
4950
RecoveryBarmanServerName string
5051
}
@@ -56,6 +57,15 @@ func NewFromCluster(cluster *cnpgv1.Cluster) *PluginConfiguration {
5657
metadata.PluginName,
5758
)
5859

60+
serverName := cluster.Name
61+
for _, plugin := range cluster.Spec.Plugins {
62+
if plugin.IsEnabled() && plugin.Name == metadata.PluginName {
63+
if pluginServerName, ok := plugin.Parameters["serverName"]; ok {
64+
serverName = pluginServerName
65+
}
66+
}
67+
}
68+
5969
recoveryServerName := ""
6070
recoveryBarmanObjectName := ""
6171

@@ -70,6 +80,7 @@ func NewFromCluster(cluster *cnpgv1.Cluster) *PluginConfiguration {
7080
result := &PluginConfiguration{
7181
// used for the backup/archive
7282
BarmanObjectName: helper.Parameters["barmanObjectName"],
83+
ServerName: serverName,
7384
// used for restore/wal_restore
7485
RecoveryBarmanServerName: recoveryServerName,
7586
RecoveryBarmanObjectName: recoveryBarmanObjectName,

internal/cnpgi/operator/lifecycle.go

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,13 @@ func reconcileJob(
115115
return nil, nil
116116
}
117117

118+
// Since we're recovering from an existing object store,
119+
// we set our primary object store name to the recovery one.
120+
// This won't be needed anymore when wal-restore will be able
121+
// to check two object stores
122+
pluginConfiguration.BarmanObjectName = pluginConfiguration.RecoveryBarmanObjectName
123+
pluginConfiguration.ServerName = pluginConfiguration.RecoveryBarmanServerName
124+
118125
var job batchv1.Job
119126
if err := decoder.DecodeObject(
120127
request.GetObjectDefinition(),
@@ -175,10 +182,14 @@ func reconcilePod(
175182

176183
mutatedPod := pod.DeepCopy()
177184

178-
if err := reconcilePodSpec(pluginConfiguration, cluster, &mutatedPod.Spec, "postgres", corev1.Container{
179-
Args: []string{"instance"},
180-
}); err != nil {
181-
return nil, fmt.Errorf("while reconciling pod spec for pod: %w", err)
185+
if len(pluginConfiguration.BarmanObjectName) != 0 {
186+
if err := reconcilePodSpec(pluginConfiguration, cluster, &mutatedPod.Spec, "postgres", corev1.Container{
187+
Args: []string{"instance"},
188+
}); err != nil {
189+
return nil, fmt.Errorf("while reconciling pod spec for pod: %w", err)
190+
}
191+
} else {
192+
contextLogger.Debug("No need to mutate instance with no backup & archiving configuration")
182193
}
183194

184195
patch, err := object.CreatePatch(mutatedPod, pod)
@@ -212,6 +223,10 @@ func reconcilePodSpec(
212223
Name: "BARMAN_OBJECT_NAME",
213224
Value: cfg.BarmanObjectName,
214225
},
226+
{
227+
Name: "SERVER_NAME",
228+
Value: cfg.ServerName,
229+
},
215230
{
216231
// TODO: should we really use this one?
217232
// should we mount an emptyDir volume just for that?

internal/cnpgi/restore/identity.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,13 @@ func (i IdentityImplementation) GetPluginCapabilities(
3838
},
3939
},
4040
},
41+
{
42+
Type: &identity.PluginCapability_Service_{
43+
Service: &identity.PluginCapability_Service{
44+
Type: identity.PluginCapability_Service_TYPE_WAL_SERVICE,
45+
},
46+
},
47+
},
4148
},
4249
}, nil
4350
}

0 commit comments

Comments
 (0)