Skip to content

pdcsi rox workflow fix #869

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions deploy/kubernetes/images/alpha/image.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
apiVersion: builtin
kind: ImageTagTransformer
metadata:
name: imagetag-csi-provisioner-alpha
imageTag:
name: k8s.gcr.io/sig-storage/csi-provisioner
newTag: "v3.0.0"
1 change: 1 addition & 0 deletions deploy/kubernetes/images/alpha/kustomization.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ namespace:
gce-pd-csi-driver
resources:
- ../stable-master/
- image.yaml
20 changes: 20 additions & 0 deletions deploy/kubernetes/overlays/alpha/controller_readonly.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
kind: Deployment
apiVersion: apps/v1
metadata:
name: csi-gce-pd-controller
spec:
template:
spec:
containers:
- name: csi-provisioner
args:
- "--v=5"
- "--csi-address=/csi/csi.sock"
- "--feature-gates=Topology=true"
- "--http-endpoint=:22011"
- "--leader-election-namespace=$(PDCSI_NAMESPACE)"
- "--timeout=250s"
- "--extra-create-metadata"
- "--leader-election"
- "--default-fstype=ext4"
- "--controller-publish-readonly=true"
2 changes: 2 additions & 0 deletions deploy/kubernetes/overlays/alpha/kustomization.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,7 @@ kind: Kustomization
namespace: gce-pd-csi-driver
resources:
- ../../base/
patchesStrategicMerge:
- controller_readonly.yaml
transformers:
- ../../images/alpha
5 changes: 5 additions & 0 deletions pkg/gce-pd-csi-driver/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,12 @@ func (gceCS *GCEControllerServer) CreateVolume(ctx context.Context, req *csi.Cre
return nil, status.Error(codes.Internal, fmt.Sprintf("CreateVolume disk from source volume %v is not ready", sourceVolKey))
}
}
} else { // if VolumeContentSource is nil, validate access mode is not read only
if readonly, _ := getReadOnlyFromCapabilities(volumeCapabilities); readonly {
return nil, status.Error(codes.InvalidArgument, "VolumeContentSource must be provided when AccessMode is set to read only")
}
}

// Create the disk
var disk *gce.CloudDisk
switch params.ReplicationType {
Expand Down
9 changes: 2 additions & 7 deletions pkg/gce-pd-csi-driver/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -358,19 +358,14 @@ func TestCreateVolumeArguments(t *testing.T) {
},
},
{
name: "success with MULTI_NODE_READER_ONLY",
name: "fail with MULTI_NODE_READER_ONLY",
req: &csi.CreateVolumeRequest{
Name: "test-name",
CapacityRange: stdCapRange,
VolumeCapabilities: createVolumeCapabilities(csi.VolumeCapability_AccessMode_MULTI_NODE_READER_ONLY),
Parameters: stdParams,
},
expVol: &csi.Volume{
CapacityBytes: common.GbToBytes(20),
VolumeId: testVolumeID,
VolumeContext: nil,
AccessibleTopology: stdTopology,
},
expErrCode: codes.InvalidArgument,
},
{
name: "fail with mount/MULTI_NODE_MULTI_WRITER capabilities",
Expand Down
22 changes: 22 additions & 0 deletions pkg/gce-pd-csi-driver/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ const (
volumeLimitBig int64 = 127
defaultLinuxFsType = "ext4"
defaultWindowsFsType = "ntfs"
fsTypeExt3 = "ext3"
)

func getDefaultFsType() string {
Expand Down Expand Up @@ -311,8 +312,29 @@ func (ns *GCENodeServer) NodeStageVolume(ctx context.Context, req *csi.NodeStage
return &csi.NodeStageVolumeResponse{}, nil
}

readonly, _ := getReadOnlyFromCapability(volumeCapability)
if readonly {
options = append(options, "ro")
klog.V(4).Infof("CSI volume is read-only, mounting with extra option ro")
}

err = formatAndMount(devicePath, stagingTargetPath, fstype, options, ns.Mounter)
if err != nil {
// If a volume is created from a content source like snapshot or cloning, the filesystem might get marked
// as "dirty" even if it is otherwise consistent and ext3/4 will try to restore to a consistent state by replaying
// the journal which is not possible in read-only mode. So we'll try again with noload option to skip it. This may
// allow mounting of an actually inconsistent filesystem, but because the mount is read-only no further damage should
// be caused.
if readonly && (fstype == defaultLinuxFsType || fstype == fsTypeExt3) {
klog.V(4).Infof("Failed to mount CSI volume read-only, retry mounting with extra option noload")

options = append(options, "noload")
err = formatAndMount(devicePath, stagingTargetPath, fstype, options, ns.Mounter)
if err == nil {
klog.V(4).Infof("NodeStageVolume succeeded with \"noload\" option on %v to %s", volumeID, stagingTargetPath)
return &csi.NodeStageVolumeResponse{}, nil
}
}
return nil, status.Error(codes.Internal,
fmt.Sprintf("Failed to format and mount device from (%q) to (%q) with fstype (%q) and options (%q): %v",
devicePath, stagingTargetPath, fstype, options, err))
Expand Down
25 changes: 25 additions & 0 deletions pkg/gce-pd-csi-driver/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,31 @@ func getMultiWriterFromCapabilities(vcs []*csi.VolumeCapability) (bool, error) {
return false, nil
}

func getReadOnlyFromCapability(vc *csi.VolumeCapability) (bool, error) {
if vc.GetAccessMode() == nil {
return false, errors.New("access mode is nil")
}
mode := vc.GetAccessMode().GetMode()
return (mode == csi.VolumeCapability_AccessMode_MULTI_NODE_READER_ONLY ||
mode == csi.VolumeCapability_AccessMode_SINGLE_NODE_READER_ONLY), nil
}

func getReadOnlyFromCapabilities(vcs []*csi.VolumeCapability) (bool, error) {
if vcs == nil {
return false, errors.New("volume capabilities is nil")
}
for _, vc := range vcs {
readOnly, err := getReadOnlyFromCapability(vc)
if err != nil {
return false, err
}
if readOnly {
return true, nil
}
}
return false, nil
}

func collectMountOptions(fsType string, mntFlags []string) []string {
var options []string

Expand Down
57 changes: 57 additions & 0 deletions pkg/gce-pd-csi-driver/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,3 +234,60 @@ func TestGetMultiWriterFromCapabilities(t *testing.T) {
}
}
}

func TestGetReadOnlyFromCapabilities(t *testing.T) {
testCases := []struct {
name string
vc []*csi.VolumeCapability
expVal bool
expErr bool
}{
{
name: "false with empty capabilities",
vc: []*csi.VolumeCapability{},
expVal: false,
},
{
name: "fail with capabilities no access mode",
vc: []*csi.VolumeCapability{
{
AccessType: &csi.VolumeCapability_Mount{
Mount: &csi.VolumeCapability_MountVolume{},
},
},
},
expErr: true,
},
{
name: "false with SINGLE_NODE_WRITER capabilities",
vc: createVolumeCapabilities(csi.VolumeCapability_AccessMode_SINGLE_NODE_WRITER),
expVal: false,
},
{
name: "true with MULTI_NODE_READER_ONLY capabilities",
vc: createBlockVolumeCapabilities(csi.VolumeCapability_AccessMode_MULTI_NODE_READER_ONLY),
expVal: true,
},
{
name: "true with SINGLE_NODE_READER_ONLY capabilities",
vc: createVolumeCapabilities(csi.VolumeCapability_AccessMode_SINGLE_NODE_READER_ONLY),
expVal: true,
},
}

for _, tc := range testCases {
t.Logf("Running test: %v", tc.name)
val, err := getReadOnlyFromCapabilities(tc.vc)
if tc.expErr && err == nil {
t.Fatalf("Expected error but didn't get any")
}
if !tc.expErr && err != nil {
t.Fatalf("Did not expect error but got: %v", err)
}
if err != nil {
if tc.expVal != val {
t.Fatalf("Expected '%t' but got '%t'", tc.expVal, val)
}
}
}
}