Skip to content

Commit 3973588

Browse files
committed
Add hostPath.createSnapshotFromVolume() helper function
1 parent 9774cb0 commit 3973588

File tree

2 files changed

+20
-14
lines changed

2 files changed

+20
-14
lines changed

pkg/hostpath/controllerserver.go

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ import (
3434

3535
"github.com/container-storage-interface/spec/lib/go/csi"
3636
"k8s.io/klog/v2"
37-
utilexec "k8s.io/utils/exec"
3837

3938
"github.com/kubernetes-csi/csi-driver-host-path/pkg/state"
4039
)
@@ -543,21 +542,10 @@ func (hp *hostPath) CreateSnapshot(ctx context.Context, req *csi.CreateSnapshotR
543542

544543
snapshotID := uuid.NewUUID().String()
545544
creationTime := ptypes.TimestampNow()
546-
volPath := hostPathVolume.VolPath
547545
file := hp.getSnapshotPath(snapshotID)
548546

549-
var cmd []string
550-
if hostPathVolume.VolAccessType == state.BlockAccess {
551-
glog.V(4).Infof("Creating snapshot of Raw Block Mode Volume")
552-
cmd = []string{"cp", volPath, file}
553-
} else {
554-
glog.V(4).Infof("Creating snapshot of Filsystem Mode Volume")
555-
cmd = []string{"tar", "czf", file, "-C", volPath, "."}
556-
}
557-
executor := utilexec.New()
558-
out, err := executor.Command(cmd[0], cmd[1:]...).CombinedOutput()
559-
if err != nil {
560-
return nil, fmt.Errorf("failed create snapshot: %w: %s", err, out)
547+
if err := hp.createSnapshotFromVolume(hostPathVolume, file); err != nil {
548+
return nil, err
561549
}
562550

563551
glog.V(4).Infof("create volume snapshot %s", file)

pkg/hostpath/hostpath.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -377,3 +377,21 @@ func (hp *hostPath) getAttachCount() int64 {
377377
}
378378
return count
379379
}
380+
381+
func (hp *hostPath) createSnapshotFromVolume(vol state.Volume, file string) error {
382+
var cmd []string
383+
if vol.VolAccessType == state.BlockAccess {
384+
glog.V(4).Infof("Creating snapshot of Raw Block Mode Volume")
385+
cmd = []string{"cp", vol.VolPath, file}
386+
} else {
387+
glog.V(4).Infof("Creating snapshot of Filsystem Mode Volume")
388+
cmd = []string{"tar", "czf", file, "-C", vol.VolPath, "."}
389+
}
390+
executor := utilexec.New()
391+
out, err := executor.Command(cmd[0], cmd[1:]...).CombinedOutput()
392+
if err != nil {
393+
return fmt.Errorf("failed create snapshot: %w: %s", err, out)
394+
}
395+
396+
return nil
397+
}

0 commit comments

Comments
 (0)