Skip to content

Commit db89bf4

Browse files
leonardocefcanovai
authored andcommitted
feat: separate recovery object store from replica source
Signed-off-by: Leonardo Cecchi <[email protected]>
1 parent 56a163b commit db89bf4

File tree

23 files changed

+328
-461
lines changed

23 files changed

+328
-461
lines changed

api/v1/objectstore_types.go

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -24,26 +24,11 @@ import (
2424

2525
// InstanceSidecarConfiguration defines the configuration for the sidecar that runs in the instance pods.
2626
type InstanceSidecarConfiguration struct {
27-
// The expiration time of the cache entries not managed by the informers. Expressed in seconds.
28-
// +optional
29-
// +kubebuilder:validation:Minimum=0
30-
// +kubebuilder:validation:Maximum=3600
31-
// +kubebuilder:default=180
32-
CacheTTL *int `json:"cacheTTL,omitempty"`
33-
34-
// The environment to be explicitly passed to the sidecar
27+
// The environment to be explicitely passed to the sidecar
3528
// +optional
3629
Env []corev1.EnvVar `json:"env,omitempty"`
3730
}
3831

39-
// GetCacheTTL returns the cache TTL value, defaulting to 180 seconds if not set.
40-
func (i InstanceSidecarConfiguration) GetCacheTTL() int {
41-
if i.CacheTTL == nil {
42-
return 180
43-
}
44-
return *i.CacheTTL
45-
}
46-
4732
// ObjectStoreSpec defines the desired state of ObjectStore.
4833
type ObjectStoreSpec struct {
4934
Configuration barmanapi.BarmanObjectStoreConfiguration `json:"configuration"`

api/v1/zz_generated.deepcopy.go

Lines changed: 6 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

config/crd/bases/barmancloud.cnpg.io_objectstores.yaml

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -382,15 +382,8 @@ spec:
382382
description: InstanceSidecarConfiguration defines the configuration
383383
for the sidecar that runs in the instance pods.
384384
properties:
385-
cacheTTL:
386-
default: 180
387-
description: The expiration time of the cache entries not managed
388-
by the informers. Expressed in seconds.
389-
maximum: 3600
390-
minimum: 0
391-
type: integer
392385
env:
393-
description: The environment to be explicitly passed to the sidecar
386+
description: The environment to be explicitely passed to the sidecar
394387
items:
395388
description: EnvVar represents an environment variable present
396389
in a Container.

docs/examples/cluster-example.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ metadata:
44
name: cluster-example
55
spec:
66
instances: 3
7-
imagePullPolicy: IfNotPresent
7+
imagePullPolicy: Always
88
plugins:
99
- name: barman-cloud.cloudnative-pg.io
1010
parameters:

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ require (
1010
github.com/cloudnative-pg/barman-cloud v0.0.0-20241105055149-ae6c2408bd14
1111
github.com/cloudnative-pg/cloudnative-pg v1.24.1-0.20241113134512-8608232c2813
1212
github.com/cloudnative-pg/cnpg-i v0.0.0-20241109002750-8abd359df734
13-
github.com/cloudnative-pg/cnpg-i-machinery v0.0.0-20241129144432-bd94f16685d3
13+
github.com/cloudnative-pg/cnpg-i-machinery v0.0.0-20241205093641-958e207b8afe
1414
github.com/cloudnative-pg/machinery v0.0.0-20241105070525-042a028b767c
1515
github.com/docker/docker v27.3.1+incompatible
1616
github.com/onsi/ginkgo/v2 v2.21.0

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ github.com/cloudnative-pg/cnpg-i v0.0.0-20241109002750-8abd359df734 h1:4jq/FUrlA
3434
github.com/cloudnative-pg/cnpg-i v0.0.0-20241109002750-8abd359df734/go.mod h1:3U7miYasKr2rYCQzrn/IvbSQc0OpYF8ieZt2FKG4nv0=
3535
github.com/cloudnative-pg/cnpg-i-machinery v0.0.0-20241129144432-bd94f16685d3 h1:hKTlmgyOq5ZS7t1eVa4SY1hH361gZ7VIb0an+BH9rJs=
3636
github.com/cloudnative-pg/cnpg-i-machinery v0.0.0-20241129144432-bd94f16685d3/go.mod h1:X6r1fRuUEIAv4+5SSBY2RmQ201K6GcptOXgnmaX/8tY=
37+
github.com/cloudnative-pg/cnpg-i-machinery v0.0.0-20241205093641-958e207b8afe h1:gUGqx4eTHreM0QWbszSx6wnbBw9Vavp5uYl4uA9fh1k=
38+
github.com/cloudnative-pg/cnpg-i-machinery v0.0.0-20241205093641-958e207b8afe/go.mod h1:X6r1fRuUEIAv4+5SSBY2RmQ201K6GcptOXgnmaX/8tY=
3739
github.com/cloudnative-pg/machinery v0.0.0-20241105070525-042a028b767c h1:t0RBU2gBiwJQ9XGeXlHPBYpsTscSKHgB5TfcWaiwanc=
3840
github.com/cloudnative-pg/machinery v0.0.0-20241105070525-042a028b767c/go.mod h1:uBHGRIk5rt07mO4zjIC1uvGBWTH6PqIiD1PfpvPGZKU=
3941
github.com/containerd/log v0.1.0 h1:TCJt7ioM2cr/tfR8GPbGf9/VRAX8D2B4PjzCpfX540I=

internal/cmd/instance/main.go

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,8 @@ func NewCmd() *cobra.Command {
1818
RunE: func(cmd *cobra.Command, _ []string) error {
1919
requiredSettings := []string{
2020
"namespace",
21-
"barman-object-name",
22-
"cluster-name",
2321
"pod-name",
2422
"spool-directory",
25-
"server-name",
2623
}
2724

2825
for _, k := range requiredSettings {
@@ -36,16 +33,9 @@ func NewCmd() *cobra.Command {
3633
}
3734

3835
_ = viper.BindEnv("namespace", "NAMESPACE")
39-
_ = viper.BindEnv("cluster-name", "CLUSTER_NAME")
4036
_ = viper.BindEnv("pod-name", "POD_NAME")
4137
_ = viper.BindEnv("pgdata", "PGDATA")
4238
_ = viper.BindEnv("spool-directory", "SPOOL_DIRECTORY")
4339

44-
_ = viper.BindEnv("barman-object-name", "BARMAN_OBJECT_NAME")
45-
_ = viper.BindEnv("server-name", "SERVER_NAME")
46-
47-
_ = viper.BindEnv("recovery-barman-object-name", "RECOVERY_BARMAN_OBJECT_NAME")
48-
_ = viper.BindEnv("recovery-server-name", "RECOVERY_SERVER_NAME")
49-
5040
return cmd
5141
}

internal/cmd/restore/main.go

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,8 @@ func NewCmd() *cobra.Command {
2020
RunE: func(cmd *cobra.Command, _ []string) error {
2121
requiredSettings := []string{
2222
"namespace",
23-
"cluster-name",
2423
"pod-name",
2524
"spool-directory",
26-
27-
// IMPORTANT: barman-object-name and server-name are not required
28-
// to restore a cluster.
29-
"recovery-barman-object-name",
30-
"recovery-server-name",
3125
}
3226

3327
for _, k := range requiredSettings {
@@ -41,16 +35,9 @@ func NewCmd() *cobra.Command {
4135
}
4236

4337
_ = viper.BindEnv("namespace", "NAMESPACE")
44-
_ = viper.BindEnv("cluster-name", "CLUSTER_NAME")
4538
_ = viper.BindEnv("pod-name", "POD_NAME")
4639
_ = viper.BindEnv("pgdata", "PGDATA")
4740
_ = viper.BindEnv("spool-directory", "SPOOL_DIRECTORY")
4841

49-
_ = viper.BindEnv("barman-object-name", "BARMAN_OBJECT_NAME")
50-
_ = viper.BindEnv("server-name", "SERVER_NAME")
51-
52-
_ = viper.BindEnv("recovery-barman-object-name", "RECOVERY_BARMAN_OBJECT_NAME")
53-
_ = viper.BindEnv("recovery-server-name", "RECOVERY_SERVER_NAME")
54-
5542
return cmd
5643
}

internal/cnpgi/common/errors.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package common
2+
3+
// WALNotFoundError is raised when a WAL file has not been found in the object store
4+
type WALNotFoundError struct {
5+
}
6+
7+
// ShouldPrintStackTrace tells whether the sidecar log stream should contain the stack trace
8+
func (e WALNotFoundError) ShouldPrintStackTrace() bool {
9+
return false
10+
}
11+
12+
// Error implements the error interface
13+
func (e WALNotFoundError) Error() string {
14+
return "WAL file not found"
15+
}

internal/cnpgi/common/wal.go

Lines changed: 27 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -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
2627
type 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
}
@@ -131,39 +125,39 @@ func (w WALServiceImplementation) Restore(
131125
walName := request.GetSourceWalName()
132126
destinationPath := request.GetDestinationFileName()
133127

134-
var cluster cnpgv1.Cluster
135-
if err := w.Client.Get(ctx, w.ClusterObjectKey, &cluster); err != nil {
128+
configuration, err := config.NewFromClusterJSON(request.ClusterDefinition)
129+
if err != nil {
136130
return nil, err
137131
}
138132

139133
var objectStore barmancloudv1.ObjectStore
140134
var serverName string
141135

142136
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 {
137+
case configuration.Cluster.IsReplica() && configuration.Cluster.Status.CurrentPrimary == w.InstanceName:
138+
// Designated primary on replica cluster, using replica source object store
139+
serverName = configuration.ReplicaSourceServerName
140+
if err := w.Client.Get(ctx, configuration.GetReplicaSourceBarmanObjectKey(), &objectStore); err != nil {
147141
return nil, err
148142
}
149143

150-
case cluster.Status.CurrentPrimary == "":
144+
case configuration.Cluster.Status.CurrentPrimary == "":
151145
// Recovery from object store, using recovery object store
152-
serverName = w.RecoveryServerName
153-
if err := w.Client.Get(ctx, w.RecoveryBarmanObjectKey, &objectStore); err != nil {
146+
serverName = configuration.RecoveryServerName
147+
if err := w.Client.Get(ctx, configuration.GetRecoveryBarmanObjectKey(), &objectStore); err != nil {
154148
return nil, err
155149
}
156150

157151
default:
158152
// Using cluster object store
159-
serverName = w.ServerName
160-
if err := w.Client.Get(ctx, w.BarmanObjectKey, &objectStore); err != nil {
153+
serverName = configuration.ServerName
154+
if err := w.Client.Get(ctx, configuration.GetBarmanObjectKey(), &objectStore); err != nil {
161155
return nil, err
162156
}
163157
}
164158

165159
return &wal.WALRestoreResult{}, w.restoreFromBarmanObjectStore(
166-
ctx, &cluster, &objectStore, serverName, walName, destinationPath)
160+
ctx, configuration.Cluster, &objectStore, serverName, walName, destinationPath)
167161
}
168162

169163
func (w WALServiceImplementation) restoreFromBarmanObjectStore(
@@ -246,7 +240,11 @@ func (w WALServiceImplementation) restoreFromBarmanObjectStore(
246240
// is the one that PostgreSQL has requested to restore.
247241
// The failure has already been logged in walRestorer.RestoreList method
248242
if walStatus[0].Err != nil {
249-
return walStatus[0].Err
243+
if errors.Is(walStatus[0].Err, barmanRestorer.ErrWALNotFound) {
244+
return WALNotFoundError{}
245+
} else {
246+
return walStatus[0].Err
247+
}
250248
}
251249

252250
// We skip this step if streaming connection is not available

0 commit comments

Comments
 (0)