From cacf3fdfcb630ddf327bdc67911118566bc731da Mon Sep 17 00:00:00 2001 From: Armando Ruocco Date: Fri, 22 Nov 2024 16:38:03 +0100 Subject: [PATCH] feat: add wal-restore capabilities during restore Signed-off-by: Armando Ruocco --- internal/cnpgi/{instance => common}/wal.go | 7 +++---- .../cnpgi/{instance => common}/wal_import.go | 2 +- internal/cnpgi/instance/start.go | 4 +++- internal/cnpgi/restore/manager.go | 19 +++++++++++++++++-- internal/cnpgi/restore/restore.go | 7 ++++--- internal/cnpgi/restore/start.go | 16 ++++++++++++++++ 6 files changed, 44 insertions(+), 11 deletions(-) rename internal/cnpgi/{instance => common}/wal.go (98%) rename internal/cnpgi/{instance => common}/wal_import.go (99%) diff --git a/internal/cnpgi/instance/wal.go b/internal/cnpgi/common/wal.go similarity index 98% rename from internal/cnpgi/instance/wal.go rename to internal/cnpgi/common/wal.go index 12c8d2a..9d9e5fd 100644 --- a/internal/cnpgi/instance/wal.go +++ b/internal/cnpgi/common/wal.go @@ -1,4 +1,4 @@ -package instance +package common import ( "context" @@ -19,7 +19,6 @@ import ( "sigs.k8s.io/controller-runtime/pkg/client" barmancloudv1 "github.com/cloudnative-pg/plugin-barman-cloud/api/v1" - "github.com/cloudnative-pg/plugin-barman-cloud/internal/cnpgi/common" "github.com/cloudnative-pg/plugin-barman-cloud/internal/cnpgi/metadata" ) @@ -152,7 +151,7 @@ func (w WALServiceImplementation) Restore( barmanConfiguration := &objectStore.Spec.Configuration - env := common.GetRestoreCABundleEnv(barmanConfiguration) + env := GetRestoreCABundleEnv(barmanConfiguration) credentialsEnv, err := barmanCredentials.EnvSetBackupCloudCredentials( ctx, w.Client, @@ -163,7 +162,7 @@ func (w WALServiceImplementation) Restore( if err != nil { return nil, fmt.Errorf("while getting recover credentials: %w", err) } - env = common.MergeEnv(env, credentialsEnv) + env = MergeEnv(env, credentialsEnv) // TODO: refactor this code elsewhere serverName := cluster.Name diff --git a/internal/cnpgi/instance/wal_import.go b/internal/cnpgi/common/wal_import.go similarity index 99% rename from internal/cnpgi/instance/wal_import.go rename to internal/cnpgi/common/wal_import.go index a2005dd..0c4ca62 100644 --- a/internal/cnpgi/instance/wal_import.go +++ b/internal/cnpgi/common/wal_import.go @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and limitations under the License. */ -package instance +package common import ( "errors" diff --git a/internal/cnpgi/instance/start.go b/internal/cnpgi/instance/start.go index 34cb89a..535cd22 100644 --- a/internal/cnpgi/instance/start.go +++ b/internal/cnpgi/instance/start.go @@ -8,6 +8,8 @@ import ( "github.com/cloudnative-pg/cnpg-i/pkg/wal" "google.golang.org/grpc" "sigs.k8s.io/controller-runtime/pkg/client" + + "github.com/cloudnative-pg/plugin-barman-cloud/internal/cnpgi/common" ) // CNPGI is the implementation of the PostgreSQL sidecar @@ -26,7 +28,7 @@ type CNPGI struct { // Start starts the GRPC service func (c *CNPGI) Start(ctx context.Context) error { enrich := func(server *grpc.Server) error { - wal.RegisterWALServer(server, WALServiceImplementation{ + wal.RegisterWALServer(server, common.WALServiceImplementation{ BarmanObjectKey: c.BarmanObjectKey, ClusterObjectKey: c.ClusterObjectKey, InstanceName: c.InstanceName, diff --git a/internal/cnpgi/restore/manager.go b/internal/cnpgi/restore/manager.go index 9bd79c4..4bf1866 100644 --- a/internal/cnpgi/restore/manager.go +++ b/internal/cnpgi/restore/manager.go @@ -33,6 +33,7 @@ func Start(ctx context.Context) error { setupLog.Info("Starting barman cloud instance plugin") namespace := viper.GetString("namespace") clusterName := viper.GetString("cluster-name") + boName := viper.GetString("barman-object-name") objs := map[client.Object]cache.ByObject{ &cnpgv1.Cluster{}: { @@ -43,6 +44,15 @@ func Start(ctx context.Context) error { }, } + if boName != "" { + objs[&barmancloudv1.ObjectStore{}] = cache.ByObject{ + Field: fields.OneTermEqualSelector("metadata.name", boName), + Namespaces: map[string]cache.Config{ + namespace: {}, + }, + } + } + mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{ Scheme: scheme, Cache: cache.Options{ @@ -65,12 +75,17 @@ func Start(ctx context.Context) error { if err := mgr.Add(&CNPGI{ PluginPath: viper.GetString("plugin-path"), SpoolDirectory: viper.GetString("spool-directory"), + BarmanObjectKey: client.ObjectKey{ + Namespace: namespace, + Name: boName, + }, ClusterObjectKey: client.ObjectKey{ Namespace: namespace, Name: clusterName, }, - Client: mgr.GetClient(), - PGDataPath: viper.GetString("pgdata"), + Client: mgr.GetClient(), + PGDataPath: viper.GetString("pgdata"), + InstanceName: viper.GetString("pod-name"), }); err != nil { setupLog.Error(err, "unable to create CNPGI runnable") return err diff --git a/internal/cnpgi/restore/restore.go b/internal/cnpgi/restore/restore.go index 1a8abad..696cd97 100644 --- a/internal/cnpgi/restore/restore.go +++ b/internal/cnpgi/restore/restore.go @@ -4,6 +4,10 @@ import ( "context" "errors" "fmt" + "os" + "os/exec" + "path" + "github.com/cloudnative-pg/barman-cloud/pkg/api" barmanArchiver "github.com/cloudnative-pg/barman-cloud/pkg/archiver" barmanCapabilities "github.com/cloudnative-pg/barman-cloud/pkg/capabilities" @@ -21,9 +25,6 @@ import ( "github.com/cloudnative-pg/machinery/pkg/log" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/types" - "os" - "os/exec" - "path" "sigs.k8s.io/controller-runtime/pkg/client" barmancloudv1 "github.com/cloudnative-pg/plugin-barman-cloud/api/v1" diff --git a/internal/cnpgi/restore/start.go b/internal/cnpgi/restore/start.go index da06988..263d382 100644 --- a/internal/cnpgi/restore/start.go +++ b/internal/cnpgi/restore/start.go @@ -2,20 +2,26 @@ package restore import ( "context" + "path" "github.com/cloudnative-pg/cnpg-i-machinery/pkg/pluginhelper/http" restore "github.com/cloudnative-pg/cnpg-i/pkg/restore/job" + "github.com/cloudnative-pg/cnpg-i/pkg/wal" "google.golang.org/grpc" "sigs.k8s.io/controller-runtime/pkg/client" + + "github.com/cloudnative-pg/plugin-barman-cloud/internal/cnpgi/common" ) // CNPGI is the implementation of the PostgreSQL sidecar type CNPGI struct { PluginPath string SpoolDirectory string + BarmanObjectKey client.ObjectKey ClusterObjectKey client.ObjectKey Client client.Client PGDataPath string + InstanceName string } // Start starts the GRPC service @@ -24,6 +30,16 @@ func (c *CNPGI) Start(ctx context.Context) error { const PgWalVolumePgWalPath = "/var/lib/postgresql/wal/pg_wal" enrich := func(server *grpc.Server) error { + wal.RegisterWALServer(server, common.WALServiceImplementation{ + BarmanObjectKey: c.BarmanObjectKey, + ClusterObjectKey: c.ClusterObjectKey, + InstanceName: c.InstanceName, + Client: c.Client, + SpoolDirectory: c.SpoolDirectory, + PGDataPath: c.PGDataPath, + PGWALPath: path.Join(c.PGDataPath, "pg_wal"), + }) + restore.RegisterRestoreJobHooksServer(server, &JobHookImpl{ Client: c.Client, ClusterObjectKey: c.ClusterObjectKey,