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