From d3bfcfedb9c6d44b0df1e42c247aa47ae6369519 Mon Sep 17 00:00:00 2001 From: Maxim Rubchinsky Date: Fri, 15 Nov 2024 12:20:36 +0200 Subject: [PATCH] move FirstNotEmpty Signed-off-by: Maxim Rubchinsky --- .../alicloud/alicloud_cloud_config.go | 21 +++++++++++++----- .../alicloud/alicloud_cloud_config_test.go | 22 +++++++++++++++++++ cluster-autoscaler/cloudprovider/util_test.go | 22 ------------------- cluster-autoscaler/cloudprovider/utils.go | 12 ---------- 4 files changed, 38 insertions(+), 39 deletions(-) diff --git a/cluster-autoscaler/cloudprovider/alicloud/alicloud_cloud_config.go b/cluster-autoscaler/cloudprovider/alicloud/alicloud_cloud_config.go index 9b748c7966db..97a7021e03de 100644 --- a/cluster-autoscaler/cloudprovider/alicloud/alicloud_cloud_config.go +++ b/cluster-autoscaler/cloudprovider/alicloud/alicloud_cloud_config.go @@ -19,7 +19,6 @@ package alicloud import ( "os" - "k8s.io/autoscaler/cluster-autoscaler/cloudprovider" "k8s.io/autoscaler/cluster-autoscaler/cloudprovider/alicloud/metadata" "k8s.io/klog/v2" ) @@ -64,19 +63,19 @@ func (cc *cloudConfig) isValid() bool { } if cc.OIDCProviderARN == "" { - cc.OIDCProviderARN = cloudprovider.FirstNotEmpty(os.Getenv(oidcProviderARN), os.Getenv(oldOidcProviderARN)) + cc.OIDCProviderARN = firstNotEmpty(os.Getenv(oidcProviderARN), os.Getenv(oldOidcProviderARN)) } if cc.OIDCTokenFilePath == "" { - cc.OIDCTokenFilePath = cloudprovider.FirstNotEmpty(os.Getenv(oidcTokenFilePath), os.Getenv(oldOidcTokenFilePath)) + cc.OIDCTokenFilePath = firstNotEmpty(os.Getenv(oidcTokenFilePath), os.Getenv(oldOidcTokenFilePath)) } if cc.RoleARN == "" { - cc.RoleARN = cloudprovider.FirstNotEmpty(os.Getenv(roleARN), os.Getenv(oldRoleARN)) + cc.RoleARN = firstNotEmpty(os.Getenv(roleARN), os.Getenv(oldRoleARN)) } if cc.RoleSessionName == "" { - cc.RoleSessionName = cloudprovider.FirstNotEmpty(os.Getenv(roleSessionName), os.Getenv(oldRoleSessionName)) + cc.RoleSessionName = firstNotEmpty(os.Getenv(roleSessionName), os.Getenv(oldRoleSessionName)) } if cc.RegionId != "" && cc.AccessKeyID != "" && cc.AccessKeySecret != "" { @@ -134,3 +133,15 @@ func (cc *cloudConfig) getRegion() string { } return r } + +// firstNotEmpty returns the first non-empty string from the input list. +// If all strings are empty or no arguments are provided, it returns an empty string. +func firstNotEmpty(strs ...string) string { + for _, str := range strs { + if str != "" { + return str + } + } + + return "" +} diff --git a/cluster-autoscaler/cloudprovider/alicloud/alicloud_cloud_config_test.go b/cluster-autoscaler/cloudprovider/alicloud/alicloud_cloud_config_test.go index ce0397e9e0cd..66d5bd055584 100644 --- a/cluster-autoscaler/cloudprovider/alicloud/alicloud_cloud_config_test.go +++ b/cluster-autoscaler/cloudprovider/alicloud/alicloud_cloud_config_test.go @@ -55,3 +55,25 @@ func TestOldRRSACloudConfigIsValid(t *testing.T) { assert.True(t, cfg.isValid()) assert.True(t, cfg.RRSAEnabled) } + +func TestFirstNotEmpty(t *testing.T) { + // Test case where the first non-empty string is at the beginning + result := firstNotEmpty("hello", "world", "test") + assert.Equal(t, "hello", result) + + // Test case where the first non-empty string is in the middle + result = firstNotEmpty("", "foo", "bar") + assert.Equal(t, "foo", result) + + // Test case where the first non-empty string is at the end + result = firstNotEmpty("", "", "baz") + assert.Equal(t, "baz", result) + + // Test case where all strings are empty + result = firstNotEmpty("", "", "") + assert.Equal(t, "", result) + + // Test case with no arguments + result = firstNotEmpty() + assert.Equal(t, "", result) +} diff --git a/cluster-autoscaler/cloudprovider/util_test.go b/cluster-autoscaler/cloudprovider/util_test.go index 6599095b9745..ba23d61807a3 100644 --- a/cluster-autoscaler/cloudprovider/util_test.go +++ b/cluster-autoscaler/cloudprovider/util_test.go @@ -50,25 +50,3 @@ func TestJoinStringMaps(t *testing.T) { result := JoinStringMaps(map1, map2, map3) assert.Equal(t, map[string]string{"1": "a", "2": "d", "3": "c", "5": "e"}, result) } - -func TestFirstNotEmpty(t *testing.T) { - // Test case where the first non-empty string is at the beginning - result := FirstNotEmpty("hello", "world", "test") - assert.Equal(t, "hello", result) - - // Test case where the first non-empty string is in the middle - result = FirstNotEmpty("", "foo", "bar") - assert.Equal(t, "foo", result) - - // Test case where the first non-empty string is at the end - result = FirstNotEmpty("", "", "baz") - assert.Equal(t, "baz", result) - - // Test case where all strings are empty - result = FirstNotEmpty("", "", "") - assert.Equal(t, "", result) - - // Test case with no arguments - result = FirstNotEmpty() - assert.Equal(t, "", result) -} diff --git a/cluster-autoscaler/cloudprovider/utils.go b/cluster-autoscaler/cloudprovider/utils.go index e4588c68333c..8242fd19c66b 100644 --- a/cluster-autoscaler/cloudprovider/utils.go +++ b/cluster-autoscaler/cloudprovider/utils.go @@ -125,15 +125,3 @@ func JoinStringMaps(items ...map[string]string) map[string]string { } return result } - -// FirstNotEmpty returns the first non-empty string from the input list. -// If all strings are empty or no arguments are provided, it returns an empty string. -func FirstNotEmpty(strs ...string) string { - for _, str := range strs { - if str != "" { - return str - } - } - - return "" -}