Skip to content

Commit

Permalink
chore: review
Browse files Browse the repository at this point in the history
Signed-off-by: Leonardo Cecchi <[email protected]>
  • Loading branch information
leonardoce committed Sep 27, 2023
1 parent cdfd628 commit 13a58e0
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 30 deletions.
23 changes: 2 additions & 21 deletions docs/src/samples/cluster-restore-snapshot-full.yaml
Original file line number Diff line number Diff line change
@@ -1,37 +1,18 @@
apiVersion: postgresql.cnpg.io/v1
kind: Cluster
metadata:
name: cluster-restore-pitr
name: cluster-restore-full
spec:
instances: 3

storage:
size: 1Gi
storageClass: csi-hostpath-sc

externalClusters:
- name: origin

barmanObjectStore:
serverName: cluster-example-with-backup
destinationPath: s3://backups/
endpointURL: http://minio:9000
s3Credentials:
accessKeyId:
name: minio
key: ACCESS_KEY_ID
secretAccessKey:
name: minio
key: ACCESS_SECRET_KEY
wal:
maxParallel: 8

bootstrap:
recovery:
source: origin

volumeSnapshots:
storage:
name: cluster-example-with-backup-3-1692618163
name: cluster-example-2-1695821489
kind: VolumeSnapshot
apiGroup: snapshot.storage.k8s.io
7 changes: 6 additions & 1 deletion internal/cmd/manager/instance/restoresnapshot/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"github.com/cloudnative-pg/cloudnative-pg/internal/management/istio"
"github.com/cloudnative-pg/cloudnative-pg/internal/management/linkerd"
"github.com/cloudnative-pg/cloudnative-pg/pkg/management"
"github.com/cloudnative-pg/cloudnative-pg/pkg/management/log"
"github.com/cloudnative-pg/cloudnative-pg/pkg/management/postgres"
)

Expand Down Expand Up @@ -56,7 +57,11 @@ func NewCmd() *cobra.Command {
PgWal: pgWal,
}

return execute(ctx, info)
err := execute(ctx, info)
if err != nil {
log.Error(err, "Error while recovering Volume Snapshot backup")
}
return err
},
PostRunE: func(cmd *cobra.Command, args []string) error {
if err := istio.TryInvokeQuitEndpoint(cmd.Context()); err != nil {
Expand Down
8 changes: 7 additions & 1 deletion pkg/fileutils/fileutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -357,9 +357,15 @@ func RemoveFiles(basePath string, filePaths []string) error {
for _, pattern := range filePaths {
if len(pattern) >= 2 && pattern[len(pattern)-2:] == "/*" {
dirPath := filepath.Join(basePath, pattern[:len(pattern)-2])
if err := RemoveDirectoryContent(dirPath); err != nil {
dirExists, err := FileExists(dirPath)
if err != nil {
return err
}
if dirExists {
if err := RemoveDirectoryContent(dirPath); err != nil {
return err
}
}
continue
}

Expand Down
1 change: 1 addition & 0 deletions pkg/fileutils/fileutils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,7 @@ var _ = Describe("RemoveFiles", func() {
err := RemoveFiles(tempDir, []string{
"file1.txt",
"dir1/*",
"non_existent_dir/*",
})
Expect(err).NotTo(HaveOccurred())

Expand Down
14 changes: 7 additions & 7 deletions pkg/management/postgres/restore.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,20 +88,20 @@ func (info InitInfo) RestoreSnapshot(ctx context.Context, cli client.Client) err
return err
}

if cluster.Spec.Bootstrap == nil || cluster.Spec.Bootstrap.Recovery == nil ||
cluster.Spec.Bootstrap.Recovery.Source == "" {
// We are recovering from an existing PVC snapshot, we
// don't need to invoke the recovery job
return nil
}

log.Info("Recovering from volume snapshot",
"sourceName", cluster.Spec.Bootstrap.Recovery.Source)

if err := fileutils.RemoveRestoreExcludedFiles(info.PgData); err != nil {
return fmt.Errorf("error while cleaning up the recovered PGDATA: %w", err)
}

if cluster.Spec.Bootstrap == nil || cluster.Spec.Bootstrap.Recovery == nil ||
cluster.Spec.Bootstrap.Recovery.Source == "" {
// We are recovering from an existing PVC snapshot, we
// don't need to invoke the recovery job
return nil
}

backup, env, err := info.createBackupObjectForSnapshotRestore(ctx, cli, cluster)
if err != nil {
return err
Expand Down

0 comments on commit 13a58e0

Please sign in to comment.