Skip to content

Commit 6c018cc

Browse files
committed
Add volume expansion support
1 parent 09a18b7 commit 6c018cc

File tree

3 files changed

+67
-0
lines changed

3 files changed

+67
-0
lines changed

pkg/hostpath/controllerserver.go

+37
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ func NewControllerServer(ephemeral bool) *controllerServer {
6363
csi.ControllerServiceCapability_RPC_CREATE_DELETE_SNAPSHOT,
6464
csi.ControllerServiceCapability_RPC_LIST_SNAPSHOTS,
6565
csi.ControllerServiceCapability_RPC_CLONE_VOLUME,
66+
csi.ControllerServiceCapability_RPC_EXPAND_VOLUME,
6667
}),
6768
}
6869
}
@@ -457,6 +458,42 @@ func (cs *controllerServer) ListSnapshots(ctx context.Context, req *csi.ListSnap
457458
}, nil
458459
}
459460

461+
func (cs *controllerServer) ControllerExpandVolume(ctx context.Context, req *csi.ControllerExpandVolumeRequest) (*csi.ControllerExpandVolumeResponse, error) {
462+
463+
volID := req.GetVolumeId()
464+
if len(volID) == 0 {
465+
return nil, status.Error(codes.InvalidArgument, "Volume ID missing in request")
466+
}
467+
468+
capRange := req.GetCapacityRange()
469+
if capRange == nil {
470+
return nil, status.Error(codes.InvalidArgument, "Capacity range not provided")
471+
}
472+
473+
capacity := int64(capRange.GetRequiredBytes())
474+
if capacity >= maxStorageCapacity {
475+
return nil, status.Errorf(codes.OutOfRange, "Requested capacity %d exceeds maximum allowed %d", capacity, maxStorageCapacity)
476+
}
477+
478+
exVol, err := getVolumeByID(volID)
479+
if err != nil {
480+
// Assume not found error
481+
return nil, status.Errorf(codes.NotFound, "Could not get volume %s: %v", volID, err)
482+
}
483+
484+
if exVol.VolSize < capacity {
485+
exVol.VolSize = capacity
486+
if err := updateHostpathVolume(volID, exVol); err != nil {
487+
return nil, status.Errorf(codes.Internal, "Could not update volume %s: %v", volID, err)
488+
}
489+
}
490+
491+
return &csi.ControllerExpandVolumeResponse{
492+
CapacityBytes: exVol.VolSize,
493+
NodeExpansionRequired: true,
494+
}, nil
495+
}
496+
460497
func convertSnapshot(snap hostPathSnapshot) *csi.ListSnapshotsResponse {
461498
entries := []*csi.ListSnapshotsResponse_Entry{
462499
{

pkg/hostpath/hostpath.go

+12
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,18 @@ func createHostpathVolume(volID, name string, cap int64, volAccessType accessTyp
209209
return &hostpathVol, nil
210210
}
211211

212+
// updateVolume updates the existing hostpath volume.
213+
func updateHostpathVolume(volID string, volume hostPathVolume) error {
214+
glog.V(4).Infof("updating hostpath volume: %s", volID)
215+
216+
if _, err := getVolumeByID(volID); err != nil {
217+
return err
218+
}
219+
220+
hostPathVolumes[volID] = volume
221+
return nil
222+
}
223+
212224
// deleteVolume deletes the directory for the hostpath volume.
213225
func deleteHostpathVolume(volID string) error {
214226
glog.V(4).Infof("deleting hostpath volume: %s", volID)

pkg/hostpath/nodeserver.go

+18
Original file line numberDiff line numberDiff line change
@@ -277,10 +277,28 @@ func (ns *nodeServer) NodeGetCapabilities(ctx context.Context, req *csi.NodeGetC
277277
},
278278
},
279279
},
280+
{
281+
Type: &csi.NodeServiceCapability_Rpc{
282+
Rpc: &csi.NodeServiceCapability_RPC{
283+
Type: csi.NodeServiceCapability_RPC_EXPAND_VOLUME,
284+
},
285+
},
286+
},
280287
},
281288
}, nil
282289
}
283290

284291
func (ns *nodeServer) NodeGetVolumeStats(ctx context.Context, in *csi.NodeGetVolumeStatsRequest) (*csi.NodeGetVolumeStatsResponse, error) {
285292
return nil, status.Error(codes.Unimplemented, "")
286293
}
294+
295+
// NodeExpandVolume is only implemented so the driver can be used for e2e testing.
296+
func (ns *nodeServer) NodeExpandVolume(ctx context.Context, req *csi.NodeExpandVolumeRequest) (*csi.NodeExpandVolumeResponse, error) {
297+
298+
volID := req.GetVolumeId()
299+
if len(volID) == 0 {
300+
return nil, status.Error(codes.InvalidArgument, "Volume ID not provided")
301+
}
302+
303+
return &csi.NodeExpandVolumeResponse{}, nil
304+
}

0 commit comments

Comments
 (0)