expander.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /*
  2. Copyright 2019 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package csi
  14. import (
  15. "context"
  16. "errors"
  17. "fmt"
  18. api "k8s.io/api/core/v1"
  19. utilfeature "k8s.io/apiserver/pkg/util/feature"
  20. "k8s.io/klog"
  21. "k8s.io/kubernetes/pkg/features"
  22. "k8s.io/kubernetes/pkg/volume"
  23. "k8s.io/kubernetes/pkg/volume/util"
  24. )
  25. var _ volume.NodeExpandableVolumePlugin = &csiPlugin{}
  26. func (c *csiPlugin) RequiresFSResize() bool {
  27. // We could check plugin's node capability but we instead are going to rely on
  28. // NodeExpand to do the right thing and return early if plugin does not have
  29. // node expansion capability.
  30. if !utilfeature.DefaultFeatureGate.Enabled(features.ExpandCSIVolumes) {
  31. klog.V(4).Infof("Resizing is not enabled for CSI volume")
  32. return false
  33. }
  34. return true
  35. }
  36. func (c *csiPlugin) NodeExpand(resizeOptions volume.NodeResizeOptions) (bool, error) {
  37. klog.V(4).Infof(log("Expander.NodeExpand(%s)", resizeOptions.DeviceMountPath))
  38. csiSource, err := getCSISourceFromSpec(resizeOptions.VolumeSpec)
  39. if err != nil {
  40. return false, errors.New(log("Expander.NodeExpand failed to get CSI persistent source: %v", err))
  41. }
  42. csClient, err := newCsiDriverClient(csiDriverName(csiSource.Driver))
  43. if err != nil {
  44. return false, err
  45. }
  46. fsVolume, err := util.CheckVolumeModeFilesystem(resizeOptions.VolumeSpec)
  47. if err != nil {
  48. return false, errors.New(log("Expander.NodeExpand failed to check VolumeMode of source: %v", err))
  49. }
  50. return c.nodeExpandWithClient(resizeOptions, csiSource, csClient, fsVolume)
  51. }
  52. func (c *csiPlugin) nodeExpandWithClient(
  53. resizeOptions volume.NodeResizeOptions,
  54. csiSource *api.CSIPersistentVolumeSource,
  55. csClient csiClient,
  56. fsVolume bool) (bool, error) {
  57. driverName := csiSource.Driver
  58. ctx, cancel := context.WithTimeout(context.Background(), csiTimeout)
  59. defer cancel()
  60. nodeExpandSet, err := csClient.NodeSupportsNodeExpand(ctx)
  61. if err != nil {
  62. return false, fmt.Errorf("Expander.NodeExpand failed to check if node supports expansion : %v", err)
  63. }
  64. if !nodeExpandSet {
  65. return false, fmt.Errorf("Expander.NodeExpand found CSI plugin %s/%s to not support node expansion", c.GetPluginName(), driverName)
  66. }
  67. // Check whether "STAGE_UNSTAGE_VOLUME" is set
  68. stageUnstageSet, err := csClient.NodeSupportsStageUnstage(ctx)
  69. if err != nil {
  70. return false, fmt.Errorf("Expander.NodeExpand failed to check if plugins supports stage_unstage %v", err)
  71. }
  72. // if plugin does not support STAGE_UNSTAGE but CSI volume path is staged
  73. // it must mean this was placeholder staging performed by k8s and not CSI staging
  74. // in which case we should return from here so as volume can be node published
  75. // before we can resize
  76. if !stageUnstageSet && resizeOptions.CSIVolumePhase == volume.CSIVolumeStaged {
  77. return false, nil
  78. }
  79. volumeTargetPath := resizeOptions.DeviceMountPath
  80. if !fsVolume {
  81. volumeTargetPath = resizeOptions.DevicePath
  82. }
  83. _, err = csClient.NodeExpandVolume(ctx, csiSource.VolumeHandle, volumeTargetPath, resizeOptions.NewSize)
  84. if err != nil {
  85. return false, fmt.Errorf("Expander.NodeExpand failed to expand the volume : %v", err)
  86. }
  87. return true, nil
  88. }