Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure Kubernetes compatible format for Location when setting up driver #45

Merged
merged 2 commits into from
Sep 28, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion pkg/azuredisk/controllerserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ func (d *Driver) CreateVolume(ctx context.Context, req *csi.CreateVolumeRequest)
// make volume scheduled on all 3 availability zones
for i := 1; i <= 3; i++ {
topology := &csi.Topology{
Segments: map[string]string{topologyKey: fmt.Sprintf("%s-%d", diskParams.Location, i)},
Segments: map[string]string{topologyKey: fmt.Sprintf("%s-%d", azureutils.LowerCaseRegion(diskParams.Location), i)},
}
accessibleTopology = append(accessibleTopology, topology)
}
Expand All @@ -179,6 +179,11 @@ func (d *Driver) CreateVolume(ctx context.Context, req *csi.CreateVolumeRequest)
}
}

// convert diskZone to Azure compatible format
if diskZone != "" {
diskZone = azureutils.UpperCaseZone(diskParams.Location, diskZone)
}

if d.enableDiskCapacityCheck {
if ok, err := d.checkDiskCapacity(ctx, diskParams.SubscriptionID, diskParams.ResourceGroup, diskParams.DiskName, requestGiB); !ok {
return nil, err
Expand Down
14 changes: 13 additions & 1 deletion pkg/azureutils/azure_disk_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -442,7 +442,7 @@ func IsValidAvailabilityZone(zone, region string) bool {
index := strings.Index(zone, "-")
return index > 0 && index < len(zone)-1
}
return strings.HasPrefix(zone, fmt.Sprintf("%s-", region))
return strings.HasPrefix(zone, fmt.Sprintf("%s-", LowerCaseRegion(region)))
}

func IsValidDiskURI(diskURI string) error {
Expand Down Expand Up @@ -718,6 +718,18 @@ func PickAvailabilityZone(requirement *csi.TopologyRequirement, region, topology
return ""
}

// LowerCaseRegion replaces a region string containing upper case characters and whitespaces with lowercase characters and no whitespaces.
// This is required because Kubernetes does not allow whitespaces or upper case characters as label values.
func LowerCaseRegion(region string) string {
return strings.ToLower(strings.ReplaceAll(region, " ", ""))
}

// UpperCaseZone converts a Kubernetes conformant zone string to a format compatible with Azure.
func UpperCaseZone(upperCaseRegion string, lowerCaseZone string) string {
zoneSuffix := strings.TrimPrefix(lowerCaseZone, LowerCaseRegion(upperCaseRegion))
return upperCaseRegion + zoneSuffix
}

func checkDiskName(diskName string) bool {
length := len(diskName)

Expand Down
24 changes: 23 additions & 1 deletion pkg/azureutils/azure_disk_utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -910,13 +910,14 @@ func TestIsAvailabilityZone(t *testing.T) {
expected bool
}{
{"empty string should return false", "", "eastus", false},
{"wrong farmat should return false", "123", "eastus", false},
{"wrong format should return false", "123", "eastus", false},
{"wrong location should return false", "chinanorth-1", "eastus", false},
{"correct zone should return true", "eastus-1", "eastus", true},
{"empty location should return true", "eastus-1", "", true},
{"empty location with fault domain should return false", "1", "", false},
{"empty location with wrong format should return false", "-1", "", false},
{"empty location with wrong format should return false", "eastus-", "", false},
{"upper case format with white space should return true", "eastus-1 ", "East US", true},
}

for _, test := range tests {
Expand Down Expand Up @@ -1681,6 +1682,27 @@ func TestPickAvailabilityZone(t *testing.T) {
}
},
},
{
name: "upper case region",
testFunc: func(t *testing.T) {
expectedresponse := "testregion-01"
region := "Test Region"
mp := make(map[string]string)
mp["N/A"] = "testregion-01"
topology := &csi.Topology{
Segments: mp,
}
topologies := []*csi.Topology{}
topologies = append(topologies, topology)
req := &csi.TopologyRequirement{
Preferred: topologies,
}
actualresponse := PickAvailabilityZone(req, region, "N/A")
if !reflect.DeepEqual(expectedresponse, actualresponse) {
t.Errorf("actualresponse: (%v), expectedresponse: (%v)", actualresponse, expectedresponse)
}
},
},
}
for _, tc := range testCases {
t.Run(tc.name, tc.testFunc)
Expand Down
Loading