mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-08-21 17:48:01 +00:00
fix azure disk translation issue
This commit is contained in:
parent
93119f4503
commit
18955db416
@ -44,6 +44,7 @@ const (
|
|||||||
var (
|
var (
|
||||||
managedDiskPathRE = regexp.MustCompile(`.*/subscriptions/(?:.*)/resourceGroups/(?:.*)/providers/Microsoft.Compute/disks/(.+)`)
|
managedDiskPathRE = regexp.MustCompile(`.*/subscriptions/(?:.*)/resourceGroups/(?:.*)/providers/Microsoft.Compute/disks/(.+)`)
|
||||||
unmanagedDiskPathRE = regexp.MustCompile(`http(?:.*)://(?:.*)/vhds/(.+)`)
|
unmanagedDiskPathRE = regexp.MustCompile(`http(?:.*)://(?:.*)/vhds/(.+)`)
|
||||||
|
managed = string(v1.AzureManagedDisk)
|
||||||
)
|
)
|
||||||
|
|
||||||
var _ InTreePlugin = &azureDiskCSITranslator{}
|
var _ InTreePlugin = &azureDiskCSITranslator{}
|
||||||
@ -110,7 +111,7 @@ func (t *azureDiskCSITranslator) TranslateInTreeInlineVolumeToCSI(volume *v1.Vol
|
|||||||
CSI: &v1.CSIPersistentVolumeSource{
|
CSI: &v1.CSIPersistentVolumeSource{
|
||||||
Driver: AzureDiskDriverName,
|
Driver: AzureDiskDriverName,
|
||||||
VolumeHandle: azureSource.DataDiskURI,
|
VolumeHandle: azureSource.DataDiskURI,
|
||||||
VolumeAttributes: map[string]string{azureDiskKind: "Managed"},
|
VolumeAttributes: map[string]string{azureDiskKind: managed},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
AccessModes: []v1.PersistentVolumeAccessMode{v1.ReadWriteOnce},
|
AccessModes: []v1.PersistentVolumeAccessMode{v1.ReadWriteOnce},
|
||||||
@ -127,9 +128,7 @@ func (t *azureDiskCSITranslator) TranslateInTreeInlineVolumeToCSI(volume *v1.Vol
|
|||||||
pv.Spec.PersistentVolumeSource.CSI.FSType = *azureSource.FSType
|
pv.Spec.PersistentVolumeSource.CSI.FSType = *azureSource.FSType
|
||||||
pv.Spec.PersistentVolumeSource.CSI.VolumeAttributes[azureDiskFSType] = *azureSource.FSType
|
pv.Spec.PersistentVolumeSource.CSI.VolumeAttributes[azureDiskFSType] = *azureSource.FSType
|
||||||
}
|
}
|
||||||
if azureSource.Kind != nil {
|
pv.Spec.PersistentVolumeSource.CSI.VolumeAttributes[azureDiskKind] = managed
|
||||||
pv.Spec.PersistentVolumeSource.CSI.VolumeAttributes[azureDiskKind] = string(*azureSource.Kind)
|
|
||||||
}
|
|
||||||
|
|
||||||
return pv, nil
|
return pv, nil
|
||||||
}
|
}
|
||||||
@ -147,7 +146,7 @@ func (t *azureDiskCSITranslator) TranslateInTreePVToCSI(pv *v1.PersistentVolume)
|
|||||||
// refer to https://github.com/kubernetes-sigs/azuredisk-csi-driver/blob/master/docs/driver-parameters.md
|
// refer to https://github.com/kubernetes-sigs/azuredisk-csi-driver/blob/master/docs/driver-parameters.md
|
||||||
csiSource = &v1.CSIPersistentVolumeSource{
|
csiSource = &v1.CSIPersistentVolumeSource{
|
||||||
Driver: AzureDiskDriverName,
|
Driver: AzureDiskDriverName,
|
||||||
VolumeAttributes: map[string]string{azureDiskKind: "Managed"},
|
VolumeAttributes: map[string]string{azureDiskKind: managed},
|
||||||
VolumeHandle: azureSource.DataDiskURI,
|
VolumeHandle: azureSource.DataDiskURI,
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
@ -160,10 +159,7 @@ func (t *azureDiskCSITranslator) TranslateInTreePVToCSI(pv *v1.PersistentVolume)
|
|||||||
csiSource.FSType = *azureSource.FSType
|
csiSource.FSType = *azureSource.FSType
|
||||||
csiSource.VolumeAttributes[azureDiskFSType] = *azureSource.FSType
|
csiSource.VolumeAttributes[azureDiskFSType] = *azureSource.FSType
|
||||||
}
|
}
|
||||||
|
csiSource.VolumeAttributes[azureDiskKind] = managed
|
||||||
if azureSource.Kind != nil {
|
|
||||||
csiSource.VolumeAttributes[azureDiskKind] = string(*azureSource.Kind)
|
|
||||||
}
|
|
||||||
|
|
||||||
if azureSource.ReadOnly != nil {
|
if azureSource.ReadOnly != nil {
|
||||||
csiSource.ReadOnly = *azureSource.ReadOnly
|
csiSource.ReadOnly = *azureSource.ReadOnly
|
||||||
@ -208,11 +204,7 @@ func (t *azureDiskCSITranslator) TranslateCSIPVToInTree(pv *v1.PersistentVolume)
|
|||||||
if fsType, ok := csiSource.VolumeAttributes[azureDiskFSType]; ok && fsType != "" {
|
if fsType, ok := csiSource.VolumeAttributes[azureDiskFSType]; ok && fsType != "" {
|
||||||
azureSource.FSType = &fsType
|
azureSource.FSType = &fsType
|
||||||
}
|
}
|
||||||
|
azureSource.Kind = &managed
|
||||||
if kind, ok := csiSource.VolumeAttributes[azureDiskKind]; ok && kind != "" {
|
|
||||||
diskKind := v1.AzureDataDiskKind(kind)
|
|
||||||
azureSource.Kind = &diskKind
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pv.Spec.CSI = nil
|
pv.Spec.CSI = nil
|
||||||
|
@ -166,6 +166,7 @@ func TestTranslateAzureDiskInTreePVToCSI(t *testing.T) {
|
|||||||
cachingMode := corev1.AzureDataDiskCachingMode("cachingmode")
|
cachingMode := corev1.AzureDataDiskCachingMode("cachingmode")
|
||||||
fsType := "fstype"
|
fsType := "fstype"
|
||||||
readOnly := true
|
readOnly := true
|
||||||
|
diskURI := "/subscriptions/12/resourceGroups/23/providers/Microsoft.Compute/disks/name"
|
||||||
|
|
||||||
cases := []struct {
|
cases := []struct {
|
||||||
name string
|
name string
|
||||||
@ -178,18 +179,18 @@ func TestTranslateAzureDiskInTreePVToCSI(t *testing.T) {
|
|||||||
expErr: true,
|
expErr: true,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "no azure file volume",
|
name: "no azure disk volume",
|
||||||
volume: &corev1.PersistentVolume{},
|
volume: &corev1.PersistentVolume{},
|
||||||
expErr: true,
|
expErr: true,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "azure file volume",
|
name: "azure disk volume",
|
||||||
volume: &corev1.PersistentVolume{
|
volume: &corev1.PersistentVolume{
|
||||||
Spec: corev1.PersistentVolumeSpec{
|
Spec: corev1.PersistentVolumeSpec{
|
||||||
PersistentVolumeSource: corev1.PersistentVolumeSource{
|
PersistentVolumeSource: corev1.PersistentVolumeSource{
|
||||||
AzureDisk: &corev1.AzureDiskVolumeSource{
|
AzureDisk: &corev1.AzureDiskVolumeSource{
|
||||||
CachingMode: &cachingMode,
|
CachingMode: &cachingMode,
|
||||||
DataDiskURI: "datadiskuri",
|
DataDiskURI: diskURI,
|
||||||
FSType: &fsType,
|
FSType: &fsType,
|
||||||
ReadOnly: &readOnly,
|
ReadOnly: &readOnly,
|
||||||
},
|
},
|
||||||
@ -208,7 +209,7 @@ func TestTranslateAzureDiskInTreePVToCSI(t *testing.T) {
|
|||||||
azureDiskFSType: fsType,
|
azureDiskFSType: fsType,
|
||||||
azureDiskKind: "Managed",
|
azureDiskKind: "Managed",
|
||||||
},
|
},
|
||||||
VolumeHandle: "datadiskuri",
|
VolumeHandle: diskURI,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
@ -233,6 +234,74 @@ func TestTranslateAzureDiskInTreePVToCSI(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestTranslateTranslateCSIPVToInTree(t *testing.T) {
|
||||||
|
cachingMode := corev1.AzureDataDiskCachingMode("cachingmode")
|
||||||
|
fsType := "fstype"
|
||||||
|
readOnly := true
|
||||||
|
diskURI := "/subscriptions/12/resourceGroups/23/providers/Microsoft.Compute/disks/name"
|
||||||
|
managed := v1.AzureManagedDisk
|
||||||
|
|
||||||
|
translator := NewAzureDiskCSITranslator()
|
||||||
|
cases := []struct {
|
||||||
|
name string
|
||||||
|
volume *corev1.PersistentVolume
|
||||||
|
expVol *corev1.PersistentVolume
|
||||||
|
expErr bool
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "azure disk volume",
|
||||||
|
volume: &corev1.PersistentVolume{
|
||||||
|
Spec: corev1.PersistentVolumeSpec{
|
||||||
|
PersistentVolumeSource: corev1.PersistentVolumeSource{
|
||||||
|
CSI: &corev1.CSIPersistentVolumeSource{
|
||||||
|
Driver: "disk.csi.azure.com",
|
||||||
|
FSType: "fstype",
|
||||||
|
ReadOnly: true,
|
||||||
|
VolumeAttributes: map[string]string{
|
||||||
|
azureDiskCachingMode: "cachingmode",
|
||||||
|
azureDiskFSType: fsType,
|
||||||
|
azureDiskKind: "managed",
|
||||||
|
},
|
||||||
|
VolumeHandle: diskURI,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
expVol: &corev1.PersistentVolume{
|
||||||
|
Spec: corev1.PersistentVolumeSpec{
|
||||||
|
PersistentVolumeSource: corev1.PersistentVolumeSource{
|
||||||
|
AzureDisk: &corev1.AzureDiskVolumeSource{
|
||||||
|
CachingMode: &cachingMode,
|
||||||
|
DataDiskURI: diskURI,
|
||||||
|
FSType: &fsType,
|
||||||
|
ReadOnly: &readOnly,
|
||||||
|
Kind: &managed,
|
||||||
|
DiskName: "name",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
expErr: false,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tc := range cases {
|
||||||
|
t.Logf("Testing %v", tc.name)
|
||||||
|
got, err := translator.TranslateCSIPVToInTree(tc.volume)
|
||||||
|
if err != nil && !tc.expErr {
|
||||||
|
t.Errorf("Did not expect error but got: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if err == nil && tc.expErr {
|
||||||
|
t.Errorf("Expected error, but did not get one.")
|
||||||
|
}
|
||||||
|
|
||||||
|
if !reflect.DeepEqual(got, tc.expVol) {
|
||||||
|
t.Errorf("Got parameters: %v, expected :%v", got, tc.expVol)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestTranslateInTreeStorageClassToCSI(t *testing.T) {
|
func TestTranslateInTreeStorageClassToCSI(t *testing.T) {
|
||||||
translator := NewAzureDiskCSITranslator()
|
translator := NewAzureDiskCSITranslator()
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user