Skip to content

Commit

Permalink
cephfs: refactor code for improved reusability
Browse files Browse the repository at this point in the history
Signed-off-by: Praveen M <m.praveen@ibm.com>
  • Loading branch information
iPraveenParihar committed Mar 27, 2024
1 parent ed046fd commit 442ba67
Showing 1 changed file with 27 additions and 37 deletions.
64 changes: 27 additions & 37 deletions internal/cephfs/controllerserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,31 @@ func checkValidCreateVolumeRequest(
return nil
}

func buildCreateVolumeResponse(
req *csi.CreateVolumeRequest,
volOptions *store.VolumeOptions,
vID *store.VolumeIdentifier,
) *csi.CreateVolumeResponse {
volumeContext := util.GetVolumeContext(req.GetParameters())
volumeContext["subvolumeName"] = vID.FsSubvolName
volumeContext["subvolumePath"] = volOptions.RootPath
volume := &csi.Volume{
VolumeId: vID.VolumeID,
CapacityBytes: volOptions.Size,
ContentSource: req.GetVolumeContentSource(),
VolumeContext: volumeContext,
}
if volOptions.Topology != nil {
volume.AccessibleTopology = []*csi.Topology{
{
Segments: volOptions.Topology,
},
}
}

return &csi.CreateVolumeResponse{Volume: volume}
}

// CreateVolume creates a reservation and the volume in backend, if it is not already present.
//
//nolint:gocognit,gocyclo,nestif,cyclop // TODO: reduce complexity
Expand Down Expand Up @@ -372,25 +397,7 @@ func (cs *ControllerServer) CreateVolume(
}
}

// remove kubernetes csi prefixed parameters.
volumeContext := k8s.RemoveCSIPrefixedParameters(req.GetParameters())
volumeContext["subvolumeName"] = vID.FsSubvolName
volumeContext["subvolumePath"] = volOptions.RootPath
volume := &csi.Volume{
VolumeId: vID.VolumeID,
CapacityBytes: volOptions.Size,
ContentSource: req.GetVolumeContentSource(),
VolumeContext: volumeContext,
}
if volOptions.Topology != nil {
volume.AccessibleTopology = []*csi.Topology{
{
Segments: volOptions.Topology,
},
}
}

return &csi.CreateVolumeResponse{Volume: volume}, nil
return buildCreateVolumeResponse(req, volOptions, vID), nil
}

// Reservation
Expand Down Expand Up @@ -463,25 +470,8 @@ func (cs *ControllerServer) CreateVolume(

log.DebugLog(ctx, "cephfs: successfully created backing volume named %s for request name %s",
vID.FsSubvolName, requestName)
// remove kubernetes csi prefixed parameters.
volumeContext := k8s.RemoveCSIPrefixedParameters(req.GetParameters())
volumeContext["subvolumeName"] = vID.FsSubvolName
volumeContext["subvolumePath"] = volOptions.RootPath
volume := &csi.Volume{
VolumeId: vID.VolumeID,
CapacityBytes: volOptions.Size,
ContentSource: req.GetVolumeContentSource(),
VolumeContext: volumeContext,
}
if volOptions.Topology != nil {
volume.AccessibleTopology = []*csi.Topology{
{
Segments: volOptions.Topology,
},
}
}

return &csi.CreateVolumeResponse{Volume: volume}, nil
return buildCreateVolumeResponse(req, volOptions, vID), nil
}

// DeleteVolume deletes the volume in backend and its reservation.
Expand Down

0 comments on commit 442ba67

Please sign in to comment.