From 0d4799964558b1e96587737613d6e79e1679cb82 Mon Sep 17 00:00:00 2001 From: andyzhangx Date: Thu, 17 Sep 2020 13:19:13 +0000 Subject: [PATCH 1/2] fix azure file migration panic --- .../src/k8s.io/csi-translation-lib/plugins/azure_file.go | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/staging/src/k8s.io/csi-translation-lib/plugins/azure_file.go b/staging/src/k8s.io/csi-translation-lib/plugins/azure_file.go index ff1c2b0b517..94ef714fe27 100644 --- a/staging/src/k8s.io/csi-translation-lib/plugins/azure_file.go +++ b/staging/src/k8s.io/csi-translation-lib/plugins/azure_file.go @@ -119,8 +119,10 @@ func (t *azureFileCSITranslator) TranslateInTreePVToCSI(pv *v1.PersistentVolume) accountName = azureSource.SecretName } resourceGroup := "" - if v, ok := pv.ObjectMeta.Annotations[resourceGroupAnnotation]; ok { - resourceGroup = v + if pv.ObjectMeta.Annotations != nil { + if v, ok := pv.ObjectMeta.Annotations[resourceGroupAnnotation]; ok { + resourceGroup = v + } } volumeID := fmt.Sprintf(volumeIDTemplate, resourceGroup, accountName, azureSource.ShareName, "") @@ -183,6 +185,9 @@ func (t *azureFileCSITranslator) TranslateCSIPVToInTree(pv *v1.PersistentVolume) pv.Spec.CSI = nil pv.Spec.AzureFile = azureSource if resourceGroup != "" { + if pv.ObjectMeta.Annotations == nil { + pv.ObjectMeta.Annotations = map[string]string{} + } pv.ObjectMeta.Annotations[resourceGroupAnnotation] = resourceGroup } From 3be5fe15d9dace2cb93f08420758598e1070b081 Mon Sep 17 00:00:00 2001 From: Sakuralbj Date: Fri, 18 Sep 2020 18:52:29 +0800 Subject: [PATCH 2/2] test: add unit-test for TranslateCSIPVToInTree. --- .../plugins/azure_file_test.go | 114 ++++++++++++++++++ 1 file changed, 114 insertions(+) diff --git a/staging/src/k8s.io/csi-translation-lib/plugins/azure_file_test.go b/staging/src/k8s.io/csi-translation-lib/plugins/azure_file_test.go index ffc4be5d29e..66c4dc14453 100644 --- a/staging/src/k8s.io/csi-translation-lib/plugins/azure_file_test.go +++ b/staging/src/k8s.io/csi-translation-lib/plugins/azure_file_test.go @@ -282,6 +282,120 @@ func TestTranslateAzureFileInTreePVToCSI(t *testing.T) { } } +func TestTranslateCSIPVToInTree(t *testing.T) { + translator := NewAzureFileCSITranslator() + + secretNamespace := "secretnamespace" + mp := make(map[string]string) + mp["shareName"] = "unit-test" + cases := []struct { + name string + volume *corev1.PersistentVolume + expVol *corev1.PersistentVolume + expErr bool + }{ + { + name: "empty volume", + expErr: true, + }, + { + name: "resource group empty", + volume: &corev1.PersistentVolume{ + Spec: corev1.PersistentVolumeSpec{ + PersistentVolumeSource: corev1.PersistentVolumeSource{ + CSI: &corev1.CSIPersistentVolumeSource{ + NodeStageSecretRef: &corev1.SecretReference{ + Name: "ut", + Namespace: secretNamespace, + }, + ReadOnly: true, + VolumeAttributes: mp, + }, + }, + }, + }, + expVol: &corev1.PersistentVolume{ + Spec: corev1.PersistentVolumeSpec{ + PersistentVolumeSource: corev1.PersistentVolumeSource{ + AzureFile: &corev1.AzureFilePersistentVolumeSource{ + SecretName: "ut", + SecretNamespace: &secretNamespace, + ReadOnly: true, + ShareName: "unit-test", + }, + }, + }, + }, + expErr: false, + }, + { + name: "translate from volume handle error", + volume: &corev1.PersistentVolume{ + Spec: corev1.PersistentVolumeSpec{ + PersistentVolumeSource: corev1.PersistentVolumeSource{ + CSI: &corev1.CSIPersistentVolumeSource{ + VolumeHandle: "unit-test", + ReadOnly: true, + VolumeAttributes: mp, + }, + }, + }, + }, + expErr: true, + }, + { + name: "translate from volume handle", + volume: &corev1.PersistentVolume{ + ObjectMeta: metav1.ObjectMeta{ + Name: "file.csi.azure.com-sharename", + }, + Spec: corev1.PersistentVolumeSpec{ + PersistentVolumeSource: corev1.PersistentVolumeSource{ + CSI: &corev1.CSIPersistentVolumeSource{ + VolumeHandle: "rg#st#pvc-file-dynamic#diskname.vhd", + ReadOnly: true, + VolumeAttributes: mp, + }, + }, + }, + }, + expVol: &corev1.PersistentVolume{ + ObjectMeta: metav1.ObjectMeta{ + Name: "file.csi.azure.com-sharename", + Annotations: map[string]string{resourceGroupAnnotation: "rg"}, + }, + Spec: corev1.PersistentVolumeSpec{ + PersistentVolumeSource: corev1.PersistentVolumeSource{ + AzureFile: &corev1.AzureFilePersistentVolumeSource{ + SecretName: "azure-storage-account-st-secret", + ShareName: "pvc-file-dynamic", + ReadOnly: true, + }, + }, + }, + }, + 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 TestGetStorageAccount(t *testing.T) { tests := []struct { secretName string