1
0
Fork 0
mirror of https://github.com/kastenhq/kubestr.git synced 2024-12-14 11:57:56 +00:00

Refactoring the file-restore command to use a --fromPVC arg (#299)

* Refactoring the file-restore command to use a --toPVC arg

* Fixing the sc variable value unused lint error

* Setting MutuallyExclusive and OneRequired flag options for --fromSnapshot and --fromPVC
This commit is contained in:
Shlok Chaudhari 2024-09-05 18:41:52 -05:00 committed by GitHub
parent 59f68eb571
commit 9f6248beb5
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 343 additions and 155 deletions

View file

@ -129,16 +129,18 @@ var (
}
fromSnapshot string
fromPVC string
toPVC string
path string
restoreFileCmd = &cobra.Command{
Use: "file-restore",
Short: "Restore file(s) from a VolumeSnapshot to it's source PVC",
Long: "Restore file(s) from a given CSI provisioned VolumeSnapshot to a PVC.",
Short: "Restore file(s) from a Snapshot or PVC to it's source PVC",
Long: "Restore file(s) from a given CSI provisioned VolumeSnapshot or PersistentVolumeClaim to another PersistentVolumeClaim.",
Args: cobra.ExactArgs(0),
RunE: func(cmd *cobra.Command, args []string) error {
return FileRestore(context.Background(),
fromSnapshot,
fromPVC,
toPVC,
namespace,
csiCheckRunAsUser,
@ -227,8 +229,10 @@ func init() {
browseCmd.AddCommand(browseSnapshotCmd)
rootCmd.AddCommand(restoreFileCmd)
restoreFileCmd.Flags().StringVarP(&fromSnapshot, "fromSnapshot", "f", "", "The name of a VolumeSnapshot. (Required)")
_ = restoreFileCmd.MarkFlagRequired("fromSnapshot")
restoreFileCmd.Flags().StringVarP(&fromSnapshot, "fromSnapshot", "f", "", "The name of a VolumeSnapshot.")
restoreFileCmd.Flags().StringVarP(&fromPVC, "fromPVC", "v", "", "The name of a PersistentVolumeClaim.")
restoreFileCmd.MarkFlagsMutuallyExclusive("fromSnapshot", "fromPVC")
restoreFileCmd.MarkFlagsOneRequired("fromSnapshot", "fromPVC")
restoreFileCmd.Flags().StringVarP(&toPVC, "toPVC", "t", "", "The name of a PersistentVolumeClaim.")
restoreFileCmd.Flags().StringVarP(&namespace, "namespace", "n", fio.DefaultNS, "The namespace of both the given PVC & VS.")
restoreFileCmd.Flags().Int64VarP(&csiCheckRunAsUser, "runAsUser", "u", 0, "Runs the inspector pod as a user (int)")
@ -459,8 +463,9 @@ func CsiSnapshotBrowse(ctx context.Context,
}
func FileRestore(ctx context.Context,
snapshotName string,
pvcName string,
fromSnapshotName string,
fromPVCName string,
toPVCName string,
namespace string,
runAsUser int64,
localPort int,
@ -481,12 +486,13 @@ func FileRestore(ctx context.Context,
DynCli: dyncli,
}
err = fileRestoreRunner.RunFileRestore(ctx, &csitypes.FileRestoreArgs{
SnapshotName: snapshotName,
PVCName: pvcName,
Namespace: namespace,
RunAsUser: runAsUser,
LocalPort: localPort,
Path: path,
FromSnapshotName: fromSnapshotName,
FromPVCName: fromPVCName,
ToPVCName: toPVCName,
Namespace: namespace,
RunAsUser: runAsUser,
LocalPort: localPort,
Path: path,
})
if err != nil {
fmt.Printf("Failed to run file-restore (%s)\n", err.Error())

View file

@ -53,23 +53,22 @@ func (f *FileRestoreRunner) RunFileRestore(ctx context.Context, args *types.File
func (f *FileRestoreRunner) RunFileRestoreHelper(ctx context.Context, args *types.FileRestoreArgs) error {
defer func() {
fmt.Println("Cleaning up browser pod & restored PVC.")
f.restoreSteps.Cleanup(ctx, f.restorePVC, f.pod)
f.restoreSteps.Cleanup(ctx, args, f.restorePVC, f.pod)
}()
if f.KubeCli == nil || f.DynCli == nil {
return fmt.Errorf("cli uninitialized")
}
fmt.Println("Fetching the snapshot.")
vs, sourcePVC, sc, err := f.restoreSteps.ValidateArgs(ctx, args)
fmt.Println("Fetching the snapshot or PVC.")
vs, restorePVC, sourcePVC, sc, err := f.restoreSteps.ValidateArgs(ctx, args)
if err != nil {
return errors.Wrap(err, "Failed to validate arguments.")
}
f.snapshot = vs
fmt.Println("Creating the restored PVC & browser Pod.")
f.pod, f.restorePVC, err = f.restoreSteps.CreateInspectorApplication(ctx, args, f.snapshot, sourcePVC, sc)
fmt.Println("Creating the browser pod & mounting the PVCs.")
f.pod, f.restorePVC, err = f.restoreSteps.CreateInspectorApplication(ctx, args, f.snapshot, restorePVC, sourcePVC, sc)
if err != nil {
return errors.Wrap(err, "Failed to create inspector application.")
}
@ -95,11 +94,11 @@ func (f *FileRestoreRunner) RunFileRestoreHelper(ctx context.Context, args *type
//go:generate go run github.com/golang/mock/mockgen -destination=mocks/mock_file_restore_stepper.go -package=mocks . FileRestoreStepper
type FileRestoreStepper interface {
ValidateArgs(ctx context.Context, args *types.FileRestoreArgs) (*snapv1.VolumeSnapshot, *v1.PersistentVolumeClaim, *sv1.StorageClass, error)
CreateInspectorApplication(ctx context.Context, args *types.FileRestoreArgs, snapshot *snapv1.VolumeSnapshot, sourcePVC *v1.PersistentVolumeClaim, storageClass *sv1.StorageClass) (*v1.Pod, *v1.PersistentVolumeClaim, error)
ValidateArgs(ctx context.Context, args *types.FileRestoreArgs) (*snapv1.VolumeSnapshot, *v1.PersistentVolumeClaim, *v1.PersistentVolumeClaim, *sv1.StorageClass, error)
CreateInspectorApplication(ctx context.Context, args *types.FileRestoreArgs, snapshot *snapv1.VolumeSnapshot, restorePVC *v1.PersistentVolumeClaim, sourcePVC *v1.PersistentVolumeClaim, storageClass *sv1.StorageClass) (*v1.Pod, *v1.PersistentVolumeClaim, error)
ExecuteCopyCommand(ctx context.Context, args *types.FileRestoreArgs, pod *v1.Pod) (string, error)
PortForwardAPod(pod *v1.Pod, localPort int) error
Cleanup(ctx context.Context, restorePVC *v1.PersistentVolumeClaim, pod *v1.Pod)
Cleanup(ctx context.Context, args *types.FileRestoreArgs, restorePVC *v1.PersistentVolumeClaim, pod *v1.Pod)
}
type fileRestoreSteps struct {
@ -112,77 +111,107 @@ type fileRestoreSteps struct {
SnapshotGroupVersion *metav1.GroupVersionForDiscovery
}
func (f *fileRestoreSteps) ValidateArgs(ctx context.Context, args *types.FileRestoreArgs) (*snapv1.VolumeSnapshot, *v1.PersistentVolumeClaim, *sv1.StorageClass, error) {
func (f *fileRestoreSteps) ValidateArgs(ctx context.Context, args *types.FileRestoreArgs) (*snapv1.VolumeSnapshot, *v1.PersistentVolumeClaim, *v1.PersistentVolumeClaim, *sv1.StorageClass, error) {
if err := args.Validate(); err != nil {
return nil, nil, nil, errors.Wrap(err, "Failed to validate input arguments")
return nil, nil, nil, nil, errors.Wrap(err, "Failed to validate input arguments")
}
if err := f.validateOps.ValidateNamespace(ctx, args.Namespace); err != nil {
return nil, nil, nil, errors.Wrap(err, "Failed to validate Namespace")
return nil, nil, nil, nil, errors.Wrap(err, "Failed to validate Namespace")
}
groupVersion, err := f.versionFetchOps.GetCSISnapshotGroupVersion()
if err != nil {
return nil, nil, nil, errors.Wrap(err, "Failed to fetch groupVersion")
return nil, nil, nil, nil, errors.Wrap(err, "Failed to fetch groupVersion")
}
f.SnapshotGroupVersion = groupVersion
snapshot, err := f.validateOps.ValidateVolumeSnapshot(ctx, args.SnapshotName, args.Namespace, groupVersion)
if err != nil {
return nil, nil, nil, errors.Wrap(err, "Failed to validate VolumeSnapshot")
}
var sourcePVC *v1.PersistentVolumeClaim
if args.PVCName == "" {
fmt.Println("Fetching the source PVC from snapshot.")
if *snapshot.Spec.Source.PersistentVolumeClaimName == "" {
return nil, nil, nil, errors.Wrap(err, "Failed to fetch source PVC. VolumeSnapshot does not have a PVC as it's source")
}
sourcePVC, err = f.validateOps.ValidatePVC(ctx, *snapshot.Spec.Source.PersistentVolumeClaimName, args.Namespace)
var snapshot *snapv1.VolumeSnapshot
var restorePVC, sourcePVC *v1.PersistentVolumeClaim
var sc *sv1.StorageClass
if args.FromSnapshotName != "" {
fmt.Println("Fetching the snapshot.")
snapshot, err := f.validateOps.ValidateVolumeSnapshot(ctx, args.FromSnapshotName, args.Namespace, groupVersion)
if err != nil {
return nil, nil, nil, errors.Wrap(err, "Failed to validate source PVC")
return nil, nil, nil, nil, errors.Wrap(err, "Failed to validate VolumeSnapshot")
}
if args.ToPVCName == "" {
fmt.Println("Fetching the source PVC from snapshot.")
if *snapshot.Spec.Source.PersistentVolumeClaimName == "" {
return nil, nil, nil, nil, errors.Wrap(err, "Failed to fetch source PVC. VolumeSnapshot does not have a PVC as it's source")
}
sourcePVC, err = f.validateOps.ValidatePVC(ctx, *snapshot.Spec.Source.PersistentVolumeClaimName, args.Namespace)
if err != nil {
return nil, nil, nil, nil, errors.Wrap(err, "Failed to validate source PVC")
}
} else {
fmt.Println("Fetching the source PVC.")
sourcePVC, err = f.validateOps.ValidatePVC(ctx, args.ToPVCName, args.Namespace)
if err != nil {
return nil, nil, nil, nil, errors.Wrap(err, "Failed to validate source PVC")
}
}
sc, err = f.validateOps.ValidateStorageClass(ctx, *sourcePVC.Spec.StorageClassName)
if err != nil {
return nil, nil, nil, nil, errors.Wrap(err, "Failed to validate StorageClass for source PVC")
}
uVSC, err := f.validateOps.ValidateVolumeSnapshotClass(ctx, *snapshot.Spec.VolumeSnapshotClassName, groupVersion)
if err != nil {
return nil, nil, nil, nil, errors.Wrap(err, "Failed to validate VolumeSnapshotClass")
}
vscDriver := getDriverNameFromUVSC(*uVSC, groupVersion.GroupVersion)
if sc.Provisioner != vscDriver {
return nil, nil, nil, nil, fmt.Errorf("StorageClass provisioner (%s) and VolumeSnapshotClass driver (%s) are different.", sc.Provisioner, vscDriver)
}
} else {
fmt.Println("Fetching the source PVC.")
sourcePVC, err = f.validateOps.ValidatePVC(ctx, args.PVCName, args.Namespace)
fmt.Println("Fetching the restore PVC.")
restorePVC, err = f.validateOps.ValidatePVC(ctx, args.FromPVCName, args.Namespace)
if err != nil {
return nil, nil, nil, errors.Wrap(err, "Failed to validate source PVC")
return nil, nil, nil, nil, errors.Wrap(err, "Failed to validate restore PVC")
}
fmt.Println("Fetching the source PVC.")
sourcePVC, err = f.validateOps.ValidatePVC(ctx, args.ToPVCName, args.Namespace)
if err != nil {
return nil, nil, nil, nil, errors.Wrap(err, "Failed to validate source PVC")
}
_, err = f.validateOps.ValidateStorageClass(ctx, *restorePVC.Spec.StorageClassName)
if err != nil {
return nil, nil, nil, nil, errors.Wrap(err, "Failed to validate StorageClass for restore PVC")
}
sc, err = f.validateOps.ValidateStorageClass(ctx, *sourcePVC.Spec.StorageClassName)
if err != nil {
return nil, nil, nil, nil, errors.Wrap(err, "Failed to validate StorageClass for source PVC")
}
}
for _, sourceAccessMode := range sourcePVC.Spec.AccessModes {
if sourceAccessMode == v1.ReadWriteOncePod {
return nil, nil, nil, fmt.Errorf("Unsupported %s AccessMode found in source PVC. Supported AccessModes are ReadOnlyMany & ReadWriteMany", sourceAccessMode)
return nil, nil, nil, nil, fmt.Errorf("Unsupported %s AccessMode found in source PVC. Supported AccessModes are ReadOnlyMany & ReadWriteMany", sourceAccessMode)
}
}
sc, err := f.validateOps.ValidateStorageClass(ctx, *sourcePVC.Spec.StorageClassName)
if err != nil {
return nil, nil, nil, errors.Wrap(err, "Failed to validate StorageClass")
}
uVSC, err := f.validateOps.ValidateVolumeSnapshotClass(ctx, *snapshot.Spec.VolumeSnapshotClassName, groupVersion)
if err != nil {
return nil, nil, nil, errors.Wrap(err, "Failed to validate VolumeSnapshotClass")
}
vscDriver := getDriverNameFromUVSC(*uVSC, groupVersion.GroupVersion)
if sc.Provisioner != vscDriver {
return nil, nil, nil, fmt.Errorf("StorageClass provisioner (%s) and VolumeSnapshotClass driver (%s) are different.", sc.Provisioner, vscDriver)
}
return snapshot, sourcePVC, sc, nil
return snapshot, restorePVC, sourcePVC, sc, nil
}
func (f *fileRestoreSteps) CreateInspectorApplication(ctx context.Context, args *types.FileRestoreArgs, snapshot *snapv1.VolumeSnapshot, sourcePVC *v1.PersistentVolumeClaim, storageClass *sv1.StorageClass) (*v1.Pod, *v1.PersistentVolumeClaim, error) {
snapshotAPIGroup := "snapshot.storage.k8s.io"
snapshotKind := "VolumeSnapshot"
dataSource := &v1.TypedLocalObjectReference{
APIGroup: &snapshotAPIGroup,
Kind: snapshotKind,
Name: snapshot.Name,
}
pvcArgs := &types.CreatePVCArgs{
GenerateName: clonedPVCGenerateName,
StorageClass: storageClass.Name,
Namespace: args.Namespace,
DataSource: dataSource,
RestoreSize: snapshot.Status.RestoreSize,
}
restorePVC, err := f.createAppOps.CreatePVC(ctx, pvcArgs)
if err != nil {
return nil, nil, errors.Wrap(err, "Failed to restore PVC")
func (f *fileRestoreSteps) CreateInspectorApplication(ctx context.Context, args *types.FileRestoreArgs, snapshot *snapv1.VolumeSnapshot, restorePVC *v1.PersistentVolumeClaim, sourcePVC *v1.PersistentVolumeClaim, storageClass *sv1.StorageClass) (*v1.Pod, *v1.PersistentVolumeClaim, error) {
restoreMountPath := "/srv/restore-pvc-data"
if args.FromSnapshotName != "" {
snapshotAPIGroup := "snapshot.storage.k8s.io"
snapshotKind := "VolumeSnapshot"
dataSource := &v1.TypedLocalObjectReference{
APIGroup: &snapshotAPIGroup,
Kind: snapshotKind,
Name: snapshot.Name,
}
pvcArgs := &types.CreatePVCArgs{
GenerateName: clonedPVCGenerateName,
StorageClass: storageClass.Name,
Namespace: args.Namespace,
DataSource: dataSource,
RestoreSize: snapshot.Status.RestoreSize,
}
var err error
restorePVC, err = f.createAppOps.CreatePVC(ctx, pvcArgs)
if err != nil {
return nil, nil, errors.Wrap(err, "Failed to restore PVC")
}
restoreMountPath = "/srv/snapshot-data"
}
podArgs := &types.CreatePodArgs{
GenerateName: clonedPodGenerateName,
@ -192,7 +221,7 @@ func (f *fileRestoreSteps) CreateInspectorApplication(ctx context.Context, args
ContainerArgs: []string{"--noauth"},
PVCMap: map[string]types.VolumePath{
restorePVC.Name: {
MountPath: "/srv/snapshot-data",
MountPath: restoreMountPath,
},
sourcePVC.Name: {
MountPath: "/srv/source-data",
@ -209,7 +238,7 @@ func (f *fileRestoreSteps) CreateInspectorApplication(ctx context.Context, args
ContainerArgs: []string{"-c", "while true; do sleep 3600; done"},
PVCMap: map[string]types.VolumePath{
restorePVC.Name: {
MountPath: "/snapshot-data",
MountPath: restoreMountPath,
},
sourcePVC.Name: {
MountPath: "/source-data",
@ -284,13 +313,17 @@ func (f *fileRestoreSteps) PortForwardAPod(pod *v1.Pod, localPort int) error {
return nil
}
func (f *fileRestoreSteps) Cleanup(ctx context.Context, restorePVC *v1.PersistentVolumeClaim, pod *v1.Pod) {
if restorePVC != nil {
err := f.cleanerOps.DeletePVC(ctx, restorePVC.Name, restorePVC.Namespace)
if err != nil {
fmt.Println("Failed to delete restore PVC", restorePVC)
func (f *fileRestoreSteps) Cleanup(ctx context.Context, args *types.FileRestoreArgs, restorePVC *v1.PersistentVolumeClaim, pod *v1.Pod) {
if args.FromSnapshotName != "" {
fmt.Println("Cleaning up restore PVC.")
if restorePVC != nil {
err := f.cleanerOps.DeletePVC(ctx, restorePVC.Name, restorePVC.Namespace)
if err != nil {
fmt.Println("Failed to delete restore PVC", restorePVC)
}
}
}
fmt.Println("Cleaning up browser pod.")
if pod != nil {
err := f.cleanerOps.DeletePod(ctx, pod.Name, pod.Namespace)
if err != nil {

View file

@ -3,6 +3,7 @@ package csi
import (
"context"
"fmt"
"k8s.io/apimachinery/pkg/api/resource"
"github.com/golang/mock/gomock"
"github.com/kastenhq/kubestr/pkg/common"
@ -12,7 +13,6 @@ import (
. "gopkg.in/check.v1"
v1 "k8s.io/api/core/v1"
sv1 "k8s.io/api/storage/v1"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
)
@ -33,8 +33,8 @@ func (s *CSITestSuite) TestFileRestoreValidateArgs(c *C) {
}{
{ // valid args
args: &types.FileRestoreArgs{
SnapshotName: "vs",
Namespace: "ns",
FromSnapshotName: "vs",
Namespace: "ns",
},
prepare: func(f *fields) {
gomock.InOrder(
@ -84,10 +84,59 @@ func (s *CSITestSuite) TestFileRestoreValidateArgs(c *C) {
},
errChecker: IsNil,
},
{ // valid args
args: &types.FileRestoreArgs{
FromPVCName: "restorePVC",
ToPVCName: "sourcePVC",
Namespace: "ns",
},
prepare: func(f *fields) {
gomock.InOrder(
f.validateOps.EXPECT().ValidateNamespace(gomock.Any(), "ns").Return(nil),
f.versionOps.EXPECT().GetCSISnapshotGroupVersion().Return(
&metav1.GroupVersionForDiscovery{
GroupVersion: common.SnapshotAlphaVersion,
}, nil),
f.validateOps.EXPECT().ValidatePVC(gomock.Any(), "restorePVC", "ns").Return(
&v1.PersistentVolumeClaim{
ObjectMeta: metav1.ObjectMeta{
Name: "restorePVC",
Namespace: "ns",
},
Spec: v1.PersistentVolumeClaimSpec{
VolumeName: "vol",
StorageClassName: &scName,
},
}, nil,
),
f.validateOps.EXPECT().ValidatePVC(gomock.Any(), "sourcePVC", "ns").Return(
&v1.PersistentVolumeClaim{
ObjectMeta: metav1.ObjectMeta{
Name: "sourcePVC",
Namespace: "ns",
},
Spec: v1.PersistentVolumeClaimSpec{
VolumeName: "vol",
StorageClassName: &scName,
},
}, nil,
),
f.validateOps.EXPECT().ValidateStorageClass(gomock.Any(), scName).Return(
&sv1.StorageClass{
Provisioner: "p1",
}, nil),
f.validateOps.EXPECT().ValidateStorageClass(gomock.Any(), scName).Return(
&sv1.StorageClass{
Provisioner: "p1",
}, nil),
)
},
errChecker: IsNil,
},
{ // driver mismatch
args: &types.FileRestoreArgs{
SnapshotName: "vs",
Namespace: "ns",
FromSnapshotName: "vs",
Namespace: "ns",
},
prepare: func(f *fields) {
gomock.InOrder(
@ -139,8 +188,8 @@ func (s *CSITestSuite) TestFileRestoreValidateArgs(c *C) {
},
{ // vsc error
args: &types.FileRestoreArgs{
SnapshotName: "vs",
Namespace: "ns",
FromSnapshotName: "vs",
Namespace: "ns",
},
prepare: func(f *fields) {
gomock.InOrder(
@ -180,8 +229,8 @@ func (s *CSITestSuite) TestFileRestoreValidateArgs(c *C) {
},
{ // get driver versionn error
args: &types.FileRestoreArgs{
SnapshotName: "vs",
Namespace: "ns",
FromSnapshotName: "vs",
Namespace: "ns",
},
prepare: func(f *fields) {
gomock.InOrder(
@ -193,8 +242,8 @@ func (s *CSITestSuite) TestFileRestoreValidateArgs(c *C) {
},
{ // sc error
args: &types.FileRestoreArgs{
SnapshotName: "vs",
Namespace: "ns",
FromSnapshotName: "vs",
Namespace: "ns",
},
prepare: func(f *fields) {
gomock.InOrder(
@ -233,8 +282,8 @@ func (s *CSITestSuite) TestFileRestoreValidateArgs(c *C) {
},
{ // validate vs error
args: &types.FileRestoreArgs{
SnapshotName: "vs",
Namespace: "ns",
FromSnapshotName: "vs",
Namespace: "ns",
},
prepare: func(f *fields) {
gomock.InOrder(
@ -247,8 +296,8 @@ func (s *CSITestSuite) TestFileRestoreValidateArgs(c *C) {
},
{ // validate ns error
args: &types.FileRestoreArgs{
SnapshotName: "vs",
Namespace: "ns",
FromSnapshotName: "vs",
Namespace: "ns",
},
prepare: func(f *fields) {
gomock.InOrder(
@ -259,15 +308,15 @@ func (s *CSITestSuite) TestFileRestoreValidateArgs(c *C) {
},
{ // validate vs error
args: &types.FileRestoreArgs{
SnapshotName: "",
Namespace: "ns",
FromSnapshotName: "",
Namespace: "ns",
},
errChecker: NotNil,
},
{ // validate ns error
args: &types.FileRestoreArgs{
SnapshotName: "dfd",
Namespace: "",
FromSnapshotName: "dfd",
Namespace: "",
},
errChecker: NotNil,
},
@ -285,7 +334,7 @@ func (s *CSITestSuite) TestFileRestoreValidateArgs(c *C) {
validateOps: f.validateOps,
versionFetchOps: f.versionOps,
}
_, _, _, err := stepper.ValidateArgs(ctx, tc.args)
_, _, _, _, err := stepper.ValidateArgs(ctx, tc.args)
c.Check(err, tc.errChecker)
}
}
@ -298,25 +347,27 @@ func (s *CSITestSuite) TestCreateInspectorApplicationForFileRestore(c *C) {
createAppOps *mocks.MockApplicationCreator
}
for _, tc := range []struct {
args *types.FileRestoreArgs
snapshot *snapv1.VolumeSnapshot
sc *sv1.StorageClass
prepare func(f *fields)
errChecker Checker
podChecker Checker
pvcChecker Checker
args *types.FileRestoreArgs
fromSnapshot *snapv1.VolumeSnapshot
fromPVC *v1.PersistentVolumeClaim
sc *sv1.StorageClass
prepare func(f *fields)
errChecker Checker
podChecker Checker
pvcChecker Checker
}{
{
args: &types.FileRestoreArgs{
Namespace: "ns",
RunAsUser: 100,
Namespace: "ns",
RunAsUser: 100,
FromSnapshotName: "vs",
},
sc: &sv1.StorageClass{
ObjectMeta: metav1.ObjectMeta{
Name: "sc",
},
},
snapshot: &snapv1.VolumeSnapshot{
fromSnapshot: &snapv1.VolumeSnapshot{
ObjectMeta: metav1.ObjectMeta{
Name: "vs",
},
@ -324,6 +375,7 @@ func (s *CSITestSuite) TestCreateInspectorApplicationForFileRestore(c *C) {
RestoreSize: &resourceQuantity,
},
},
fromPVC: &v1.PersistentVolumeClaim{},
prepare: func(f *fields) {
gomock.InOrder(
f.createAppOps.EXPECT().CreatePVC(gomock.Any(), &types.CreatePVCArgs{
@ -369,15 +421,61 @@ func (s *CSITestSuite) TestCreateInspectorApplicationForFileRestore(c *C) {
},
{
args: &types.FileRestoreArgs{
Namespace: "ns",
RunAsUser: 100,
Namespace: "ns",
RunAsUser: 100,
FromPVCName: "restorePVC",
},
sc: &sv1.StorageClass{
ObjectMeta: metav1.ObjectMeta{
Name: "sc",
},
},
snapshot: &snapv1.VolumeSnapshot{
fromSnapshot: &snapv1.VolumeSnapshot{},
fromPVC: &v1.PersistentVolumeClaim{
ObjectMeta: metav1.ObjectMeta{
Name: "restorePVC",
},
},
prepare: func(f *fields) {
gomock.InOrder(
f.createAppOps.EXPECT().CreatePod(gomock.Any(), &types.CreatePodArgs{
GenerateName: clonedPodGenerateName,
Namespace: "ns",
ContainerArgs: []string{"--noauth"},
RunAsUser: 100,
ContainerImage: "filebrowser/filebrowser:v2",
PVCMap: map[string]types.VolumePath{
"restorePVC": {
MountPath: "/srv/restore-pvc-data",
},
"sourcePVC": {
MountPath: "/srv/source-data",
},
},
}).Return(&v1.Pod{
ObjectMeta: metav1.ObjectMeta{
Name: "pod",
},
}, nil),
f.createAppOps.EXPECT().WaitForPodReady(gomock.Any(), "ns", "pod").Return(nil),
)
},
errChecker: IsNil,
podChecker: NotNil,
pvcChecker: NotNil,
},
{
args: &types.FileRestoreArgs{
Namespace: "ns",
RunAsUser: 100,
FromSnapshotName: "vs",
},
sc: &sv1.StorageClass{
ObjectMeta: metav1.ObjectMeta{
Name: "sc",
},
},
fromSnapshot: &snapv1.VolumeSnapshot{
ObjectMeta: metav1.ObjectMeta{
Name: "vs",
},
@ -385,6 +483,7 @@ func (s *CSITestSuite) TestCreateInspectorApplicationForFileRestore(c *C) {
RestoreSize: &resourceQuantity,
},
},
fromPVC: &v1.PersistentVolumeClaim{},
prepare: func(f *fields) {
gomock.InOrder(
f.createAppOps.EXPECT().CreatePVC(gomock.Any(), &types.CreatePVCArgs{
@ -430,15 +529,16 @@ func (s *CSITestSuite) TestCreateInspectorApplicationForFileRestore(c *C) {
},
{
args: &types.FileRestoreArgs{
Namespace: "ns",
RunAsUser: 100,
Namespace: "ns",
RunAsUser: 100,
FromSnapshotName: "vs",
},
sc: &sv1.StorageClass{
ObjectMeta: metav1.ObjectMeta{
Name: "sc",
},
},
snapshot: &snapv1.VolumeSnapshot{
fromSnapshot: &snapv1.VolumeSnapshot{
ObjectMeta: metav1.ObjectMeta{
Name: "vs",
},
@ -446,6 +546,7 @@ func (s *CSITestSuite) TestCreateInspectorApplicationForFileRestore(c *C) {
RestoreSize: &resourceQuantity,
},
},
fromPVC: &v1.PersistentVolumeClaim{},
prepare: func(f *fields) {
gomock.InOrder(
f.createAppOps.EXPECT().CreatePVC(gomock.Any(), gomock.Any()).Return(&v1.PersistentVolumeClaim{
@ -462,15 +563,16 @@ func (s *CSITestSuite) TestCreateInspectorApplicationForFileRestore(c *C) {
},
{
args: &types.FileRestoreArgs{
Namespace: "ns",
RunAsUser: 100,
Namespace: "ns",
RunAsUser: 100,
FromSnapshotName: "vs",
},
sc: &sv1.StorageClass{
ObjectMeta: metav1.ObjectMeta{
Name: "sc",
},
},
snapshot: &snapv1.VolumeSnapshot{
fromSnapshot: &snapv1.VolumeSnapshot{
ObjectMeta: metav1.ObjectMeta{
Name: "vs",
},
@ -478,6 +580,7 @@ func (s *CSITestSuite) TestCreateInspectorApplicationForFileRestore(c *C) {
RestoreSize: &resourceQuantity,
},
},
fromPVC: &v1.PersistentVolumeClaim{},
prepare: func(f *fields) {
gomock.InOrder(
f.createAppOps.EXPECT().CreatePVC(gomock.Any(), gomock.Any()).Return(nil, fmt.Errorf("error")),
@ -515,7 +618,7 @@ func (s *CSITestSuite) TestCreateInspectorApplicationForFileRestore(c *C) {
},
},
}
pod, pvc, err := stepper.CreateInspectorApplication(ctx, tc.args, tc.snapshot, &sourcePVC, tc.sc)
pod, pvc, err := stepper.CreateInspectorApplication(ctx, tc.args, tc.fromSnapshot, tc.fromPVC, &sourcePVC, tc.sc)
c.Check(err, tc.errChecker)
c.Check(pod, tc.podChecker)
c.Check(pvc, tc.pvcChecker)
@ -532,11 +635,17 @@ func (s *CSITestSuite) TestFileRestoreCleanup(c *C) {
cleanerOps *mocks.MockCleaner
}
for _, tc := range []struct {
args *types.FileRestoreArgs
restorePVC *v1.PersistentVolumeClaim
pod *v1.Pod
prepare func(f *fields)
}{
{
args: &types.FileRestoreArgs{
Namespace: "ns",
RunAsUser: 100,
FromSnapshotName: "vs",
},
restorePVC: &v1.PersistentVolumeClaim{
ObjectMeta: metav1.ObjectMeta{
Name: "restorePVC",
@ -557,6 +666,37 @@ func (s *CSITestSuite) TestFileRestoreCleanup(c *C) {
},
},
{
args: &types.FileRestoreArgs{
Namespace: "ns",
RunAsUser: 100,
FromSnapshotName: "",
FromPVCName: "restorePVC",
ToPVCName: "sourcePVC",
},
restorePVC: &v1.PersistentVolumeClaim{
ObjectMeta: metav1.ObjectMeta{
Name: "restorePVC",
Namespace: "ns",
},
},
pod: &v1.Pod{
ObjectMeta: metav1.ObjectMeta{
Name: "pod",
Namespace: "ns",
},
},
prepare: func(f *fields) {
gomock.InOrder(
f.cleanerOps.EXPECT().DeletePod(ctx, "pod", "ns").Return(nil),
)
},
},
{
args: &types.FileRestoreArgs{
Namespace: "ns",
RunAsUser: 100,
FromSnapshotName: "vs",
},
restorePVC: &v1.PersistentVolumeClaim{
ObjectMeta: metav1.ObjectMeta{
Name: "restorePVC",
@ -589,6 +729,6 @@ func (s *CSITestSuite) TestFileRestoreCleanup(c *C) {
cleanerOps: f.cleanerOps,
SnapshotGroupVersion: groupversion,
}
stepper.Cleanup(ctx, tc.restorePVC, tc.pod)
stepper.Cleanup(ctx, tc.args, tc.restorePVC, tc.pod)
}
}

View file

@ -39,10 +39,10 @@ func (s *CSITestSuite) TestRunFileRestoreHelper(c *C) {
prepare: func(f *fields) {
gomock.InOrder(
f.stepperOps.EXPECT().ValidateArgs(gomock.Any(), gomock.Any()).Return(
&snapv1.VolumeSnapshot{}, &v1.PersistentVolumeClaim{}, &sv1.StorageClass{}, nil,
&snapv1.VolumeSnapshot{}, &v1.PersistentVolumeClaim{}, &v1.PersistentVolumeClaim{}, &sv1.StorageClass{}, nil,
),
f.stepperOps.EXPECT().CreateInspectorApplication(gomock.Any(), gomock.Any(),
&snapv1.VolumeSnapshot{}, &v1.PersistentVolumeClaim{}, &sv1.StorageClass{},
&snapv1.VolumeSnapshot{}, &v1.PersistentVolumeClaim{}, &v1.PersistentVolumeClaim{}, &sv1.StorageClass{},
).Return(
&v1.Pod{
ObjectMeta: metav1.ObjectMeta{
@ -66,7 +66,7 @@ func (s *CSITestSuite) TestRunFileRestoreHelper(c *C) {
},
}, gomock.Any(),
).Return(nil),
f.stepperOps.EXPECT().Cleanup(gomock.Any(),
f.stepperOps.EXPECT().Cleanup(gomock.Any(), gomock.Any(),
&v1.PersistentVolumeClaim{
ObjectMeta: metav1.ObjectMeta{
Name: "pvc1",
@ -91,10 +91,10 @@ func (s *CSITestSuite) TestRunFileRestoreHelper(c *C) {
args: &types.FileRestoreArgs{},
prepare: func(f *fields) {
gomock.InOrder(
f.stepperOps.EXPECT().ValidateArgs(gomock.Any(), gomock.Any()).Return(nil, nil, nil, nil),
f.stepperOps.EXPECT().CreateInspectorApplication(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(nil, nil, nil),
f.stepperOps.EXPECT().ValidateArgs(gomock.Any(), gomock.Any()).Return(nil, nil, nil, nil, nil),
f.stepperOps.EXPECT().CreateInspectorApplication(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(nil, nil, nil),
f.stepperOps.EXPECT().PortForwardAPod(gomock.Any(), gomock.Any()).Return(fmt.Errorf("portforward error")),
f.stepperOps.EXPECT().Cleanup(gomock.Any(), gomock.Any(), gomock.Any()),
f.stepperOps.EXPECT().Cleanup(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()),
)
},
errChecker: NotNil,
@ -106,9 +106,9 @@ func (s *CSITestSuite) TestRunFileRestoreHelper(c *C) {
args: &types.FileRestoreArgs{},
prepare: func(f *fields) {
gomock.InOrder(
f.stepperOps.EXPECT().ValidateArgs(gomock.Any(), gomock.Any()).Return(nil, nil, nil, nil),
f.stepperOps.EXPECT().CreateInspectorApplication(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(nil, nil, fmt.Errorf("createapp error")),
f.stepperOps.EXPECT().Cleanup(gomock.Any(), gomock.Any(), gomock.Any()),
f.stepperOps.EXPECT().ValidateArgs(gomock.Any(), gomock.Any()).Return(nil, nil, nil, nil, nil),
f.stepperOps.EXPECT().CreateInspectorApplication(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(nil, nil, fmt.Errorf("createapp error")),
f.stepperOps.EXPECT().Cleanup(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()),
)
},
errChecker: NotNil,
@ -120,8 +120,8 @@ func (s *CSITestSuite) TestRunFileRestoreHelper(c *C) {
args: &types.FileRestoreArgs{},
prepare: func(f *fields) {
gomock.InOrder(
f.stepperOps.EXPECT().ValidateArgs(gomock.Any(), gomock.Any()).Return(nil, nil, nil, fmt.Errorf("snapshot error")),
f.stepperOps.EXPECT().Cleanup(gomock.Any(), gomock.Any(), gomock.Any()),
f.stepperOps.EXPECT().ValidateArgs(gomock.Any(), gomock.Any()).Return(nil, nil, nil, nil, fmt.Errorf("snapshot error")),
f.stepperOps.EXPECT().Cleanup(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()),
)
},
errChecker: NotNil,
@ -133,8 +133,8 @@ func (s *CSITestSuite) TestRunFileRestoreHelper(c *C) {
args: &types.FileRestoreArgs{},
prepare: func(f *fields) {
gomock.InOrder(
f.stepperOps.EXPECT().ValidateArgs(gomock.Any(), gomock.Any()).Return(nil, nil, nil, fmt.Errorf("validate error")),
f.stepperOps.EXPECT().Cleanup(gomock.Any(), gomock.Any(), gomock.Any()),
f.stepperOps.EXPECT().ValidateArgs(gomock.Any(), gomock.Any()).Return(nil, nil, nil, nil, fmt.Errorf("validate error")),
f.stepperOps.EXPECT().Cleanup(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()),
)
},
errChecker: NotNil,
@ -146,7 +146,7 @@ func (s *CSITestSuite) TestRunFileRestoreHelper(c *C) {
args: &types.FileRestoreArgs{},
prepare: func(f *fields) {
gomock.InOrder(
f.stepperOps.EXPECT().Cleanup(gomock.Any(), gomock.Any(), gomock.Any()),
f.stepperOps.EXPECT().Cleanup(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()),
)
},
errChecker: NotNil,
@ -158,7 +158,7 @@ func (s *CSITestSuite) TestRunFileRestoreHelper(c *C) {
args: &types.FileRestoreArgs{},
prepare: func(f *fields) {
gomock.InOrder(
f.stepperOps.EXPECT().Cleanup(gomock.Any(), gomock.Any(), gomock.Any()),
f.stepperOps.EXPECT().Cleanup(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()),
)
},
errChecker: NotNil,
@ -187,6 +187,7 @@ func (s *CSITestSuite) TestFileRestoreRunner(c *C) {
r := &FileRestoreRunner{
restoreSteps: &fileRestoreSteps{},
}
err := r.RunFileRestoreHelper(ctx, nil)
args := types.FileRestoreArgs{}
err := r.RunFileRestoreHelper(ctx, &args)
c.Check(err, NotNil)
}

View file

@ -39,21 +39,21 @@ func (m *MockFileRestoreStepper) EXPECT() *MockFileRestoreStepperMockRecorder {
}
// Cleanup mocks base method.
func (m *MockFileRestoreStepper) Cleanup(arg0 context.Context, arg1 *v10.PersistentVolumeClaim, arg2 *v10.Pod) {
func (m *MockFileRestoreStepper) Cleanup(arg0 context.Context, arg1 *types.FileRestoreArgs, arg2 *v10.PersistentVolumeClaim, arg3 *v10.Pod) {
m.ctrl.T.Helper()
m.ctrl.Call(m, "Cleanup", arg0, arg1, arg2)
m.ctrl.Call(m, "Cleanup", arg0, arg1, arg2, arg3)
}
// Cleanup indicates an expected call of Cleanup.
func (mr *MockFileRestoreStepperMockRecorder) Cleanup(arg0, arg1, arg2 interface{}) *gomock.Call {
func (mr *MockFileRestoreStepperMockRecorder) Cleanup(arg0, arg1, arg2, arg3 interface{}) *gomock.Call {
mr.mock.ctrl.T.Helper()
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Cleanup", reflect.TypeOf((*MockFileRestoreStepper)(nil).Cleanup), arg0, arg1, arg2)
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Cleanup", reflect.TypeOf((*MockFileRestoreStepper)(nil).Cleanup), arg0, arg1, arg2, arg3)
}
// CreateInspectorApplication mocks base method.
func (m *MockFileRestoreStepper) CreateInspectorApplication(arg0 context.Context, arg1 *types.FileRestoreArgs, arg2 *v1.VolumeSnapshot, arg3 *v10.PersistentVolumeClaim, arg4 *v11.StorageClass) (*v10.Pod, *v10.PersistentVolumeClaim, error) {
func (m *MockFileRestoreStepper) CreateInspectorApplication(arg0 context.Context, arg1 *types.FileRestoreArgs, arg2 *v1.VolumeSnapshot, arg3, arg4 *v10.PersistentVolumeClaim, arg5 *v11.StorageClass) (*v10.Pod, *v10.PersistentVolumeClaim, error) {
m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "CreateInspectorApplication", arg0, arg1, arg2, arg3, arg4)
ret := m.ctrl.Call(m, "CreateInspectorApplication", arg0, arg1, arg2, arg3, arg4, arg5)
ret0, _ := ret[0].(*v10.Pod)
ret1, _ := ret[1].(*v10.PersistentVolumeClaim)
ret2, _ := ret[2].(error)
@ -61,9 +61,9 @@ func (m *MockFileRestoreStepper) CreateInspectorApplication(arg0 context.Context
}
// CreateInspectorApplication indicates an expected call of CreateInspectorApplication.
func (mr *MockFileRestoreStepperMockRecorder) CreateInspectorApplication(arg0, arg1, arg2, arg3, arg4 interface{}) *gomock.Call {
func (mr *MockFileRestoreStepperMockRecorder) CreateInspectorApplication(arg0, arg1, arg2, arg3, arg4, arg5 interface{}) *gomock.Call {
mr.mock.ctrl.T.Helper()
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "CreateInspectorApplication", reflect.TypeOf((*MockFileRestoreStepper)(nil).CreateInspectorApplication), arg0, arg1, arg2, arg3, arg4)
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "CreateInspectorApplication", reflect.TypeOf((*MockFileRestoreStepper)(nil).CreateInspectorApplication), arg0, arg1, arg2, arg3, arg4, arg5)
}
// ExecuteCopyCommand mocks base method.
@ -96,14 +96,15 @@ func (mr *MockFileRestoreStepperMockRecorder) PortForwardAPod(arg0, arg1 interfa
}
// ValidateArgs mocks base method.
func (m *MockFileRestoreStepper) ValidateArgs(arg0 context.Context, arg1 *types.FileRestoreArgs) (*v1.VolumeSnapshot, *v10.PersistentVolumeClaim, *v11.StorageClass, error) {
func (m *MockFileRestoreStepper) ValidateArgs(arg0 context.Context, arg1 *types.FileRestoreArgs) (*v1.VolumeSnapshot, *v10.PersistentVolumeClaim, *v10.PersistentVolumeClaim, *v11.StorageClass, error) {
m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "ValidateArgs", arg0, arg1)
ret0, _ := ret[0].(*v1.VolumeSnapshot)
ret1, _ := ret[1].(*v10.PersistentVolumeClaim)
ret2, _ := ret[2].(*v11.StorageClass)
ret3, _ := ret[3].(error)
return ret0, ret1, ret2, ret3
ret2, _ := ret[2].(*v10.PersistentVolumeClaim)
ret3, _ := ret[3].(*v11.StorageClass)
ret4, _ := ret[4].(error)
return ret0, ret1, ret2, ret3, ret4
}
// ValidateArgs indicates an expected call of ValidateArgs.

View file

@ -163,16 +163,23 @@ func (p *SnapshotBrowseArgs) Validate() error {
}
type FileRestoreArgs struct {
SnapshotName string
PVCName string
Namespace string
RunAsUser int64
LocalPort int
Path string
FromSnapshotName string
FromPVCName string
ToPVCName string
Namespace string
RunAsUser int64
LocalPort int
Path string
}
func (f *FileRestoreArgs) Validate() error {
if f.SnapshotName == "" || f.Namespace == "" {
if (f.FromSnapshotName == "" && f.FromPVCName == "") || (f.FromSnapshotName != "" && f.FromPVCName != "") {
return fmt.Errorf("Either --fromSnapshot or --fromPVC argument must be specified. Both cannot be specified together.")
}
if f.FromPVCName != "" && f.ToPVCName == "" {
return fmt.Errorf("--toPVC argument must be specified if using --fromPVC.")
}
if f.Namespace == "" {
return fmt.Errorf("Invalid FileRestoreArgs (%v)", f)
}
return nil