mirror of
https://github.com/vmware-tanzu/velero.git
synced 2026-08-28 03:46:14 +00:00
* Update CRDs and CLI to support in-place restore (#10038) Update CRDs(Restore, DataDownload, PodVolumeRestore) and restore create CLI to support in-place restore Signed-off-by: Wenkai Yin(尹文开) <yinw@vmware.com> * Update Kopia(filesystem) uploader to support incremental and deleteExtraFile during restore (#10066) Update Kopia(filesystem) uploader to support incremental and deleteExtraFile during restore Signed-off-by: Wenkai Yin(尹文开) <yinw@vmware.com> * Update Restore Exposer and PVC CSI to support in-place restore (#10104) 1. Update Restore Exposer to support exposing with existing PV for in-place restore 2. Update PVC CSI RIA to continue the restore process for in-place restore Signed-off-by: Wenkai Yin(尹文开) <yinw@vmware.com> * Update Block uploader to support increase restore (#10244) Update Block uploader to support increase restore Signed-off-by: Wenkai Yin(尹文开) <yinw@vmware.com> * Update Exposer to recreate the target PV if the volume mode is different with the restore PVC (#10257) Update Exposer to recreate the target PV if the volume mode is different with t he restore PVC Signed-off-by: Wenkai Yin(尹文开) <yinw@vmware.com> * Preserve PVC selected-node annotation via carrier annotation for in-place restore For in-place volume data restore, the existing PVC is deleted and recreated. For StorageClasses with the WaitForFirstConsumer volume binding mode, losing the volume.kubernetes.io/selected-node annotation could let the scheduler place the recreated workload Pod in a different zone than the original PV, leaving it stuck in ContainerCreating. Instead of relying on RestoreItemAction execution order (the generic PVC RIA unconditionally strips the selected-node annotation), the PVC CSI RIA now captures the annotation from the existing PVC right before deleting it and carries it on the target PVC via the Velero-internal restore.velero.io/inplace-restore-selected-node annotation. The restore engine translates the carrier back to the Kubernetes annotation after all RestoreItemActions have run and always strips the carrier so it never lands on the cluster. This makes the behavior independent of RIA ordering: the Kubernetes annotation is stripped by default on every path (including when the target PVC does not exist and Velero falls back to provisioning a new PVC), and preservation only happens when the CSI RIA explicitly captured a value from the existing PVC. Signed-off-by: chlins <chlins.zhang@gmail.com> * Update the control path to make the in-place incremental restore with block data mover work E2E (#10410) Update the control path to make the in-place incremental restore with block data mover work E2E Signed-off-by: Wenkai Yin(尹文开) <yinw@vmware.com> --------- Signed-off-by: Wenkai Yin(尹文开) <yinw@vmware.com> Signed-off-by: chlins <chlins.zhang@gmail.com> Co-authored-by: chlins <chlins.zhang@gmail.com>
313 lines
9.9 KiB
Go
313 lines
9.9 KiB
Go
/*
|
|
Copyright The Velero Contributors.
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
you may not use this file except in compliance with the License.
|
|
You may obtain a copy of the License at
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
Unless required by applicable law or agreed to in writing, software
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
See the License for the specific language governing permissions and
|
|
limitations under the License.
|
|
*/
|
|
|
|
package datamover
|
|
|
|
import (
|
|
"context"
|
|
"crypto/fips140"
|
|
"fmt"
|
|
"os"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/bombsimon/logrusr/v3"
|
|
"github.com/cockroachdb/errors"
|
|
"github.com/sirupsen/logrus"
|
|
"github.com/spf13/cobra"
|
|
corev1api "k8s.io/api/core/v1"
|
|
"k8s.io/apimachinery/pkg/fields"
|
|
"k8s.io/apimachinery/pkg/runtime"
|
|
"k8s.io/client-go/kubernetes"
|
|
"k8s.io/klog/v2"
|
|
ctrl "sigs.k8s.io/controller-runtime"
|
|
|
|
"github.com/vmware-tanzu/velero/internal/credentials"
|
|
velerov1api "github.com/vmware-tanzu/velero/pkg/apis/velero/v1"
|
|
velerov2alpha1api "github.com/vmware-tanzu/velero/pkg/apis/velero/v2alpha1"
|
|
"github.com/vmware-tanzu/velero/pkg/buildinfo"
|
|
"github.com/vmware-tanzu/velero/pkg/cbtservice"
|
|
"github.com/vmware-tanzu/velero/pkg/client"
|
|
"github.com/vmware-tanzu/velero/pkg/cmd/util/signals"
|
|
"github.com/vmware-tanzu/velero/pkg/datamover"
|
|
"github.com/vmware-tanzu/velero/pkg/datapath"
|
|
"github.com/vmware-tanzu/velero/pkg/repository"
|
|
"github.com/vmware-tanzu/velero/pkg/uploader"
|
|
"github.com/vmware-tanzu/velero/pkg/util/filesystem"
|
|
"github.com/vmware-tanzu/velero/pkg/util/kube"
|
|
"github.com/vmware-tanzu/velero/pkg/util/logging"
|
|
|
|
ctlcache "sigs.k8s.io/controller-runtime/pkg/cache"
|
|
ctlclient "sigs.k8s.io/controller-runtime/pkg/client"
|
|
)
|
|
|
|
type dataMoverRestoreConfig struct {
|
|
volumePath string
|
|
volumeMode string
|
|
ddName string
|
|
cacheDir string
|
|
resourceTimeout time.Duration
|
|
cbtSAName string
|
|
vsNamespace string
|
|
volumeID string
|
|
}
|
|
|
|
func NewRestoreCommand(f client.Factory) *cobra.Command {
|
|
logLevelFlag := logging.LogLevelFlag(logrus.InfoLevel)
|
|
formatFlag := logging.NewFormatFlag()
|
|
|
|
config := dataMoverRestoreConfig{}
|
|
|
|
command := &cobra.Command{
|
|
Use: "restore",
|
|
Short: "Run the velero data-mover restore",
|
|
Long: "Run the velero data-mover restore",
|
|
Hidden: true,
|
|
Run: func(c *cobra.Command, args []string) {
|
|
logLevel := logLevelFlag.Parse()
|
|
logrus.Infof("Setting log-level to %s", strings.ToUpper(logLevel.String()))
|
|
|
|
logger := logging.DefaultLogger(logLevel, formatFlag.Parse())
|
|
logger.Infof("Starting Velero data-mover restore %s (%s)", buildinfo.Version, buildinfo.FormattedGitSHA())
|
|
|
|
f.SetBasename(fmt.Sprintf("%s-%s", c.Parent().Name(), c.Name()))
|
|
s, err := newdataMoverRestore(logger, f, config)
|
|
if err != nil {
|
|
kube.ExitPodWithMessage(logger, false, "Failed to create data mover restore, %v", err)
|
|
}
|
|
|
|
// Disable FIPS-140 compliance check, because Kopia doesn't support FIPS-140 yet.
|
|
fips140.WithoutEnforcement(func() {
|
|
s.run()
|
|
})
|
|
},
|
|
}
|
|
|
|
command.Flags().Var(logLevelFlag, "log-level", fmt.Sprintf("The level at which to log. Valid values are %s.", strings.Join(logLevelFlag.AllowedValues(), ", ")))
|
|
command.Flags().Var(formatFlag, "log-format", fmt.Sprintf("The format for log output. Valid values are %s.", strings.Join(formatFlag.AllowedValues(), ", ")))
|
|
command.Flags().StringVar(&config.volumePath, "volume-path", config.volumePath, "The full path of the volume to be restored")
|
|
command.Flags().StringVar(&config.volumeMode, "volume-mode", config.volumeMode, "The mode of the volume to be restored")
|
|
command.Flags().StringVar(&config.ddName, "data-download", config.ddName, "The data download name")
|
|
command.Flags().StringVar(&config.cacheDir, "cache-volume-path", config.cacheDir, "The full path of the cache volume")
|
|
command.Flags().DurationVar(&config.resourceTimeout, "resource-timeout", config.resourceTimeout, "How long to wait for resource processes which are not covered by other specific timeout parameters.")
|
|
command.Flags().StringVar(&config.cbtSAName, "cbt-sa-name", config.cbtSAName, "The name of the service account used by CSI's CBT service")
|
|
command.Flags().StringVar(&config.vsNamespace, "vs-namespace", config.vsNamespace, "The namespace of the VolumeSnapshot")
|
|
command.Flags().StringVar(&config.volumeID, "volume-id", config.volumeID, "The volume ID of the snapshot")
|
|
|
|
_ = command.MarkFlagRequired("volume-path")
|
|
_ = command.MarkFlagRequired("volume-mode")
|
|
_ = command.MarkFlagRequired("data-download")
|
|
_ = command.MarkFlagRequired("resource-timeout")
|
|
|
|
return command
|
|
}
|
|
|
|
type dataMoverRestore struct {
|
|
logger logrus.FieldLogger
|
|
ctx context.Context
|
|
cancelFunc context.CancelFunc
|
|
client ctlclient.Client
|
|
cache ctlcache.Cache
|
|
namespace string
|
|
nodeName string
|
|
config dataMoverRestoreConfig
|
|
kubeClient kubernetes.Interface
|
|
dataPathMgr *datapath.Manager
|
|
cbtService cbtservice.Service
|
|
}
|
|
|
|
func newdataMoverRestore(logger logrus.FieldLogger, factory client.Factory, config dataMoverRestoreConfig) (*dataMoverRestore, error) {
|
|
ctx, cancelFunc := context.WithCancel(context.Background())
|
|
|
|
clientConfig, err := factory.ClientConfig()
|
|
if err != nil {
|
|
cancelFunc()
|
|
return nil, errors.Wrap(err, "error to create client config")
|
|
}
|
|
|
|
ctrl.SetLogger(logrusr.New(logger))
|
|
klog.SetLogger(logrusr.New(logger)) // klog.Logger is used by k8s.io/client-go
|
|
|
|
scheme := runtime.NewScheme()
|
|
if err := velerov1api.AddToScheme(scheme); err != nil {
|
|
cancelFunc()
|
|
return nil, errors.Wrap(err, "error to add velero v1 scheme")
|
|
}
|
|
|
|
if err := velerov2alpha1api.AddToScheme(scheme); err != nil {
|
|
cancelFunc()
|
|
return nil, errors.Wrap(err, "error to add velero v2alpha1 scheme")
|
|
}
|
|
|
|
if err := corev1api.AddToScheme(scheme); err != nil {
|
|
cancelFunc()
|
|
return nil, errors.Wrap(err, "error to add core v1 scheme")
|
|
}
|
|
|
|
nodeName := os.Getenv("NODE_NAME")
|
|
|
|
// use a field selector to filter to only pods scheduled on this node.
|
|
cacheOption := ctlcache.Options{
|
|
Scheme: scheme,
|
|
ByObject: map[ctlclient.Object]ctlcache.ByObject{
|
|
&corev1api.Pod{}: {
|
|
Field: fields.Set{"spec.nodeName": nodeName}.AsSelector(),
|
|
},
|
|
&velerov2alpha1api.DataDownload{}: {
|
|
Field: fields.Set{"metadata.namespace": factory.Namespace()}.AsSelector(),
|
|
},
|
|
},
|
|
}
|
|
|
|
cli, err := ctlclient.New(clientConfig, ctlclient.Options{
|
|
Scheme: scheme,
|
|
})
|
|
if err != nil {
|
|
cancelFunc()
|
|
return nil, errors.Wrap(err, "error to create client")
|
|
}
|
|
|
|
var cache ctlcache.Cache
|
|
retry := 10
|
|
for {
|
|
cache, err = ctlcache.New(clientConfig, cacheOption)
|
|
if err == nil {
|
|
break
|
|
}
|
|
|
|
retry--
|
|
if retry == 0 {
|
|
break
|
|
}
|
|
|
|
logger.WithError(err).Warn("Failed to create client cache, need retry")
|
|
|
|
time.Sleep(time.Second)
|
|
}
|
|
|
|
if err != nil {
|
|
cancelFunc()
|
|
return nil, errors.Wrap(err, "error to create client cache")
|
|
}
|
|
|
|
s := &dataMoverRestore{
|
|
logger: logger,
|
|
ctx: ctx,
|
|
cancelFunc: cancelFunc,
|
|
client: cli,
|
|
cache: cache,
|
|
config: config,
|
|
namespace: factory.Namespace(),
|
|
nodeName: nodeName,
|
|
cbtService: cbtservice.NewService(
|
|
logger,
|
|
config.vsNamespace,
|
|
config.cbtSAName,
|
|
clientConfig,
|
|
),
|
|
}
|
|
|
|
s.kubeClient, err = factory.KubeClient()
|
|
if err != nil {
|
|
cancelFunc()
|
|
return nil, errors.Wrap(err, "error to create kube client")
|
|
}
|
|
|
|
s.dataPathMgr = datapath.NewManager(1)
|
|
|
|
return s, nil
|
|
}
|
|
|
|
var funcCreateDataPathRestore = (*dataMoverRestore).createDataPathService
|
|
|
|
func (s *dataMoverRestore) run() {
|
|
signals.CancelOnShutdown(s.cancelFunc, s.logger)
|
|
go func() {
|
|
if err := s.cache.Start(s.ctx); err != nil {
|
|
s.logger.WithError(err).Warn("error starting cache")
|
|
}
|
|
}()
|
|
|
|
s.runDataPath()
|
|
}
|
|
|
|
func (s *dataMoverRestore) runDataPath() {
|
|
s.logger.Infof("Starting micro service in node %s for dd %s", s.nodeName, s.config.ddName)
|
|
|
|
dpService, err := funcCreateDataPathRestore(s)
|
|
if err != nil {
|
|
s.cancelFunc()
|
|
funcExitWithMessage(s.logger, false, "Failed to create data path service for DataDownload %s: %v", s.config.ddName, err)
|
|
return
|
|
}
|
|
|
|
s.logger.Infof("Starting data path service %s", s.config.ddName)
|
|
|
|
err = dpService.Init()
|
|
if err != nil {
|
|
dpService.Shutdown()
|
|
s.cancelFunc()
|
|
funcExitWithMessage(s.logger, false, "Failed to init data path service for DataDownload %s: %v", s.config.ddName, err)
|
|
return
|
|
}
|
|
|
|
result, err := dpService.RunCancelableDataPath(s.ctx)
|
|
if err != nil {
|
|
dpService.Shutdown()
|
|
s.cancelFunc()
|
|
funcExitWithMessage(s.logger, false, "Failed to run data path service for DataDownload %s: %v", s.config.ddName, err)
|
|
return
|
|
}
|
|
|
|
s.logger.WithField("dd", s.config.ddName).Info("Data path service completed")
|
|
|
|
dpService.Shutdown()
|
|
|
|
s.logger.WithField("dd", s.config.ddName).Info("Data path service is shut down")
|
|
|
|
s.cancelFunc()
|
|
|
|
funcExitWithMessage(s.logger, true, result)
|
|
}
|
|
|
|
func (s *dataMoverRestore) createDataPathService() (dataPathService, error) {
|
|
credentialFileStore, err := funcNewCredentialFileStore(
|
|
s.client,
|
|
s.namespace,
|
|
credentials.DefaultStoreDirectory(),
|
|
filesystem.NewFileSystem(),
|
|
)
|
|
if err != nil {
|
|
return nil, errors.Wrapf(err, "error to create credential file store")
|
|
}
|
|
|
|
credSecretStore, err := funcNewCredentialSecretStore(s.client, s.namespace)
|
|
if err != nil {
|
|
return nil, errors.Wrapf(err, "error to create credential secret store")
|
|
}
|
|
|
|
credGetter := &credentials.CredentialGetter{FromFile: credentialFileStore, FromSecret: credSecretStore}
|
|
|
|
duInformer, err := s.cache.GetInformer(s.ctx, &velerov2alpha1api.DataDownload{})
|
|
if err != nil {
|
|
return nil, errors.Wrap(err, "error to get controller-runtime informer from manager")
|
|
}
|
|
|
|
repoEnsurer := repository.NewEnsurer(s.client, s.logger, s.config.resourceTimeout)
|
|
|
|
return datamover.NewRestoreMicroService(s.ctx, s.client, s.kubeClient, s.config.ddName, s.namespace, s.nodeName, datapath.AccessPoint{
|
|
ByPath: s.config.volumePath,
|
|
VolMode: uploader.PersistentVolumeMode(s.config.volumeMode),
|
|
}, s.dataPathMgr, repoEnsurer, credGetter, duInformer, s.config.cacheDir, s.config.volumeID, s.cbtService, s.logger), nil
|
|
}
|