Skip to content

Commit

Permalink
Fixing errors in --path flag for kubestr file-restore
Browse files Browse the repository at this point in the history
  • Loading branch information
shlokc9 committed Sep 13, 2024
1 parent 9f6248b commit 1bc41f6
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 25 deletions.
35 changes: 20 additions & 15 deletions pkg/csi/file_restore_inspector.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -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)
}
Expand Down Expand Up @@ -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"
Expand All @@ -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,
Expand All @@ -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",
Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion pkg/csi/file_restore_inspector_steps_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
5 changes: 3 additions & 2 deletions pkg/csi/file_restore_inspector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ func (s *CSITestSuite) TestRunFileRestoreHelper(c *C) {
Namespace: "ns",
},
},
"",
nil,
),
f.stepperOps.EXPECT().PortForwardAPod(
Expand Down Expand Up @@ -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()),
)
Expand All @@ -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()),
)
},
Expand Down
15 changes: 8 additions & 7 deletions pkg/csi/mocks/mock_file_restore_stepper.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 1bc41f6

Please sign in to comment.