From b260c145282cf5cf1c025a60e31a0e1ddced73c0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Wei=C3=9Fe?= Date: Thu, 28 Sep 2023 11:51:57 +0200 Subject: [PATCH] fix: ensure Kubernets conformant format for location MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Daniel Weiße --- pkg/azureutils/azure_disk_utils.go | 5 +++++ pkg/azureutils/azure_disk_utils_test.go | 17 +++++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/pkg/azureutils/azure_disk_utils.go b/pkg/azureutils/azure_disk_utils.go index b4c2260393..9295b74898 100644 --- a/pkg/azureutils/azure_disk_utils.go +++ b/pkg/azureutils/azure_disk_utils.go @@ -216,6 +216,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, diff --git a/pkg/azureutils/azure_disk_utils_test.go b/pkg/azureutils/azure_disk_utils_test.go index 96fa64725b..81386f73f9 100644 --- a/pkg/azureutils/azure_disk_utils_test.go +++ b/pkg/azureutils/azure_disk_utils_test.go @@ -23,6 +23,7 @@ import ( "io/ioutil" "os" "reflect" + "regexp" "runtime" "strings" "testing" @@ -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) @@ -318,6 +320,7 @@ users: desc string createFakeCredFile bool createFakeKubeConfig bool + credFile string kubeconfig string userAgent string allowEmptyCloudConfig bool @@ -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 { @@ -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 { @@ -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)