From 86a89d5425faa0f1a090abf4556597b741a6b2a0 Mon Sep 17 00:00:00 2001 From: Praveen M Date: Wed, 27 Mar 2024 16:48:56 +0530 Subject: [PATCH] cephfs: refactor code for improved reusability Signed-off-by: Praveen M --- internal/cephfs/controllerserver.go | 64 ++++++++++++----------------- 1 file changed, 27 insertions(+), 37 deletions(-) diff --git a/internal/cephfs/controllerserver.go b/internal/cephfs/controllerserver.go index e6de82ea5..28e3837e2 100644 --- a/internal/cephfs/controllerserver.go +++ b/internal/cephfs/controllerserver.go @@ -256,6 +256,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 @@ -376,25 +401,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 @@ -467,25 +474,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.