-
Notifications
You must be signed in to change notification settings - Fork 275
feat: volume clone from source volume #426
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
k8s-ci-robot
merged 3 commits into
kubernetes-csi:master
from
wozniakjan:volume_to_volume_copy
Mar 15, 2023
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
--- | ||
apiVersion: v1 | ||
kind: PersistentVolumeClaim | ||
metadata: | ||
name: pvc-nfs-clone | ||
namespace: default | ||
spec: | ||
accessModes: | ||
- ReadWriteMany | ||
resources: | ||
requests: | ||
storage: 10Gi | ||
storageClassName: nfs-csi | ||
dataSource: | ||
kind: PersistentVolumeClaim | ||
name: pvc-nfs-dynamic |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
@@ -19,6 +19,7 @@ package nfs | |||||||||
import ( | ||||||||||
"fmt" | ||||||||||
"os" | ||||||||||
"os/exec" | ||||||||||
"path/filepath" | ||||||||||
"regexp" | ||||||||||
"strconv" | ||||||||||
|
@@ -143,12 +144,19 @@ func (cs *ControllerServer) CreateVolume(ctx context.Context, req *csi.CreateVol | |||||||||
} | ||||||||||
} | ||||||||||
|
||||||||||
if req.GetVolumeContentSource() != nil { | ||||||||||
if err := cs.copyVolume(ctx, req, nfsVol); err != nil { | ||||||||||
return nil, err | ||||||||||
} | ||||||||||
} | ||||||||||
|
||||||||||
setKeyValueInMap(parameters, paramSubDir, nfsVol.subDir) | ||||||||||
return &csi.CreateVolumeResponse{ | ||||||||||
Volume: &csi.Volume{ | ||||||||||
VolumeId: nfsVol.id, | ||||||||||
CapacityBytes: 0, // by setting it to zero, Provisioner will use PVC requested size as PV size | ||||||||||
VolumeContext: parameters, | ||||||||||
ContentSource: req.GetVolumeContentSource(), | ||||||||||
}, | ||||||||||
}, nil | ||||||||||
} | ||||||||||
|
@@ -307,6 +315,58 @@ func (cs *ControllerServer) internalUnmount(ctx context.Context, vol *nfsVolume) | |||||||||
return err | ||||||||||
} | ||||||||||
|
||||||||||
func (cs *ControllerServer) copyFromVolume(ctx context.Context, req *csi.CreateVolumeRequest, dstVol *nfsVolume) error { | ||||||||||
srcVol, err := getNfsVolFromID(req.GetVolumeContentSource().GetVolume().GetVolumeId()) | ||||||||||
if err != nil { | ||||||||||
return status.Error(codes.NotFound, err.Error()) | ||||||||||
} | ||||||||||
// Note that the source path must include trailing '/.', can't use 'filepath.Join()' as it performs path cleaning | ||||||||||
srcPath := fmt.Sprintf("%v/.", getInternalVolumePath(cs.Driver.workingMountDir, srcVol)) | ||||||||||
dstPath := getInternalVolumePath(cs.Driver.workingMountDir, dstVol) | ||||||||||
klog.V(2).Infof("copy volume from volume %v -> %v", srcPath, dstPath) | ||||||||||
|
||||||||||
var volCap *csi.VolumeCapability | ||||||||||
if len(req.GetVolumeCapabilities()) > 0 { | ||||||||||
volCap = req.GetVolumeCapabilities()[0] | ||||||||||
} | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am not sure if req capabilities apply for both src and dst volume internal mounting. I took this from here: csi-driver-nfs/pkg/nfs/controllerserver.go Lines 120 to 123 in 5edf03c
|
||||||||||
if err = cs.internalMount(ctx, srcVol, nil, volCap); err != nil { | ||||||||||
return status.Errorf(codes.Internal, "failed to mount src nfs server: %v", err.Error()) | ||||||||||
} | ||||||||||
defer func() { | ||||||||||
if err = cs.internalUnmount(ctx, srcVol); err != nil { | ||||||||||
klog.Warningf("failed to unmount nfs server: %v", err.Error()) | ||||||||||
} | ||||||||||
}() | ||||||||||
if err = cs.internalMount(ctx, dstVol, nil, volCap); err != nil { | ||||||||||
return status.Errorf(codes.Internal, "failed to mount dst nfs server: %v", err.Error()) | ||||||||||
} | ||||||||||
defer func() { | ||||||||||
if err = cs.internalUnmount(ctx, dstVol); err != nil { | ||||||||||
klog.Warningf("failed to unmount dst nfs server: %v", err.Error()) | ||||||||||
} | ||||||||||
}() | ||||||||||
|
||||||||||
// recursive 'cp' with '-a' to handle symlinks | ||||||||||
out, err := exec.Command("cp", "-a", srcPath, dstPath).CombinedOutput() | ||||||||||
if err != nil { | ||||||||||
return status.Error(codes.Internal, fmt.Sprintf("%v: %v", err, string(out))) | ||||||||||
} | ||||||||||
klog.V(2).Infof("copied %s -> %s", srcPath, dstPath) | ||||||||||
return nil | ||||||||||
} | ||||||||||
|
||||||||||
func (cs *ControllerServer) copyVolume(ctx context.Context, req *csi.CreateVolumeRequest, vol *nfsVolume) error { | ||||||||||
wozniakjan marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||
vs := req.VolumeContentSource | ||||||||||
switch vs.Type.(type) { | ||||||||||
case *csi.VolumeContentSource_Snapshot: | ||||||||||
return status.Error(codes.Unimplemented, "Currently only volume copy from another volume is supported") | ||||||||||
case *csi.VolumeContentSource_Volume: | ||||||||||
return cs.copyFromVolume(ctx, req, vol) | ||||||||||
default: | ||||||||||
return status.Errorf(codes.InvalidArgument, "%v not a proper volume source", vs) | ||||||||||
} | ||||||||||
} | ||||||||||
|
||||||||||
// newNFSVolume Convert VolumeCreate parameters to an nfsVolume | ||||||||||
func newNFSVolume(name string, size int64, params map[string]string) (*nfsVolume, error) { | ||||||||||
var server, baseDir, subDir string | ||||||||||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.