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 8588419187c..6a688d7098f 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 @@ -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) 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 19ba1f5225b..80a6519da13 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 @@ -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 {