Merge pull request #94853 from andyzhangx/azurefile-migration-issue

fix azure file migration panic
This commit is contained in:
Kubernetes Prow Robot 2020-09-18 11:24:47 -07:00 committed by GitHub
commit 57e9a41ae3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 121 additions and 2 deletions

View File

@ -119,9 +119,11 @@ func (t *azureFileCSITranslator) TranslateInTreePVToCSI(pv *v1.PersistentVolume)
accountName = azureSource.SecretName
}
resourceGroup := ""
if pv.ObjectMeta.Annotations != nil {
if v, ok := pv.ObjectMeta.Annotations[resourceGroupAnnotation]; ok {
resourceGroup = v
}
}
volumeID := fmt.Sprintf(volumeIDTemplate, resourceGroup, accountName, azureSource.ShareName, "")
var (
@ -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
}

View File

@ -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