-
Notifications
You must be signed in to change notification settings - Fork 135
/
azurefile.go
1185 lines (1068 loc) · 44.3 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
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
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"
"context"
"encoding/binary"
"errors"
"fmt"
"net/url"
"strconv"
"strings"
"sync"
"time"
"github.com/Azure/azure-sdk-for-go/services/storage/mgmt/2021-09-01/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"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
v1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/klog/v2"
"k8s.io/kubernetes/pkg/volume/util"
mount "k8s.io/mount-utils"
csicommon "sigs.k8s.io/azurefile-csi-driver/pkg/csi-common"
"sigs.k8s.io/azurefile-csi-driver/pkg/mounter"
fileutil "sigs.k8s.io/azurefile-csi-driver/pkg/util"
"sigs.k8s.io/cloud-provider-azure/pkg/azureclients/fileclient"
azcache "sigs.k8s.io/cloud-provider-azure/pkg/cache"
azure "sigs.k8s.io/cloud-provider-azure/pkg/provider"
"sigs.k8s.io/cloud-provider-azure/pkg/retry"
)
const (
DefaultDriverName = "file.csi.azure.com"
separator = "#"
volumeIDTemplate = "%s#%s#%s#%s#%s#%s"
secretNameTemplate = "azure-storage-account-%s-secret"
serviceURLTemplate = "https://%s.file.%s"
fileURLTemplate = "https://%s.file.%s/%s/%s"
subnetTemplate = "/subscriptions/%s/resourceGroups/%s/providers/Microsoft.Network/virtualNetworks/%s/subnets/%s"
fileMode = "file_mode"
dirMode = "dir_mode"
actimeo = "actimeo"
noResvPort = "noresvport"
mfsymlinks = "mfsymlinks"
defaultFileMode = "0777"
defaultDirMode = "0777"
defaultActimeo = "30"
// 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
minimumAccountQuota = 100 // GB
// key of snapshot name in metadata
snapshotNameKey = "initiator"
shareNameField = "sharename"
accessTierField = "accesstier"
shareAccessTierField = "shareaccesstier"
accountAccessTierField = "accountaccesstier"
rootSquashTypeField = "rootsquashtype"
diskNameField = "diskname"
folderNameField = "foldername"
serverNameField = "server"
fsTypeField = "fstype"
protocolField = "protocol"
matchTagsField = "matchtags"
tagsField = "tags"
storageAccountField = "storageaccount"
storageAccountTypeField = "storageaccounttype"
skuNameField = "skuname"
enableLargeFileSharesField = "enablelargefileshares"
subscriptionIDField = "subscriptionid"
resourceGroupField = "resourcegroup"
locationField = "location"
secretNamespaceField = "secretnamespace"
secretNameField = "secretname"
createAccountField = "createaccount"
useDataPlaneAPIField = "usedataplaneapi"
storeAccountKeyField = "storeaccountkey"
getLatestAccountKeyField = "getlatestaccountkey"
useSecretCacheField = "usesecretcache"
getAccountKeyFromSecretField = "getaccountkeyfromsecret"
disableDeleteRetentionPolicyField = "disabledeleteretentionpolicy"
allowBlobPublicAccessField = "allowblobpublicaccess"
storageEndpointSuffixField = "storageendpointsuffix"
fsGroupChangePolicyField = "fsgroupchangepolicy"
ephemeralField = "csi.storage.k8s.io/ephemeral"
podNamespaceField = "csi.storage.k8s.io/pod.namespace"
serviceAccountTokenField = "csi.storage.k8s.io/serviceAccount.tokens"
clientIDField = "clientID"
tenantIDField = "tenantID"
mountOptionsField = "mountoptions"
mountPermissionsField = "mountpermissions"
falseValue = "false"
trueValue = "true"
defaultSecretAccountName = "azurestorageaccountname"
defaultSecretAccountKey = "azurestorageaccountkey"
proxyMount = "proxy-mount"
cifs = "cifs"
smb = "smb"
nfs = "nfs"
ext4 = "ext4"
ext3 = "ext3"
ext2 = "ext2"
xfs = "xfs"
vhdSuffix = ".vhd"
metaDataNode = "node"
networkEndpointTypeField = "networkendpointtype"
vnetResourceGroupField = "vnetresourcegroup"
vnetNameField = "vnetname"
subnetNameField = "subnetname"
shareNamePrefixField = "sharenameprefix"
requireInfraEncryptionField = "requireinfraencryption"
enableMultichannelField = "enablemultichannel"
standard = "standard"
premium = "premium"
selectRandomMatchingAccountField = "selectrandommatchingaccount"
accountQuotaField = "accountquota"
accountNotProvisioned = "StorageAccountIsNotProvisioned"
// this is a workaround fix for 429 throttling issue, will update cloud provider for better fix later
tooManyRequests = "TooManyRequests"
shareBeingDeleted = "The specified share is being deleted"
clientThrottled = "client throttled"
// accountLimitExceed returned by different API
accountLimitExceedManagementAPI = "TotalSharesProvisionedCapacityExceedsAccountLimit"
accountLimitExceedDataPlaneAPI = "specified share does not exist"
fileShareNotFound = "ErrorCode=ShareNotFound"
statusCodeNotFound = "StatusCode=404"
httpCodeNotFound = "HTTPStatusCode: 404"
// define different sleep time when hit throttling
accountOpThrottlingSleepSec = 16
fileOpThrottlingSleepSec = 180
maxThrottlingSleepSec = 1200
defaultAccountNamePrefix = "f"
defaultNamespace = "default"
pvcNameKey = "csi.storage.k8s.io/pvc/name"
pvcNamespaceKey = "csi.storage.k8s.io/pvc/namespace"
pvNameKey = "csi.storage.k8s.io/pv/name"
pvcNameMetadata = "${pvc.metadata.name}"
pvcNamespaceMetadata = "${pvc.metadata.namespace}"
pvNameMetadata = "${pv.metadata.name}"
defaultStorageEndPointSuffix = "core.windows.net"
VolumeID = "volumeid"
SourceResourceID = "source_resource_id"
SnapshotName = "snapshot_name"
SnapshotID = "snapshot_id"
FSGroupChangeNone = "None"
)
var (
supportedFsTypeList = []string{cifs, smb, nfs, ext4, ext3, ext2, xfs}
supportedProtocolList = []string{smb, nfs}
supportedDiskFsTypeList = []string{ext4, ext3, ext2, xfs}
supportedFSGroupChangePolicyList = []string{FSGroupChangeNone, string(v1.FSGroupChangeAlways), string(v1.FSGroupChangeOnRootMismatch)}
retriableErrors = []string{accountNotProvisioned, tooManyRequests, shareBeingDeleted, clientThrottled}
// azcopyCloneVolumeOptions used in volume cloning and set --check-length to false because volume data may be in changing state, copy volume is not same as current source volume
azcopyCloneVolumeOptions = []string{"--recursive", "--check-length=false"}
// azcopySnapshotRestoreOptions used in smb snapshot restore and set --check-length to true because snapshot data is changeless
azcopySnapshotRestoreOptions = []string{"--recursive", "--check-length=true"}
)
// Driver implements all interfaces of CSI drivers
type Driver struct {
csicommon.CSIDriver
cloud *azure.Cloud
cloudConfigSecretName string
cloudConfigSecretNamespace string
customUserAgent string
userAgentSuffix string
fsGroupChangePolicy string
allowEmptyCloudConfig bool
allowInlineVolumeKeyAccessWithIdentity bool
enableVHDDiskFeature bool
enableGetVolumeStats bool
enableVolumeMountGroup bool
appendMountErrorHelpLink bool
mountPermissions uint64
kubeAPIQPS float64
kubeAPIBurst int
enableWindowsHostProcess bool
removeSMBMountOnWindows bool
appendClosetimeoOption bool
appendNoShareSockOption bool
appendNoResvPortOption bool
appendActimeoOption bool
printVolumeStatsCallLogs bool
mounter *mount.SafeFormatAndMount
server *grpc.Server
// lock per volume attach (only for vhd disk feature)
volLockMap *lockMap
// only for nfs feature
subnetLockMap *lockMap
// a map storing all volumes with ongoing operations so that additional operations
// for that same volume (as defined by VolumeID) return an Aborted error
volumeLocks *volumeLocks
// a map storing all volumes created by this driver <volumeName, accountName>
volMap sync.Map
// a timed cache storing all account name and keys retrieved by this driver <accountName, accountkey>
accountCacheMap azcache.Resource
// a map storing all secret names created by this driver <secretCacheKey, "">
secretCacheMap azcache.Resource
// a map storing all volumes using data plane API <volumeID, "">
dataPlaneAPIVolMap sync.Map
// a timed cache storing all storage accounts that are using data plane API temporarily
dataPlaneAPIAccountCache azcache.Resource
// a timed cache storing account search history (solve account list throttling issue)
accountSearchCache azcache.Resource
// a timed cache storing whether skipMatchingTag is added or removed recently
skipMatchingTagCache azcache.Resource
// a timed cache when resize file share failed due to account limit exceeded
resizeFileShareFailureCache azcache.Resource
// a timed cache storing volume stats <volumeID, volumeStats>
volStatsCache azcache.Resource
// a timed cache storing account which should use sastoken for azcopy based volume cloning
azcopySasTokenCache azcache.Resource
// sas expiry time for azcopy in volume clone and snapshot restore
sasTokenExpirationMinutes int
// azcopy timeout for volume clone and snapshot restore
waitForAzCopyTimeoutMinutes int
// azcopy for provide exec mock for ut
azcopy *fileutil.Azcopy
kubeconfig string
endpoint string
}
// 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(options *DriverOptions) *Driver {
driver := Driver{}
driver.Name = options.DriverName
driver.Version = driverVersion
driver.NodeID = options.NodeID
driver.cloudConfigSecretName = options.CloudConfigSecretName
driver.cloudConfigSecretNamespace = options.CloudConfigSecretNamespace
driver.customUserAgent = options.CustomUserAgent
driver.userAgentSuffix = options.UserAgentSuffix
driver.allowEmptyCloudConfig = options.AllowEmptyCloudConfig
driver.allowInlineVolumeKeyAccessWithIdentity = options.AllowInlineVolumeKeyAccessWithIdentity
driver.enableVHDDiskFeature = options.EnableVHDDiskFeature
driver.enableVolumeMountGroup = options.EnableVolumeMountGroup
driver.enableGetVolumeStats = options.EnableGetVolumeStats
driver.appendMountErrorHelpLink = options.AppendMountErrorHelpLink
driver.mountPermissions = options.MountPermissions
driver.fsGroupChangePolicy = options.FSGroupChangePolicy
driver.kubeAPIQPS = options.KubeAPIQPS
driver.kubeAPIBurst = options.KubeAPIBurst
driver.enableWindowsHostProcess = options.EnableWindowsHostProcess
driver.removeSMBMountOnWindows = options.RemoveSMBMountOnWindows
driver.appendClosetimeoOption = options.AppendClosetimeoOption
driver.appendNoShareSockOption = options.AppendNoShareSockOption
driver.appendNoResvPortOption = options.AppendNoResvPortOption
driver.appendActimeoOption = options.AppendActimeoOption
driver.printVolumeStatsCallLogs = options.PrintVolumeStatsCallLogs
driver.sasTokenExpirationMinutes = options.SasTokenExpirationMinutes
driver.waitForAzCopyTimeoutMinutes = options.WaitForAzCopyTimeoutMinutes
driver.volLockMap = newLockMap()
driver.subnetLockMap = newLockMap()
driver.volumeLocks = newVolumeLocks()
driver.azcopy = &fileutil.Azcopy{}
driver.kubeconfig = options.KubeConfig
driver.endpoint = options.Endpoint
var err error
getter := func(key string) (interface{}, error) { return nil, nil }
if driver.secretCacheMap, err = azcache.NewTimedCache(time.Minute, getter, false); err != nil {
klog.Fatalf("%v", err)
}
if driver.accountSearchCache, err = azcache.NewTimedCache(time.Minute, getter, false); err != nil {
klog.Fatalf("%v", err)
}
if options.SkipMatchingTagCacheExpireInMinutes <= 0 {
options.SkipMatchingTagCacheExpireInMinutes = 30 // default expire in 30 minutes
}
if driver.skipMatchingTagCache, err = azcache.NewTimedCache(time.Duration(options.SkipMatchingTagCacheExpireInMinutes)*time.Minute, getter, false); err != nil {
klog.Fatalf("%v", err)
}
if driver.accountCacheMap, err = azcache.NewTimedCache(3*time.Minute, getter, false); err != nil {
klog.Fatalf("%v", err)
}
if driver.dataPlaneAPIAccountCache, err = azcache.NewTimedCache(10*time.Minute, getter, false); err != nil {
klog.Fatalf("%v", err)
}
if driver.azcopySasTokenCache, err = azcache.NewTimedCache(15*time.Minute, getter, false); err != nil {
klog.Fatalf("%v", err)
}
if driver.resizeFileShareFailureCache, err = azcache.NewTimedCache(3*time.Minute, getter, false); err != nil {
klog.Fatalf("%v", err)
}
if options.VolStatsCacheExpireInMinutes <= 0 {
options.VolStatsCacheExpireInMinutes = 10 // default expire in 10 minutes
}
if driver.volStatsCache, err = azcache.NewTimedCache(time.Duration(options.VolStatsCacheExpireInMinutes)*time.Minute, getter, false); err != nil {
klog.Fatalf("%v", err)
}
return &driver
}
// Run driver initialization
func (d *Driver) Run(ctx context.Context) error {
versionMeta, err := GetVersionYAML(d.Name)
if err != nil {
klog.Fatalf("%v", err)
}
klog.Infof("\nDRIVER INFORMATION:\n-------------------\n%s\n\nStreaming logs below:", versionMeta)
if d.NodeID == "" {
// nodeid is not needed in controller component
klog.Warning("nodeid is empty")
}
userAgent := GetUserAgent(d.Name, d.customUserAgent, d.userAgentSuffix)
klog.V(2).Infof("driver userAgent: %s", userAgent)
d.cloud, err = getCloudProvider(context.Background(), d.kubeconfig, d.NodeID, d.cloudConfigSecretName, d.cloudConfigSecretNamespace, userAgent, d.allowEmptyCloudConfig, d.enableWindowsHostProcess, d.kubeAPIQPS, d.kubeAPIBurst)
if err != nil {
klog.Fatalf("failed to get Azure Cloud Provider, error: %v", err)
}
klog.V(2).Infof("cloud: %s, location: %s, rg: %s, VnetName: %s, VnetResourceGroup: %s, SubnetName: %s", d.cloud.Cloud, d.cloud.Location, d.cloud.ResourceGroup, d.cloud.VnetName, d.cloud.VnetResourceGroup, d.cloud.SubnetName)
d.mounter, err = mounter.NewSafeMounter(d.enableWindowsHostProcess)
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_CREATE_DELETE_SNAPSHOT,
csi.ControllerServiceCapability_RPC_EXPAND_VOLUME,
csi.ControllerServiceCapability_RPC_SINGLE_NODE_MULTI_WRITER,
csi.ControllerServiceCapability_RPC_CLONE_VOLUME,
})
d.AddVolumeCapabilityAccessModes([]csi.VolumeCapability_AccessMode_Mode{
csi.VolumeCapability_AccessMode_SINGLE_NODE_WRITER,
csi.VolumeCapability_AccessMode_SINGLE_NODE_READER_ONLY,
csi.VolumeCapability_AccessMode_SINGLE_NODE_SINGLE_WRITER,
csi.VolumeCapability_AccessMode_SINGLE_NODE_MULTI_WRITER,
csi.VolumeCapability_AccessMode_MULTI_NODE_READER_ONLY,
csi.VolumeCapability_AccessMode_MULTI_NODE_SINGLE_WRITER,
csi.VolumeCapability_AccessMode_MULTI_NODE_MULTI_WRITER,
})
nodeCap := []csi.NodeServiceCapability_RPC_Type{
csi.NodeServiceCapability_RPC_STAGE_UNSTAGE_VOLUME,
csi.NodeServiceCapability_RPC_SINGLE_NODE_MULTI_WRITER,
}
if d.enableVolumeMountGroup {
nodeCap = append(nodeCap, csi.NodeServiceCapability_RPC_VOLUME_MOUNT_GROUP)
}
if d.enableGetVolumeStats {
nodeCap = append(nodeCap, csi.NodeServiceCapability_RPC_GET_VOLUME_STATS)
}
d.AddNodeServiceCapabilities(nodeCap)
//setup grpc server
opts := []grpc.ServerOption{
grpc.UnaryInterceptor(csicommon.LogGRPC),
}
server := grpc.NewServer(opts...)
csi.RegisterIdentityServer(server, d)
csi.RegisterControllerServer(server, d)
csi.RegisterNodeServer(server, d)
d.server = server
listener, err := csicommon.ListenEndpoint(d.endpoint)
if err != nil {
klog.Fatalf("failed to listen endpoint: %v", err)
}
go func() {
<-ctx.Done()
d.server.GracefulStop()
}()
if err = d.server.Serve(listener); errors.Is(err, grpc.ErrServerStopped) {
klog.Infof("gRPC server stopped serving")
return nil
}
return err
}
// getFileShareQuota return (-1, nil) means file share does not exist
func (d *Driver) getFileShareQuota(ctx context.Context, subsID, resourceGroupName, accountName, fileShareName string, secrets map[string]string) (int, error) {
if len(secrets) > 0 {
accountName, accountKey, err := getStorageAccount(secrets)
if err != nil {
return -1, err
}
fileClient, err := newAzureFileClient(accountName, accountKey, d.getStorageEndPointSuffix(), &retry.Backoff{Steps: 1})
if err != nil {
return -1, err
}
return fileClient.GetFileShareQuota(ctx, fileShareName)
}
fileShare, err := d.cloud.GetFileShare(ctx, subsID, resourceGroupName, accountName, fileShareName)
if err != nil {
if strings.Contains(err.Error(), "ShareNotFound") {
return -1, nil
}
return -1, err
}
if fileShare.FileShareProperties == nil || fileShare.FileShareProperties.ShareQuota == nil {
return -1, fmt.Errorf("FileShareProperties or FileShareProperties.ShareQuota is nil")
}
return int(*fileShare.FileShareProperties.ShareQuota), nil
}
// get file share info according to volume id, e.g.
// input: "rg#f5713de20cde511e8ba4900#fileShareName#diskname.vhd#uuid#namespace#subsID"
// output: rg, f5713de20cde511e8ba4900, fileShareName, diskname.vhd, namespace, subsID
func GetFileShareInfo(id string) (string, 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)
}
rg := segments[0]
var diskName, namespace, subsID string
if len(segments) > 3 {
diskName = segments[3]
}
if rg == "" {
// in csi migration, rg could be empty, then the 5th element is namespace
// https://github.com/kubernetes/kubernetes/blob/v1.23.5/staging/src/k8s.io/csi-translation-lib/plugins/azure_file.go#L137
if len(segments) > 4 {
namespace = segments[4]
}
} else {
if len(segments) > 5 {
namespace = segments[5]
}
if len(segments) > 6 {
subsID = segments[6]
}
}
return rg, segments[1], segments[2], diskName, namespace, subsID, nil
}
// check whether mountOptions contains file_mode, dir_mode, vers, if not, append default mode
func appendDefaultCifsMountOptions(mountOptions []string, appendNoShareSockOption, appendClosetimeoOption bool) []string {
var defaultMountOptions = map[string]string{
fileMode: defaultFileMode,
dirMode: defaultDirMode,
actimeo: defaultActimeo,
mfsymlinks: "",
}
if appendClosetimeoOption {
defaultMountOptions["sloppy,closetimeo=0"] = ""
}
if appendNoShareSockOption {
defaultMountOptions["nosharesock"] = ""
}
// stores the mount options already included in mountOptions
included := make(map[string]bool)
for _, mountOption := range mountOptions {
for k := range defaultMountOptions {
if strings.HasPrefix(mountOption, k) {
included[k] = true
}
}
// actimeo would set both acregmax and acdirmax, so we only need to check one of them
if strings.Contains(mountOption, "acregmax") || strings.Contains(mountOption, "acdirmax") {
included[actimeo] = true
}
}
allMountOptions := mountOptions
for k, v := range defaultMountOptions {
if _, isIncluded := included[k]; !isIncluded {
if v != "" {
allMountOptions = append(allMountOptions, fmt.Sprintf("%s=%s", k, v))
} else {
allMountOptions = append(allMountOptions, k)
}
}
}
return allMountOptions
}
// check whether mountOptions contains actimeo, if not, append default mode
func appendDefaultNfsMountOptions(mountOptions []string, appendNoResvPortOption, appendActimeoOption bool) []string {
var defaultMountOptions = map[string]string{}
if appendNoResvPortOption {
defaultMountOptions[noResvPort] = ""
}
if appendActimeoOption {
defaultMountOptions[actimeo] = defaultActimeo
}
if len(defaultMountOptions) == 0 {
return mountOptions
}
// stores the mount options already included in mountOptions
included := make(map[string]bool)
for _, mountOption := range mountOptions {
for k := range defaultMountOptions {
if strings.HasPrefix(mountOption, k) {
included[k] = true
}
}
}
allMountOptions := mountOptions
for k, v := range defaultMountOptions {
if _, isIncluded := included[k]; !isIncluded {
if v != "" {
allMountOptions = append(allMountOptions, fmt.Sprintf("%s=%s", k, v))
} else {
allMountOptions = append(allMountOptions, k)
}
}
}
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 {
v = strings.TrimSpace(v)
switch strings.ToLower(k) {
case "accountname":
accountName = v
case defaultSecretAccountName: // for compatibility with built-in azurefile plugin
accountName = v
case "accountkey":
accountKey = v
case defaultSecretAccountKey: // for compatibility with built-in azurefile plugin
accountKey = v
}
}
if accountName == "" {
return "", "", fmt.Errorf("could not find accountname or azurestorageaccountname field in secrets")
}
if accountKey == "" {
return "", "", fmt.Errorf("could not find accountkey or azurestorageaccountkey field in secrets")
}
accountName = strings.TrimSpace(accountName)
klog.V(4).Infof("got storage account(%s) from secret", accountName)
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 strings.ToLower(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 (last element)
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[len(segments)-1], 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)
return pathErr != nil && mount.IsCorruptedMnt(pathErr)
}
// GetAccountInfo get account info
// return <rgName, accountName, accountKey, fileShareName, diskName, subsID, err>
func (d *Driver) GetAccountInfo(ctx context.Context, volumeID string, secrets, reqContext map[string]string) (string, string, string, string, string, string, error) {
rgName, accountName, fileShareName, diskName, secretNamespace, subsID, err := GetFileShareInfo(volumeID)
if err != nil {
// ignore volumeID parsing error
klog.Warningf("parsing volumeID(%s) return with error: %v", volumeID, err)
err = nil
}
var protocol, accountKey, secretName, pvcNamespace string
// getAccountKeyFromSecret indicates whether get account key only from k8s secret
var getAccountKeyFromSecret, getLatestAccountKey bool
var clientID, tenantID, serviceAccountToken string
for k, v := range reqContext {
switch strings.ToLower(k) {
case subscriptionIDField:
subsID = v
case resourceGroupField:
rgName = v
case storageAccountField:
accountName = v
case getAccountKeyFromSecretField:
if strings.EqualFold(v, trueValue) {
getAccountKeyFromSecret = true
}
case shareNameField:
fileShareName = v
case diskNameField:
diskName = v
case protocolField:
protocol = v
case secretNameField:
secretName = v
case secretNamespaceField:
secretNamespace = v
case pvcNamespaceKey:
pvcNamespace = v
case getLatestAccountKeyField:
if getLatestAccountKey, err = strconv.ParseBool(v); err != nil {
return rgName, accountName, accountKey, fileShareName, diskName, subsID, fmt.Errorf("invalid %s: %s in volume context", getLatestAccountKeyField, v)
}
case strings.ToLower(clientIDField):
clientID = v
case strings.ToLower(tenantIDField):
tenantID = v
case strings.ToLower(serviceAccountTokenField):
serviceAccountToken = v
}
}
if tenantID == "" {
tenantID = d.cloud.TenantID
}
if rgName == "" {
rgName = d.cloud.ResourceGroup
}
if subsID == "" {
subsID = d.cloud.SubscriptionID
}
if protocol == nfs && fileShareName != "" {
// nfs protocol does not need account key, return directly
return rgName, accountName, accountKey, fileShareName, diskName, subsID, err
}
if secretNamespace == "" {
if pvcNamespace == "" {
secretNamespace = defaultNamespace
} else {
secretNamespace = pvcNamespace
}
}
// if client id is specified, we only use service account token to get account key
if clientID != "" {
klog.V(2).Infof("clientID(%s) is specified, use service account token to get account key", clientID)
accountKey, err := d.cloud.GetStorageAccesskeyFromServiceAccountToken(ctx, subsID, accountName, rgName, clientID, tenantID, serviceAccountToken)
return rgName, accountName, accountKey, fileShareName, diskName, subsID, err
}
if len(secrets) == 0 {
// if request context does not contain secrets, get secrets in the following order:
// 1. get account key from cache first
cache, errCache := d.accountCacheMap.Get(accountName, azcache.CacheReadTypeDefault)
if errCache != nil {
return rgName, accountName, accountKey, fileShareName, diskName, subsID, errCache
}
if cache != nil {
accountKey = cache.(string)
} else {
if secretName == "" && accountName != "" {
secretName = fmt.Sprintf(secretNameTemplate, accountName)
}
if secretName != "" {
var name string
// 2. if not found in cache, get account key from kubernetes secret
name, accountKey, err = d.GetStorageAccountFromSecret(ctx, secretName, secretNamespace)
if name != "" {
accountName = name
}
if err != nil {
// 3. if failed to get account key from kubernetes secret, use cluster identity to get account key
klog.Warningf("GetStorageAccountFromSecret(%s, %s) failed with error: %v", secretName, secretNamespace, err)
if !getAccountKeyFromSecret && d.cloud.StorageAccountClient != nil && accountName != "" {
klog.V(2).Infof("use cluster identity to get account key from (%s, %s, %s)", subsID, rgName, accountName)
accountKey, err = d.cloud.GetStorageAccesskey(ctx, subsID, accountName, rgName, getLatestAccountKey)
if err != nil {
klog.Errorf("GetStorageAccesskey(%s, %s, %s) failed with error: %v", subsID, rgName, accountName, err)
}
}
}
}
}
} else { // if request context contains secrets, get account name and key directly
var account string
account, accountKey, err = getStorageAccount(secrets)
if account != "" {
accountName = account
}
if err != nil {
klog.Errorf("getStorageAccount failed with error: %v", err)
}
}
if err == nil && accountKey != "" {
d.accountCacheMap.Set(accountName, accountKey)
}
return rgName, accountName, accountKey, fileShareName, diskName, subsID, err
}
func isSupportedProtocol(protocol string) bool {
if protocol == "" {
return true
}
for _, v := range supportedProtocolList {
if protocol == v {
return true
}
}
return false
}
func isSupportedShareAccessTier(accessTier string) bool {
if accessTier == "" {
return true
}
for _, tier := range storage.PossibleShareAccessTierValues() {
if accessTier == string(tier) {
return true
}
}
return false
}
func isSupportedAccountAccessTier(accessTier string) bool {
if accessTier == "" {
return true
}
for _, tier := range storage.PossibleAccessTierValues() {
if accessTier == string(tier) {
return true
}
}
return false
}
func isSupportedRootSquashType(rootSquashType string) bool {
if rootSquashType == "" {
return true
}
for _, tier := range storage.PossibleRootSquashTypeValues() {
if rootSquashType == string(tier) {
return true
}
}
return false
}
func isSupportedFSGroupChangePolicy(policy string) bool {
if policy == "" {
return true
}
for _, v := range supportedFSGroupChangePolicyList {
if policy == v {
return true
}
}
return false
}
// CreateFileShare creates a file share
func (d *Driver) CreateFileShare(ctx context.Context, accountOptions *azure.AccountOptions, shareOptions *fileclient.ShareOptions, secrets map[string]string) error {
return wait.ExponentialBackoff(d.cloud.RequestBackoff(), func() (bool, error) {
var err error
if len(secrets) > 0 {
accountName, accountKey, rerr := getStorageAccount(secrets)
if rerr != nil {
return true, rerr
}
fileClient, rerr := newAzureFileClient(accountName, accountKey, d.getStorageEndPointSuffix(), &retry.Backoff{Steps: 1})
if rerr != nil {
return true, rerr
}
err = fileClient.CreateFileShare(ctx, shareOptions)
} else {
_, err = d.cloud.FileClient.WithSubscriptionID(accountOptions.SubscriptionID).CreateFileShare(ctx, accountOptions.ResourceGroup, accountOptions.Name, shareOptions, "")
}
if isRetriableError(err) {
klog.Warningf("CreateFileShare(%s) on account(%s) failed with error(%v), waiting for retrying", shareOptions.Name, accountOptions.Name, err)
sleepIfThrottled(err, fileOpThrottlingSleepSec)
return false, nil
}
return true, err
})
}
// DeleteFileShare deletes a file share using storage account name and key
func (d *Driver) DeleteFileShare(ctx context.Context, subsID, resourceGroup, accountName, shareName string, secrets map[string]string) error {
return wait.ExponentialBackoff(d.cloud.RequestBackoff(), func() (bool, error) {
var err error
if len(secrets) > 0 {
accountName, accountKey, rerr := getStorageAccount(secrets)
if rerr != nil {
return true, rerr
}
fileClient, rerr := newAzureFileClient(accountName, accountKey, d.getStorageEndPointSuffix(), &retry.Backoff{Steps: 1})
if rerr != nil {
return true, rerr
}
err = fileClient.DeleteFileShare(ctx, shareName)
} else {
err = d.cloud.DeleteFileShare(ctx, subsID, resourceGroup, accountName, shareName)
}
if err != nil {
if strings.Contains(err.Error(), statusCodeNotFound) ||
strings.Contains(err.Error(), httpCodeNotFound) ||
strings.Contains(err.Error(), fileShareNotFound) {
klog.Warningf("DeleteFileShare(%s) on account(%s) failed with error(%v), return as success", shareName, accountName, err)
return true, nil
}
}
if isRetriableError(err) {
klog.Warningf("DeleteFileShare(%s) on account(%s) failed with error(%v), waiting for retrying", shareName, accountName, err)
if strings.Contains(strings.ToLower(err.Error()), strings.ToLower(tooManyRequests)) {
klog.Warningf("switch to use data plane API instead for account %s since it's throttled", accountName)
d.dataPlaneAPIAccountCache.Set(accountName, "")
return true, err
}
return false, nil
}
return true, err
})
}
// ResizeFileShare resizes a file share
func (d *Driver) ResizeFileShare(ctx context.Context, subsID, resourceGroup, accountName, shareName string, sizeGiB int, secrets map[string]string) error {
return wait.ExponentialBackoff(d.cloud.RequestBackoff(), func() (bool, error) {
var err error
if len(secrets) > 0 {
accountName, accountKey, rerr := getStorageAccount(secrets)
if rerr != nil {
return true, rerr
}
fileClient, rerr := newAzureFileClient(accountName, accountKey, d.getStorageEndPointSuffix(), &retry.Backoff{Steps: 1})
if rerr != nil {
return true, rerr
}
err = fileClient.ResizeFileShare(ctx, shareName, sizeGiB)
} else {
err = d.cloud.ResizeFileShare(ctx, subsID, resourceGroup, accountName, shareName, sizeGiB)
}
if isRetriableError(err) {
klog.Warningf("ResizeFileShare(%s) on account(%s) with new size(%d) failed with error(%v), waiting for retrying", shareName, accountName, sizeGiB, err)
sleepIfThrottled(err, fileOpThrottlingSleepSec)
return false, nil
}
return true, err
})
}
// copyFileShare copies a fileshare in the same storage account
func (d *Driver) copyFileShare(ctx context.Context, req *csi.CreateVolumeRequest, accountSASToken string, authAzcopyEnv []string, secretName, secretNamespace string, secrets map[string]string, shareOptions *fileclient.ShareOptions, accountOptions *azure.AccountOptions, storageEndpointSuffix string) error {
if shareOptions.Protocol == storage.EnabledProtocolsNFS {
return fmt.Errorf("protocol nfs is not supported for volume cloning")
}
var sourceVolumeID string
if req.GetVolumeContentSource() != nil && req.GetVolumeContentSource().GetVolume() != nil {
sourceVolumeID = req.GetVolumeContentSource().GetVolume().GetVolumeId()