From 2803ec12900615c33d5afe4259beaddd5cdebcc5 Mon Sep 17 00:00:00 2001 From: Niels de Vos Date: Fri, 5 Jul 2024 10:26:47 +0200 Subject: [PATCH] rbd: add a ToCSI() function to the Volume interface A VolumeGroup CSI-Addons object contains a list of CSI Volumes. A ToCSI() function makes creating such a list much simpler. Signed-off-by: Niels de Vos --- internal/rbd/controllerserver.go | 48 +++++++++++++++++++++----------- internal/rbd/types/volume.go | 7 +++++ 2 files changed, 38 insertions(+), 17 deletions(-) diff --git a/internal/rbd/controllerserver.go b/internal/rbd/controllerserver.go index 640827796..1f32e6dec 100644 --- a/internal/rbd/controllerserver.go +++ b/internal/rbd/controllerserver.go @@ -239,34 +239,48 @@ func (cs *ControllerServer) parseVolCreateRequest( return rbdVol, nil } -func buildCreateVolumeResponse(req *csi.CreateVolumeRequest, rbdVol *rbdVolume) *csi.CreateVolumeResponse { - volumeContext := util.GetVolumeContext(req.GetParameters()) - volumeContext["pool"] = rbdVol.Pool - volumeContext["journalPool"] = rbdVol.JournalPool - volumeContext["imageName"] = rbdVol.RbdImageName +func (rbdVol *rbdVolume) ToCSI(ctx context.Context) *csi.Volume { + vol := &csi.Volume{ + VolumeId: rbdVol.VolID, + CapacityBytes: rbdVol.VolSize, + VolumeContext: map[string]string{ + "pool": rbdVol.Pool, + "journalPool": rbdVol.JournalPool, + "imageName": rbdVol.RbdImageName, + }, + } + if rbdVol.RadosNamespace != "" { - volumeContext["radosNamespace"] = rbdVol.RadosNamespace + vol.VolumeContext["radosNamespace"] = rbdVol.RadosNamespace } if rbdVol.DataPool != "" { - volumeContext["dataPool"] = rbdVol.DataPool - } - - volume := &csi.Volume{ - VolumeId: rbdVol.VolID, - CapacityBytes: rbdVol.VolSize, - VolumeContext: volumeContext, - ContentSource: req.GetVolumeContentSource(), + vol.VolumeContext["dataPool"] = rbdVol.DataPool } if rbdVol.Topology != nil { - volume.AccessibleTopology = []*csi.Topology{ + vol.AccessibleTopology = []*csi.Topology{ { Segments: rbdVol.Topology, }, } } + return vol +} + +func buildCreateVolumeResponse( + ctx context.Context, + req *csi.CreateVolumeRequest, + rbdVol *rbdVolume, +) *csi.CreateVolumeResponse { + volume := rbdVol.ToCSI(ctx) + volume.ContentSource = req.GetVolumeContentSource() + + for param, value := range util.GetVolumeContext(req.GetParameters()) { + volume.VolumeContext[param] = value + } + return &csi.CreateVolumeResponse{Volume: volume} } @@ -410,7 +424,7 @@ func (cs *ControllerServer) CreateVolume( return nil, status.Error(codes.Internal, err.Error()) } - return buildCreateVolumeResponse(req, rbdVol), nil + return buildCreateVolumeResponse(ctx, req, rbdVol), nil } // flattenParentImage is to be called before proceeding with creating volume, @@ -545,7 +559,7 @@ func (cs *ControllerServer) repairExistingVolume(ctx context.Context, req *csi.C return nil, err } - return buildCreateVolumeResponse(req, rbdVol), nil + return buildCreateVolumeResponse(ctx, req, rbdVol), nil } // check snapshots on the rbd image, as we have limit from krbd that an image diff --git a/internal/rbd/types/volume.go b/internal/rbd/types/volume.go index 19b5f3337..99961a733 100644 --- a/internal/rbd/types/volume.go +++ b/internal/rbd/types/volume.go @@ -18,13 +18,20 @@ package types import ( "context" + + "github.com/container-storage-interface/spec/lib/go/csi" ) type Volume interface { // Destroy frees the resources used by the Volume. Destroy(ctx context.Context) + // Delete removes the volume from the storage backend. Delete(ctx context.Context) error + // GetID returns the CSI VolumeID for the volume. GetID(ctx context.Context) (string, error) + + // ToCSI creates a CSI protocol formatted struct of the volume. + ToCSI(ctx context.Context) *csi.Volume }