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

Fixing errors in --path flag for kubestr file-restore (#300)

This commit is contained in:
Shlok Chaudhari 2024-09-13 13:29:53 -05:00 committed by GitHub
parent 9f6248beb5
commit 93fd80991a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 32 additions and 25 deletions

View file

@ -68,18 +68,23 @@ func (f *FileRestoreRunner) RunFileRestoreHelper(ctx context.Context, args *type
f.snapshot = vs f.snapshot = vs
fmt.Println("Creating the browser pod & mounting the PVCs.") fmt.Println("Creating the browser pod & mounting the PVCs.")
f.pod, f.restorePVC, err = f.restoreSteps.CreateInspectorApplication(ctx, args, f.snapshot, restorePVC, sourcePVC, sc) var restoreMountPath string
f.pod, f.restorePVC, restoreMountPath, err = f.restoreSteps.CreateInspectorApplication(ctx, args, f.snapshot, restorePVC, sourcePVC, sc)
if err != nil { if err != nil {
return errors.Wrap(err, "Failed to create inspector application.") return errors.Wrap(err, "Failed to create inspector application.")
} }
if args.Path != "" { if args.Path != "" {
fmt.Printf("Restoring the file %s\n", args.Path) fmt.Printf("Restoring the file %s\n", args.Path)
_, err := f.restoreSteps.ExecuteCopyCommand(ctx, args, f.pod) _, err := f.restoreSteps.ExecuteCopyCommand(ctx, args, f.pod, restoreMountPath)
if err != nil { if err != nil {
return errors.Wrap(err, "Failed to execute cp command in pod.") return errors.Wrap(err, "Failed to execute cp command in pod.")
} }
fmt.Printf("File restored from VolumeSnapshot %s to Source PVC %s.\n", f.snapshot.Name, sourcePVC.Name) if args.FromSnapshotName != "" {
fmt.Printf("File restored from VolumeSnapshot %s to Source PVC %s.\n", f.snapshot.Name, sourcePVC.Name)
} else {
fmt.Printf("File restored from PVC %s to Source PVC %s.\n", f.restorePVC.Name, sourcePVC.Name)
}
return nil return nil
} }
@ -95,8 +100,8 @@ 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 //go:generate go run github.com/golang/mock/mockgen -destination=mocks/mock_file_restore_stepper.go -package=mocks . FileRestoreStepper
type FileRestoreStepper interface { type FileRestoreStepper interface {
ValidateArgs(ctx context.Context, args *types.FileRestoreArgs) (*snapv1.VolumeSnapshot, *v1.PersistentVolumeClaim, *v1.PersistentVolumeClaim, *sv1.StorageClass, 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) CreateInspectorApplication(ctx context.Context, args *types.FileRestoreArgs, snapshot *snapv1.VolumeSnapshot, restorePVC *v1.PersistentVolumeClaim, sourcePVC *v1.PersistentVolumeClaim, storageClass *sv1.StorageClass) (*v1.Pod, *v1.PersistentVolumeClaim, string, error)
ExecuteCopyCommand(ctx context.Context, args *types.FileRestoreArgs, pod *v1.Pod) (string, error) ExecuteCopyCommand(ctx context.Context, args *types.FileRestoreArgs, pod *v1.Pod, restoreMountPath string) (string, error)
PortForwardAPod(pod *v1.Pod, localPort int) error PortForwardAPod(pod *v1.Pod, localPort int) error
Cleanup(ctx context.Context, args *types.FileRestoreArgs, restorePVC *v1.PersistentVolumeClaim, pod *v1.Pod) Cleanup(ctx context.Context, args *types.FileRestoreArgs, restorePVC *v1.PersistentVolumeClaim, pod *v1.Pod)
} }
@ -189,8 +194,8 @@ func (f *fileRestoreSteps) ValidateArgs(ctx context.Context, args *types.FileRes
return snapshot, restorePVC, sourcePVC, sc, nil return snapshot, restorePVC, sourcePVC, sc, nil
} }
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) { 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, string, error) {
restoreMountPath := "/srv/restore-pvc-data" restoreMountPath := "/restore-pvc-data"
if args.FromSnapshotName != "" { if args.FromSnapshotName != "" {
snapshotAPIGroup := "snapshot.storage.k8s.io" snapshotAPIGroup := "snapshot.storage.k8s.io"
snapshotKind := "VolumeSnapshot" snapshotKind := "VolumeSnapshot"
@ -209,9 +214,9 @@ func (f *fileRestoreSteps) CreateInspectorApplication(ctx context.Context, args
var err error var err error
restorePVC, err = f.createAppOps.CreatePVC(ctx, pvcArgs) restorePVC, err = f.createAppOps.CreatePVC(ctx, pvcArgs)
if err != nil { if err != nil {
return nil, nil, errors.Wrap(err, "Failed to restore PVC") return nil, nil, "", errors.Wrap(err, "Failed to restore PVC")
} }
restoreMountPath = "/srv/snapshot-data" restoreMountPath = "/snapshot-data"
} }
podArgs := &types.CreatePodArgs{ podArgs := &types.CreatePodArgs{
GenerateName: clonedPodGenerateName, GenerateName: clonedPodGenerateName,
@ -221,7 +226,7 @@ func (f *fileRestoreSteps) CreateInspectorApplication(ctx context.Context, args
ContainerArgs: []string{"--noauth"}, ContainerArgs: []string{"--noauth"},
PVCMap: map[string]types.VolumePath{ PVCMap: map[string]types.VolumePath{
restorePVC.Name: { restorePVC.Name: {
MountPath: restoreMountPath, MountPath: fmt.Sprintf("/srv%s", restoreMountPath),
}, },
sourcePVC.Name: { sourcePVC.Name: {
MountPath: "/srv/source-data", MountPath: "/srv/source-data",
@ -248,16 +253,16 @@ func (f *fileRestoreSteps) CreateInspectorApplication(ctx context.Context, args
} }
pod, err := f.createAppOps.CreatePod(ctx, podArgs) pod, err := f.createAppOps.CreatePod(ctx, podArgs)
if err != nil { if err != nil {
return nil, restorePVC, errors.Wrap(err, "Failed to create browse Pod") return nil, restorePVC, "", errors.Wrap(err, "Failed to create browse Pod")
} }
if err = f.createAppOps.WaitForPodReady(ctx, args.Namespace, pod.Name); err != nil { if err = f.createAppOps.WaitForPodReady(ctx, args.Namespace, pod.Name); err != nil {
return pod, restorePVC, errors.Wrap(err, "Pod failed to become ready") return pod, restorePVC, "", errors.Wrap(err, "Pod failed to become ready")
} }
return pod, restorePVC, nil return pod, restorePVC, restoreMountPath, nil
} }
func (f *fileRestoreSteps) ExecuteCopyCommand(ctx context.Context, args *types.FileRestoreArgs, pod *v1.Pod) (string, error) { func (f *fileRestoreSteps) ExecuteCopyCommand(ctx context.Context, args *types.FileRestoreArgs, pod *v1.Pod, restoreMountPath string) (string, error) {
command := []string{"cp", "-rf", fmt.Sprintf("/snapshot-data%s", args.Path), fmt.Sprintf("/source-data%s", args.Path)} command := []string{"cp", "-rf", fmt.Sprintf("%s%s", restoreMountPath, args.Path), fmt.Sprintf("/source-data%s", args.Path)}
stdout, err := f.kubeExecutor.Exec(ctx, args.Namespace, pod.Name, pod.Spec.Containers[0].Name, command) stdout, err := f.kubeExecutor.Exec(ctx, args.Namespace, pod.Name, pod.Spec.Containers[0].Name, command)
if err != nil { if err != nil {
return "", errors.Wrapf(err, "Error running command:(%v)", command) return "", errors.Wrapf(err, "Error running command:(%v)", command)

View file

@ -618,7 +618,7 @@ func (s *CSITestSuite) TestCreateInspectorApplicationForFileRestore(c *C) {
}, },
}, },
} }
pod, pvc, err := stepper.CreateInspectorApplication(ctx, tc.args, tc.fromSnapshot, tc.fromPVC, &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(err, tc.errChecker)
c.Check(pod, tc.podChecker) c.Check(pod, tc.podChecker)
c.Check(pvc, tc.pvcChecker) c.Check(pvc, tc.pvcChecker)

View file

@ -56,6 +56,7 @@ func (s *CSITestSuite) TestRunFileRestoreHelper(c *C) {
Namespace: "ns", Namespace: "ns",
}, },
}, },
"",
nil, nil,
), ),
f.stepperOps.EXPECT().PortForwardAPod( f.stepperOps.EXPECT().PortForwardAPod(
@ -92,7 +93,7 @@ func (s *CSITestSuite) TestRunFileRestoreHelper(c *C) {
prepare: func(f *fields) { prepare: func(f *fields) {
gomock.InOrder( gomock.InOrder(
f.stepperOps.EXPECT().ValidateArgs(gomock.Any(), gomock.Any()).Return(nil, nil, 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().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().PortForwardAPod(gomock.Any(), gomock.Any()).Return(fmt.Errorf("portforward error")),
f.stepperOps.EXPECT().Cleanup(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()), f.stepperOps.EXPECT().Cleanup(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()),
) )
@ -107,7 +108,7 @@ func (s *CSITestSuite) TestRunFileRestoreHelper(c *C) {
prepare: func(f *fields) { prepare: func(f *fields) {
gomock.InOrder( gomock.InOrder(
f.stepperOps.EXPECT().ValidateArgs(gomock.Any(), gomock.Any()).Return(nil, nil, 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, fmt.Errorf("createapp error")), 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()), f.stepperOps.EXPECT().Cleanup(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()),
) )
}, },

View file

@ -51,13 +51,14 @@ func (mr *MockFileRestoreStepperMockRecorder) Cleanup(arg0, arg1, arg2, arg3 int
} }
// CreateInspectorApplication mocks base method. // CreateInspectorApplication mocks base method.
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) { func (m *MockFileRestoreStepper) CreateInspectorApplication(arg0 context.Context, arg1 *types.FileRestoreArgs, arg2 *v1.VolumeSnapshot, arg3, arg4 *v10.PersistentVolumeClaim, arg5 *v11.StorageClass) (*v10.Pod, *v10.PersistentVolumeClaim, string, error) {
m.ctrl.T.Helper() m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "CreateInspectorApplication", arg0, arg1, arg2, arg3, arg4, arg5) ret := m.ctrl.Call(m, "CreateInspectorApplication", arg0, arg1, arg2, arg3, arg4, arg5)
ret0, _ := ret[0].(*v10.Pod) ret0, _ := ret[0].(*v10.Pod)
ret1, _ := ret[1].(*v10.PersistentVolumeClaim) ret1, _ := ret[1].(*v10.PersistentVolumeClaim)
ret2, _ := ret[2].(error) ret2, _ := ret[2].(string)
return ret0, ret1, ret2 ret3, _ := ret[3].(error)
return ret0, ret1, ret2, ret3
} }
// CreateInspectorApplication indicates an expected call of CreateInspectorApplication. // CreateInspectorApplication indicates an expected call of CreateInspectorApplication.
@ -67,18 +68,18 @@ func (mr *MockFileRestoreStepperMockRecorder) CreateInspectorApplication(arg0, a
} }
// ExecuteCopyCommand mocks base method. // ExecuteCopyCommand mocks base method.
func (m *MockFileRestoreStepper) ExecuteCopyCommand(arg0 context.Context, arg1 *types.FileRestoreArgs, arg2 *v10.Pod) (string, error) { func (m *MockFileRestoreStepper) ExecuteCopyCommand(arg0 context.Context, arg1 *types.FileRestoreArgs, arg2 *v10.Pod, arg3 string) (string, error) {
m.ctrl.T.Helper() m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "ExecuteCopyCommand", arg0, arg1, arg2) ret := m.ctrl.Call(m, "ExecuteCopyCommand", arg0, arg1, arg2, arg3)
ret0, _ := ret[0].(string) ret0, _ := ret[0].(string)
ret1, _ := ret[1].(error) ret1, _ := ret[1].(error)
return ret0, ret1 return ret0, ret1
} }
// ExecuteCopyCommand indicates an expected call of ExecuteCopyCommand. // ExecuteCopyCommand indicates an expected call of ExecuteCopyCommand.
func (mr *MockFileRestoreStepperMockRecorder) ExecuteCopyCommand(arg0, arg1, arg2 interface{}) *gomock.Call { func (mr *MockFileRestoreStepperMockRecorder) ExecuteCopyCommand(arg0, arg1, arg2, arg3 interface{}) *gomock.Call {
mr.mock.ctrl.T.Helper() mr.mock.ctrl.T.Helper()
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "ExecuteCopyCommand", reflect.TypeOf((*MockFileRestoreStepper)(nil).ExecuteCopyCommand), arg0, arg1, arg2) return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "ExecuteCopyCommand", reflect.TypeOf((*MockFileRestoreStepper)(nil).ExecuteCopyCommand), arg0, arg1, arg2, arg3)
} }
// PortForwardAPod mocks base method. // PortForwardAPod mocks base method.