Skip to content

Commit

Permalink
fix azure file migration issue
Browse files Browse the repository at this point in the history
refactor

fix comments

fix annoation nil map

add ut
  • Loading branch information
andyzhangx committed Jan 23, 2021
1 parent 68b1d9b commit 82dfad9
Show file tree
Hide file tree
Showing 2 changed files with 164 additions and 28 deletions.
53 changes: 35 additions & 18 deletions staging/src/k8s.io/csi-translation-lib/plugins/azure_file.go
Expand Up @@ -37,11 +37,11 @@ const (
volumeIDTemplate = "%s#%s#%s#%s"
// Parameter names defined in azure file CSI driver, refer to
// https://github.com/kubernetes-sigs/azurefile-csi-driver/blob/master/docs/driver-parameters.md
azureFileShareName = "shareName"

secretNameTemplate = "azure-storage-account-%s-secret"
defaultSecretNamespace = "default"

shareNameField = "sharename"
secretNameField = "secretname"
secretNamespaceField = "secretnamespace"
secretNameTemplate = "azure-storage-account-%s-secret"
defaultSecretNamespace = "default"
resourceGroupAnnotation = "kubernetes.io/azure-file-resource-group"
)

Expand Down Expand Up @@ -90,7 +90,7 @@ func (t *azureFileCSITranslator) TranslateInTreeInlineVolumeToCSI(volume *v1.Vol
Driver: AzureFileDriverName,
VolumeHandle: fmt.Sprintf(volumeIDTemplate, "", accountName, azureSource.ShareName, ""),
ReadOnly: azureSource.ReadOnly,
VolumeAttributes: map[string]string{azureFileShareName: azureSource.ShareName},
VolumeAttributes: map[string]string{shareNameField: azureSource.ShareName},
NodeStageSecretRef: &v1.SecretReference{
Name: azureSource.SecretName,
Namespace: defaultSecretNamespace,
Expand Down Expand Up @@ -135,7 +135,7 @@ func (t *azureFileCSITranslator) TranslateInTreePVToCSI(pv *v1.PersistentVolume)
Namespace: defaultSecretNamespace,
},
ReadOnly: azureSource.ReadOnly,
VolumeAttributes: map[string]string{azureFileShareName: azureSource.ShareName},
VolumeAttributes: map[string]string{shareNameField: azureSource.ShareName},
VolumeHandle: volumeID,
}
)
Expand Down Expand Up @@ -163,31 +163,48 @@ func (t *azureFileCSITranslator) TranslateCSIPVToInTree(pv *v1.PersistentVolume)
ReadOnly: csiSource.ReadOnly,
}

for k, v := range csiSource.VolumeAttributes {
switch strings.ToLower(k) {
case shareNameField:
azureSource.ShareName = v
case secretNameField:
azureSource.SecretName = v
case secretNamespaceField:
ns := v
azureSource.SecretNamespace = &ns
}
}

resourceGroup := ""
if csiSource.NodeStageSecretRef != nil && csiSource.NodeStageSecretRef.Name != "" {
azureSource.SecretName = csiSource.NodeStageSecretRef.Name
azureSource.SecretNamespace = &csiSource.NodeStageSecretRef.Namespace
if csiSource.VolumeAttributes != nil {
if shareName, ok := csiSource.VolumeAttributes[azureFileShareName]; ok {
azureSource.ShareName = shareName
}
}
} else {
}
if azureSource.ShareName == "" || azureSource.SecretName == "" {
rg, storageAccount, fileShareName, _, err := getFileShareInfo(csiSource.VolumeHandle)
if err != nil {
return nil, err
}
azureSource.ShareName = fileShareName
azureSource.SecretName = fmt.Sprintf(secretNameTemplate, storageAccount)
if azureSource.ShareName == "" {
azureSource.ShareName = fileShareName
}
if azureSource.SecretName == "" {
azureSource.SecretName = fmt.Sprintf(secretNameTemplate, storageAccount)
}
resourceGroup = rg
}

if azureSource.SecretNamespace == nil {
ns := defaultSecretNamespace
azureSource.SecretNamespace = &ns
}

pv.Spec.CSI = nil
pv.Spec.AzureFile = azureSource
if pv.ObjectMeta.Annotations == nil {
pv.ObjectMeta.Annotations = map[string]string{}
}
if resourceGroup != "" {
if pv.ObjectMeta.Annotations == nil {
pv.ObjectMeta.Annotations = map[string]string{}
}
pv.ObjectMeta.Annotations[resourceGroupAnnotation] = resourceGroup
}

Expand Down
139 changes: 129 additions & 10 deletions staging/src/k8s.io/csi-translation-lib/plugins/azure_file_test.go
Expand Up @@ -140,7 +140,7 @@ func TestTranslateAzureFileInTreeStorageClassToCSI(t *testing.T) {
Namespace: "default",
},
ReadOnly: true,
VolumeAttributes: map[string]string{azureFileShareName: "sharename"},
VolumeAttributes: map[string]string{shareNameField: "sharename"},
VolumeHandle: "#secretname#sharename#",
},
},
Expand Down Expand Up @@ -217,7 +217,7 @@ func TestTranslateAzureFileInTreePVToCSI(t *testing.T) {
Name: "secretname",
Namespace: secretNamespace,
},
VolumeAttributes: map[string]string{azureFileShareName: "sharename"},
VolumeAttributes: map[string]string{shareNameField: "sharename"},
VolumeHandle: "#secretname#sharename#",
},
},
Expand Down Expand Up @@ -256,7 +256,7 @@ func TestTranslateAzureFileInTreePVToCSI(t *testing.T) {
Name: "secretname",
Namespace: secretNamespace,
},
VolumeAttributes: map[string]string{azureFileShareName: "sharename"},
VolumeAttributes: map[string]string{shareNameField: "sharename"},
VolumeHandle: "rg#secretname#sharename#",
},
},
Expand Down Expand Up @@ -285,9 +285,18 @@ func TestTranslateAzureFileInTreePVToCSI(t *testing.T) {
func TestTranslateCSIPVToInTree(t *testing.T) {
translator := NewAzureFileCSITranslator()

secretName := "secretname"
secretNamespace := "secretnamespace"
shareName := "sharename"
defaultNS := "default"
mp := make(map[string]string)
mp["shareName"] = "unit-test"
mp["shareName"] = shareName

secretMap := make(map[string]string)
secretMap["shareName"] = shareName
secretMap["secretName"] = secretName
secretMap["secretNamespace"] = secretNamespace

cases := []struct {
name string
volume *corev1.PersistentVolume
Expand Down Expand Up @@ -315,13 +324,16 @@ func TestTranslateCSIPVToInTree(t *testing.T) {
},
},
expVol: &corev1.PersistentVolume{
ObjectMeta: metav1.ObjectMeta{
Annotations: map[string]string{},
},
Spec: corev1.PersistentVolumeSpec{
PersistentVolumeSource: corev1.PersistentVolumeSource{
AzureFile: &corev1.AzureFilePersistentVolumeSource{
SecretName: "ut",
SecretNamespace: &secretNamespace,
ReadOnly: true,
ShareName: "unit-test",
ShareName: shareName,
},
},
},
Expand All @@ -334,7 +346,7 @@ func TestTranslateCSIPVToInTree(t *testing.T) {
Spec: corev1.PersistentVolumeSpec{
PersistentVolumeSource: corev1.PersistentVolumeSource{
CSI: &corev1.CSIPersistentVolumeSource{
VolumeHandle: "unit-test",
VolumeHandle: shareName,
ReadOnly: true,
VolumeAttributes: mp,
},
Expand All @@ -344,7 +356,76 @@ func TestTranslateCSIPVToInTree(t *testing.T) {
expErr: true,
},
{
name: "translate from volume handle",
name: "translate from VolumeAttributes",
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: shareName,
SecretNamespace: &defaultNS,
ReadOnly: true,
},
},
},
},
expErr: false,
},
{
name: "translate from SecretMap VolumeAttributes",
volume: &corev1.PersistentVolume{
ObjectMeta: metav1.ObjectMeta{
Name: "file.csi.azure.com-sharename",
Annotations: map[string]string{},
},
Spec: corev1.PersistentVolumeSpec{
PersistentVolumeSource: corev1.PersistentVolumeSource{
CSI: &corev1.CSIPersistentVolumeSource{
VolumeHandle: "rg#st#pvc-file-dynamic#diskname.vhd",
ReadOnly: true,
VolumeAttributes: secretMap,
},
},
},
},
expVol: &corev1.PersistentVolume{
ObjectMeta: metav1.ObjectMeta{
Name: "file.csi.azure.com-sharename",
Annotations: map[string]string{},
},
Spec: corev1.PersistentVolumeSpec{
PersistentVolumeSource: corev1.PersistentVolumeSource{
AzureFile: &corev1.AzureFilePersistentVolumeSource{
SecretName: secretName,
SecretNamespace: &secretNamespace,
ShareName: shareName,
ReadOnly: true,
},
},
},
},
expErr: false,
},
{
name: "translate from NodeStageSecretRef",
volume: &corev1.PersistentVolume{
ObjectMeta: metav1.ObjectMeta{
Name: "file.csi.azure.com-sharename",
Expand All @@ -355,6 +436,43 @@ func TestTranslateCSIPVToInTree(t *testing.T) {
VolumeHandle: "rg#st#pvc-file-dynamic#diskname.vhd",
ReadOnly: true,
VolumeAttributes: mp,
NodeStageSecretRef: &corev1.SecretReference{
Name: secretName,
Namespace: secretNamespace,
},
},
},
},
},
expVol: &corev1.PersistentVolume{
ObjectMeta: metav1.ObjectMeta{
Name: "file.csi.azure.com-sharename",
Annotations: map[string]string{},
},
Spec: corev1.PersistentVolumeSpec{
PersistentVolumeSource: corev1.PersistentVolumeSource{
AzureFile: &corev1.AzureFilePersistentVolumeSource{
SecretName: secretName,
ShareName: shareName,
SecretNamespace: &secretNamespace,
ReadOnly: true,
},
},
},
},
expErr: false,
},
{
name: "translate from VolumeHandle",
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,
},
},
},
Expand All @@ -367,9 +485,10 @@ func TestTranslateCSIPVToInTree(t *testing.T) {
Spec: corev1.PersistentVolumeSpec{
PersistentVolumeSource: corev1.PersistentVolumeSource{
AzureFile: &corev1.AzureFilePersistentVolumeSource{
SecretName: "azure-storage-account-st-secret",
ShareName: "pvc-file-dynamic",
ReadOnly: true,
SecretName: "azure-storage-account-st-secret",
ShareName: "pvc-file-dynamic",
SecretNamespace: &defaultNS,
ReadOnly: true,
},
},
},
Expand Down

0 comments on commit 82dfad9

Please sign in to comment.