Skip to content

Commit

Permalink
move FirstNotEmpty
Browse files Browse the repository at this point in the history
Signed-off-by: Maxim Rubchinsky <[email protected]>
  • Loading branch information
maximrub committed Nov 15, 2024
1 parent bfc0563 commit d3bfcfe
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 39 deletions.
21 changes: 16 additions & 5 deletions cluster-autoscaler/cloudprovider/alicloud/alicloud_cloud_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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 != "" {
Expand Down Expand Up @@ -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 ""
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
22 changes: 0 additions & 22 deletions cluster-autoscaler/cloudprovider/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
12 changes: 0 additions & 12 deletions cluster-autoscaler/cloudprovider/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 ""
}

0 comments on commit d3bfcfe

Please sign in to comment.