Merge pull request #109154 from jsafrane/fix-azure-disk-migrated-zones

Fix translation of Azure availability zones
This commit is contained in:
Kubernetes Prow Robot 2022-04-05 06:15:36 -07:00 committed by GitHub
commit c6478308f8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 49 additions and 1 deletions

View File

@ -45,6 +45,7 @@ var (
managedDiskPathRE = regexp.MustCompile(`.*/subscriptions/(?:.*)/resourceGroups/(?:.*)/providers/Microsoft.Compute/disks/(.+)`)
unmanagedDiskPathRE = regexp.MustCompile(`http(?:.*)://(?:.*)/vhds/(.+)`)
managed = string(v1.AzureManagedDisk)
unzonedCSIRegionRE = regexp.MustCompile(`^[0-9]+$`)
)
var _ InTreePlugin = &azureDiskCSITranslator{}
@ -86,6 +87,7 @@ func (t *azureDiskCSITranslator) TranslateInTreeStorageClassToCSI(sc *storage.St
}
sc.AllowedTopologies = newTopologies
}
sc.AllowedTopologies = t.replaceFailureDomainsToCSI(sc.AllowedTopologies)
sc.Parameters = params
@ -273,3 +275,34 @@ func getDiskName(diskURI string) (string, error) {
}
return matches[1], nil
}
// Replace topology values for failure domains ("<number>") to "",
// as it's the value that the CSI driver expects.
func (t *azureDiskCSITranslator) replaceFailureDomainsToCSI(terms []v1.TopologySelectorTerm) []v1.TopologySelectorTerm {
if terms == nil {
return nil
}
newTopologies := []v1.TopologySelectorTerm{}
for _, term := range terms {
newTerm := term.DeepCopy()
for i := range newTerm.MatchLabelExpressions {
exp := &newTerm.MatchLabelExpressions[i]
if exp.Key == AzureDiskTopologyKey {
for j := range exp.Values {
if unzonedCSIRegionRE.Match([]byte(exp.Values[j])) {
// Topologies "0", "1" etc are used when in-tree topology is translated to CSI in Azure
// regions that don't have availability zones. E.g.:
// topology.kubernetes.io/region: westus
// topology.kubernetes.io/zone: "0"
// The CSI driver uses zone "" instead of "0" in this case.
// topology.disk.csi.azure.com/zone": ""
exp.Values[j] = ""
}
}
}
}
newTopologies = append(newTopologies, *newTerm)
}
return newTopologies
}

View File

@ -97,7 +97,7 @@ func TestGetDiskName(t *testing.T) {
}
}
func TestTranslateAzureDiskInTreeStorageClassToCSI(t *testing.T) {
func TestTranslateAzureDiskInTreeInlineVolumeToCSI(t *testing.T) {
sharedBlobDiskKind := v1.AzureDedicatedBlobDisk
translator := NewAzureDiskCSITranslator()
@ -495,6 +495,21 @@ func TestTranslateInTreeStorageClassToCSI(t *testing.T) {
options: NewStorageClass(map[string]string{"zone": "foo"}, generateToplogySelectors(AzureDiskTopologyKey, []string{"foo"})),
expErr: true,
},
{
name: "topology in regions without zones",
options: NewStorageClass(map[string]string{}, generateToplogySelectors(v1.LabelTopologyZone, []string{"0"})),
expOptions: NewStorageClass(map[string]string{}, generateToplogySelectors(AzureDiskTopologyKey, []string{""})),
},
{
name: "longer topology in regions without zones",
options: NewStorageClass(map[string]string{}, generateToplogySelectors(v1.LabelTopologyZone, []string{"1234"})),
expOptions: NewStorageClass(map[string]string{}, generateToplogySelectors(AzureDiskTopologyKey, []string{""})),
},
{
name: "topology in regions with zones",
options: NewStorageClass(map[string]string{}, generateToplogySelectors(v1.LabelTopologyZone, []string{"centralus-1"})),
expOptions: NewStorageClass(map[string]string{}, generateToplogySelectors(AzureDiskTopologyKey, []string{"centralus-1"})),
},
}
for _, tc := range tcs {