From 1bc41f664a55a83b9afe1c757d1939e735265c95 Mon Sep 17 00:00:00 2001 From: Shlok Chaudhari Date: Thu, 12 Sep 2024 22:52:02 -0500 Subject: [PATCH] Fixing errors in --path flag for kubestr file-restore --- pkg/csi/file_restore_inspector.go | 35 +++++++++++--------- pkg/csi/file_restore_inspector_steps_test.go | 2 +- pkg/csi/file_restore_inspector_test.go | 5 +-- pkg/csi/mocks/mock_file_restore_stepper.go | 15 +++++---- 4 files changed, 32 insertions(+), 25 deletions(-) diff --git a/pkg/csi/file_restore_inspector.go b/pkg/csi/file_restore_inspector.go index 85d5799..8d508a8 100644 --- a/pkg/csi/file_restore_inspector.go +++ b/pkg/csi/file_restore_inspector.go @@ -68,18 +68,23 @@ func (f *FileRestoreRunner) RunFileRestoreHelper(ctx context.Context, args *type f.snapshot = vs 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 { return errors.Wrap(err, "Failed to create inspector application.") } if 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 { 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 } @@ -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 type FileRestoreStepper interface { 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) + 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, restoreMountPath string) (string, error) PortForwardAPod(pod *v1.Pod, localPort int) error 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 } -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" +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 := "/restore-pvc-data" if args.FromSnapshotName != "" { snapshotAPIGroup := "snapshot.storage.k8s.io" snapshotKind := "VolumeSnapshot" @@ -209,9 +214,9 @@ func (f *fileRestoreSteps) CreateInspectorApplication(ctx context.Context, args var err error restorePVC, err = f.createAppOps.CreatePVC(ctx, pvcArgs) 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{ GenerateName: clonedPodGenerateName, @@ -221,7 +226,7 @@ func (f *fileRestoreSteps) CreateInspectorApplication(ctx context.Context, args ContainerArgs: []string{"--noauth"}, PVCMap: map[string]types.VolumePath{ restorePVC.Name: { - MountPath: restoreMountPath, + MountPath: fmt.Sprintf("/srv%s", restoreMountPath), }, sourcePVC.Name: { MountPath: "/srv/source-data", @@ -248,16 +253,16 @@ func (f *fileRestoreSteps) CreateInspectorApplication(ctx context.Context, args } pod, err := f.createAppOps.CreatePod(ctx, podArgs) 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 { - 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) { - command := []string{"cp", "-rf", fmt.Sprintf("/snapshot-data%s", args.Path), fmt.Sprintf("/source-data%s", args.Path)} +func (f *fileRestoreSteps) ExecuteCopyCommand(ctx context.Context, args *types.FileRestoreArgs, pod *v1.Pod, restoreMountPath string) (string, error) { + 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) if err != nil { return "", errors.Wrapf(err, "Error running command:(%v)", command) diff --git a/pkg/csi/file_restore_inspector_steps_test.go b/pkg/csi/file_restore_inspector_steps_test.go index e7542a2..4a08759 100644 --- a/pkg/csi/file_restore_inspector_steps_test.go +++ b/pkg/csi/file_restore_inspector_steps_test.go @@ -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(pod, tc.podChecker) c.Check(pvc, tc.pvcChecker) diff --git a/pkg/csi/file_restore_inspector_test.go b/pkg/csi/file_restore_inspector_test.go index fd2f227..901cba5 100644 --- a/pkg/csi/file_restore_inspector_test.go +++ b/pkg/csi/file_restore_inspector_test.go @@ -56,6 +56,7 @@ func (s *CSITestSuite) TestRunFileRestoreHelper(c *C) { Namespace: "ns", }, }, + "", nil, ), f.stepperOps.EXPECT().PortForwardAPod( @@ -92,7 +93,7 @@ func (s *CSITestSuite) TestRunFileRestoreHelper(c *C) { prepare: func(f *fields) { gomock.InOrder( 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().Cleanup(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()), ) @@ -107,7 +108,7 @@ func (s *CSITestSuite) TestRunFileRestoreHelper(c *C) { prepare: func(f *fields) { gomock.InOrder( 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()), ) }, diff --git a/pkg/csi/mocks/mock_file_restore_stepper.go b/pkg/csi/mocks/mock_file_restore_stepper.go index 541dc73..500db99 100644 --- a/pkg/csi/mocks/mock_file_restore_stepper.go +++ b/pkg/csi/mocks/mock_file_restore_stepper.go @@ -51,13 +51,14 @@ func (mr *MockFileRestoreStepperMockRecorder) Cleanup(arg0, arg1, arg2, arg3 int } // 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() 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) - return ret0, ret1, ret2 + ret2, _ := ret[2].(string) + ret3, _ := ret[3].(error) + return ret0, ret1, ret2, ret3 } // CreateInspectorApplication indicates an expected call of CreateInspectorApplication. @@ -67,18 +68,18 @@ func (mr *MockFileRestoreStepperMockRecorder) CreateInspectorApplication(arg0, a } // 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() - ret := m.ctrl.Call(m, "ExecuteCopyCommand", arg0, arg1, arg2) + ret := m.ctrl.Call(m, "ExecuteCopyCommand", arg0, arg1, arg2, arg3) ret0, _ := ret[0].(string) ret1, _ := ret[1].(error) return ret0, ret1 } // 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() - 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.