2019-04-07 17:07:55 +00:00
|
|
|
/*
|
|
|
|
Copyright 2019 The Kubernetes Authors.
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package csi
|
|
|
|
|
|
|
|
import (
|
2019-09-27 21:51:53 +00:00
|
|
|
"errors"
|
2019-04-07 17:07:55 +00:00
|
|
|
"fmt"
|
|
|
|
|
2020-08-10 17:43:49 +00:00
|
|
|
"google.golang.org/grpc/codes"
|
|
|
|
"google.golang.org/grpc/status"
|
2019-04-07 17:07:55 +00:00
|
|
|
api "k8s.io/api/core/v1"
|
2019-08-30 18:33:25 +00:00
|
|
|
utilfeature "k8s.io/apiserver/pkg/util/feature"
|
2020-08-10 17:43:49 +00:00
|
|
|
"k8s.io/klog/v2"
|
2019-08-30 18:33:25 +00:00
|
|
|
"k8s.io/kubernetes/pkg/features"
|
2019-04-07 17:07:55 +00:00
|
|
|
"k8s.io/kubernetes/pkg/volume"
|
2019-09-27 21:51:53 +00:00
|
|
|
"k8s.io/kubernetes/pkg/volume/util"
|
2020-08-10 17:43:49 +00:00
|
|
|
volumetypes "k8s.io/kubernetes/pkg/volume/util/types"
|
2019-04-07 17:07:55 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var _ volume.NodeExpandableVolumePlugin = &csiPlugin{}
|
|
|
|
|
|
|
|
func (c *csiPlugin) RequiresFSResize() bool {
|
2019-08-30 18:33:25 +00:00
|
|
|
// We could check plugin's node capability but we instead are going to rely on
|
|
|
|
// NodeExpand to do the right thing and return early if plugin does not have
|
|
|
|
// node expansion capability.
|
|
|
|
if !utilfeature.DefaultFeatureGate.Enabled(features.ExpandCSIVolumes) {
|
|
|
|
klog.V(4).Infof("Resizing is not enabled for CSI volume")
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
2019-04-07 17:07:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *csiPlugin) NodeExpand(resizeOptions volume.NodeResizeOptions) (bool, error) {
|
|
|
|
klog.V(4).Infof(log("Expander.NodeExpand(%s)", resizeOptions.DeviceMountPath))
|
|
|
|
csiSource, err := getCSISourceFromSpec(resizeOptions.VolumeSpec)
|
|
|
|
if err != nil {
|
2019-09-27 21:51:53 +00:00
|
|
|
return false, errors.New(log("Expander.NodeExpand failed to get CSI persistent source: %v", err))
|
2019-04-07 17:07:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
csClient, err := newCsiDriverClient(csiDriverName(csiSource.Driver))
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
2019-09-27 21:51:53 +00:00
|
|
|
fsVolume, err := util.CheckVolumeModeFilesystem(resizeOptions.VolumeSpec)
|
|
|
|
if err != nil {
|
|
|
|
return false, errors.New(log("Expander.NodeExpand failed to check VolumeMode of source: %v", err))
|
|
|
|
}
|
2019-04-07 17:07:55 +00:00
|
|
|
|
2019-09-27 21:51:53 +00:00
|
|
|
return c.nodeExpandWithClient(resizeOptions, csiSource, csClient, fsVolume)
|
2019-04-07 17:07:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *csiPlugin) nodeExpandWithClient(
|
|
|
|
resizeOptions volume.NodeResizeOptions,
|
|
|
|
csiSource *api.CSIPersistentVolumeSource,
|
2019-09-27 21:51:53 +00:00
|
|
|
csClient csiClient,
|
|
|
|
fsVolume bool) (bool, error) {
|
2019-04-07 17:07:55 +00:00
|
|
|
driverName := csiSource.Driver
|
|
|
|
|
2021-03-18 22:40:29 +00:00
|
|
|
ctx, cancel := createCSIOperationContext(resizeOptions.VolumeSpec, csiTimeout)
|
2019-04-07 17:07:55 +00:00
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
nodeExpandSet, err := csClient.NodeSupportsNodeExpand(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return false, fmt.Errorf("Expander.NodeExpand failed to check if node supports expansion : %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !nodeExpandSet {
|
|
|
|
return false, fmt.Errorf("Expander.NodeExpand found CSI plugin %s/%s to not support node expansion", c.GetPluginName(), driverName)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check whether "STAGE_UNSTAGE_VOLUME" is set
|
|
|
|
stageUnstageSet, err := csClient.NodeSupportsStageUnstage(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return false, fmt.Errorf("Expander.NodeExpand failed to check if plugins supports stage_unstage %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// if plugin does not support STAGE_UNSTAGE but CSI volume path is staged
|
|
|
|
// it must mean this was placeholder staging performed by k8s and not CSI staging
|
|
|
|
// in which case we should return from here so as volume can be node published
|
|
|
|
// before we can resize
|
|
|
|
if !stageUnstageSet && resizeOptions.CSIVolumePhase == volume.CSIVolumeStaged {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
2020-08-10 17:43:49 +00:00
|
|
|
pv := resizeOptions.VolumeSpec.PersistentVolume
|
|
|
|
if pv == nil {
|
|
|
|
return false, fmt.Errorf("Expander.NodeExpand failed to find associated PersistentVolume for plugin %s", c.GetPluginName())
|
|
|
|
}
|
|
|
|
|
|
|
|
opts := csiResizeOptions{
|
|
|
|
volumePath: resizeOptions.DeviceMountPath,
|
|
|
|
stagingTargetPath: resizeOptions.DeviceStagePath,
|
|
|
|
volumeID: csiSource.VolumeHandle,
|
|
|
|
newSize: resizeOptions.NewSize,
|
|
|
|
fsType: csiSource.FSType,
|
|
|
|
accessMode: api.ReadWriteOnce,
|
|
|
|
mountOptions: pv.Spec.MountOptions,
|
|
|
|
}
|
|
|
|
|
2019-09-27 21:51:53 +00:00
|
|
|
if !fsVolume {
|
2020-08-10 17:43:49 +00:00
|
|
|
// for block volumes the volumePath in CSI NodeExpandvolumeRequest is
|
|
|
|
// basically same as DevicePath because block devices are not mounted and hence
|
|
|
|
// DeviceMountPath does not get populated in resizeOptions.DeviceMountPath
|
|
|
|
opts.volumePath = resizeOptions.DevicePath
|
|
|
|
opts.fsType = fsTypeBlockName
|
|
|
|
}
|
|
|
|
|
|
|
|
if pv.Spec.AccessModes != nil {
|
|
|
|
opts.accessMode = pv.Spec.AccessModes[0]
|
2019-09-27 21:51:53 +00:00
|
|
|
}
|
|
|
|
|
2020-08-10 17:43:49 +00:00
|
|
|
_, err = csClient.NodeExpandVolume(ctx, opts)
|
2019-04-07 17:07:55 +00:00
|
|
|
if err != nil {
|
2020-08-10 17:43:49 +00:00
|
|
|
if inUseError(err) {
|
|
|
|
failedConditionErr := fmt.Errorf("Expander.NodeExpand failed to expand the volume : %w", volumetypes.NewFailedPreconditionError(err.Error()))
|
|
|
|
return false, failedConditionErr
|
|
|
|
}
|
|
|
|
return false, fmt.Errorf("Expander.NodeExpand failed to expand the volume : %w", err)
|
2019-04-07 17:07:55 +00:00
|
|
|
}
|
|
|
|
return true, nil
|
|
|
|
}
|
2020-08-10 17:43:49 +00:00
|
|
|
|
|
|
|
func inUseError(err error) bool {
|
|
|
|
st, ok := status.FromError(err)
|
|
|
|
if !ok {
|
|
|
|
// not a grpc error
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
// if this is a failed precondition error then that means driver does not support expansion
|
|
|
|
// of in-use volumes
|
|
|
|
// More info - https://github.com/container-storage-interface/spec/blob/master/spec.md#controllerexpandvolume-errors
|
|
|
|
if st.Code() == codes.FailedPrecondition {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|