Skip to content

Commit 57e9a41

Browse files
authored
Merge pull request kubernetes#94853 from andyzhangx/azurefile-migration-issue
fix azure file migration panic
2 parents c6cdc02 + 3be5fe1 commit 57e9a41

File tree

2 files changed

+121
-2
lines changed

2 files changed

+121
-2
lines changed

staging/src/k8s.io/csi-translation-lib/plugins/azure_file.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,8 +119,10 @@ func (t *azureFileCSITranslator) TranslateInTreePVToCSI(pv *v1.PersistentVolume)
119119
accountName = azureSource.SecretName
120120
}
121121
resourceGroup := ""
122-
if v, ok := pv.ObjectMeta.Annotations[resourceGroupAnnotation]; ok {
123-
resourceGroup = v
122+
if pv.ObjectMeta.Annotations != nil {
123+
if v, ok := pv.ObjectMeta.Annotations[resourceGroupAnnotation]; ok {
124+
resourceGroup = v
125+
}
124126
}
125127
volumeID := fmt.Sprintf(volumeIDTemplate, resourceGroup, accountName, azureSource.ShareName, "")
126128

@@ -183,6 +185,9 @@ func (t *azureFileCSITranslator) TranslateCSIPVToInTree(pv *v1.PersistentVolume)
183185
pv.Spec.CSI = nil
184186
pv.Spec.AzureFile = azureSource
185187
if resourceGroup != "" {
188+
if pv.ObjectMeta.Annotations == nil {
189+
pv.ObjectMeta.Annotations = map[string]string{}
190+
}
186191
pv.ObjectMeta.Annotations[resourceGroupAnnotation] = resourceGroup
187192
}
188193

staging/src/k8s.io/csi-translation-lib/plugins/azure_file_test.go

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,120 @@ func TestTranslateAzureFileInTreePVToCSI(t *testing.T) {
282282
}
283283
}
284284

285+
func TestTranslateCSIPVToInTree(t *testing.T) {
286+
translator := NewAzureFileCSITranslator()
287+
288+
secretNamespace := "secretnamespace"
289+
mp := make(map[string]string)
290+
mp["shareName"] = "unit-test"
291+
cases := []struct {
292+
name string
293+
volume *corev1.PersistentVolume
294+
expVol *corev1.PersistentVolume
295+
expErr bool
296+
}{
297+
{
298+
name: "empty volume",
299+
expErr: true,
300+
},
301+
{
302+
name: "resource group empty",
303+
volume: &corev1.PersistentVolume{
304+
Spec: corev1.PersistentVolumeSpec{
305+
PersistentVolumeSource: corev1.PersistentVolumeSource{
306+
CSI: &corev1.CSIPersistentVolumeSource{
307+
NodeStageSecretRef: &corev1.SecretReference{
308+
Name: "ut",
309+
Namespace: secretNamespace,
310+
},
311+
ReadOnly: true,
312+
VolumeAttributes: mp,
313+
},
314+
},
315+
},
316+
},
317+
expVol: &corev1.PersistentVolume{
318+
Spec: corev1.PersistentVolumeSpec{
319+
PersistentVolumeSource: corev1.PersistentVolumeSource{
320+
AzureFile: &corev1.AzureFilePersistentVolumeSource{
321+
SecretName: "ut",
322+
SecretNamespace: &secretNamespace,
323+
ReadOnly: true,
324+
ShareName: "unit-test",
325+
},
326+
},
327+
},
328+
},
329+
expErr: false,
330+
},
331+
{
332+
name: "translate from volume handle error",
333+
volume: &corev1.PersistentVolume{
334+
Spec: corev1.PersistentVolumeSpec{
335+
PersistentVolumeSource: corev1.PersistentVolumeSource{
336+
CSI: &corev1.CSIPersistentVolumeSource{
337+
VolumeHandle: "unit-test",
338+
ReadOnly: true,
339+
VolumeAttributes: mp,
340+
},
341+
},
342+
},
343+
},
344+
expErr: true,
345+
},
346+
{
347+
name: "translate from volume handle",
348+
volume: &corev1.PersistentVolume{
349+
ObjectMeta: metav1.ObjectMeta{
350+
Name: "file.csi.azure.com-sharename",
351+
},
352+
Spec: corev1.PersistentVolumeSpec{
353+
PersistentVolumeSource: corev1.PersistentVolumeSource{
354+
CSI: &corev1.CSIPersistentVolumeSource{
355+
VolumeHandle: "rg#st#pvc-file-dynamic#diskname.vhd",
356+
ReadOnly: true,
357+
VolumeAttributes: mp,
358+
},
359+
},
360+
},
361+
},
362+
expVol: &corev1.PersistentVolume{
363+
ObjectMeta: metav1.ObjectMeta{
364+
Name: "file.csi.azure.com-sharename",
365+
Annotations: map[string]string{resourceGroupAnnotation: "rg"},
366+
},
367+
Spec: corev1.PersistentVolumeSpec{
368+
PersistentVolumeSource: corev1.PersistentVolumeSource{
369+
AzureFile: &corev1.AzureFilePersistentVolumeSource{
370+
SecretName: "azure-storage-account-st-secret",
371+
ShareName: "pvc-file-dynamic",
372+
ReadOnly: true,
373+
},
374+
},
375+
},
376+
},
377+
expErr: false,
378+
},
379+
}
380+
381+
for _, tc := range cases {
382+
t.Logf("Testing %v", tc.name)
383+
got, err := translator.TranslateCSIPVToInTree(tc.volume)
384+
if err != nil && !tc.expErr {
385+
t.Errorf("Did not expect error but got: %v", err)
386+
}
387+
388+
if err == nil && tc.expErr {
389+
t.Errorf("Expected error, but did not get one.")
390+
}
391+
392+
if !reflect.DeepEqual(got, tc.expVol) {
393+
t.Errorf("Got parameters: %v, expected :%v", got, tc.expVol)
394+
}
395+
}
396+
397+
}
398+
285399
func TestGetStorageAccount(t *testing.T) {
286400
tests := []struct {
287401
secretName string

0 commit comments

Comments
 (0)