Fix translation of Azure availability zones

Some Azure regions do not have any availability zones. Nodes in such zones have
these labels, where "0" is a fault domain, not an availability zone.

  "topology.kubernetes.io/region": "westus"
  "topology.kubernetes.io/zone": "0"

Azure Disk CSI driver uses zone "" in this case:
  "topology.disk.csi.azure.com/zone": ""

Fix the CSI translation to translate the fault domain "0" to "". Any value that is just
a number and not "<region>-<number>" ("centralus-1") needs to be translated to
"" in similar fashion.
This commit is contained in:
Jan Safranek 2022-03-30 14:58:07 +02:00
parent 691d4c3989
commit ac9c93224e
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 {