Skip to content

Commit 6342dad

Browse files
committed
Ensure that StagingPath is supplied to blockVolume expansion
1 parent 69613da commit 6342dad

File tree

8 files changed

+83
-37
lines changed

8 files changed

+83
-37
lines changed

pkg/volume/csi/csi_block.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -107,9 +107,9 @@ func (m *csiBlockMapper) GetGlobalMapPath(spec *volume.Spec) (string, error) {
107107
return dir, nil
108108
}
109109

110-
// getStagingPath returns a staging path for a directory (on the node) that should be used on NodeStageVolume/NodeUnstageVolume
110+
// GetStagingPath returns a staging path for a directory (on the node) that should be used on NodeStageVolume/NodeUnstageVolume
111111
// Example: plugins/kubernetes.io/csi/volumeDevices/staging/{specName}
112-
func (m *csiBlockMapper) getStagingPath() string {
112+
func (m *csiBlockMapper) GetStagingPath() string {
113113
return filepath.Join(m.plugin.host.GetVolumeDevicePluginDir(CSIPluginName), "staging", m.specName)
114114
}
115115

@@ -143,7 +143,7 @@ func (m *csiBlockMapper) stageVolumeForBlock(
143143
) (string, error) {
144144
klog.V(4).Infof(log("blockMapper.stageVolumeForBlock called"))
145145

146-
stagingPath := m.getStagingPath()
146+
stagingPath := m.GetStagingPath()
147147
klog.V(4).Infof(log("blockMapper.stageVolumeForBlock stagingPath set [%s]", stagingPath))
148148

149149
// Check whether "STAGE_UNSTAGE_VOLUME" is set
@@ -237,7 +237,7 @@ func (m *csiBlockMapper) publishVolumeForBlock(
237237
ctx,
238238
m.volumeID,
239239
m.readOnly,
240-
m.getStagingPath(),
240+
m.GetStagingPath(),
241241
publishPath,
242242
accessMode,
243243
publishVolumeInfo,
@@ -435,7 +435,7 @@ func (m *csiBlockMapper) TearDownDevice(globalMapPath, devicePath string) error
435435
}
436436

437437
// Call NodeUnstageVolume
438-
stagingPath := m.getStagingPath()
438+
stagingPath := m.GetStagingPath()
439439
if _, err := os.Stat(stagingPath); err != nil {
440440
if os.IsNotExist(err) {
441441
klog.V(4).Infof(log("blockMapper.TearDownDevice stagingPath(%s) has already been deleted, skip calling NodeUnstageVolume", stagingPath))
@@ -471,7 +471,7 @@ func (m *csiBlockMapper) cleanupOrphanDeviceFiles() error {
471471

472472
// Remove artifacts of NodeStage.
473473
// stagingPath: xxx/plugins/kubernetes.io/csi/volumeDevices/staging/<volume name>
474-
stagingPath := m.getStagingPath()
474+
stagingPath := m.GetStagingPath()
475475
if err := os.Remove(stagingPath); err != nil && !os.IsNotExist(err) {
476476
return errors.New(log("failed to delete volume staging path [%s]: %v", stagingPath, err))
477477
}

pkg/volume/csi/csi_block_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ func TestBlockMapperGetStagingPath(t *testing.T) {
123123
t.Fatalf("Failed to make a new Mapper: %v", err)
124124
}
125125

126-
path := csiMapper.getStagingPath()
126+
path := csiMapper.GetStagingPath()
127127

128128
if tc.path != path {
129129
t.Errorf("expecting path %s, got %s", tc.path, path)

pkg/volume/csi/csi_client.go

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -282,16 +282,22 @@ func (c *csiDriverClient) NodeExpandVolume(ctx context.Context, opts csiResizeOp
282282
defer closer.Close()
283283

284284
req := &csipbv1.NodeExpandVolumeRequest{
285-
VolumeId: opts.volumeID,
286-
VolumePath: opts.volumePath,
287-
StagingTargetPath: opts.stagingTargetPath,
288-
CapacityRange: &csipbv1.CapacityRange{RequiredBytes: opts.newSize.Value()},
285+
VolumeId: opts.volumeID,
286+
VolumePath: opts.volumePath,
287+
CapacityRange: &csipbv1.CapacityRange{RequiredBytes: opts.newSize.Value()},
289288
VolumeCapability: &csipbv1.VolumeCapability{
290289
AccessMode: &csipbv1.VolumeCapability_AccessMode{
291290
Mode: asCSIAccessModeV1(opts.accessMode),
292291
},
293292
},
294293
}
294+
295+
// not all CSI drivers support NodeStageUnstage and hence the StagingTargetPath
296+
// should only be set when available
297+
if opts.stagingTargetPath != "" {
298+
req.StagingTargetPath = opts.stagingTargetPath
299+
}
300+
295301
if opts.fsType == fsTypeBlockName {
296302
req.VolumeCapability.AccessType = &csipbv1.VolumeCapability_Block{
297303
Block: &csipbv1.VolumeCapability_BlockVolume{},

pkg/volume/csi/expander.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,11 @@ func (c *csiPlugin) nodeExpandWithClient(
108108
accessMode: api.ReadWriteOnce,
109109
mountOptions: pv.Spec.MountOptions,
110110
}
111+
111112
if !fsVolume {
113+
// for block volumes the volumePath in CSI NodeExpandvolumeRequest is
114+
// basically same as DevicePath because block devices are not mounted and hence
115+
// DeviceMountPath does not get populated in resizeOptions.DeviceMountPath
112116
opts.volumePath = resizeOptions.DevicePath
113117
opts.fsType = fsTypeBlockName
114118
}

pkg/volume/local/local.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -621,6 +621,11 @@ func (m *localVolumeMapper) MapPodDevice() (string, error) {
621621
return globalPath, nil
622622
}
623623

624+
// GetStagingPath returns
625+
func (m *localVolumeMapper) GetStagingPath() string {
626+
return ""
627+
}
628+
624629
// localVolumeUnmapper implements the BlockVolumeUnmapper interface for local volumes.
625630
type localVolumeUnmapper struct {
626631
*localVolume

pkg/volume/testing/testing.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -998,6 +998,10 @@ func (fv *FakeVolume) SetUpDevice() (string, error) {
998998
return "", nil
999999
}
10001000

1001+
func (fv *FakeVolume) GetStagingPath() string {
1002+
return filepath.Join(fv.Plugin.Host.GetVolumeDevicePluginDir(utilstrings.EscapeQualifiedName(fv.Plugin.PluginName)), "staging", fv.VolName)
1003+
}
1004+
10011005
// Block volume support
10021006
func (fv *FakeVolume) GetSetUpDeviceCallCount() int {
10031007
fv.RLock()

pkg/volume/util/operationexecutor/operation_generator.go

Lines changed: 49 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1498,41 +1498,64 @@ func (og *operationGenerator) GenerateExpandInUseVolumeFunc(
14981498
var simpleErr, detailedErr error
14991499
resizeOptions := volume.NodeResizeOptions{
15001500
VolumeSpec: volumeToMount.VolumeSpec,
1501+
DevicePath: volumeToMount.DevicePath,
1502+
}
1503+
fsVolume, err := util.CheckVolumeModeFilesystem(volumeToMount.VolumeSpec)
1504+
if err != nil {
1505+
return volumeToMount.GenerateError("NodeExpandvolume.CheckVolumeModeFilesystem failed", err)
15011506
}
15021507

1503-
attachableVolumePlugin, _ :=
1504-
og.volumePluginMgr.FindAttachablePluginBySpec(volumeToMount.VolumeSpec)
1508+
if fsVolume {
1509+
volumeMounter, newMounterErr := volumePlugin.NewMounter(
1510+
volumeToMount.VolumeSpec,
1511+
volumeToMount.Pod,
1512+
volume.VolumeOptions{})
1513+
if newMounterErr != nil {
1514+
return volumeToMount.GenerateError("NodeExpandVolume.NewMounter initialization failed", newMounterErr)
1515+
}
15051516

1506-
if attachableVolumePlugin != nil {
1507-
volumeAttacher, _ := attachableVolumePlugin.NewAttacher()
1508-
if volumeAttacher != nil {
1509-
resizeOptions.CSIVolumePhase = volume.CSIVolumeStaged
1510-
resizeOptions.DevicePath = volumeToMount.DevicePath
1511-
dmp, err := volumeAttacher.GetDeviceMountPath(volumeToMount.VolumeSpec)
1517+
resizeOptions.DeviceMountPath = volumeMounter.GetPath()
1518+
1519+
deviceMountableVolumePlugin, _ := og.volumePluginMgr.FindDeviceMountablePluginBySpec(volumeToMount.VolumeSpec)
1520+
var volumeDeviceMounter volume.DeviceMounter
1521+
if deviceMountableVolumePlugin != nil {
1522+
volumeDeviceMounter, _ = deviceMountableVolumePlugin.NewDeviceMounter()
1523+
}
1524+
1525+
if volumeDeviceMounter != nil {
1526+
deviceStagePath, err := volumeDeviceMounter.GetDeviceMountPath(volumeToMount.VolumeSpec)
15121527
if err != nil {
15131528
return volumeToMount.GenerateError("NodeExpandVolume.GetDeviceMountPath failed", err)
15141529
}
1515-
resizeOptions.DeviceMountPath = dmp
1516-
resizeOptions.DeviceStagePath = dmp
1517-
resizeDone, simpleErr, detailedErr = og.doOnlineExpansion(volumeToMount, actualStateOfWorld, resizeOptions)
1518-
if simpleErr != nil || detailedErr != nil {
1519-
return simpleErr, detailedErr
1520-
}
1521-
if resizeDone {
1522-
return nil, nil
1523-
}
1530+
resizeOptions.DeviceStagePath = deviceStagePath
1531+
}
1532+
} else {
1533+
// Get block volume mapper plugin
1534+
blockVolumePlugin, err :=
1535+
og.volumePluginMgr.FindMapperPluginBySpec(volumeToMount.VolumeSpec)
1536+
if err != nil {
1537+
return volumeToMount.GenerateError("MapVolume.FindMapperPluginBySpec failed", err)
1538+
}
1539+
1540+
if blockVolumePlugin == nil {
1541+
return volumeToMount.GenerateError("MapVolume.FindMapperPluginBySpec failed to find BlockVolumeMapper plugin. Volume plugin is nil.", nil)
1542+
}
1543+
1544+
blockVolumeMapper, newMapperErr := blockVolumePlugin.NewBlockVolumeMapper(
1545+
volumeToMount.VolumeSpec,
1546+
volumeToMount.Pod,
1547+
volume.VolumeOptions{})
1548+
if newMapperErr != nil {
1549+
return volumeToMount.GenerateError("MapVolume.NewBlockVolumeMapper initialization failed", newMapperErr)
1550+
}
1551+
1552+
// if plugin supports custom mappers lets add DeviceStagePath
1553+
if customBlockVolumeMapper, ok := blockVolumeMapper.(volume.CustomBlockVolumeMapper); ok {
1554+
resizeOptions.DeviceStagePath = customBlockVolumeMapper.GetStagingPath()
15241555
}
1525-
}
1526-
// if we are here that means volume plugin does not support attach interface
1527-
volumeMounter, newMounterErr := volumePlugin.NewMounter(
1528-
volumeToMount.VolumeSpec,
1529-
volumeToMount.Pod,
1530-
volume.VolumeOptions{})
1531-
if newMounterErr != nil {
1532-
return volumeToMount.GenerateError("NodeExpandVolume.NewMounter initialization failed", newMounterErr)
15331556
}
15341557

1535-
resizeOptions.DeviceMountPath = volumeMounter.GetPath()
1558+
// if we are doing online expansion then volume is already published
15361559
resizeOptions.CSIVolumePhase = volume.CSIVolumePublished
15371560
resizeDone, simpleErr, detailedErr = og.doOnlineExpansion(volumeToMount, actualStateOfWorld, resizeOptions)
15381561
if simpleErr != nil || detailedErr != nil {

pkg/volume/volume.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,10 @@ type CustomBlockVolumeMapper interface {
183183
// If empty string is returned, the path retuned by attacher.Attach() and
184184
// attacher.WaitForAttach() will be used.
185185
MapPodDevice() (publishPath string, err error)
186+
187+
// GetStagingPath returns path that was used for staging the volume
188+
// it is mainly used by CSI plugins
189+
GetStagingPath() string
186190
}
187191

188192
// BlockVolumeUnmapper interface is an unmapper interface for block volume.

0 commit comments

Comments
 (0)