Skip to content

Commit

Permalink
controller: refact the duplcated codes for the clone operation
Browse files Browse the repository at this point in the history
    - also correct the description of command

Signed-off-by: Vicente Cheng <vicente.cheng@suse.com>
  • Loading branch information
Vicente-Cheng committed Sep 18, 2024
1 parent 4e3dacd commit 94e7c8c
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 57 deletions.
6 changes: 3 additions & 3 deletions cmd/provisioner/createsnap.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func createSnapCmd() *cli.Command {
Flags: []cli.Flag{
&cli.StringFlag{
Name: flagSnapName,
Usage: "Required. Specify lv name.",
Usage: "Required. Specify snapshot name.",
},
&cli.Int64Flag{
Name: flagLVSize,
Expand All @@ -28,7 +28,7 @@ func createSnapCmd() *cli.Command {
},
&cli.StringFlag{
Name: flagLVName,
Usage: "Required. the name of the volumegroup",
Usage: "Required. Specify the logical volume name.",
},
&cli.StringFlag{
Name: flagLVMType,
Expand Down Expand Up @@ -60,7 +60,7 @@ func createSnap(c *cli.Context) error {
}
snapName := c.String(flagSnapName)
if snapName == "" {
return fmt.Errorf("invalid empty flag %v", flagLVMType)
return fmt.Errorf("invalid empty flag %v", flagSnapName)
}
lvType := c.String(flagLVMType)
if lvType == "" {
Expand Down
4 changes: 2 additions & 2 deletions cmd/provisioner/deletesnap.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func deleteSnapCmd() *cli.Command {
Flags: []cli.Flag{
&cli.StringFlag{
Name: flagSnapName,
Usage: "Required. Specify lv name.",
Usage: "Required. Specify snapshot name.",
},
&cli.StringFlag{
Name: flagVGName,
Expand All @@ -40,7 +40,7 @@ func deleteSnap(c *cli.Context) error {
}
snapName := c.String(flagSnapName)
if snapName == "" {
return fmt.Errorf("invalid empty flag %v", flagLVMType)
return fmt.Errorf("invalid empty flag %v", flagSnapName)
}

klog.Infof("delete snapshot: %s/%s", vgName, snapName)
Expand Down
81 changes: 29 additions & 52 deletions pkg/lvm/controllerserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,36 +195,27 @@ func (cs *controllerServer) CreateVolume(ctx context.Context, req *csi.CreateVol
}, nil
}

func (cs *controllerServer) cloneFromSnapshot(ctx context.Context, snapContent *snapv1.VolumeSnapshotContent, dstName, dstNode, dstLVType, dstVGName string, dstSize int64) error {
srcVolID := *snapContent.Spec.Source.VolumeHandle
srcVol, err := cs.kubeClient.CoreV1().PersistentVolumes().Get(ctx, srcVolID, metav1.GetOptions{})
if err != nil {
klog.Errorf("error getting volume: %v", err)
return err
}
func (cs *controllerServer) generateVolumeActionForClone(srcVol *v1.PersistentVolume, srcLVName, dstName, dstNode, dstLVType, dstVGName string, srcSize, dstSize int64) (volumeAction, error) {
ns := srcVol.Spec.NodeAffinity.Required.NodeSelectorTerms
srcNode := ns[0].MatchExpressions[0].Values[0]
srcVgName := srcVol.Spec.CSI.VolumeAttributes["vgName"]
srcVgType := srcVol.Spec.CSI.VolumeAttributes["type"]
restoreSize := *snapContent.Status.RestoreSize
srcType := srcVol.Spec.CSI.VolumeAttributes["type"]

snapshotLVName := fmt.Sprintf("lvm-%s", *snapContent.Status.SnapshotHandle)
//srcSnapDev := fmt.Sprintf("/dev/%s/%s", srcVgName, snapshotLVName)
srcInfo := &srcInfo{
srcLVName: snapshotLVName,
srcLVName: srcLVName,
srcVGName: srcVgName,
srcType: srcVgType,
srcType: srcType,
}
klog.V(4).Infof("cloning volume from %s/%s ", srcVgName, snapshotLVName)
klog.V(4).Infof("cloning volume from %s/%s ", srcVgName, srcLVName)

if restoreSize > dstSize {
return status.Error(codes.InvalidArgument, "source volume size is larger than destination volume size")
if srcSize > dstSize {
return volumeAction{}, status.Errorf(codes.InvalidArgument, "source/snapshot volume size(%v) is larger than destination volume size(%v)", srcSize, dstSize)
}
if srcNode != dstNode {
return status.Errorf(codes.InvalidArgument, "source (%s) and destination (%s) nodes are different (not supported)", srcNode, dstNode)
return volumeAction{}, status.Errorf(codes.InvalidArgument, "source (%s) and destination (%s) nodes are different (not supported)", srcNode, dstNode)
}

va := volumeAction{
return volumeAction{
action: actionTypeClone,
name: dstName,
nodeName: dstNode,
Expand All @@ -237,7 +228,23 @@ func (cs *controllerServer) cloneFromSnapshot(ctx context.Context, snapContent *
vgName: dstVGName,
hostWritePath: cs.hostWritePath,
srcInfo: srcInfo,
}, nil
}

func (cs *controllerServer) cloneFromSnapshot(ctx context.Context, snapContent *snapv1.VolumeSnapshotContent, dstName, dstNode, dstLVType, dstVGName string, dstSize int64) error {
srcVolID := *snapContent.Spec.Source.VolumeHandle
srcVol, err := cs.kubeClient.CoreV1().PersistentVolumes().Get(ctx, srcVolID, metav1.GetOptions{})
if err != nil {
klog.Errorf("error getting volume: %v", err)
return err
}
restoreSize := *snapContent.Status.RestoreSize
snapshotLVName := fmt.Sprintf("lvm-%s", *snapContent.Status.SnapshotHandle)
va, err := cs.generateVolumeActionForClone(srcVol, snapshotLVName, dstName, dstNode, dstLVType, dstVGName, restoreSize, dstSize)
if err != nil {
return err
}

if err := createProvisionerPod(ctx, va); err != nil {
klog.Errorf("error creating provisioner pod :%v", err)
return err
Expand All @@ -247,47 +254,17 @@ func (cs *controllerServer) cloneFromSnapshot(ctx context.Context, snapContent *
}

func (cs *controllerServer) cloneFromVolume(ctx context.Context, srcVol *v1.PersistentVolume, dstName, dstNode, dstLVType, dstVGName string, dstSize int64) error {
ns := srcVol.Spec.NodeAffinity.Required.NodeSelectorTerms
srcNode := ns[0].MatchExpressions[0].Values[0]
srcVgName := srcVol.Spec.CSI.VolumeAttributes["vgName"]
srcType := srcVol.Spec.CSI.VolumeAttributes["type"]
srcSizeStr := srcVol.Spec.CSI.VolumeAttributes["RequiredBytes"]
srcSize, err := strconv.ParseInt(srcSizeStr, 10, 64)
if err != nil {
klog.Errorf("error parsing srcSize: %v", err)
return err
return status.Errorf(codes.InvalidArgument, "error parsing srcSize: %v", err)
}

//srcDev := fmt.Sprintf("/dev/%s/%s", srcVgName, srcVol.GetName())
srcLVName := srcVol.GetName()
srcInfo := &srcInfo{
srcLVName: srcLVName,
srcVGName: srcVgName,
srcType: srcType,
}
klog.V(4).Infof("cloning volume from %s/%s ", srcVgName, srcLVName)

if srcSize > dstSize {
return status.Error(codes.InvalidArgument, "source volume size is larger than destination volume size")
}
if srcNode != dstNode {
return status.Errorf(codes.InvalidArgument, "source (%s) and destination (%s) nodes are different (not supported)", srcNode, dstNode)
va, err := cs.generateVolumeActionForClone(srcVol, srcLVName, dstName, dstNode, dstLVType, dstVGName, srcSize, dstSize)
if err != nil {
return err
}

va := volumeAction{
action: actionTypeClone,
name: dstName,
nodeName: dstNode,
size: dstSize,
lvmType: dstLVType,
pullPolicy: cs.pullPolicy,
provisionerImage: cs.provisionerImage,
kubeClient: cs.kubeClient,
namespace: cs.namespace,
vgName: dstVGName,
hostWritePath: cs.hostWritePath,
srcInfo: srcInfo,
}
if err := createProvisionerPod(ctx, va); err != nil {
klog.Errorf("error creating provisioner pod :%v", err)
return err
Expand Down

0 comments on commit 94e7c8c

Please sign in to comment.