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 <ndevos@ibm.com>
This commit is contained in:
Niels de Vos 2024-07-05 10:26:47 +02:00 committed by mergify[bot]
parent 2dd2ac8e91
commit 2803ec1290
2 changed files with 38 additions and 17 deletions

View File

@ -239,34 +239,48 @@ func (cs *ControllerServer) parseVolCreateRequest(
return rbdVol, nil return rbdVol, nil
} }
func buildCreateVolumeResponse(req *csi.CreateVolumeRequest, rbdVol *rbdVolume) *csi.CreateVolumeResponse { func (rbdVol *rbdVolume) ToCSI(ctx context.Context) *csi.Volume {
volumeContext := util.GetVolumeContext(req.GetParameters()) vol := &csi.Volume{
volumeContext["pool"] = rbdVol.Pool VolumeId: rbdVol.VolID,
volumeContext["journalPool"] = rbdVol.JournalPool CapacityBytes: rbdVol.VolSize,
volumeContext["imageName"] = rbdVol.RbdImageName VolumeContext: map[string]string{
"pool": rbdVol.Pool,
"journalPool": rbdVol.JournalPool,
"imageName": rbdVol.RbdImageName,
},
}
if rbdVol.RadosNamespace != "" { if rbdVol.RadosNamespace != "" {
volumeContext["radosNamespace"] = rbdVol.RadosNamespace vol.VolumeContext["radosNamespace"] = rbdVol.RadosNamespace
} }
if rbdVol.DataPool != "" { if rbdVol.DataPool != "" {
volumeContext["dataPool"] = rbdVol.DataPool vol.VolumeContext["dataPool"] = rbdVol.DataPool
}
volume := &csi.Volume{
VolumeId: rbdVol.VolID,
CapacityBytes: rbdVol.VolSize,
VolumeContext: volumeContext,
ContentSource: req.GetVolumeContentSource(),
} }
if rbdVol.Topology != nil { if rbdVol.Topology != nil {
volume.AccessibleTopology = []*csi.Topology{ vol.AccessibleTopology = []*csi.Topology{
{ {
Segments: rbdVol.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} return &csi.CreateVolumeResponse{Volume: volume}
} }
@ -410,7 +424,7 @@ func (cs *ControllerServer) CreateVolume(
return nil, status.Error(codes.Internal, err.Error()) 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, // 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 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 // check snapshots on the rbd image, as we have limit from krbd that an image

View File

@ -18,13 +18,20 @@ package types
import ( import (
"context" "context"
"github.com/container-storage-interface/spec/lib/go/csi"
) )
type Volume interface { type Volume interface {
// Destroy frees the resources used by the Volume. // Destroy frees the resources used by the Volume.
Destroy(ctx context.Context) Destroy(ctx context.Context)
// Delete removes the volume from the storage backend.
Delete(ctx context.Context) error Delete(ctx context.Context) error
// GetID returns the CSI VolumeID for the volume.
GetID(ctx context.Context) (string, error) GetID(ctx context.Context) (string, error)
// ToCSI creates a CSI protocol formatted struct of the volume.
ToCSI(ctx context.Context) *csi.Volume
} }