Skip to content

Commit 7a939de

Browse files
authored
Merge pull request #1201 from andyzhangx/add-sastoken-get-cache
cleanup: add cache in sastoken fallback
2 parents 4b0fb27 + f3d6399 commit 7a939de

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
@@ -219,6 +219,8 @@ type Driver struct {
219219
accountSearchCache azcache.Resource
220220
// a timed cache storing volume stats <volumeID, volumeStats>
221221
volStatsCache azcache.Resource
222+
// a timed cache storing account which should use sastoken for azcopy based volume cloning
223+
azcopySasTokenCache azcache.Resource
222224
// sas expiry time for azcopy in volume clone
223225
sasTokenExpirationMinutes int
224226
// azcopy for provide exec mock for ut
@@ -258,6 +260,9 @@ func NewDriver(options *DriverOptions, kubeClient kubernetes.Interface, cloud *p
258260
if d.dataPlaneAPIVolCache, err = azcache.NewTimedCache(10*time.Minute, getter, false); err != nil {
259261
klog.Fatalf("%v", err)
260262
}
263+
if d.azcopySasTokenCache, err = azcache.NewTimedCache(15*time.Minute, getter, false); err != nil {
264+
klog.Fatalf("%v", err)
265+
}
261266

262267
if options.VolStatsCacheExpireInMinutes <= 0 {
263268
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
@@ -92,6 +92,7 @@ func TestNewDriver(t *testing.T) {
9292
fakedriver.Version = driverVersion
9393
fakedriver.accountSearchCache = driver.accountSearchCache
9494
fakedriver.dataPlaneAPIVolCache = driver.dataPlaneAPIVolCache
95+
fakedriver.azcopySasTokenCache = driver.azcopySasTokenCache
9596
fakedriver.volStatsCache = driver.volStatsCache
9697
fakedriver.cloud = driver.cloud
9798
assert.Equal(t, driver, fakedriver)

pkg/blob/controllerserver.go

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -829,18 +829,29 @@ func (d *Driver) authorizeAzcopyWithIdentity() ([]string, error) {
829829
// 3. azcopy returns AuthorizationPermissionMismatch error when using service principal or managed identity
830830
func (d *Driver) getSASToken(ctx context.Context, accountName, accountKey, storageEndpointSuffix string, accountOptions *azure.AccountOptions, secrets map[string]string, secretName, secretNamespace string) (string, error) {
831831
authAzcopyEnv, _ := d.authorizeAzcopyWithIdentity()
832-
useSasTokenFallBack := false
832+
useSasToken := false
833833
if len(authAzcopyEnv) > 0 {
834-
out, testErr := d.azcopy.TestListJobs(accountName, storageEndpointSuffix, authAzcopyEnv)
835-
if testErr != nil {
836-
return "", fmt.Errorf("azcopy list command failed with error(%v): %v", testErr, out)
834+
// search in cache first
835+
cache, err := d.azcopySasTokenCache.Get(accountName, azcache.CacheReadTypeDefault)
836+
if err != nil {
837+
return "", fmt.Errorf("get(%s) from azcopySasTokenCache failed with error: %v", accountName, err)
837838
}
838-
if strings.Contains(out, authorizationPermissionMismatch) {
839-
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)
840-
useSasTokenFallBack = true
839+
if cache != nil {
840+
klog.V(2).Infof("use sas token for account(%s) since this account is found in azcopySasTokenCache", accountName)
841+
useSasToken = true
842+
} else {
843+
out, testErr := d.azcopy.TestListJobs(accountName, storageEndpointSuffix, authAzcopyEnv)
844+
if testErr != nil {
845+
return "", fmt.Errorf("azcopy list command failed with error(%v): %v", testErr, out)
846+
}
847+
if strings.Contains(out, authorizationPermissionMismatch) {
848+
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)
849+
d.azcopySasTokenCache.Set(accountName, "")
850+
useSasToken = true
851+
}
841852
}
842853
}
843-
if len(secrets) > 0 || len(authAzcopyEnv) == 0 || useSasTokenFallBack {
854+
if len(secrets) > 0 || len(authAzcopyEnv) == 0 || useSasToken {
844855
var err error
845856
if accountKey == "" {
846857
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
@@ -1748,7 +1748,7 @@ func TestCopyVolume(t *testing.T) {
17481748
}
17491749
}
17501750

1751-
func Test_parseDays(t *testing.T) {
1751+
func TestParseDays(t *testing.T) {
17521752
type args struct {
17531753
dayStr string
17541754
}
@@ -1797,7 +1797,7 @@ func Test_parseDays(t *testing.T) {
17971797
}
17981798
}
17991799

1800-
func Test_generateSASToken(t *testing.T) {
1800+
func TestGenerateSASToken(t *testing.T) {
18011801
storageEndpointSuffix := "core.windows.net"
18021802
tests := []struct {
18031803
name string
@@ -1835,7 +1835,7 @@ func Test_generateSASToken(t *testing.T) {
18351835
}
18361836
}
18371837

1838-
func Test_authorizeAzcopyWithIdentity(t *testing.T) {
1838+
func TestAuthorizeAzcopyWithIdentity(t *testing.T) {
18391839
testCases := []struct {
18401840
name string
18411841
testFunc func(t *testing.T)
@@ -1966,7 +1966,7 @@ func Test_authorizeAzcopyWithIdentity(t *testing.T) {
19661966
}
19671967
}
19681968

1969-
func Test_getSASToken(t *testing.T) {
1969+
func TestGetSASToken(t *testing.T) {
19701970
testCases := []struct {
19711971
name string
19721972
testFunc func(t *testing.T)

0 commit comments

Comments
 (0)