Skip to content

Commit 590f1a4

Browse files
authored
Merge pull request kubernetes#91093 from andyzhangx/azurefile-annotation
fix: azure file migration support on annotation behavior change
2 parents 12088aa + 6b41341 commit 590f1a4

File tree

2 files changed

+52
-2
lines changed

2 files changed

+52
-2
lines changed

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

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ const (
4141

4242
secretNameTemplate = "azure-storage-account-%s-secret"
4343
defaultSecretNamespace = "default"
44+
45+
resourceGroupAnnotation = "kubernetes.io/azure-file-resource-group"
4446
)
4547

4648
var _ InTreePlugin = &azureFileCSITranslator{}
@@ -116,7 +118,11 @@ func (t *azureFileCSITranslator) TranslateInTreePVToCSI(pv *v1.PersistentVolume)
116118
klog.Warningf("getStorageAccountName(%s) returned with error: %v", azureSource.SecretName, err)
117119
accountName = azureSource.SecretName
118120
}
119-
volumeID := fmt.Sprintf(volumeIDTemplate, "", accountName, azureSource.ShareName, "")
121+
resourceGroup := ""
122+
if v, ok := pv.ObjectMeta.Annotations[resourceGroupAnnotation]; ok {
123+
resourceGroup = v
124+
}
125+
volumeID := fmt.Sprintf(volumeIDTemplate, resourceGroup, accountName, azureSource.ShareName, "")
120126

121127
var (
122128
// refer to https://github.com/kubernetes-sigs/azurefile-csi-driver/blob/master/docs/driver-parameters.md
@@ -155,6 +161,7 @@ func (t *azureFileCSITranslator) TranslateCSIPVToInTree(pv *v1.PersistentVolume)
155161
ReadOnly: csiSource.ReadOnly,
156162
}
157163

164+
resourceGroup := ""
158165
if csiSource.NodeStageSecretRef != nil && csiSource.NodeStageSecretRef.Name != "" {
159166
azureSource.SecretName = csiSource.NodeStageSecretRef.Name
160167
azureSource.SecretNamespace = &csiSource.NodeStageSecretRef.Namespace
@@ -164,16 +171,20 @@ func (t *azureFileCSITranslator) TranslateCSIPVToInTree(pv *v1.PersistentVolume)
164171
}
165172
}
166173
} else {
167-
_, storageAccount, fileShareName, _, err := getFileShareInfo(csiSource.VolumeHandle)
174+
rg, storageAccount, fileShareName, _, err := getFileShareInfo(csiSource.VolumeHandle)
168175
if err != nil {
169176
return nil, err
170177
}
171178
azureSource.ShareName = fileShareName
172179
azureSource.SecretName = fmt.Sprintf(secretNameTemplate, storageAccount)
180+
resourceGroup = rg
173181
}
174182

175183
pv.Spec.CSI = nil
176184
pv.Spec.AzureFile = azureSource
185+
if resourceGroup != "" {
186+
pv.ObjectMeta.Annotations[resourceGroupAnnotation] = resourceGroup
187+
}
177188

178189
return pv, nil
179190
}

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

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,45 @@ func TestTranslateAzureFileInTreePVToCSI(t *testing.T) {
224224
},
225225
},
226226
},
227+
{
228+
name: "azure file volume with rg annotation",
229+
volume: &corev1.PersistentVolume{
230+
ObjectMeta: metav1.ObjectMeta{
231+
Name: "file.csi.azure.com-sharename",
232+
Annotations: map[string]string{resourceGroupAnnotation: "rg"},
233+
},
234+
Spec: corev1.PersistentVolumeSpec{
235+
PersistentVolumeSource: corev1.PersistentVolumeSource{
236+
AzureFile: &corev1.AzureFilePersistentVolumeSource{
237+
ShareName: "sharename",
238+
SecretName: "secretname",
239+
SecretNamespace: &secretNamespace,
240+
ReadOnly: true,
241+
},
242+
},
243+
},
244+
},
245+
expVol: &corev1.PersistentVolume{
246+
ObjectMeta: metav1.ObjectMeta{
247+
Name: "file.csi.azure.com-sharename",
248+
Annotations: map[string]string{resourceGroupAnnotation: "rg"},
249+
},
250+
Spec: corev1.PersistentVolumeSpec{
251+
PersistentVolumeSource: corev1.PersistentVolumeSource{
252+
CSI: &corev1.CSIPersistentVolumeSource{
253+
Driver: "file.csi.azure.com",
254+
ReadOnly: true,
255+
NodeStageSecretRef: &corev1.SecretReference{
256+
Name: "secretname",
257+
Namespace: secretNamespace,
258+
},
259+
VolumeAttributes: map[string]string{azureFileShareName: "sharename"},
260+
VolumeHandle: "rg#secretname#sharename#",
261+
},
262+
},
263+
},
264+
},
265+
},
227266
}
228267

229268
for _, tc := range cases {

0 commit comments

Comments
 (0)