Skip to content

Commit 7070f90

Browse files
authored
Merge pull request #8446 from maximrub/cherry-pick-8171-to-1.32
Cherry pick 8171 to 1.32
2 parents adadd3d + f0d3b51 commit 7070f90

File tree

5 files changed

+74
-46
lines changed

5 files changed

+74
-46
lines changed

cluster-autoscaler/cloudprovider/alicloud/alibaba-cloud-sdk-go/sdk/auth/signers/signer_oidc.go

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,23 +19,26 @@ package signers
1919
import (
2020
"encoding/json"
2121
"fmt"
22+
"net/http"
23+
"os"
24+
"runtime"
25+
"strconv"
26+
"strings"
27+
"time"
28+
2229
"github.com/jmespath/go-jmespath"
2330
"k8s.io/autoscaler/cluster-autoscaler/cloudprovider/alicloud/alibaba-cloud-sdk-go/sdk/auth/credentials"
2431
"k8s.io/autoscaler/cluster-autoscaler/cloudprovider/alicloud/alibaba-cloud-sdk-go/sdk/errors"
2532
"k8s.io/autoscaler/cluster-autoscaler/cloudprovider/alicloud/alibaba-cloud-sdk-go/sdk/requests"
2633
"k8s.io/autoscaler/cluster-autoscaler/cloudprovider/alicloud/alibaba-cloud-sdk-go/sdk/responses"
2734
"k8s.io/autoscaler/cluster-autoscaler/cloudprovider/alicloud/alibaba-cloud-sdk-go/sdk/utils"
2835
"k8s.io/klog/v2"
29-
"net/http"
30-
"os"
31-
"runtime"
32-
"strconv"
33-
"strings"
34-
"time"
3536
)
3637

3738
const (
3839
defaultOIDCDurationSeconds = 3600
40+
oidcTokenFilePath = "ALIBABA_CLOUD_OIDC_TOKEN_FILE"
41+
oldOidcTokenFilePath = "ALICLOUD_OIDC_TOKEN_FILE_PATH"
3942
)
4043

4144
// OIDCSigner is kind of signer
@@ -149,7 +152,7 @@ func (signer *OIDCSigner) getOIDCToken(OIDCTokenFilePath string) string {
149152
tokenPath := OIDCTokenFilePath
150153
_, err := os.Stat(tokenPath)
151154
if os.IsNotExist(err) {
152-
tokenPath = os.Getenv("ALIBABA_CLOUD_OIDC_TOKEN_FILE")
155+
tokenPath = utils.FirstNotEmpty(os.Getenv(oidcTokenFilePath), os.Getenv(oldOidcTokenFilePath))
153156
if tokenPath == "" {
154157
klog.Error("oidc token file path is missing")
155158
return ""

cluster-autoscaler/cloudprovider/alicloud/alibaba-cloud-sdk-go/sdk/utils/utils.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,12 @@ import (
2222
"encoding/hex"
2323
"encoding/json"
2424
"fmt"
25-
"github.com/google/uuid"
2625
"net/url"
2726
"reflect"
2827
"strconv"
2928
"time"
29+
30+
"github.com/google/uuid"
3031
)
3132

3233
/* if you use go 1.10 or higher, you can hack this util by these to avoid "TimeZone.zip not found" on Windows */
@@ -127,3 +128,15 @@ func InitStructWithDefaultTag(bean interface{}) {
127128
}
128129
}
129130
}
131+
132+
// FirstNotEmpty returns the first non-empty string from the input list.
133+
// If all strings are empty or no arguments are provided, it returns an empty string.
134+
func FirstNotEmpty(strs ...string) string {
135+
for _, str := range strs {
136+
if str != "" {
137+
return str
138+
}
139+
}
140+
141+
return ""
142+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
Copyright 2018 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package utils
18+
19+
import (
20+
"testing"
21+
22+
"github.com/stretchr/testify/assert"
23+
)
24+
25+
func TestFirstNotEmpty(t *testing.T) {
26+
// Test case where the first non-empty string is at the beginning
27+
result := FirstNotEmpty("hello", "world", "test")
28+
assert.Equal(t, "hello", result)
29+
30+
// Test case where the first non-empty string is in the middle
31+
result = FirstNotEmpty("", "foo", "bar")
32+
assert.Equal(t, "foo", result)
33+
34+
// Test case where the first non-empty string is at the end
35+
result = FirstNotEmpty("", "", "baz")
36+
assert.Equal(t, "baz", result)
37+
38+
// Test case where all strings are empty
39+
result = FirstNotEmpty("", "", "")
40+
assert.Equal(t, "", result)
41+
42+
// Test case with no arguments
43+
result = FirstNotEmpty()
44+
assert.Equal(t, "", result)
45+
}

cluster-autoscaler/cloudprovider/alicloud/alicloud_cloud_config.go

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package alicloud
1919
import (
2020
"os"
2121

22+
"k8s.io/autoscaler/cluster-autoscaler/cloudprovider/alicloud/alibaba-cloud-sdk-go/sdk/utils"
2223
"k8s.io/autoscaler/cluster-autoscaler/cloudprovider/alicloud/metadata"
2324
"k8s.io/klog/v2"
2425
)
@@ -63,19 +64,19 @@ func (cc *cloudConfig) isValid() bool {
6364
}
6465

6566
if cc.OIDCProviderARN == "" {
66-
cc.OIDCProviderARN = firstNotEmpty(os.Getenv(oidcProviderARN), os.Getenv(oldOidcProviderARN))
67+
cc.OIDCProviderARN = utils.FirstNotEmpty(os.Getenv(oidcProviderARN), os.Getenv(oldOidcProviderARN))
6768
}
6869

6970
if cc.OIDCTokenFilePath == "" {
70-
cc.OIDCTokenFilePath = firstNotEmpty(os.Getenv(oidcTokenFilePath), os.Getenv(oldOidcTokenFilePath))
71+
cc.OIDCTokenFilePath = utils.FirstNotEmpty(os.Getenv(oidcTokenFilePath), os.Getenv(oldOidcTokenFilePath))
7172
}
7273

7374
if cc.RoleARN == "" {
74-
cc.RoleARN = firstNotEmpty(os.Getenv(roleARN), os.Getenv(oldRoleARN))
75+
cc.RoleARN = utils.FirstNotEmpty(os.Getenv(roleARN), os.Getenv(oldRoleARN))
7576
}
7677

7778
if cc.RoleSessionName == "" {
78-
cc.RoleSessionName = firstNotEmpty(os.Getenv(roleSessionName), os.Getenv(oldRoleSessionName))
79+
cc.RoleSessionName = utils.FirstNotEmpty(os.Getenv(roleSessionName), os.Getenv(oldRoleSessionName))
7980
}
8081

8182
if cc.RegionId != "" && cc.AccessKeyID != "" && cc.AccessKeySecret != "" {
@@ -133,15 +134,3 @@ func (cc *cloudConfig) getRegion() string {
133134
}
134135
return r
135136
}
136-
137-
// firstNotEmpty returns the first non-empty string from the input list.
138-
// If all strings are empty or no arguments are provided, it returns an empty string.
139-
func firstNotEmpty(strs ...string) string {
140-
for _, str := range strs {
141-
if str != "" {
142-
return str
143-
}
144-
}
145-
146-
return ""
147-
}

cluster-autoscaler/cloudprovider/alicloud/alicloud_cloud_config_test.go

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -55,25 +55,3 @@ func TestOldRRSACloudConfigIsValid(t *testing.T) {
5555
assert.True(t, cfg.isValid())
5656
assert.True(t, cfg.RRSAEnabled)
5757
}
58-
59-
func TestFirstNotEmpty(t *testing.T) {
60-
// Test case where the first non-empty string is at the beginning
61-
result := firstNotEmpty("hello", "world", "test")
62-
assert.Equal(t, "hello", result)
63-
64-
// Test case where the first non-empty string is in the middle
65-
result = firstNotEmpty("", "foo", "bar")
66-
assert.Equal(t, "foo", result)
67-
68-
// Test case where the first non-empty string is at the end
69-
result = firstNotEmpty("", "", "baz")
70-
assert.Equal(t, "baz", result)
71-
72-
// Test case where all strings are empty
73-
result = firstNotEmpty("", "", "")
74-
assert.Equal(t, "", result)
75-
76-
// Test case with no arguments
77-
result = firstNotEmpty()
78-
assert.Equal(t, "", result)
79-
}

0 commit comments

Comments
 (0)