Merge pull request #84809 from davidz627/fix/migReadOnly
Make GCE PD Access Mode reflect readonly status when translating an inline read only volume
This commit is contained in:
		@@ -202,7 +202,14 @@ func (g *gcePersistentDiskCSITranslator) TranslateInTreeInlineVolumeToCSI(volume
 | 
				
			|||||||
		partition = strconv.Itoa(int(pdSource.Partition))
 | 
							partition = strconv.Itoa(int(pdSource.Partition))
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	pv := &v1.PersistentVolume{
 | 
						var am v1.PersistentVolumeAccessMode
 | 
				
			||||||
 | 
						if pdSource.ReadOnly {
 | 
				
			||||||
 | 
							am = v1.ReadOnlyMany
 | 
				
			||||||
 | 
						} else {
 | 
				
			||||||
 | 
							am = v1.ReadWriteOnce
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						return &v1.PersistentVolume{
 | 
				
			||||||
		ObjectMeta: metav1.ObjectMeta{
 | 
							ObjectMeta: metav1.ObjectMeta{
 | 
				
			||||||
			// A.K.A InnerVolumeSpecName required to match for Unmount
 | 
								// A.K.A InnerVolumeSpecName required to match for Unmount
 | 
				
			||||||
			Name: volume.Name,
 | 
								Name: volume.Name,
 | 
				
			||||||
@@ -219,10 +226,9 @@ func (g *gcePersistentDiskCSITranslator) TranslateInTreeInlineVolumeToCSI(volume
 | 
				
			|||||||
					},
 | 
										},
 | 
				
			||||||
				},
 | 
									},
 | 
				
			||||||
			},
 | 
								},
 | 
				
			||||||
			AccessModes: []v1.PersistentVolumeAccessMode{v1.ReadWriteOnce},
 | 
								AccessModes: []v1.PersistentVolumeAccessMode{am},
 | 
				
			||||||
		},
 | 
							},
 | 
				
			||||||
	}
 | 
						}, nil
 | 
				
			||||||
	return pv, nil
 | 
					 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// TranslateInTreePVToCSI takes a PV with GCEPersistentDisk set from in-tree
 | 
					// TranslateInTreePVToCSI takes a PV with GCEPersistentDisk set from in-tree
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -369,3 +369,35 @@ func TestBackwardCompatibleAccessModes(t *testing.T) {
 | 
				
			|||||||
		}
 | 
							}
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func TestInlineReadOnly(t *testing.T) {
 | 
				
			||||||
 | 
						g := NewGCEPersistentDiskCSITranslator()
 | 
				
			||||||
 | 
						pv, err := g.TranslateInTreeInlineVolumeToCSI(&v1.Volume{
 | 
				
			||||||
 | 
							VolumeSource: v1.VolumeSource{
 | 
				
			||||||
 | 
								GCEPersistentDisk: &v1.GCEPersistentDiskVolumeSource{
 | 
				
			||||||
 | 
									PDName:   "foo",
 | 
				
			||||||
 | 
									ReadOnly: true,
 | 
				
			||||||
 | 
								},
 | 
				
			||||||
 | 
							},
 | 
				
			||||||
 | 
						})
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							t.Fatalf("Failed to translate in tree inline volume to CSI: %v", err)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if pv == nil || pv.Spec.PersistentVolumeSource.CSI == nil {
 | 
				
			||||||
 | 
							t.Fatal("PV or volume source unexpectedly nil")
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if !pv.Spec.PersistentVolumeSource.CSI.ReadOnly {
 | 
				
			||||||
 | 
							t.Error("PV readonly value not true")
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						ams := pv.Spec.AccessModes
 | 
				
			||||||
 | 
						if len(ams) != 1 {
 | 
				
			||||||
 | 
							t.Errorf("got am %v, expected length of 1", ams)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if ams[0] != v1.ReadOnlyMany {
 | 
				
			||||||
 | 
							t.Errorf("got am %v, expected access mode of ReadOnlyMany", ams[0])
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user