Skip to content

Commit

Permalink
Merge pull request #1991 from k8s-infra-cherrypick-robot/cherry-pick-…
Browse files Browse the repository at this point in the history
…1981-to-release-1.27

[release-1.27] fix: ensure Kubernets conformant format for location
  • Loading branch information
k8s-ci-robot authored Oct 1, 2023
2 parents 3a35431 + 7066012 commit e3f4781
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
5 changes: 5 additions & 0 deletions pkg/azureutils/azure_disk_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,11 @@ func GetCloudProviderFromClient(ctx context.Context, kubeClient *clientset.Clien
return nil, fmt.Errorf("no cloud config provided, error: %v", err)
}
} else {
// Location may be either upper case with spaces (e.g. "East US") or lower case without spaces (e.g. "eastus")
// Kubernetes does not allow whitespaces in label values, e.g. for topology keys
// ensure Kubernetes compatible format for Location by enforcing lowercase-no-space format
config.Location = strings.ToLower(strings.ReplaceAll(config.Location, " ", ""))

// disable disk related rate limit
config.DiskRateLimit = &azclients.RateLimitConfig{
CloudProviderRateLimit: false,
Expand Down
17 changes: 17 additions & 0 deletions pkg/azureutils/azure_disk_utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"io/ioutil"
"os"
"reflect"
"regexp"
"runtime"
"strings"
"testing"
Expand Down Expand Up @@ -267,6 +268,7 @@ users:
}

func TestGetCloudProvider(t *testing.T) {
locationRxp := regexp.MustCompile("(([A-Za-z0-9][-A-Za-z0-9_.]*)?[A-Za-z0-9])?")
fakeCredFile, err := testutil.GetWorkDirPath("fake-cred-file.json")
if err != nil {
t.Errorf("GetWorkDirPath failed with %v", err)
Expand Down Expand Up @@ -318,6 +320,7 @@ users:
desc string
createFakeCredFile bool
createFakeKubeConfig bool
credFile string
kubeconfig string
userAgent string
allowEmptyCloudConfig bool
Expand Down Expand Up @@ -353,6 +356,15 @@ users:
allowEmptyCloudConfig: true,
expectedErr: nil,
},
{
desc: "[success] out of cluster & in cluster, no kubeconfig, a fake credential file with upper case Location format",
createFakeCredFile: true,
kubeconfig: "",
credFile: "location: \"East US\"\n",
userAgent: "useragent",
allowEmptyCloudConfig: false,
expectedErr: nil,
},
}

for _, test := range tests {
Expand All @@ -364,6 +376,10 @@ users:
os.Remove(fakeCredFile)
}()

if err := os.WriteFile(fakeCredFile, []byte(test.credFile), 0666); err != nil {
t.Error(err)
}

t.Setenv(consts.DefaultAzureCredentialFileEnv, fakeCredFile)
}
if test.createFakeKubeConfig {
Expand All @@ -383,6 +399,7 @@ users:
t.Errorf("desc: %s,\n input: %q, GetCloudProvider err: %v, expectedErr: %v", test.desc, test.kubeconfig, err, test.expectedErr)
}
if cloud != nil {
assert.Regexp(t, locationRxp, cloud.Location)
assert.Equal(t, cloud.UserAgent, test.userAgent)
assert.Equal(t, cloud.DiskRateLimit != nil && cloud.DiskRateLimit.CloudProviderRateLimit, false)
assert.Equal(t, cloud.SnapshotRateLimit != nil && cloud.SnapshotRateLimit.CloudProviderRateLimit, false)
Expand Down

0 comments on commit e3f4781

Please sign in to comment.