Merge pull request #108000 from RomanBednar/af-namespace-fix

AzureFile: Volume without secretNamespace fails to mount after translating to CSI
This commit is contained in:
Kubernetes Prow Robot 2022-03-08 22:00:21 -08:00 committed by GitHub
commit bbc2dbb980
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 77 additions and 1 deletions

View File

@ -129,9 +129,21 @@ func (t *azureFileCSITranslator) TranslateInTreePVToCSI(pv *v1.PersistentVolume)
resourceGroup = v
}
}
namespace := defaultSecretNamespace
// Secret is required when mounting a volume but pod presence cannot be assumed - we should not try to read pod now.
namespace := ""
// Try to read SecretNamespace from source pv.
if azureSource.SecretNamespace != nil {
namespace = *azureSource.SecretNamespace
} else {
// Try to read namespace from ClaimRef which should be always present.
if pv.Spec.ClaimRef != nil {
namespace = pv.Spec.ClaimRef.Namespace
}
}
if len(namespace) == 0 {
return nil, fmt.Errorf("could not find a secret namespace in PersistentVolumeSource or ClaimRef")
}
volumeID := fmt.Sprintf(volumeIDTemplate, resourceGroup, accountName, azureSource.ShareName, pv.ObjectMeta.Name, namespace)

View File

@ -223,6 +223,25 @@ func TestTranslateAzureFileInTreePVToCSI(t *testing.T) {
volume: &corev1.PersistentVolume{},
expErr: true,
},
{
name: "return error if secret namespace could not be found",
volume: &corev1.PersistentVolume{
ObjectMeta: metav1.ObjectMeta{
Name: "uuid",
Annotations: map[string]string{resourceGroupAnnotation: "rg"},
},
Spec: corev1.PersistentVolumeSpec{
PersistentVolumeSource: corev1.PersistentVolumeSource{
AzureFile: &corev1.AzureFilePersistentVolumeSource{
ShareName: "sharename",
SecretName: "secretname",
ReadOnly: true,
},
},
},
},
expErr: true,
},
{
name: "azure file volume",
volume: &corev1.PersistentVolume{
@ -299,6 +318,51 @@ func TestTranslateAzureFileInTreePVToCSI(t *testing.T) {
},
},
},
{
name: "get secret namespace from ClaimRef when it's missing in pv spec source",
volume: &corev1.PersistentVolume{
ObjectMeta: metav1.ObjectMeta{
Name: "uuid",
Annotations: map[string]string{resourceGroupAnnotation: "rg"},
},
Spec: corev1.PersistentVolumeSpec{
PersistentVolumeSource: corev1.PersistentVolumeSource{
AzureFile: &corev1.AzureFilePersistentVolumeSource{
ShareName: "sharename",
SecretName: "secretname",
//SecretNamespace: &secretNamespace,
ReadOnly: true,
},
},
ClaimRef: &corev1.ObjectReference{
Namespace: secretNamespace,
},
},
},
expVol: &corev1.PersistentVolume{
ObjectMeta: metav1.ObjectMeta{
Name: "uuid",
Annotations: map[string]string{resourceGroupAnnotation: "rg"},
},
Spec: corev1.PersistentVolumeSpec{
PersistentVolumeSource: corev1.PersistentVolumeSource{
CSI: &corev1.CSIPersistentVolumeSource{
Driver: "file.csi.azure.com",
ReadOnly: true,
NodeStageSecretRef: &corev1.SecretReference{
Name: "secretname",
Namespace: secretNamespace,
},
VolumeAttributes: map[string]string{shareNameField: "sharename"},
VolumeHandle: "rg#secretname#sharename#uuid#secretnamespace",
},
},
ClaimRef: &corev1.ObjectReference{
Namespace: secretNamespace,
},
},
},
},
}
for _, tc := range cases {