Skip to content

Commit 68ef58d

Browse files
andyzhangxk8s-infra-cherrypick-robot
authored andcommitted
fix: add cache in sastoken fallback
fix
1 parent 2df0715 commit 68ef58d

File tree

4 files changed

+29
-12
lines changed

4 files changed

+29
-12
lines changed

pkg/blob/blob.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,8 @@ type Driver struct {
212212
accountSearchCache azcache.Resource
213213
// a timed cache storing volume stats <volumeID, volumeStats>
214214
volStatsCache azcache.Resource
215+
// a timed cache storing account which should use sastoken for azcopy based volume cloning
216+
azcopySasTokenCache azcache.Resource
215217
// sas expiry time for azcopy in volume clone
216218
sasTokenExpirationMinutes int
217219
// azcopy for provide exec mock for ut
@@ -256,6 +258,9 @@ func NewDriver(options *DriverOptions) *Driver {
256258
if d.dataPlaneAPIVolCache, err = azcache.NewTimedCache(10*time.Minute, getter, false); err != nil {
257259
klog.Fatalf("%v", err)
258260
}
261+
if d.azcopySasTokenCache, err = azcache.NewTimedCache(15*time.Minute, getter, false); err != nil {
262+
klog.Fatalf("%v", err)
263+
}
259264

260265
if options.VolStatsCacheExpireInMinutes <= 0 {
261266
options.VolStatsCacheExpireInMinutes = 10 // default expire in 10 minutes

pkg/blob/blob_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ func TestNewDriver(t *testing.T) {
9191
fakedriver.Version = driverVersion
9292
fakedriver.accountSearchCache = driver.accountSearchCache
9393
fakedriver.dataPlaneAPIVolCache = driver.dataPlaneAPIVolCache
94+
fakedriver.azcopySasTokenCache = driver.azcopySasTokenCache
9495
fakedriver.volStatsCache = driver.volStatsCache
9596
assert.Equal(t, driver, fakedriver)
9697
}

pkg/blob/controllerserver.go

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -828,18 +828,29 @@ func (d *Driver) authorizeAzcopyWithIdentity() ([]string, error) {
828828
// 3. azcopy returns AuthorizationPermissionMismatch error when using service principal or managed identity
829829
func (d *Driver) getSASToken(ctx context.Context, accountName, accountKey, storageEndpointSuffix string, accountOptions *azure.AccountOptions, secrets map[string]string, secretName, secretNamespace string) (string, error) {
830830
authAzcopyEnv, _ := d.authorizeAzcopyWithIdentity()
831-
useSasTokenFallBack := false
831+
useSasToken := false
832832
if len(authAzcopyEnv) > 0 {
833-
out, testErr := d.azcopy.TestListJobs(accountName, storageEndpointSuffix, authAzcopyEnv)
834-
if testErr != nil {
835-
return "", fmt.Errorf("azcopy list command failed with error(%v): %v", testErr, out)
833+
// search in cache first
834+
cache, err := d.azcopySasTokenCache.Get(accountName, azcache.CacheReadTypeDefault)
835+
if err != nil {
836+
return "", fmt.Errorf("get(%s) from azcopySasTokenCache failed with error: %v", accountName, err)
836837
}
837-
if strings.Contains(out, authorizationPermissionMismatch) {
838-
klog.Warningf("azcopy list failed with AuthorizationPermissionMismatch error, should assign \"Storage Blob Data Contributor\" role to controller identity, fall back to use sas token, original output: %v", out)
839-
useSasTokenFallBack = true
838+
if cache != nil {
839+
klog.V(2).Infof("use sas token for account(%s) since this account is found in azcopySasTokenCache", accountName)
840+
useSasToken = true
841+
} else {
842+
out, testErr := d.azcopy.TestListJobs(accountName, storageEndpointSuffix, authAzcopyEnv)
843+
if testErr != nil {
844+
return "", fmt.Errorf("azcopy list command failed with error(%v): %v", testErr, out)
845+
}
846+
if strings.Contains(out, authorizationPermissionMismatch) {
847+
klog.Warningf("azcopy list failed with AuthorizationPermissionMismatch error, should assign \"Storage Blob Data Contributor\" role to controller identity, fall back to use sas token, original output: %v", out)
848+
d.azcopySasTokenCache.Set(accountName, "")
849+
useSasToken = true
850+
}
840851
}
841852
}
842-
if len(secrets) > 0 || len(authAzcopyEnv) == 0 || useSasTokenFallBack {
853+
if len(secrets) > 0 || len(authAzcopyEnv) == 0 || useSasToken {
843854
var err error
844855
if accountKey == "" {
845856
if _, accountKey, err = d.GetStorageAccesskey(ctx, accountOptions, secrets, secretName, secretNamespace); err != nil {

pkg/blob/controllerserver_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1789,7 +1789,7 @@ func TestCopyVolume(t *testing.T) {
17891789
}
17901790
}
17911791

1792-
func Test_parseDays(t *testing.T) {
1792+
func TestParseDays(t *testing.T) {
17931793
type args struct {
17941794
dayStr string
17951795
}
@@ -1838,7 +1838,7 @@ func Test_parseDays(t *testing.T) {
18381838
}
18391839
}
18401840

1841-
func Test_generateSASToken(t *testing.T) {
1841+
func TestGenerateSASToken(t *testing.T) {
18421842
storageEndpointSuffix := "core.windows.net"
18431843
tests := []struct {
18441844
name string
@@ -1876,7 +1876,7 @@ func Test_generateSASToken(t *testing.T) {
18761876
}
18771877
}
18781878

1879-
func Test_authorizeAzcopyWithIdentity(t *testing.T) {
1879+
func TestAuthorizeAzcopyWithIdentity(t *testing.T) {
18801880
testCases := []struct {
18811881
name string
18821882
testFunc func(t *testing.T)
@@ -1995,7 +1995,7 @@ func Test_authorizeAzcopyWithIdentity(t *testing.T) {
19951995
}
19961996
}
19971997

1998-
func Test_getSASToken(t *testing.T) {
1998+
func TestGetSASToken(t *testing.T) {
19991999
testCases := []struct {
20002000
name string
20012001
testFunc func(t *testing.T)

0 commit comments

Comments
 (0)