Skip to content

Commit

Permalink
Simplify the code
Browse files Browse the repository at this point in the history
  • Loading branch information
gnufied authored and huffmanca committed Jul 12, 2020
1 parent 9a7b073 commit ade2f83
Showing 1 changed file with 23 additions and 56 deletions.
79 changes: 23 additions & 56 deletions pkg/volume/csi/csi_mounter.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ import (
"k8s.io/klog/v2"

api "k8s.io/api/core/v1"
v1 "k8s.io/api/core/v1"
storage "k8s.io/api/storage/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/types"
Expand Down Expand Up @@ -278,30 +277,16 @@ func (c *csiMountMgr) SetUpAt(dir string, mounterArgs volume.MounterArgs) error
klog.V(2).Info(log("error checking for SELinux support: %s", err))
}

fsGroupFeatureGateEnabled := utilfeature.DefaultFeatureGate.Enabled(features.CSIVolumeFSGroupPolicy)
// If the feature gate isn't enabled, then adjust the CSIDriver to use the ReadWriteOnceWithFSTypeFSGroupPolicy
// policy. This keeps the default behavior.
if !fsGroupFeatureGateEnabled {
c.fsGroupPolicy = storage.ReadWriteOnceWithFSTypeFSGroupPolicy
}

// If the the FSGroupPolicy isn't NoneFSGroupPolicy, then we should attempt to modify
// the fsGroup. At this point the feature gate is enabled, so we should proceed,
// or it's disabled, at which point we should evaluate the fstype and pv.AccessMode
// and update the fsGroup appropriately.
if c.fsGroupPolicy != storage.NoneFSGroupPolicy {

// The following logic is derived from https://github.com/kubernetes/kubernetes/issues/66323
// if fstype is "", then skip fsgroup (could be indication of non-block filesystem)
// if fstype is provided and pv.AccessMode == ReadWriteOnly, then apply fsgroup
err = c.applyFSGroup(fsType, mounterArgs.FsGroup, mounterArgs.FSGroupChangePolicy)
if c.supportsFSGroup(fsType, mounterArgs.FsGroup, c.fsGroupPolicy) {
err := volume.SetVolumeOwnership(c, mounterArgs.FsGroup, mounterArgs.FSGroupChangePolicy)
if err != nil {
// At this point mount operation is successful:
// 1. Since volume can not be used by the pod because of invalid permissions, we must return error
// 2. Since mount is successful, we must record volume as mounted in uncertain state, so it can be
// cleaned up.
return volumetypes.NewUncertainProgressError(fmt.Sprintf("applyFSGroup failed for vol %s: %v", c.volumeID, err))
}
klog.V(4).Info(log("mounter.SetupAt fsGroup [%d] applied successfully to %s", *mounterArgs.FsGroup, c.volumeID))
}

klog.V(4).Infof(log("mounter.SetUp successfully requested NodePublish [%s]", dir))
Expand Down Expand Up @@ -386,48 +371,30 @@ func (c *csiMountMgr) TearDownAt(dir string) error {
return nil
}

// applyFSGroup applies the volume ownership it derives its logic
// from https://github.com/kubernetes/kubernetes/issues/66323
// 1) if fstype is "", then skip fsgroup (could be indication of non-block filesystem)
// 2) if fstype is provided and pv.AccessMode == ReadWriteOnly and !c.spec.ReadOnly then apply fsgroup
func (c *csiMountMgr) applyFSGroup(fsType string, fsGroup *int64, fsGroupChangePolicy *v1.PodFSGroupChangePolicy) error {
if c.fsGroupPolicy == storage.FileFSGroupPolicy || fsGroup != nil {

// If the FSGroupPolicy is ReadWriteOnceWithFSTypeFSGroupPolicy perform additional checks
// to determine if we should proceed with modifying the fsGroup.
if c.fsGroupPolicy == storage.ReadWriteOnceWithFSTypeFSGroupPolicy {
if fsType == "" {
klog.V(4).Info(log("mounter.SetupAt WARNING: skipping fsGroup, fsType not provided"))
return nil
}

accessModes := c.spec.PersistentVolume.Spec.AccessModes
if c.spec.PersistentVolume.Spec.AccessModes == nil {
klog.V(4).Info(log("mounter.SetupAt WARNING: skipping fsGroup, access modes not provided"))
return nil
}
if !hasReadWriteOnce(accessModes) {
klog.V(4).Info(log("mounter.SetupAt WARNING: skipping fsGroup, only support ReadWriteOnce access mode"))
return nil
}

if c.readOnly {
klog.V(4).Info(log("mounter.SetupAt WARNING: skipping fsGroup, volume is readOnly"))
return nil
}
}
func (c *csiMountMgr) supportsFSGroup(fsType string, fsGroup *int64, driverPolicy storage.FSGroupPolicy) bool {
if fsGroup == nil || driverPolicy == storage.NoneFSGroupPolicy || c.readOnly {
return false
}

err := volume.SetVolumeOwnership(c, fsGroup, fsGroupChangePolicy)
if err != nil {
return err
}
if driverPolicy == storage.FileFSGroupPolicy {
return true
}

if fsGroup != nil {
klog.V(4).Info(log("mounter.SetupAt fsGroup [%d] applied successfully to %s", *fsGroup, c.volumeID))
}
if fsType == "" {
klog.V(4).Info(log("mounter.SetupAt WARNING: skipping fsGroup, fsType not provided"))
return false
}

return nil
accessModes := c.spec.PersistentVolume.Spec.AccessModes
if c.spec.PersistentVolume.Spec.AccessModes == nil {
klog.V(4).Info(log("mounter.SetupAt WARNING: skipping fsGroup, access modes not provided"))
return false
}
if !hasReadWriteOnce(accessModes) {
klog.V(4).Info(log("mounter.SetupAt WARNING: skipping fsGroup, only support ReadWriteOnce access mode"))
return false
}
return true
}

// isDirMounted returns the !notMounted result from IsLikelyNotMountPoint check
Expand Down

0 comments on commit ade2f83

Please sign in to comment.