Skip to content

Commit

Permalink
rbd: add a ToCSI() function to the Volume interface
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
nixpanic committed Jul 10, 2024
1 parent c15e40c commit 72543a5
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 17 deletions.
44 changes: 27 additions & 17 deletions internal/rbd/controllerserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,34 +239,44 @@ 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
if rbdVol.RadosNamespace != "" {
volumeContext["radosNamespace"] = rbdVol.RadosNamespace
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.DataPool != "" {
volumeContext["dataPool"] = rbdVol.DataPool
if rbdVol.RadosNamespace != "" {
vol.VolumeContext["radosNamespace"] = rbdVol.RadosNamespace
}

volume := &csi.Volume{
VolumeId: rbdVol.VolID,
CapacityBytes: rbdVol.VolSize,
VolumeContext: volumeContext,
ContentSource: req.GetVolumeContentSource(),
if rbdVol.DataPool != "" {
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}
}

Expand Down Expand Up @@ -410,7 +420,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,
Expand Down Expand Up @@ -545,7 +555,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
Expand Down
7 changes: 7 additions & 0 deletions internal/rbd_types/volume.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,20 @@ package rbd_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
}

0 comments on commit 72543a5

Please sign in to comment.