@@ -22,17 +22,15 @@ import (
22
22
"net/http"
23
23
24
24
"github.com/Azure/azure-sdk-for-go/sdk/azcore/to"
25
- azs "github.com/Azure/azure-sdk-for-go/storage"
25
+ "github.com/Azure/azure-sdk-for-go/sdk/storage/azfile/service"
26
+ "github.com/Azure/azure-sdk-for-go/sdk/storage/azfile/share"
26
27
"k8s.io/klog/v2"
27
28
29
+ "sigs.k8s.io/cloud-provider-azure/pkg/azclient/utils"
28
30
"sigs.k8s.io/cloud-provider-azure/pkg/azureclients/fileclient"
29
31
"sigs.k8s.io/cloud-provider-azure/pkg/retry"
30
32
)
31
33
32
- const (
33
- useHTTPS = true
34
- )
35
-
36
34
var (
37
35
// refer https://github.com/Azure/azure-sdk-for-go/blob/master/storage/client.go#L88.
38
36
defaultValidStatusCodes = []int {
@@ -47,78 +45,84 @@ var (
47
45
type azureFileDataplaneClient struct {
48
46
accountName string
49
47
accountKey string
50
- * azs. FileServiceClient
48
+ * service. Client
51
49
}
52
50
53
51
func newAzureFileClient (accountName , accountKey , storageEndpointSuffix string , backoff * retry.Backoff ) (azureFileClient , error ) {
54
52
if storageEndpointSuffix == "" {
55
53
storageEndpointSuffix = defaultStorageEndPointSuffix
56
54
}
57
-
58
- fileClient , err := azs .NewClient (accountName , accountKey , storageEndpointSuffix , azs .DefaultAPIVersion , useHTTPS )
55
+ keyCred , err := service .NewSharedKeyCredential (accountName , accountKey )
59
56
if err != nil {
60
57
return nil , fmt .Errorf ("error creating azure client: %v" , err )
61
58
}
62
-
59
+ storageEndpoint := fmt .Sprintf ("https://%s.file." + storageEndpointSuffix , accountName )
60
+ clientOps := utils .GetDefaultAzCoreClientOption ()
63
61
if backoff != nil {
64
- fileClient .Sender = & azs.DefaultSender {
65
- RetryAttempts : backoff .Steps ,
66
- ValidStatusCodes : defaultValidStatusCodes ,
67
- RetryDuration : backoff .Duration ,
68
- }
62
+ clientOps .Retry .MaxRetries = int32 (backoff .Steps )
63
+ clientOps .Retry .StatusCodes = defaultValidStatusCodes
64
+ clientOps .Retry .RetryDelay = backoff .Duration
65
+ }
66
+ fileClient , err := service .NewClientWithSharedKeyCredential (storageEndpoint , keyCred , & service.ClientOptions {
67
+ ClientOptions : clientOps ,
68
+ })
69
+ if err != nil {
70
+ return nil , fmt .Errorf ("error creating azure client: %v" , err )
69
71
}
70
72
71
73
return & azureFileDataplaneClient {
72
- accountName : accountName ,
73
- accountKey : accountKey ,
74
- FileServiceClient : to . Ptr ( fileClient . GetFileService ()) ,
74
+ accountName : accountName ,
75
+ accountKey : accountKey ,
76
+ Client : fileClient ,
75
77
}, nil
76
78
}
77
79
78
- func (f * azureFileDataplaneClient ) CreateFileShare (_ context.Context , shareOptions * fileclient.ShareOptions ) error {
80
+ func (f * azureFileDataplaneClient ) CreateFileShare (ctx context.Context , shareOptions * fileclient.ShareOptions ) error {
79
81
if shareOptions == nil {
80
82
return fmt .Errorf ("shareOptions of account(%s) is nil" , f .accountName )
81
83
}
82
- share := f .FileServiceClient .GetShareReference (shareOptions .Name )
83
- share .Properties .Quota = shareOptions .RequestGiB
84
- newlyCreated , err := share .CreateIfNotExists (nil )
84
+ shareClient := f .Client .NewShareClient (shareOptions .Name )
85
+ _ , err := shareClient .Create (ctx , & share.CreateOptions {
86
+ Quota : to .Ptr (int32 (shareOptions .RequestGiB )),
87
+ })
88
+
85
89
if err != nil {
86
90
return fmt .Errorf ("failed to create file share, err: %v" , err )
87
91
}
88
- if ! newlyCreated {
89
- klog .V (2 ).Infof ("file share(%s) under account(%s) already exists" , shareOptions .Name , f .accountName )
90
- }
91
92
return nil
92
93
}
93
94
94
95
// delete a file share
95
- func (f * azureFileDataplaneClient ) DeleteFileShare (_ context.Context , shareName string ) error {
96
- return f .FileServiceClient .GetShareReference (shareName ).Delete (nil )
96
+ func (f * azureFileDataplaneClient ) DeleteFileShare (ctx context.Context , shareName string ) error {
97
+ _ , err := f .Client .NewShareClient (shareName ).Delete (ctx , nil )
98
+ return err
97
99
}
98
100
99
- func (f * azureFileDataplaneClient ) ResizeFileShare (_ context.Context , shareName string , sizeGiB int ) error {
100
- share := f .FileServiceClient .GetShareReference (shareName )
101
- if share .Properties .Quota >= sizeGiB {
101
+ func (f * azureFileDataplaneClient ) ResizeFileShare (ctx context.Context , shareName string , sizeGiB int ) error {
102
+ shareClient := f .Client .NewShareClient (shareName )
103
+ shareProps , err := shareClient .GetProperties (ctx , nil )
104
+ if err != nil {
105
+ return fmt .Errorf ("failed to set quota on file share %s, err: %v" , shareName , err )
106
+ }
107
+ if * shareProps .Quota >= int32 (sizeGiB ) {
102
108
klog .Warningf ("file share size(%dGi) is already greater or equal than requested size(%dGi), accountName: %s, shareName: %s" ,
103
- share . Properties .Quota , sizeGiB , f .accountName , shareName )
109
+ * shareProps .Quota , sizeGiB , f .accountName , shareName )
104
110
return nil
105
111
}
106
- share .Properties .Quota = sizeGiB
107
- if err := share .SetProperties (nil ); err != nil {
112
+ if _ , err := shareClient .SetProperties (ctx , & share.SetPropertiesOptions {
113
+ Quota : to .Ptr (int32 (sizeGiB )),
114
+ }); err != nil {
108
115
return fmt .Errorf ("failed to set quota on file share %s, err: %v" , shareName , err )
109
116
}
110
117
klog .V (4 ).Infof ("resize file share completed, accountName: %s, shareName: %s, sizeGiB: %d" , f .accountName , shareName , sizeGiB )
111
118
return nil
112
119
}
113
120
114
- func (f * azureFileDataplaneClient ) GetFileShareQuota (_ context.Context , name string ) (int , error ) {
115
- share := f .FileServiceClient . GetShareReference (name )
116
- exists , err := share . Exists ( )
121
+ func (f * azureFileDataplaneClient ) GetFileShareQuota (ctx context.Context , name string ) (int , error ) {
122
+ shareClient := f .Client . NewShareClient (name )
123
+ shareProps , err := shareClient . GetProperties ( ctx , nil )
117
124
if err != nil {
118
125
return - 1 , err
119
126
}
120
- if ! exists {
121
- return - 1 , nil
122
- }
123
- return share .Properties .Quota , nil
127
+ return int (* shareProps .Quota ), nil
124
128
}
0 commit comments