From d957dcf5a00ddf66f63afb8b7b594678dc5fe075 Mon Sep 17 00:00:00 2001 From: andyzhangx Date: Sun, 9 Jan 2022 08:36:26 +0000 Subject: [PATCH] fix: azuredisk parameter lowercase translation issue --- .../csi-translation-lib/plugins/azure_disk.go | 24 +-- .../plugins/azure_disk_test.go | 138 ++++++++++++++++-- 2 files changed, 140 insertions(+), 22 deletions(-) diff --git a/staging/src/k8s.io/csi-translation-lib/plugins/azure_disk.go b/staging/src/k8s.io/csi-translation-lib/plugins/azure_disk.go index 31145c584bc..389dd4f5b5b 100644 --- a/staging/src/k8s.io/csi-translation-lib/plugins/azure_disk.go +++ b/staging/src/k8s.io/csi-translation-lib/plugins/azure_disk.go @@ -37,8 +37,8 @@ const ( // Parameter names defined in azure disk CSI driver, refer to // https://github.com/kubernetes-sigs/azuredisk-csi-driver/blob/master/docs/driver-parameters.md azureDiskKind = "kind" - azureDiskCachingMode = "cachingMode" - azureDiskFSType = "fsType" + azureDiskCachingMode = "cachingmode" + azureDiskFSType = "fstype" ) var ( @@ -203,13 +203,19 @@ func (t *azureDiskCSITranslator) TranslateCSIPVToInTree(pv *v1.PersistentVolume) } if csiSource.VolumeAttributes != nil { - if cachingMode, ok := csiSource.VolumeAttributes[azureDiskCachingMode]; ok { - mode := v1.AzureDataDiskCachingMode(cachingMode) - azureSource.CachingMode = &mode - } - - if fsType, ok := csiSource.VolumeAttributes[azureDiskFSType]; ok && fsType != "" { - azureSource.FSType = &fsType + for k, v := range csiSource.VolumeAttributes { + switch strings.ToLower(k) { + case azureDiskCachingMode: + if v != "" { + mode := v1.AzureDataDiskCachingMode(v) + azureSource.CachingMode = &mode + } + case azureDiskFSType: + if v != "" { + fsType := v + azureSource.FSType = &fsType + } + } } azureSource.Kind = &managed } diff --git a/staging/src/k8s.io/csi-translation-lib/plugins/azure_disk_test.go b/staging/src/k8s.io/csi-translation-lib/plugins/azure_disk_test.go index ce58f15727f..1de81283360 100644 --- a/staging/src/k8s.io/csi-translation-lib/plugins/azure_disk_test.go +++ b/staging/src/k8s.io/csi-translation-lib/plugins/azure_disk_test.go @@ -220,9 +220,9 @@ func TestTranslateAzureDiskInTreePVToCSI(t *testing.T) { FSType: "fstype", ReadOnly: true, VolumeAttributes: map[string]string{ - azureDiskCachingMode: "cachingmode", - azureDiskFSType: fsType, - azureDiskKind: "Managed", + "cachingmode": "cachingmode", + azureDiskFSType: fsType, + azureDiskKind: "Managed", }, VolumeHandle: diskURI, }, @@ -267,7 +267,9 @@ func TestTranslateAzureDiskInTreePVToCSI(t *testing.T) { } func TestTranslateTranslateCSIPVToInTree(t *testing.T) { - cachingMode := corev1.AzureDataDiskCachingMode("cachingmode") + cachingModeNone := corev1.AzureDataDiskCachingNone + cachingModeReadOnly := corev1.AzureDataDiskCachingReadOnly + cachingModeReadWrite := corev1.AzureDataDiskCachingReadWrite fsType := "fstype" readOnly := true diskURI := "/subscriptions/12/resourceGroups/23/providers/Microsoft.Compute/disks/name" @@ -275,13 +277,15 @@ func TestTranslateTranslateCSIPVToInTree(t *testing.T) { translator := NewAzureDiskCSITranslator() cases := []struct { - name string - volume *corev1.PersistentVolume - expVol *corev1.PersistentVolume - expErr bool + name string + cachingMode corev1.AzureDataDiskCachingMode + volume *corev1.PersistentVolume + expVol *corev1.PersistentVolume + expErr bool }{ { - name: "azure disk volume", + name: "azure disk volume with ReadOnly cachingMode", + cachingMode: corev1.AzureDataDiskCachingReadOnly, volume: &corev1.PersistentVolume{ Spec: corev1.PersistentVolumeSpec{ PersistentVolumeSource: corev1.PersistentVolumeSource{ @@ -290,9 +294,9 @@ func TestTranslateTranslateCSIPVToInTree(t *testing.T) { FSType: "fstype", ReadOnly: true, VolumeAttributes: map[string]string{ - azureDiskCachingMode: "cachingmode", - azureDiskFSType: fsType, - azureDiskKind: "managed", + "cachingmode": "ReadOnly", + azureDiskFSType: fsType, + azureDiskKind: "managed", }, VolumeHandle: diskURI, }, @@ -303,7 +307,115 @@ func TestTranslateTranslateCSIPVToInTree(t *testing.T) { Spec: corev1.PersistentVolumeSpec{ PersistentVolumeSource: corev1.PersistentVolumeSource{ AzureDisk: &corev1.AzureDiskVolumeSource{ - CachingMode: &cachingMode, + CachingMode: &cachingModeReadOnly, + DataDiskURI: diskURI, + FSType: &fsType, + ReadOnly: &readOnly, + Kind: &managed, + DiskName: "name", + }, + }, + }, + }, + expErr: false, + }, + { + name: "azure disk volume with ReadOnly cachingMode", + cachingMode: corev1.AzureDataDiskCachingReadOnly, + 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{ + "cachingmode": "ReadOnly", + "fstype": fsType, + azureDiskKind: "managed", + }, + VolumeHandle: diskURI, + }, + }, + }, + }, + expVol: &corev1.PersistentVolume{ + Spec: corev1.PersistentVolumeSpec{ + PersistentVolumeSource: corev1.PersistentVolumeSource{ + AzureDisk: &corev1.AzureDiskVolumeSource{ + CachingMode: &cachingModeReadOnly, + DataDiskURI: diskURI, + FSType: &fsType, + ReadOnly: &readOnly, + Kind: &managed, + DiskName: "name", + }, + }, + }, + }, + expErr: false, + }, + { + name: "azure disk volume with None cachingMode", + cachingMode: corev1.AzureDataDiskCachingReadOnly, + 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{ + "cachingMode": "None", + "fsType": fsType, + azureDiskKind: "managed", + }, + VolumeHandle: diskURI, + }, + }, + }, + }, + expVol: &corev1.PersistentVolume{ + Spec: corev1.PersistentVolumeSpec{ + PersistentVolumeSource: corev1.PersistentVolumeSource{ + AzureDisk: &corev1.AzureDiskVolumeSource{ + CachingMode: &cachingModeNone, + DataDiskURI: diskURI, + FSType: &fsType, + ReadOnly: &readOnly, + Kind: &managed, + DiskName: "name", + }, + }, + }, + }, + expErr: false, + }, + { + name: "azure disk volume with ReadWrite cachingMode", + cachingMode: corev1.AzureDataDiskCachingReadOnly, + 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{ + "cachingMode": "ReadWrite", + "fsType": fsType, + azureDiskKind: "managed", + }, + VolumeHandle: diskURI, + }, + }, + }, + }, + expVol: &corev1.PersistentVolume{ + Spec: corev1.PersistentVolumeSpec{ + PersistentVolumeSource: corev1.PersistentVolumeSource{ + AzureDisk: &corev1.AzureDiskVolumeSource{ + CachingMode: &cachingModeReadWrite, DataDiskURI: diskURI, FSType: &fsType, ReadOnly: &readOnly,