Skip to content

Commit 7a4cdf5

Browse files
huffmancagnufied
authored andcommitted
Included resizing for CSI-based block volumes.
Perform a no-op when volume is of type raw block Fix bug with checking volume mounts for readonly
1 parent 5713c22 commit 7a4cdf5

File tree

10 files changed

+194
-121
lines changed

10 files changed

+194
-121
lines changed

pkg/kubelet/volumemanager/populator/desired_state_of_world_populator.go

Lines changed: 1 addition & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ import (
3535
"k8s.io/apimachinery/pkg/util/wait"
3636
utilfeature "k8s.io/apiserver/pkg/util/feature"
3737
clientset "k8s.io/client-go/kubernetes"
38-
podutil "k8s.io/kubernetes/pkg/api/v1/pod"
3938
"k8s.io/kubernetes/pkg/features"
4039
"k8s.io/kubernetes/pkg/kubelet/config"
4140
kubecontainer "k8s.io/kubernetes/pkg/kubelet/container"
@@ -384,16 +383,6 @@ func (dswp *desiredStateOfWorldPopulator) checkVolumeFSResize(
384383
// or online resize in subsequent loop(after we confirm it has been mounted).
385384
return
386385
}
387-
fsVolume, err := util.CheckVolumeModeFilesystem(volumeSpec)
388-
if err != nil {
389-
klog.Errorf("Check volume mode failed for volume %s(OuterVolumeSpecName %s): %v",
390-
uniqueVolumeName, podVolume.Name, err)
391-
return
392-
}
393-
if !fsVolume {
394-
klog.V(5).Infof("Block mode volume needn't to check file system resize request")
395-
return
396-
}
397386
if processedVolumesForFSResize.Has(string(uniqueVolumeName)) {
398387
// File system resize operation is a global operation for volume,
399388
// so we only need to check it once if more than one pod use it.
@@ -415,22 +404,7 @@ func mountedReadOnlyByPod(podVolume v1.Volume, pod *v1.Pod) bool {
415404
if podVolume.PersistentVolumeClaim.ReadOnly {
416405
return true
417406
}
418-
419-
return podutil.VisitContainers(&pod.Spec, func(c *v1.Container) bool {
420-
if !mountedReadOnlyByContainer(podVolume.Name, c) {
421-
return false
422-
}
423-
return true
424-
})
425-
}
426-
427-
func mountedReadOnlyByContainer(volumeName string, container *v1.Container) bool {
428-
for _, volumeMount := range container.VolumeMounts {
429-
if volumeMount.Name == volumeName && !volumeMount.ReadOnly {
430-
return false
431-
}
432-
}
433-
return true
407+
return false
434408
}
435409

436410
func getUniqueVolumeName(

pkg/kubelet/volumemanager/populator/desired_state_of_world_populator_test.go

Lines changed: 41 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -568,8 +568,6 @@ func TestCreateVolumeSpec_Invalid_Block_VolumeMounts(t *testing.T) {
568568
}
569569

570570
func TestCheckVolumeFSResize(t *testing.T) {
571-
mode := v1.PersistentVolumeFilesystem
572-
573571
setCapacity := func(pv *v1.PersistentVolume, pvc *v1.PersistentVolumeClaim, capacity int) {
574572
pv.Spec.Capacity = volumeCapacity(capacity)
575573
pvc.Spec.Resources.Requests = volumeCapacity(capacity)
@@ -580,6 +578,7 @@ func TestCheckVolumeFSResize(t *testing.T) {
580578
verify func(*testing.T, []v1.UniqueVolumeName, v1.UniqueVolumeName)
581579
enableResize bool
582580
readOnlyVol bool
581+
volumeMode v1.PersistentVolumeMode
583582
}{
584583
{
585584
// No resize request for volume, volumes in ASW shouldn't be marked as fsResizeRequired
@@ -591,6 +590,7 @@ func TestCheckVolumeFSResize(t *testing.T) {
591590
}
592591
},
593592
enableResize: true,
593+
volumeMode: v1.PersistentVolumeFilesystem,
594594
},
595595
{
596596
// Disable the feature gate, so volume shouldn't be marked as fsResizeRequired
@@ -603,6 +603,7 @@ func TestCheckVolumeFSResize(t *testing.T) {
603603
}
604604
},
605605
enableResize: false,
606+
volumeMode: v1.PersistentVolumeFilesystem,
606607
},
607608
{
608609
// Make volume used as ReadOnly, so volume shouldn't be marked as fsResizeRequired
@@ -616,6 +617,7 @@ func TestCheckVolumeFSResize(t *testing.T) {
616617
},
617618
readOnlyVol: true,
618619
enableResize: true,
620+
volumeMode: v1.PersistentVolumeFilesystem,
619621
},
620622
{
621623
// Clear ASW, so volume shouldn't be marked as fsResizeRequired because they are not mounted
@@ -629,6 +631,7 @@ func TestCheckVolumeFSResize(t *testing.T) {
629631
}
630632
},
631633
enableResize: true,
634+
volumeMode: v1.PersistentVolumeFilesystem,
632635
},
633636
{
634637
// volume in ASW should be marked as fsResizeRequired
@@ -647,6 +650,26 @@ func TestCheckVolumeFSResize(t *testing.T) {
647650
}
648651
},
649652
enableResize: true,
653+
volumeMode: v1.PersistentVolumeFilesystem,
654+
},
655+
{
656+
// volume in ASW should be marked as fsResizeRequired
657+
resize: func(_ *testing.T, pv *v1.PersistentVolume, pvc *v1.PersistentVolumeClaim, _ *desiredStateOfWorldPopulator) {
658+
setCapacity(pv, pvc, 2)
659+
},
660+
verify: func(t *testing.T, vols []v1.UniqueVolumeName, volName v1.UniqueVolumeName) {
661+
if len(vols) == 0 {
662+
t.Fatalf("Request resize for volume, but volume in ASW hasn't been marked as fsResizeRequired")
663+
}
664+
if len(vols) != 1 {
665+
t.Errorf("Some unexpected volumes are marked as fsResizeRequired: %v", vols)
666+
}
667+
if vols[0] != volName {
668+
t.Fatalf("Mark wrong volume as fsResizeRequired: %s", vols[0])
669+
}
670+
},
671+
enableResize: true,
672+
volumeMode: v1.PersistentVolumeBlock,
650673
},
651674
}
652675

@@ -659,7 +682,7 @@ func TestCheckVolumeFSResize(t *testing.T) {
659682
PersistentVolumeSource: v1.PersistentVolumeSource{RBD: &v1.RBDPersistentVolumeSource{}},
660683
Capacity: volumeCapacity(1),
661684
ClaimRef: &v1.ObjectReference{Namespace: "ns", Name: "file-bound"},
662-
VolumeMode: &mode,
685+
VolumeMode: &tc.volumeMode,
663686
},
664687
}
665688
pvc := &v1.PersistentVolumeClaim{
@@ -677,20 +700,31 @@ func TestCheckVolumeFSResize(t *testing.T) {
677700

678701
dswp, fakePodManager, fakeDSW := createDswpWithVolume(t, pv, pvc)
679702
fakeASW := dswp.actualStateOfWorld
703+
containers := []v1.Container{}
680704

681-
// create pod
682-
containers := []v1.Container{
683-
{
705+
if tc.volumeMode == v1.PersistentVolumeFilesystem {
706+
containers = append(containers, v1.Container{
684707
VolumeMounts: []v1.VolumeMount{
685708
{
686709
Name: pv.Name,
687710
MountPath: "/mnt",
688711
ReadOnly: tc.readOnlyVol,
689712
},
690713
},
691-
},
714+
})
715+
} else {
716+
containers = append(containers, v1.Container{
717+
VolumeDevices: []v1.VolumeDevice{
718+
{
719+
Name: pv.Name,
720+
DevicePath: "/mnt/foobar",
721+
},
722+
},
723+
})
692724
}
725+
693726
pod := createPodWithVolume("dswp-test-pod", "dswp-test-volume-name", "file-bound", containers)
727+
pod.Spec.Volumes[0].VolumeSource.PersistentVolumeClaim.ReadOnly = tc.readOnlyVol
694728
uniquePodName := types.UniquePodName(pod.UID)
695729
uniqueVolumeName := v1.UniqueVolumeName("fake-plugin/" + pod.Spec.Volumes[0].Name)
696730

pkg/volume/awsebs/aws_ebs.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,15 @@ func (plugin *awsElasticBlockStorePlugin) ExpandVolumeDevice(
324324
}
325325

326326
func (plugin *awsElasticBlockStorePlugin) NodeExpand(resizeOptions volume.NodeResizeOptions) (bool, error) {
327-
_, err := util.GenericResizeFS(plugin.host, plugin.GetPluginName(), resizeOptions.DevicePath, resizeOptions.DeviceMountPath)
327+
fsVolume, err := util.CheckVolumeModeFilesystem(resizeOptions.VolumeSpec)
328+
if err != nil {
329+
return false, fmt.Errorf("error checking VolumeMode: %v", err)
330+
}
331+
// if volume is not a fs file system, there is nothing for us to do here.
332+
if !fsVolume {
333+
return true, nil
334+
}
335+
_, err = util.GenericResizeFS(plugin.host, plugin.GetPluginName(), resizeOptions.DevicePath, resizeOptions.DeviceMountPath)
328336
if err != nil {
329337
return false, err
330338
}

pkg/volume/azure_dd/azure_dd.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,15 @@ func (plugin *azureDataDiskPlugin) ExpandVolumeDevice(
298298
}
299299

300300
func (plugin *azureDataDiskPlugin) NodeExpand(resizeOptions volume.NodeResizeOptions) (bool, error) {
301-
_, err := util.GenericResizeFS(plugin.host, plugin.GetPluginName(), resizeOptions.DevicePath, resizeOptions.DeviceMountPath)
301+
fsVolume, err := util.CheckVolumeModeFilesystem(resizeOptions.VolumeSpec)
302+
if err != nil {
303+
return false, fmt.Errorf("error checking VolumeMode: %v", err)
304+
}
305+
// if volume is not a fs file system, there is nothing for us to do here.
306+
if !fsVolume {
307+
return true, nil
308+
}
309+
_, err = util.GenericResizeFS(plugin.host, plugin.GetPluginName(), resizeOptions.DevicePath, resizeOptions.DeviceMountPath)
302310
if err != nil {
303311
return false, err
304312
}

pkg/volume/cinder/cinder.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,16 @@ func (plugin *cinderPlugin) ExpandVolumeDevice(spec *volume.Spec, newSize resour
311311
}
312312

313313
func (plugin *cinderPlugin) NodeExpand(resizeOptions volume.NodeResizeOptions) (bool, error) {
314-
_, err := util.GenericResizeFS(plugin.host, plugin.GetPluginName(), resizeOptions.DevicePath, resizeOptions.DeviceMountPath)
314+
fsVolume, err := util.CheckVolumeModeFilesystem(resizeOptions.VolumeSpec)
315+
if err != nil {
316+
return false, fmt.Errorf("error checking VolumeMode: %v", err)
317+
}
318+
// if volume is not a fs file system, there is nothing for us to do here.
319+
if !fsVolume {
320+
return true, nil
321+
}
322+
323+
_, err = util.GenericResizeFS(plugin.host, plugin.GetPluginName(), resizeOptions.DevicePath, resizeOptions.DeviceMountPath)
315324
if err != nil {
316325
return false, err
317326
}

pkg/volume/flexvolume/expander-defaults.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ limitations under the License.
1717
package flexvolume
1818

1919
import (
20+
"fmt"
21+
2022
"k8s.io/apimachinery/pkg/api/resource"
2123
"k8s.io/klog"
2224
"k8s.io/kubernetes/pkg/volume"
@@ -40,7 +42,16 @@ func (e *expanderDefaults) ExpandVolumeDevice(spec *volume.Spec, newSize resourc
4042
// generic filesystem resize
4143
func (e *expanderDefaults) NodeExpand(rsOpt volume.NodeResizeOptions) (bool, error) {
4244
klog.Warning(logPrefix(e.plugin), "using default filesystem resize for volume ", rsOpt.VolumeSpec.Name(), ", at ", rsOpt.DevicePath)
43-
_, err := util.GenericResizeFS(e.plugin.host, e.plugin.GetPluginName(), rsOpt.DevicePath, rsOpt.DeviceMountPath)
45+
fsVolume, err := util.CheckVolumeModeFilesystem(rsOpt.VolumeSpec)
46+
if err != nil {
47+
return false, fmt.Errorf("error checking VolumeMode: %v", err)
48+
}
49+
// if volume is not a fs file system, there is nothing for us to do here.
50+
if !fsVolume {
51+
return true, nil
52+
}
53+
54+
_, err = util.GenericResizeFS(e.plugin.host, e.plugin.GetPluginName(), rsOpt.DevicePath, rsOpt.DeviceMountPath)
4455
if err != nil {
4556
return false, err
4657
}

pkg/volume/gcepd/gce_pd.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,15 @@ func (plugin *gcePersistentDiskPlugin) ExpandVolumeDevice(
280280
}
281281

282282
func (plugin *gcePersistentDiskPlugin) NodeExpand(resizeOptions volume.NodeResizeOptions) (bool, error) {
283-
_, err := util.GenericResizeFS(plugin.host, plugin.GetPluginName(), resizeOptions.DevicePath, resizeOptions.DeviceMountPath)
283+
fsVolume, err := util.CheckVolumeModeFilesystem(resizeOptions.VolumeSpec)
284+
if err != nil {
285+
return false, fmt.Errorf("error checking VolumeMode: %v", err)
286+
}
287+
// if volume is not a fs file system, there is nothing for us to do here.
288+
if !fsVolume {
289+
return true, nil
290+
}
291+
_, err = util.GenericResizeFS(plugin.host, plugin.GetPluginName(), resizeOptions.DevicePath, resizeOptions.DeviceMountPath)
284292
if err != nil {
285293
return false, err
286294
}

pkg/volume/rbd/rbd.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ import (
3535
"k8s.io/kubernetes/pkg/features"
3636
"k8s.io/kubernetes/pkg/util/mount"
3737
"k8s.io/kubernetes/pkg/volume"
38+
"k8s.io/kubernetes/pkg/volume/util"
3839
volutil "k8s.io/kubernetes/pkg/volume/util"
3940
"k8s.io/kubernetes/pkg/volume/util/volumepathhandler"
4041
utilstrings "k8s.io/utils/strings"
@@ -202,7 +203,15 @@ func (plugin *rbdPlugin) ExpandVolumeDevice(spec *volume.Spec, newSize resource.
202203
}
203204

204205
func (plugin *rbdPlugin) NodeExpand(resizeOptions volume.NodeResizeOptions) (bool, error) {
205-
_, err := volutil.GenericResizeFS(plugin.host, plugin.GetPluginName(), resizeOptions.DevicePath, resizeOptions.DeviceMountPath)
206+
fsVolume, err := util.CheckVolumeModeFilesystem(resizeOptions.VolumeSpec)
207+
if err != nil {
208+
return false, fmt.Errorf("error checking VolumeMode: %v", err)
209+
}
210+
// if volume is not a fs file system, there is nothing for us to do here.
211+
if !fsVolume {
212+
return true, nil
213+
}
214+
_, err = volutil.GenericResizeFS(plugin.host, plugin.GetPluginName(), resizeOptions.DevicePath, resizeOptions.DeviceMountPath)
206215
if err != nil {
207216
return false, err
208217
}

pkg/volume/util/operationexecutor/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ go_library(
1212
"fakegenerator.go",
1313
"operation_executor.go",
1414
"operation_generator.go",
15+
"volume_operation.go",
1516
],
1617
importpath = "k8s.io/kubernetes/pkg/volume/util/operationexecutor",
1718
deps = [

0 commit comments

Comments
 (0)