-
Notifications
You must be signed in to change notification settings - Fork 136
/
azurefile.go
423 lines (373 loc) · 14.8 KB
/
azurefile.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
/*
Copyright 2019 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package azurefile
import (
"bytes"
"encoding/binary"
"fmt"
"net/url"
"strings"
"time"
azs "github.com/Azure/azure-sdk-for-go/storage"
"github.com/Azure/azure-storage-file-go/azfile"
"github.com/container-storage-interface/spec/lib/go/csi"
"github.com/pborman/uuid"
"github.com/rubiojr/go-vhd/vhd"
"golang.org/x/net/context"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/klog"
"k8s.io/kubernetes/pkg/volume/util"
"k8s.io/legacy-cloud-providers/azure"
"k8s.io/utils/mount"
csicommon "sigs.k8s.io/azurefile-csi-driver/pkg/csi-common"
"sigs.k8s.io/azurefile-csi-driver/pkg/mounter"
)
const (
DriverName = "file.csi.azure.com"
separator = "#"
volumeIDTemplate = "%s#%s#%s#%s"
secretNameTemplate = "azure-storage-account-%s-secret"
serviceURLTemplate = "https://%s.file.%s"
fileURLTemplate = "https://%s.file.%s/%s/%s"
fileMode = "file_mode"
dirMode = "dir_mode"
vers = "vers"
defaultFileMode = "0777"
defaultDirMode = "0777"
defaultVers = "3.0"
// See https://docs.microsoft.com/en-us/rest/api/storageservices/naming-and-referencing-shares--directories--files--and-metadata#share-names
fileShareNameMinLength = 3
fileShareNameMaxLength = 63
minimumPremiumShareSize = 100 // GB
// Minimum size of Azure Premium Files is 100GiB
// See https://docs.microsoft.com/en-us/azure/storage/files/storage-files-planning#provisioned-shares
defaultAzureFileQuota = 100
// key of snapshot name in metadata
snapshotNameKey = "initiator"
shareNameField = "sharename"
diskNameField = "diskname"
fsTypeField = "fstype"
secretNamespaceField = "secretnamespace"
storeAccountKeyField = "storeaccountkey"
defaultSecretAccountName = "azurestorageaccountname"
defaultSecretAccountKey = "azurestorageaccountkey"
defaultSecretNamespace = "default"
proxyMount = "proxy-mount"
cifs = "cifs"
metaDataNode = "node"
accountNotProvisioned = "StorageAccountIsNotProvisioned"
tooManyRequests = "TooManyRequests"
shareNotFound = "The specified share does not exist"
)
// Driver implements all interfaces of CSI drivers
type Driver struct {
csicommon.CSIDriver
cloud *azure.Cloud
mounter *mount.SafeFormatAndMount
// lock per volume attach (only for vhd disk feature)
volLockMap *lockMap
}
// NewDriver Creates a NewCSIDriver object. Assumes vendor version is equal to driver version &
// does not support optional driver plugin info manifest field. Refer to CSI spec for more details.
func NewDriver(nodeID string) *Driver {
driver := Driver{}
driver.Name = DriverName
driver.Version = driverVersion
driver.NodeID = nodeID
driver.volLockMap = newLockMap()
return &driver
}
// Run driver initialization
func (d *Driver) Run(endpoint, kubeconfig string) {
versionMeta, err := GetVersionYAML()
if err != nil {
klog.Fatalf("%v", err)
}
klog.Infof("\nDRIVER INFORMATION:\n-------------------\n%s\n\nStreaming logs below:", versionMeta)
cloud, err := GetCloudProvider(kubeconfig)
if err != nil || cloud.TenantID == "" || cloud.SubscriptionID == "" {
klog.Fatalf("failed to get Azure Cloud Provider, error: %v", err)
}
d.cloud = cloud
d.mounter, err = mounter.NewSafeMounter()
if err != nil {
klog.Fatalf("Failed to get safe mounter. Error: %v", err)
}
// Initialize default library driver
d.AddControllerServiceCapabilities(
[]csi.ControllerServiceCapability_RPC_Type{
csi.ControllerServiceCapability_RPC_CREATE_DELETE_VOLUME,
csi.ControllerServiceCapability_RPC_PUBLISH_UNPUBLISH_VOLUME,
csi.ControllerServiceCapability_RPC_CREATE_DELETE_SNAPSHOT,
//csi.ControllerServiceCapability_RPC_LIST_SNAPSHOTS,
csi.ControllerServiceCapability_RPC_EXPAND_VOLUME,
})
d.AddVolumeCapabilityAccessModes([]csi.VolumeCapability_AccessMode_Mode{
csi.VolumeCapability_AccessMode_SINGLE_NODE_WRITER,
csi.VolumeCapability_AccessMode_SINGLE_NODE_READER_ONLY,
csi.VolumeCapability_AccessMode_MULTI_NODE_READER_ONLY,
csi.VolumeCapability_AccessMode_MULTI_NODE_SINGLE_WRITER,
csi.VolumeCapability_AccessMode_MULTI_NODE_MULTI_WRITER,
})
d.AddNodeServiceCapabilities([]csi.NodeServiceCapability_RPC_Type{
csi.NodeServiceCapability_RPC_STAGE_UNSTAGE_VOLUME,
})
s := csicommon.NewNonBlockingGRPCServer()
// Driver d act as IdentityServer, ControllerServer and NodeServer
s.Start(endpoint, d, d, d)
s.Wait()
}
func (d *Driver) checkFileShareCapacity(accountName, accountKey, fileShareName string, requestGiB int) error {
fileClient, err := d.getFileSvcClient(accountName, accountKey)
if err != nil {
return err
}
resp, err := fileClient.ListShares(azs.ListSharesParameters{Prefix: fileShareName})
if err != nil {
return fmt.Errorf("error listing file shares: %v", err)
}
for _, share := range resp.Shares {
if share.Name == fileShareName && share.Properties.Quota != requestGiB {
return status.Errorf(codes.AlreadyExists, "the request volume already exists, but its capacity(%v) is different from (%v)", share.Properties.Quota, requestGiB)
}
}
return nil
}
func (d *Driver) checkFileShareExists(accountName, resourceGroup, name string) (bool, error) {
// find the access key with this account
accountKey, err := d.cloud.GetStorageAccesskey(accountName, resourceGroup)
if err != nil {
return false, fmt.Errorf("error getting storage key for storage account %s: %v", accountName, err)
}
fileClient, err := d.getFileSvcClient(accountName, accountKey)
if err != nil {
return false, err
}
return fileClient.GetShareReference(name).Exists()
}
func (d *Driver) getFileSvcClient(accountName, accountKey string) (*azs.FileServiceClient, error) {
fileClient, err := azs.NewClient(accountName, accountKey, d.cloud.Environment.StorageEndpointSuffix, azs.DefaultAPIVersion, true)
if err != nil {
return nil, fmt.Errorf("error creating azure client: %v", err)
}
fc := fileClient.GetFileService()
return &fc, nil
}
// get file share info according to volume id, e.g.
// input: "rg#f5713de20cde511e8ba4900#pvc-file-dynamic-17e43f84-f474-11e8-acd0-000d3a00df41#diskname.vhd"
// output: rg, f5713de20cde511e8ba4900, pvc-file-dynamic-17e43f84-f474-11e8-acd0-000d3a00df41, diskname.vhd
func getFileShareInfo(id string) (string, string, string, string, error) {
segments := strings.Split(id, separator)
if len(segments) < 3 {
return "", "", "", "", fmt.Errorf("error parsing volume id: %q, should at least contain two #", id)
}
var diskName string
if len(segments) > 3 {
diskName = segments[3]
}
return segments[0], segments[1], segments[2], diskName, nil
}
// check whether mountOptions contains file_mode, dir_mode, vers, if not, append default mode
func appendDefaultMountOptions(mountOptions []string) []string {
fileModeFlag := false
dirModeFlag := false
versFlag := false
for _, mountOption := range mountOptions {
if strings.HasPrefix(mountOption, fileMode) {
fileModeFlag = true
}
if strings.HasPrefix(mountOption, dirMode) {
dirModeFlag = true
}
if strings.HasPrefix(mountOption, vers) {
versFlag = true
}
}
allMountOptions := mountOptions
if !fileModeFlag {
allMountOptions = append(allMountOptions, fmt.Sprintf("%s=%s", fileMode, defaultFileMode))
}
if !dirModeFlag {
allMountOptions = append(allMountOptions, fmt.Sprintf("%s=%s", dirMode, defaultDirMode))
}
if !versFlag {
allMountOptions = append(allMountOptions, fmt.Sprintf("%s=%s", vers, defaultVers))
}
/* todo: looks like fsGroup is not included in CSI
if !gidFlag && fsGroup != nil {
allMountOptions = append(allMountOptions, fmt.Sprintf("%s=%d", gid, *fsGroup))
}
*/
return allMountOptions
}
// get storage account from secrets map
func getStorageAccount(secrets map[string]string) (string, string, error) {
if secrets == nil {
return "", "", fmt.Errorf("unexpected: getStorageAccount secrets is nil")
}
var accountName, accountKey string
for k, v := range secrets {
switch strings.ToLower(k) {
case "accountname":
accountName = v
case "azurestorageaccountname": // for compatibility with built-in azurefile plugin
accountName = v
case "accountkey":
accountKey = v
case "azurestorageaccountkey": // for compatibility with built-in azurefile plugin
accountKey = v
}
}
if accountName == "" {
return "", "", fmt.Errorf("could not find accountname or azurestorageaccountname field secrets(%v)", secrets)
}
if accountKey == "" {
return "", "", fmt.Errorf("could not find accountkey or azurestorageaccountkey field in secrets(%v)", secrets)
}
return accountName, accountKey, nil
}
// File share names can contain only lowercase letters, numbers, and hyphens,
// and must begin and end with a letter or a number,
// and must be from 3 through 63 characters long.
// The name cannot contain two consecutive hyphens.
//
// See https://docs.microsoft.com/en-us/rest/api/storageservices/naming-and-referencing-shares--directories--files--and-metadata#share-names
func getValidFileShareName(volumeName string) string {
fileShareName := strings.ToLower(volumeName)
if len(fileShareName) > fileShareNameMaxLength {
fileShareName = fileShareName[0:fileShareNameMaxLength]
}
if !checkShareNameBeginAndEnd(fileShareName) || len(fileShareName) < fileShareNameMinLength {
fileShareName = util.GenerateVolumeName("pvc-file", uuid.NewUUID().String(), fileShareNameMaxLength)
klog.Warningf("the requested volume name (%q) is invalid, so it is regenerated as (%q)", volumeName, fileShareName)
}
fileShareName = strings.Replace(fileShareName, "--", "-", -1)
return fileShareName
}
func checkShareNameBeginAndEnd(fileShareName string) bool {
length := len(fileShareName)
if (('a' <= fileShareName[0] && fileShareName[0] <= 'z') ||
('0' <= fileShareName[0] && fileShareName[0] <= '9')) &&
(('a' <= fileShareName[length-1] && fileShareName[length-1] <= 'z') ||
('0' <= fileShareName[length-1] && fileShareName[length-1] <= '9')) {
return true
}
return false
}
// get snapshot name according to snapshot id, e.g.
// input: "rg#f5713de20cde511e8ba4900#csivolumename#diskname#2019-08-22T07:17:53.0000000Z"
// output: 2019-08-22T07:17:53.0000000Z
func getSnapshot(id string) (string, error) {
segments := strings.Split(id, separator)
if len(segments) < 5 {
return "", fmt.Errorf("error parsing volume id: %q, should at least contain four #", id)
}
return segments[4], nil
}
func getFileURL(accountName, accountKey, storageEndpointSuffix, fileShareName, diskName string) (*azfile.FileURL, error) {
credential, err := azfile.NewSharedKeyCredential(accountName, accountKey)
if err != nil {
return nil, fmt.Errorf("NewSharedKeyCredential(%s) failed with error: %v", accountName, err)
}
u, err := url.Parse(fmt.Sprintf(fileURLTemplate, accountName, storageEndpointSuffix, fileShareName, diskName))
if err != nil {
return nil, fmt.Errorf("parse fileURLTemplate error: %v", err)
}
if u == nil {
return nil, fmt.Errorf("parse fileURLTemplate error: url is nil")
}
po := azfile.PipelineOptions{
// Set RetryOptions to control how HTTP request are retried when retryable failures occur
Retry: azfile.RetryOptions{
Policy: azfile.RetryPolicyExponential, // Use exponential backoff as opposed to linear
MaxTries: 3, // Try at most 3 times to perform the operation (set to 1 to disable retries)
TryTimeout: time.Second * 3, // Maximum time allowed for any single try
RetryDelay: time.Second * 1, // Backoff amount for each retry (exponential or linear)
MaxRetryDelay: time.Second * 3, // Max delay between retries
},
}
fileURL := azfile.NewFileURL(*u, azfile.NewPipeline(credential, po))
return &fileURL, nil
}
func createDisk(ctx context.Context, accountName, accountKey, storageEndpointSuffix, fileShareName, diskName string, diskSizeBytes int64) error {
vhdHeader := vhd.CreateFixedHeader(uint64(diskSizeBytes), &vhd.VHDOptions{})
buf := new(bytes.Buffer)
if err := binary.Write(buf, binary.BigEndian, vhdHeader); nil != err {
return fmt.Errorf("failed to write VHDHeader(%+v): %v", vhdHeader, err)
}
headerBytes := buf.Bytes()
start := diskSizeBytes - int64(len(headerBytes))
end := diskSizeBytes - 1
fileURL, err := getFileURL(accountName, accountKey, storageEndpointSuffix, fileShareName, diskName)
if err != nil {
return err
}
if fileURL == nil {
return fmt.Errorf("getFileURL(%s,%s,%s,%s) return empty fileURL", accountName, storageEndpointSuffix, fileShareName, diskName)
}
if _, err = fileURL.Create(ctx, diskSizeBytes, azfile.FileHTTPHeaders{}, azfile.Metadata{}); err != nil {
return err
}
if _, err = fileURL.UploadRange(ctx, end-start, bytes.NewReader(headerBytes[:vhd.VHD_HEADER_SIZE]), nil); err != nil {
return err
}
return nil
}
func IsCorruptedDir(dir string) bool {
_, pathErr := mount.PathExists(dir)
fmt.Printf("IsCorruptedDir(%s) returned with error: %v", dir, pathErr)
return pathErr != nil && mount.IsCorruptedMnt(pathErr)
}
func (d *Driver) GetAccountInfo(volumeID string, secrets, reqContext map[string]string) (rgName, accountName, accountKey, fileShareName, diskName string, err error) {
if len(secrets) == 0 {
rgName, accountName, fileShareName, diskName, err = getFileShareInfo(volumeID)
if err == nil {
if rgName == "" {
rgName = d.cloud.ResourceGroup
}
if d.cloud.KubeClient != nil {
secretName := fmt.Sprintf(secretNameTemplate, accountName)
secretNamespace := reqContext[secretNamespaceField]
if secretNamespace == "" {
secretNamespace = defaultSecretNamespace
}
secret, err := d.cloud.KubeClient.CoreV1().Secrets(secretNamespace).Get(context.TODO(), secretName, metav1.GetOptions{})
if err != nil {
klog.V(4).Infof("could not get secret(%v): %v", secretName, err)
} else {
accountKey = string(secret.Data[defaultSecretAccountKey][:])
}
}
if accountKey == "" {
accountKey, err = d.cloud.GetStorageAccesskey(accountName, rgName)
}
}
} else {
for k, v := range reqContext {
switch strings.ToLower(k) {
case shareNameField:
fileShareName = v
case diskNameField:
diskName = v
}
}
if fileShareName != "" {
accountName, accountKey, err = getStorageAccount(secrets)
} else {
err = fmt.Errorf("could not find sharename from context(%v)", reqContext)
}
}
return rgName, accountName, accountKey, fileShareName, diskName, err
}