feat: add wal-restore capabilities during restore

Signed-off-by: Armando Ruocco <armando.ruocco@enterprisedb.com>
This commit is contained in:
Armando Ruocco 2024-11-22 16:38:03 +01:00 committed by Francesco Canovai
parent 0141ff33ab
commit cacf3fdfcb
6 changed files with 44 additions and 11 deletions

View File

@ -1,4 +1,4 @@
package instance package common
import ( import (
"context" "context"
@ -19,7 +19,6 @@ import (
"sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/controller-runtime/pkg/client"
barmancloudv1 "github.com/cloudnative-pg/plugin-barman-cloud/api/v1" 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" "github.com/cloudnative-pg/plugin-barman-cloud/internal/cnpgi/metadata"
) )
@ -152,7 +151,7 @@ func (w WALServiceImplementation) Restore(
barmanConfiguration := &objectStore.Spec.Configuration barmanConfiguration := &objectStore.Spec.Configuration
env := common.GetRestoreCABundleEnv(barmanConfiguration) env := GetRestoreCABundleEnv(barmanConfiguration)
credentialsEnv, err := barmanCredentials.EnvSetBackupCloudCredentials( credentialsEnv, err := barmanCredentials.EnvSetBackupCloudCredentials(
ctx, ctx,
w.Client, w.Client,
@ -163,7 +162,7 @@ func (w WALServiceImplementation) Restore(
if err != nil { if err != nil {
return nil, fmt.Errorf("while getting recover credentials: %w", err) return nil, fmt.Errorf("while getting recover credentials: %w", err)
} }
env = common.MergeEnv(env, credentialsEnv) env = MergeEnv(env, credentialsEnv)
// TODO: refactor this code elsewhere // TODO: refactor this code elsewhere
serverName := cluster.Name serverName := cluster.Name

View File

@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License. limitations under the License.
*/ */
package instance package common
import ( import (
"errors" "errors"

View File

@ -8,6 +8,8 @@ import (
"github.com/cloudnative-pg/cnpg-i/pkg/wal" "github.com/cloudnative-pg/cnpg-i/pkg/wal"
"google.golang.org/grpc" "google.golang.org/grpc"
"sigs.k8s.io/controller-runtime/pkg/client" "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 // CNPGI is the implementation of the PostgreSQL sidecar
@ -26,7 +28,7 @@ type CNPGI struct {
// Start starts the GRPC service // Start starts the GRPC service
func (c *CNPGI) Start(ctx context.Context) error { func (c *CNPGI) Start(ctx context.Context) error {
enrich := func(server *grpc.Server) error { enrich := func(server *grpc.Server) error {
wal.RegisterWALServer(server, WALServiceImplementation{ wal.RegisterWALServer(server, common.WALServiceImplementation{
BarmanObjectKey: c.BarmanObjectKey, BarmanObjectKey: c.BarmanObjectKey,
ClusterObjectKey: c.ClusterObjectKey, ClusterObjectKey: c.ClusterObjectKey,
InstanceName: c.InstanceName, InstanceName: c.InstanceName,

View File

@ -33,6 +33,7 @@ func Start(ctx context.Context) error {
setupLog.Info("Starting barman cloud instance plugin") setupLog.Info("Starting barman cloud instance plugin")
namespace := viper.GetString("namespace") namespace := viper.GetString("namespace")
clusterName := viper.GetString("cluster-name") clusterName := viper.GetString("cluster-name")
boName := viper.GetString("barman-object-name")
objs := map[client.Object]cache.ByObject{ objs := map[client.Object]cache.ByObject{
&cnpgv1.Cluster{}: { &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{ mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{
Scheme: scheme, Scheme: scheme,
Cache: cache.Options{ Cache: cache.Options{
@ -65,12 +75,17 @@ func Start(ctx context.Context) error {
if err := mgr.Add(&CNPGI{ if err := mgr.Add(&CNPGI{
PluginPath: viper.GetString("plugin-path"), PluginPath: viper.GetString("plugin-path"),
SpoolDirectory: viper.GetString("spool-directory"), SpoolDirectory: viper.GetString("spool-directory"),
BarmanObjectKey: client.ObjectKey{
Namespace: namespace,
Name: boName,
},
ClusterObjectKey: client.ObjectKey{ ClusterObjectKey: client.ObjectKey{
Namespace: namespace, Namespace: namespace,
Name: clusterName, Name: clusterName,
}, },
Client: mgr.GetClient(), Client: mgr.GetClient(),
PGDataPath: viper.GetString("pgdata"), PGDataPath: viper.GetString("pgdata"),
InstanceName: viper.GetString("pod-name"),
}); err != nil { }); err != nil {
setupLog.Error(err, "unable to create CNPGI runnable") setupLog.Error(err, "unable to create CNPGI runnable")
return err return err

View File

@ -4,6 +4,10 @@ import (
"context" "context"
"errors" "errors"
"fmt" "fmt"
"os"
"os/exec"
"path"
"github.com/cloudnative-pg/barman-cloud/pkg/api" "github.com/cloudnative-pg/barman-cloud/pkg/api"
barmanArchiver "github.com/cloudnative-pg/barman-cloud/pkg/archiver" barmanArchiver "github.com/cloudnative-pg/barman-cloud/pkg/archiver"
barmanCapabilities "github.com/cloudnative-pg/barman-cloud/pkg/capabilities" barmanCapabilities "github.com/cloudnative-pg/barman-cloud/pkg/capabilities"
@ -21,9 +25,6 @@ import (
"github.com/cloudnative-pg/machinery/pkg/log" "github.com/cloudnative-pg/machinery/pkg/log"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types" "k8s.io/apimachinery/pkg/types"
"os"
"os/exec"
"path"
"sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/controller-runtime/pkg/client"
barmancloudv1 "github.com/cloudnative-pg/plugin-barman-cloud/api/v1" barmancloudv1 "github.com/cloudnative-pg/plugin-barman-cloud/api/v1"

View File

@ -2,20 +2,26 @@ package restore
import ( import (
"context" "context"
"path"
"github.com/cloudnative-pg/cnpg-i-machinery/pkg/pluginhelper/http" "github.com/cloudnative-pg/cnpg-i-machinery/pkg/pluginhelper/http"
restore "github.com/cloudnative-pg/cnpg-i/pkg/restore/job" restore "github.com/cloudnative-pg/cnpg-i/pkg/restore/job"
"github.com/cloudnative-pg/cnpg-i/pkg/wal"
"google.golang.org/grpc" "google.golang.org/grpc"
"sigs.k8s.io/controller-runtime/pkg/client" "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 // CNPGI is the implementation of the PostgreSQL sidecar
type CNPGI struct { type CNPGI struct {
PluginPath string PluginPath string
SpoolDirectory string SpoolDirectory string
BarmanObjectKey client.ObjectKey
ClusterObjectKey client.ObjectKey ClusterObjectKey client.ObjectKey
Client client.Client Client client.Client
PGDataPath string PGDataPath string
InstanceName string
} }
// Start starts the GRPC service // 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" const PgWalVolumePgWalPath = "/var/lib/postgresql/wal/pg_wal"
enrich := func(server *grpc.Server) error { 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{ restore.RegisterRestoreJobHooksServer(server, &JobHookImpl{
Client: c.Client, Client: c.Client,
ClusterObjectKey: c.ClusterObjectKey, ClusterObjectKey: c.ClusterObjectKey,