mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-26 05:03:09 +00:00
fix azure file migration issue
refactor fix comments fix annoation nil map add ut
This commit is contained in:
parent
33ee864e92
commit
1df2fcf8d4
@ -37,11 +37,11 @@ const (
|
|||||||
volumeIDTemplate = "%s#%s#%s#%s"
|
volumeIDTemplate = "%s#%s#%s#%s"
|
||||||
// Parameter names defined in azure file CSI driver, refer to
|
// Parameter names defined in azure file CSI driver, refer to
|
||||||
// https://github.com/kubernetes-sigs/azurefile-csi-driver/blob/master/docs/driver-parameters.md
|
// https://github.com/kubernetes-sigs/azurefile-csi-driver/blob/master/docs/driver-parameters.md
|
||||||
azureFileShareName = "shareName"
|
shareNameField = "sharename"
|
||||||
|
secretNameField = "secretname"
|
||||||
secretNameTemplate = "azure-storage-account-%s-secret"
|
secretNamespaceField = "secretnamespace"
|
||||||
defaultSecretNamespace = "default"
|
secretNameTemplate = "azure-storage-account-%s-secret"
|
||||||
|
defaultSecretNamespace = "default"
|
||||||
resourceGroupAnnotation = "kubernetes.io/azure-file-resource-group"
|
resourceGroupAnnotation = "kubernetes.io/azure-file-resource-group"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -90,7 +90,7 @@ func (t *azureFileCSITranslator) TranslateInTreeInlineVolumeToCSI(volume *v1.Vol
|
|||||||
Driver: AzureFileDriverName,
|
Driver: AzureFileDriverName,
|
||||||
VolumeHandle: fmt.Sprintf(volumeIDTemplate, "", accountName, azureSource.ShareName, ""),
|
VolumeHandle: fmt.Sprintf(volumeIDTemplate, "", accountName, azureSource.ShareName, ""),
|
||||||
ReadOnly: azureSource.ReadOnly,
|
ReadOnly: azureSource.ReadOnly,
|
||||||
VolumeAttributes: map[string]string{azureFileShareName: azureSource.ShareName},
|
VolumeAttributes: map[string]string{shareNameField: azureSource.ShareName},
|
||||||
NodeStageSecretRef: &v1.SecretReference{
|
NodeStageSecretRef: &v1.SecretReference{
|
||||||
Name: azureSource.SecretName,
|
Name: azureSource.SecretName,
|
||||||
Namespace: defaultSecretNamespace,
|
Namespace: defaultSecretNamespace,
|
||||||
@ -135,7 +135,7 @@ func (t *azureFileCSITranslator) TranslateInTreePVToCSI(pv *v1.PersistentVolume)
|
|||||||
Namespace: defaultSecretNamespace,
|
Namespace: defaultSecretNamespace,
|
||||||
},
|
},
|
||||||
ReadOnly: azureSource.ReadOnly,
|
ReadOnly: azureSource.ReadOnly,
|
||||||
VolumeAttributes: map[string]string{azureFileShareName: azureSource.ShareName},
|
VolumeAttributes: map[string]string{shareNameField: azureSource.ShareName},
|
||||||
VolumeHandle: volumeID,
|
VolumeHandle: volumeID,
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
@ -163,31 +163,48 @@ func (t *azureFileCSITranslator) TranslateCSIPVToInTree(pv *v1.PersistentVolume)
|
|||||||
ReadOnly: csiSource.ReadOnly,
|
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 := ""
|
resourceGroup := ""
|
||||||
if csiSource.NodeStageSecretRef != nil && csiSource.NodeStageSecretRef.Name != "" {
|
if csiSource.NodeStageSecretRef != nil && csiSource.NodeStageSecretRef.Name != "" {
|
||||||
azureSource.SecretName = csiSource.NodeStageSecretRef.Name
|
azureSource.SecretName = csiSource.NodeStageSecretRef.Name
|
||||||
azureSource.SecretNamespace = &csiSource.NodeStageSecretRef.Namespace
|
azureSource.SecretNamespace = &csiSource.NodeStageSecretRef.Namespace
|
||||||
if csiSource.VolumeAttributes != nil {
|
}
|
||||||
if shareName, ok := csiSource.VolumeAttributes[azureFileShareName]; ok {
|
if azureSource.ShareName == "" || azureSource.SecretName == "" {
|
||||||
azureSource.ShareName = shareName
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
rg, storageAccount, fileShareName, _, err := getFileShareInfo(csiSource.VolumeHandle)
|
rg, storageAccount, fileShareName, _, err := getFileShareInfo(csiSource.VolumeHandle)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
azureSource.ShareName = fileShareName
|
if azureSource.ShareName == "" {
|
||||||
azureSource.SecretName = fmt.Sprintf(secretNameTemplate, storageAccount)
|
azureSource.ShareName = fileShareName
|
||||||
|
}
|
||||||
|
if azureSource.SecretName == "" {
|
||||||
|
azureSource.SecretName = fmt.Sprintf(secretNameTemplate, storageAccount)
|
||||||
|
}
|
||||||
resourceGroup = rg
|
resourceGroup = rg
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if azureSource.SecretNamespace == nil {
|
||||||
|
ns := defaultSecretNamespace
|
||||||
|
azureSource.SecretNamespace = &ns
|
||||||
|
}
|
||||||
|
|
||||||
pv.Spec.CSI = nil
|
pv.Spec.CSI = nil
|
||||||
pv.Spec.AzureFile = azureSource
|
pv.Spec.AzureFile = azureSource
|
||||||
|
if pv.ObjectMeta.Annotations == nil {
|
||||||
|
pv.ObjectMeta.Annotations = map[string]string{}
|
||||||
|
}
|
||||||
if resourceGroup != "" {
|
if resourceGroup != "" {
|
||||||
if pv.ObjectMeta.Annotations == nil {
|
|
||||||
pv.ObjectMeta.Annotations = map[string]string{}
|
|
||||||
}
|
|
||||||
pv.ObjectMeta.Annotations[resourceGroupAnnotation] = resourceGroup
|
pv.ObjectMeta.Annotations[resourceGroupAnnotation] = resourceGroup
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -140,7 +140,7 @@ func TestTranslateAzureFileInTreeStorageClassToCSI(t *testing.T) {
|
|||||||
Namespace: "default",
|
Namespace: "default",
|
||||||
},
|
},
|
||||||
ReadOnly: true,
|
ReadOnly: true,
|
||||||
VolumeAttributes: map[string]string{azureFileShareName: "sharename"},
|
VolumeAttributes: map[string]string{shareNameField: "sharename"},
|
||||||
VolumeHandle: "#secretname#sharename#",
|
VolumeHandle: "#secretname#sharename#",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
@ -217,7 +217,7 @@ func TestTranslateAzureFileInTreePVToCSI(t *testing.T) {
|
|||||||
Name: "secretname",
|
Name: "secretname",
|
||||||
Namespace: secretNamespace,
|
Namespace: secretNamespace,
|
||||||
},
|
},
|
||||||
VolumeAttributes: map[string]string{azureFileShareName: "sharename"},
|
VolumeAttributes: map[string]string{shareNameField: "sharename"},
|
||||||
VolumeHandle: "#secretname#sharename#",
|
VolumeHandle: "#secretname#sharename#",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
@ -256,7 +256,7 @@ func TestTranslateAzureFileInTreePVToCSI(t *testing.T) {
|
|||||||
Name: "secretname",
|
Name: "secretname",
|
||||||
Namespace: secretNamespace,
|
Namespace: secretNamespace,
|
||||||
},
|
},
|
||||||
VolumeAttributes: map[string]string{azureFileShareName: "sharename"},
|
VolumeAttributes: map[string]string{shareNameField: "sharename"},
|
||||||
VolumeHandle: "rg#secretname#sharename#",
|
VolumeHandle: "rg#secretname#sharename#",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
@ -285,9 +285,18 @@ func TestTranslateAzureFileInTreePVToCSI(t *testing.T) {
|
|||||||
func TestTranslateCSIPVToInTree(t *testing.T) {
|
func TestTranslateCSIPVToInTree(t *testing.T) {
|
||||||
translator := NewAzureFileCSITranslator()
|
translator := NewAzureFileCSITranslator()
|
||||||
|
|
||||||
|
secretName := "secretname"
|
||||||
secretNamespace := "secretnamespace"
|
secretNamespace := "secretnamespace"
|
||||||
|
shareName := "sharename"
|
||||||
|
defaultNS := "default"
|
||||||
mp := make(map[string]string)
|
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 {
|
cases := []struct {
|
||||||
name string
|
name string
|
||||||
volume *corev1.PersistentVolume
|
volume *corev1.PersistentVolume
|
||||||
@ -315,13 +324,16 @@ func TestTranslateCSIPVToInTree(t *testing.T) {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
expVol: &corev1.PersistentVolume{
|
expVol: &corev1.PersistentVolume{
|
||||||
|
ObjectMeta: metav1.ObjectMeta{
|
||||||
|
Annotations: map[string]string{},
|
||||||
|
},
|
||||||
Spec: corev1.PersistentVolumeSpec{
|
Spec: corev1.PersistentVolumeSpec{
|
||||||
PersistentVolumeSource: corev1.PersistentVolumeSource{
|
PersistentVolumeSource: corev1.PersistentVolumeSource{
|
||||||
AzureFile: &corev1.AzureFilePersistentVolumeSource{
|
AzureFile: &corev1.AzureFilePersistentVolumeSource{
|
||||||
SecretName: "ut",
|
SecretName: "ut",
|
||||||
SecretNamespace: &secretNamespace,
|
SecretNamespace: &secretNamespace,
|
||||||
ReadOnly: true,
|
ReadOnly: true,
|
||||||
ShareName: "unit-test",
|
ShareName: shareName,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
@ -334,7 +346,7 @@ func TestTranslateCSIPVToInTree(t *testing.T) {
|
|||||||
Spec: corev1.PersistentVolumeSpec{
|
Spec: corev1.PersistentVolumeSpec{
|
||||||
PersistentVolumeSource: corev1.PersistentVolumeSource{
|
PersistentVolumeSource: corev1.PersistentVolumeSource{
|
||||||
CSI: &corev1.CSIPersistentVolumeSource{
|
CSI: &corev1.CSIPersistentVolumeSource{
|
||||||
VolumeHandle: "unit-test",
|
VolumeHandle: shareName,
|
||||||
ReadOnly: true,
|
ReadOnly: true,
|
||||||
VolumeAttributes: mp,
|
VolumeAttributes: mp,
|
||||||
},
|
},
|
||||||
@ -344,7 +356,7 @@ func TestTranslateCSIPVToInTree(t *testing.T) {
|
|||||||
expErr: true,
|
expErr: true,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "translate from volume handle",
|
name: "translate from VolumeAttributes",
|
||||||
volume: &corev1.PersistentVolume{
|
volume: &corev1.PersistentVolume{
|
||||||
ObjectMeta: metav1.ObjectMeta{
|
ObjectMeta: metav1.ObjectMeta{
|
||||||
Name: "file.csi.azure.com-sharename",
|
Name: "file.csi.azure.com-sharename",
|
||||||
@ -367,9 +379,116 @@ func TestTranslateCSIPVToInTree(t *testing.T) {
|
|||||||
Spec: corev1.PersistentVolumeSpec{
|
Spec: corev1.PersistentVolumeSpec{
|
||||||
PersistentVolumeSource: corev1.PersistentVolumeSource{
|
PersistentVolumeSource: corev1.PersistentVolumeSource{
|
||||||
AzureFile: &corev1.AzureFilePersistentVolumeSource{
|
AzureFile: &corev1.AzureFilePersistentVolumeSource{
|
||||||
SecretName: "azure-storage-account-st-secret",
|
SecretName: "azure-storage-account-st-secret",
|
||||||
ShareName: "pvc-file-dynamic",
|
ShareName: shareName,
|
||||||
ReadOnly: true,
|
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",
|
||||||
|
},
|
||||||
|
Spec: corev1.PersistentVolumeSpec{
|
||||||
|
PersistentVolumeSource: corev1.PersistentVolumeSource{
|
||||||
|
CSI: &corev1.CSIPersistentVolumeSource{
|
||||||
|
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,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
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: "pvc-file-dynamic",
|
||||||
|
SecretNamespace: &defaultNS,
|
||||||
|
ReadOnly: true,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
Loading…
Reference in New Issue
Block a user