/
eseries_iscsi.go
1410 lines (1162 loc) · 42.7 KB
/
eseries_iscsi.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 NetApp, Inc. All Rights Reserved.
package eseries
import (
"encoding/base64"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"math/rand"
"regexp"
"strconv"
"strings"
"github.com/RoaringBitmap/roaring"
"github.com/google/uuid"
log "github.com/sirupsen/logrus"
tridentconfig "github.com/netapp/trident/config"
"github.com/netapp/trident/storage"
sa "github.com/netapp/trident/storage_attribute"
sc "github.com/netapp/trident/storage_class"
drivers "github.com/netapp/trident/storage_drivers"
"github.com/netapp/trident/storage_drivers/eseries/api"
"github.com/netapp/trident/utils"
)
const (
DefaultHostType = "linux_dm_mp"
MinimumVolumeSizeBytes = 1048576 // 1 MiB
// Constants for internal pool attributes
Size = "size"
Region = "region"
Zone = "zone"
Media = "media"
)
// SANStorageDriver is for storage provisioning via the Web Services Proxy RESTful interface that communicates
// with E-Series controllers via the SYMbol API.
type SANStorageDriver struct {
initialized bool
Config drivers.ESeriesStorageDriverConfig
API *api.Client
physicalPools map[string]*storage.Pool
virtualPools map[string]*storage.Pool
}
func (d *SANStorageDriver) Name() string {
return drivers.EseriesIscsiStorageDriverName
}
// backendName returns the name of the backend managed by this driver instance
func (d *SANStorageDriver) backendName() string {
if d.Config.BackendName == "" {
// Use the old naming scheme if no name is specified
return "eseries_" + d.Config.HostDataIP
} else {
return d.Config.BackendName
}
}
// poolName constructs the name of the pool reported by this driver instance
func (d *SANStorageDriver) poolName(name string) string {
return fmt.Sprintf("%s_%s",
d.backendName(),
strings.Replace(name, "-", "", -1))
}
func (d *SANStorageDriver) Protocol() string {
return "iscsi"
}
// Initialize from the provided config
func (d *SANStorageDriver) Initialize(
context tridentconfig.DriverContext, configJSON string, commonConfig *drivers.CommonStorageDriverConfig,
) error {
// Trace logging hasn't been set up yet, so always do it here
fields := log.Fields{
"Method": "Initialize",
"Type": "SANStorageDriver",
}
log.WithFields(fields).Debug(">>>> Initialize")
defer log.WithFields(fields).Debug("<<<< Initialize")
commonConfig.DriverContext = context
config := &drivers.ESeriesStorageDriverConfig{}
config.CommonStorageDriverConfig = commonConfig
// Decode configJSON into ESeriesStorageDriverConfig object
err := json.Unmarshal([]byte(configJSON), &config)
if err != nil {
return fmt.Errorf("could not decode JSON configuration: %v", err)
}
d.Config = *config
// Apply config defaults
if err := d.populateConfigurationDefaults(config); err != nil {
return fmt.Errorf("could not populate configuration defaults: %v", err)
}
d.Config = *config
log.WithFields(log.Fields{
"Version": config.Version,
"StorageDriverName": config.StorageDriverName,
"DebugTraceFlags": config.DebugTraceFlags,
"DisableDelete": config.DisableDelete,
"StoragePrefix": *config.StoragePrefix,
}).Debug("Reparsed into ESeriesStorageDriverConfig")
telemetry := make(map[string]string)
telemetry["version"] = tridentconfig.OrchestratorVersion.ShortString()
telemetry["plugin"] = d.Name()
telemetry["storagePrefix"] = *d.Config.StoragePrefix
d.API = api.NewAPIClient(api.ClientConfig{
WebProxyHostname: config.WebProxyHostname,
WebProxyPort: config.WebProxyPort,
WebProxyUseHTTP: config.WebProxyUseHTTP,
WebProxyVerifyTLS: config.WebProxyVerifyTLS,
Username: config.Username,
Password: config.Password,
ControllerA: config.ControllerA,
ControllerB: config.ControllerB,
PasswordArray: config.PasswordArray,
PoolNameSearchPattern: config.PoolNameSearchPattern,
HostDataIP: config.HostDataIP,
Protocol: d.Protocol(),
AccessGroup: config.AccessGroup,
HostType: config.HostType,
DriverName: config.CommonStorageDriverConfig.StorageDriverName,
Telemetry: telemetry,
ConfigVersion: config.CommonStorageDriverConfig.Version,
DebugTraceFlags: config.CommonStorageDriverConfig.DebugTraceFlags,
})
// Connect to web services proxy
_, err = d.API.Connect()
if err != nil {
return fmt.Errorf("could not connect to Web Services Proxy: %v", err)
}
// After connected to web service, identify physical and virtual pools
if err := d.initializeStoragePools(); err != nil {
return fmt.Errorf("could not configure storage pools: %v", err)
}
// Ensure the config is valid, including virtual pool config
if err := d.validate(); err != nil {
return fmt.Errorf("could not validate SANStorageDriver config: %v", err)
}
// Log chassis serial number
chassisSerialNumber, err := d.API.GetChassisSerialNumber()
if err != nil {
log.Warnf("Could not determine chassis serial number. %v", err)
} else {
log.WithField("serialNumber", chassisSerialNumber).Info("Chassis serial number.")
d.Config.SerialNumbers = []string{chassisSerialNumber}
}
// For Docker, we create a host now
// For Kubernetes, we ensure there is a host group and warn users to populate it with hosts out of band
// For K8S CSI, we create a host group if necessary and create the hosts automatically during the Publish calls
if context == tridentconfig.ContextDocker {
// Make sure this host is logged into the E-series iSCSI target
err = utils.EnsureISCSISession(d.Config.HostDataIP)
if err != nil {
return fmt.Errorf("could not establish iSCSI session: %v", err)
}
// Make sure there is a host defined on the array for this system
_, err = d.CreateHostForLocalHost()
if err != nil {
return err
}
} else if context == tridentconfig.ContextKubernetes {
hostGroup, err := d.API.GetHostGroup(d.Config.AccessGroup)
if err != nil {
return fmt.Errorf("could not check for host group %s: %v", d.Config.AccessGroup, err)
} else if hostGroup.ClusterRef == "" {
return fmt.Errorf("host group %s doesn't exist for E-Series array %s and needs to be manually "+
"created; please also ensure all relevant Hosts are defined on the array and added to the Host Group",
d.Config.AccessGroup, d.Config.ControllerA)
} else {
log.WithFields(log.Fields{
"driver": d.Name(),
"controller": d.Config.ControllerA,
"hostGroup": hostGroup.Label,
}).Warnf("Please ensure all relevant hosts are added to Host Group %s.", d.Config.AccessGroup)
}
} else if context == tridentconfig.ContextCSI {
_, err = d.API.EnsureHostGroup()
if err != nil {
return fmt.Errorf("could not check for host group %s: %v", d.Config.AccessGroup, err)
}
}
d.initialized = true
return nil
}
func (d *SANStorageDriver) Initialized() bool {
return d.initialized
}
func (d *SANStorageDriver) Terminate() {
if d.Config.DebugTraceFlags["method"] {
fields := log.Fields{"Method": "Terminate", "Type": "SANStorageDriver"}
log.WithFields(fields).Debug(">>>> Terminate")
defer log.WithFields(fields).Debug("<<<< Terminate")
}
d.initialized = false
}
// PopulateConfigurationDefaults fills in default values for configuration settings if not supplied in the config file
func (d *SANStorageDriver) populateConfigurationDefaults(config *drivers.ESeriesStorageDriverConfig) error {
if config.DebugTraceFlags["method"] {
fields := log.Fields{"Method": "populateConfigurationDefaults", "Type": "SANStorageDriver"}
log.WithFields(fields).Debug(">>>> populateConfigurationDefaults")
defer log.WithFields(fields).Debug("<<<< populateConfigurationDefaults")
}
if config.StoragePrefix == nil {
prefix := drivers.GetDefaultStoragePrefix(config.DriverContext)
config.StoragePrefix = &prefix
}
if config.AccessGroup == "" {
config.AccessGroup = drivers.GetDefaultIgroupName(config.DriverContext)
}
if config.HostType == "" {
config.HostType = DefaultHostType
}
if config.PoolNameSearchPattern == "" {
config.PoolNameSearchPattern = ".+"
}
// Fix poorly-chosen config key
if config.HostDataIPDeprecated != "" && config.HostDataIP == "" {
config.HostDataIP = config.HostDataIPDeprecated
}
// Ensure the default volume size is valid, using a "default default" of 1G if not set
if config.Size == "" {
config.Size = drivers.DefaultVolumeSize
} else {
_, err := utils.ConvertSizeToBytes(config.Size)
if err != nil {
return fmt.Errorf("invalid config value for default volume size: %v", err)
}
}
log.WithFields(log.Fields{
"StoragePrefix": *config.StoragePrefix,
"AccessGroup": config.AccessGroup,
"HostType": config.HostType,
"PoolNameSearchPattern": config.PoolNameSearchPattern,
"Size": config.Size,
}).Debugf("Configuration defaults")
return nil
}
func (d *SANStorageDriver) initializeStoragePools() error {
d.physicalPools = make(map[string]*storage.Pool)
d.virtualPools = make(map[string]*storage.Pool)
// To identify list of media types supported by physcial pools
mediaOffers := make([]sa.Offer, 0)
// Get pools
physicalStoragePools, err := d.API.GetVolumePools("", 0, "")
if err != nil {
return fmt.Errorf("could not get storage pools from array: %v", err)
}
// Define physical pools
for _, physicalStoragePool := range physicalStoragePools {
pool := storage.NewStoragePool(nil, physicalStoragePool.Label)
pool.Attributes[sa.BackendType] = sa.NewStringOffer(d.Name())
if d.Config.Region != "" {
pool.Attributes[sa.Region] = sa.NewStringOffer(d.Config.Region)
}
if d.Config.Zone != "" {
pool.Attributes[sa.Zone] = sa.NewStringOffer(d.Config.Zone)
}
// E-series supports both "hdd" and "ssd" media types
switch physicalStoragePool.DriveMediaType {
case "hdd":
pool.Attributes[sa.Media] = sa.NewStringOffer(sa.HDD)
pool.InternalAttributes[Media] = sa.HDD
case "ssd":
pool.Attributes[sa.Media] = sa.NewStringOffer(sa.SSD)
pool.InternalAttributes[Media] = sa.SSD
}
if mediaOffer, ok := pool.Attributes[sa.Media]; ok {
mediaOffers = append(mediaOffers, mediaOffer)
}
pool.Attributes[sa.Snapshots] = sa.NewBoolOffer(false)
pool.Attributes[sa.Clones] = sa.NewBoolOffer(false)
pool.Attributes[sa.Encryption] = sa.NewBoolOffer(false)
pool.Attributes[sa.ProvisioningType] = sa.NewStringOffer(sa.Thick)
pool.InternalAttributes[Size] = d.Config.Size
pool.InternalAttributes[Region] = d.Config.Region
pool.InternalAttributes[Zone] = d.Config.Zone
d.physicalPools[pool.Name] = pool
}
// Define virtual pools
for index, vpool := range d.Config.Storage {
region := d.Config.Region
if vpool.Region != "" {
region = vpool.Region
}
zone := d.Config.Zone
if vpool.Zone != "" {
zone = vpool.Zone
}
size := d.Config.Size
if vpool.Size != "" {
size = vpool.Size
}
pool := storage.NewStoragePool(nil, d.poolName(fmt.Sprintf("pool_%d", index)))
pool.Attributes[sa.BackendType] = sa.NewStringOffer(d.Name())
pool.Attributes[sa.Snapshots] = sa.NewBoolOffer(false)
pool.Attributes[sa.Clones] = sa.NewBoolOffer(false)
pool.Attributes[sa.Encryption] = sa.NewBoolOffer(false)
pool.Attributes[sa.ProvisioningType] = sa.NewStringOffer(sa.Thick)
pool.Attributes[sa.Labels] = sa.NewLabelOffer(d.Config.Labels, vpool.Labels)
if region != "" {
pool.Attributes[sa.Region] = sa.NewStringOffer(region)
}
if zone != "" {
pool.Attributes[sa.Zone] = sa.NewStringOffer(zone)
}
if len(mediaOffers) > 0 {
pool.Attributes[sa.Media] = sa.NewStringOfferFromOffers(mediaOffers...)
pool.InternalAttributes[sa.Media] = pool.Attributes[sa.Media].ToString()
}
pool.InternalAttributes[Size] = size
pool.InternalAttributes[Region] = region
pool.InternalAttributes[Zone] = zone
d.virtualPools[pool.Name] = pool
}
return nil
}
// Validate the driver configuration
func (d *SANStorageDriver) validate() error {
if d.Config.DebugTraceFlags["method"] {
fields := log.Fields{"Method": "validate", "Type": "SANStorageDriver"}
log.WithFields(fields).Debug(">>>> validate")
defer log.WithFields(fields).Debug("<<<< validate")
}
// Make sure the essential information was specified in the config
if d.Config.WebProxyHostname == "" {
return errors.New("WebProxyHostname is empty! You must specify the host/IP for the Web Services Proxy")
}
if d.Config.ControllerA == "" || d.Config.ControllerB == "" {
return errors.New("ControllerA or ControllerB are empty! You must specify the host/IP for the " +
"E-Series storage array. If it is a simplex array just specify the same host/IP twice")
}
if d.Config.HostDataIP == "" {
return errors.New("HostDataIP is empty! You need to specify at least one of the iSCSI interface " +
"IP addresses that is connected to the E-Series array")
}
// Validate pool-level attributes
allPools := make([]*storage.Pool, 0, len(d.physicalPools)+len(d.virtualPools))
for _, pool := range d.physicalPools {
allPools = append(allPools, pool)
}
for _, pool := range d.virtualPools {
allPools = append(allPools, pool)
}
for _, pool := range allPools {
// Validate default size
if _, err := utils.ConvertSizeToBytes(pool.InternalAttributes[Size]); err != nil {
return fmt.Errorf("invalid value for default volume size in pool %s: %v", pool.Name, err)
}
// Validate media type
if pool.InternalAttributes[Media] != "" {
for _, mediaType := range strings.Split(pool.InternalAttributes[Media], ",") {
switch mediaType {
case sa.HDD, sa.SSD:
break
default:
log.Errorf("invalid media type in pool %s: %s", pool.Name, mediaType)
}
}
}
}
return nil
}
// Create is called by Docker to create a container volume. Besides the volume name, a few optional parameters such as size
// and disk media type may be provided in the opts map. If more than one pool on the storage controller can satisfy the request, the
// one with the most free space is selected.
func (d *SANStorageDriver) Create(
volConfig *storage.VolumeConfig, storagePool *storage.Pool, volAttributes map[string]sa.Request,
) error {
name := volConfig.InternalName
var fstype string
if d.Config.DebugTraceFlags["method"] {
fields := log.Fields{
"Method": "Create",
"Type": "SANStorageDriver",
"name": name,
"attrs": volAttributes,
}
log.WithFields(fields).Debug(">>>> Create")
defer log.WithFields(fields).Debug("<<<< Create")
}
// If the volume already exists, bail out
extantVolume, err := d.API.GetVolume(name)
if err != nil {
return fmt.Errorf("error checking for existing volume: %v", err)
}
if d.API.IsRefValid(extantVolume.VolumeRef) {
return drivers.NewVolumeExistsError(name)
}
// Get candidate physical pools
physicalPools, err := d.getPoolsForCreate(volConfig, storagePool, volAttributes)
if err != nil {
return err
}
// Determine volume size in bytes
requestedSize, err := utils.ConvertSizeToBytes(volConfig.Size)
if err != nil {
return fmt.Errorf("could not convert volume size %s: %v", volConfig.Size, err)
}
sizeBytes, err := strconv.ParseUint(requestedSize, 10, 64)
if err != nil {
return fmt.Errorf("%v is an invalid volume size: %v", volConfig.Size, err)
}
if sizeBytes == 0 {
defaultSize, _ := utils.ConvertSizeToBytes(d.Config.Size)
sizeBytes, _ = strconv.ParseUint(defaultSize, 10, 64)
}
if sizeBytes < MinimumVolumeSizeBytes {
return fmt.Errorf("requested volume size (%d bytes) is too small; the minimum volume size is %d bytes",
sizeBytes, MinimumVolumeSizeBytes)
}
if _, _, checkVolumeSizeLimitsError := drivers.CheckVolumeSizeLimits(sizeBytes, d.Config.CommonStorageDriverConfig); checkVolumeSizeLimitsError != nil {
return checkVolumeSizeLimitsError
}
// Get options
opts, err := d.GetVolumeOpts(volConfig, storagePool, volAttributes)
if err != nil {
return err
}
// Get media type, or default to "hdd" if not specified
mediaType := utils.GetV(opts, "mediaType", "")
fstype, err = drivers.CheckSupportedFilesystem(utils.GetV(opts, "fstype|fileSystemType", drivers.DefaultFileSystemType), name)
if err != nil {
return err
}
createErrors := make([]error, 0)
for _, physicalPool := range physicalPools {
poolName := physicalPool.Name
// expect pool of size 1
pools, err := d.API.GetVolumePools(mediaType, sizeBytes, poolName)
if err != nil {
errMessage := fmt.Sprintf("E-series pool %s not found", poolName)
log.Error(errMessage)
createErrors = append(createErrors, errors.New(errMessage))
continue
}
// get first element of the pools
pool := pools[0]
// Create the volume
vol, err := d.API.CreateVolume(name, pool.VolumeGroupRef, sizeBytes, mediaType, fstype)
if err != nil {
return fmt.Errorf("could not create volume %s: %v", name, err)
}
log.WithFields(log.Fields{
"Name": name,
"Size": sizeBytes,
"MediaType": mediaType,
"RequestedPool": storagePool.Name,
"PhysicalPool": poolName,
"VolumeRef": vol.VolumeRef,
}).Debug("Create succeeded.")
return nil
}
// All physical pools that were eligible ultimately failed, so don't try this backend again
return drivers.NewBackendIneligibleError(name, createErrors)
}
// getPoolsForCreate returns candidate storage pools for creating volumes
func (d *SANStorageDriver) getPoolsForCreate(
volConfig *storage.VolumeConfig, storagePool *storage.Pool, volAttributes map[string]sa.Request,
) ([]*storage.Pool, error) {
// If a physical pool was requested, just use it
if _, ok := d.physicalPools[storagePool.Name]; ok {
return []*storage.Pool{storagePool}, nil
}
// If a virtual pool was requested, find a physical pool to satisfy it
if _, ok := d.virtualPools[storagePool.Name]; !ok {
return nil, fmt.Errorf("could not find pool %s", storagePool.Name)
}
// Make a storage class from the volume attributes to simplify pool matching
attributesCopy := make(map[string]sa.Request)
for k, v := range volAttributes {
attributesCopy[k] = v
}
delete(attributesCopy, sa.Selector)
storageClass := sc.NewFromAttributes(attributesCopy)
// Find matching pools
candidatePools := make([]*storage.Pool, 0)
for _, pool := range d.physicalPools {
if storageClass.Matches(pool) {
candidatePools = append(candidatePools, pool)
}
}
if len(candidatePools) == 0 {
err := errors.New("backend has no physical pools that can satisfy request")
return nil, drivers.NewBackendIneligibleError(volConfig.InternalName, []error{err})
}
// Shuffle physical pools
rand.Shuffle(len(candidatePools), func(i, j int) {
candidatePools[i], candidatePools[j] = candidatePools[j], candidatePools[i]
})
return candidatePools, nil
}
// Destroy is called by Docker to delete a container volume.
func (d *SANStorageDriver) Destroy(name string) error {
if d.Config.DebugTraceFlags["method"] {
fields := log.Fields{
"Method": "Destroy",
"Type": "SANStorageDriver",
"name": name,
}
log.WithFields(fields).Debug(">>>> Destroy")
defer log.WithFields(fields).Debug("<<<< Destroy")
}
var (
err error
iSCSINodeName string
lunID int
)
vol, err := d.API.GetVolume(name)
if err != nil {
return fmt.Errorf("could not find volume %s: %v", name, err)
}
if d.Config.DriverContext == tridentconfig.ContextDocker {
// Get target info
iSCSINodeName, _, err = d.getISCSITargetInfo()
if err != nil {
log.WithField("error", err).Error("Could not get target info.")
return err
}
// Get the LUN ID
lunID = -1
for _, mapping := range vol.Mappings {
lunID = mapping.LunNumber
}
if lunID >= 0 {
// Inform the host about the device removal
utils.PrepareDeviceForRemoval(lunID, iSCSINodeName)
}
}
if d.API.IsRefValid(vol.VolumeRef) {
// Destroy volume on storage array
err = d.API.DeleteVolume(vol)
if err != nil {
return fmt.Errorf("could not destroy volume %s: %v", name, err)
}
} else {
// If volume was deleted on this storage for any reason, don't fail it here.
log.WithField("Name", name).Warn("Could not find volume on array. Allowing deletion to proceed.")
}
return nil
}
// Publish the volume to the host specified in publishInfo. This method may or may not be running on the host
// where the volume will be mounted, so it should limit itself to updating access rules, initiator groups, etc.
// that require some host identity (but not locality) as well as storage controller API access.
func (d *SANStorageDriver) Publish(name string, publishInfo *utils.VolumePublishInfo) error {
if d.Config.DebugTraceFlags["method"] {
fields := log.Fields{
"Method": "Publish",
"Type": "SANStorageDriver",
"name": name,
}
log.WithFields(fields).Debug(">>>> Publish")
defer log.WithFields(fields).Debug("<<<< Publish")
}
// Get the volume
vol, err := d.API.GetVolume(name)
if err != nil {
return fmt.Errorf("could not find volume %s: %v", name, err)
}
if !d.API.IsRefValid(vol.VolumeRef) {
return fmt.Errorf("could not find volume %s", name)
}
// Get the Target IQN
targetIQN, err := d.API.GetTargetIQN()
if err != nil {
return fmt.Errorf("could not get target IQN from array: %v", err)
}
// Get the fstype
fstype := ""
for _, tag := range vol.VolumeTags {
if tag.Key == "fstype" {
fstype = tag.Value
log.WithFields(log.Fields{"LUN": name, "fstype": fstype}).Debug("Found LUN fstype.")
break
}
}
if fstype == "" {
fstype = drivers.DefaultFileSystemType
log.WithFields(log.Fields{"LUN": name, "fstype": fstype}).Warn("LUN fstype not found, using default.")
}
var iqn string
var hostname string
var mapping api.LUNMapping
if publishInfo.Localhost {
// Lookup local host IQNs
iqns, err := utils.GetInitiatorIqns()
if err != nil {
return fmt.Errorf("error determining host initiator IQN: %v", err)
} else if len(iqns) == 0 {
return errors.New("could not determine host initiator IQN")
}
iqn = iqns[0]
// Map the volume to the local host
mapping, err = d.MapVolumeToLocalHost(vol)
if err != nil {
return fmt.Errorf("could not map volume %s: %v", name, err)
}
} else {
// Host IQN must have been passed in
if len(publishInfo.HostIQN) == 0 {
return errors.New("host initiator IQN not specified")
}
iqn = publishInfo.HostIQN[0]
hostname = publishInfo.HostName
// Get the host group
hostGroup, err := d.API.EnsureHostGroup()
if err != nil {
return fmt.Errorf("could not get host group: %v", err)
}
// See if there is already a host for the specified IQN
host, err := d.API.GetHostForIQN(iqn)
if err != nil {
return fmt.Errorf("could not get host for IQN %s: %v", iqn, err)
}
// Create the host if necessary
if host.HostRef == "" {
host, err = d.API.CreateHost(hostname, iqn, d.Config.HostType, hostGroup)
if err != nil {
return fmt.Errorf("could not create host for IQN %s: %v", iqn, err)
}
}
// If we got a host, make sure it's in the right group
if host.HostRef != "" && host.ClusterRef != hostGroup.ClusterRef {
return fmt.Errorf("found for IQN %s, but it is in host group %s: %v", iqn, d.Config.AccessGroup, err)
}
// Map the volume directly to the Host Group
mapHost := api.HostEx{
HostRef: api.NullRef,
ClusterRef: hostGroup.ClusterRef,
}
mapping, err = d.API.MapVolume(vol, mapHost)
if err != nil {
return fmt.Errorf("could not map volume %s to Host Group %s: %v", name, hostGroup.Label, err)
}
}
// Add fields needed by Attach
publishInfo.IscsiLunNumber = int32(mapping.LunNumber)
publishInfo.IscsiTargetPortal = d.Config.HostDataIP
publishInfo.IscsiTargetIQN = targetIQN
publishInfo.FilesystemType = fstype
publishInfo.UseCHAP = false
publishInfo.SharedTarget = true
return nil
}
func (d *SANStorageDriver) getISCSITargetInfo() (iSCSINodeName string, iSCSIInterfaces []string, returnError error) {
targetSettings, err := d.API.GetTargetSettings()
if err != nil {
returnError = fmt.Errorf("could not get iSCSI target info: %v", err)
return
}
iSCSINodeName = targetSettings.NodeName.IscsiNodeName
for _, portal := range targetSettings.Portals {
if portal.IPAddress.AddressType == "ipv4" {
iSCSIInterface := fmt.Sprintf("%s:%d", portal.IPAddress.Ipv4Address, portal.TCPListenPort)
iSCSIInterfaces = append(iSCSIInterfaces, iSCSIInterface)
}
}
if len(iSCSIInterfaces) == 0 {
returnError = errors.New("target has no active IPv4 iSCSI interfaces")
return
}
return
}
// CreateHostForLocalHost ensures a Host definition corresponding to the local host exists on the array,
// defining a Host & HostGroup if not.
func (d *SANStorageDriver) CreateHostForLocalHost() (api.HostEx, error) {
// Get the IQN for this host
iqns, err := utils.GetInitiatorIqns()
if err != nil {
return api.HostEx{}, fmt.Errorf("could not determine host initiator IQNs: %v", err)
}
if len(iqns) == 0 {
return api.HostEx{}, errors.New("could not determine host initiator IQNs")
}
iqn := iqns[0]
// Ensure we have an E-series host to which to map the volume
host, err := d.API.EnsureHostForIQN(iqn)
if err != nil {
return api.HostEx{}, fmt.Errorf("could not define array host for IQN %s: %v", iqn, err)
}
return host, nil
}
// MapVolumeToLocalHost gets the iSCSI identity of the local host, ensures a corresponding Host definition exists on the array
// (defining a Host & HostGroup if not), maps the specified volume to the host/group (if it isn't already), and returns the mapping info.
func (d *SANStorageDriver) MapVolumeToLocalHost(volume api.VolumeEx) (api.LUNMapping, error) {
if d.Config.DebugTraceFlags["method"] {
fields := log.Fields{
"Method": "MapVolumeToLocalHost",
"Type": "SANStorageDriver",
"volume": volume.Label,
}
log.WithFields(fields).Debug(">>>> MapVolumeToLocalHost")
defer log.WithFields(fields).Debug("<<<< MapVolumeToLocalHost")
}
// Ensure we have a host to map the volume to
host, err := d.CreateHostForLocalHost()
if err != nil {
return api.LUNMapping{}, fmt.Errorf("could not map volume %s to host: %v", volume.Label, err)
}
// Map the volume
mapping, err := d.API.MapVolume(volume, host)
if err != nil {
return api.LUNMapping{}, fmt.Errorf("could not map volume %s to host %s: %v", volume.Label, host.Label, err)
}
return mapping, nil
}
// GetSnapshot returns a snapshot of a volume, or an error if it does not exist.
func (d *SANStorageDriver) GetSnapshot(snapConfig *storage.SnapshotConfig) (*storage.Snapshot, error) {
if d.Config.DebugTraceFlags["method"] {
fields := log.Fields{
"Method": "GetSnapshot",
"Type": "SANStorageDriver",
"snapshotName": snapConfig.InternalName,
"volumeName": snapConfig.VolumeInternalName,
}
log.WithFields(fields).Debug(">>>> GetSnapshot")
defer log.WithFields(fields).Debug("<<<< GetSnapshot")
}
return nil, drivers.NewSnapshotsNotSupportedError(d.Name())
}
// SnapshotList returns the list of snapshots associated with the specified volume. The E-series volume
// plugin does not support snapshots, so this method always returns an empty array.
func (d *SANStorageDriver) GetSnapshots(volConfig *storage.VolumeConfig) ([]*storage.Snapshot, error) {
if d.Config.DebugTraceFlags["method"] {
fields := log.Fields{
"Method": "GetSnapshots",
"Type": "SANStorageDriver",
"volumeName": volConfig.InternalName,
}
log.WithFields(fields).Debug(">>>> GetSnapshots")
defer log.WithFields(fields).Debug("<<<< GetSnapshots")
}
return make([]*storage.Snapshot, 0), nil
}
// CreateSnapshot creates a snapshot for the given volume. The E-series volume plugin
// does not support cloning or snapshots, so this method always returns an error.
func (d *SANStorageDriver) CreateSnapshot(snapConfig *storage.SnapshotConfig) (*storage.Snapshot, error) {
if d.Config.DebugTraceFlags["method"] {
fields := log.Fields{
"Method": "CreateSnapshot",
"Type": "SANStorageDriver",
"snapshotName": snapConfig.InternalName,
"volumeName": snapConfig.VolumeInternalName,
}
log.WithFields(fields).Debug(">>>> CreateSnapshot")
defer log.WithFields(fields).Debug("<<<< CreateSnapshot")
}
return nil, drivers.NewSnapshotsNotSupportedError(d.Name())
}
// RestoreSnapshot restores a volume (in place) from a snapshot.
func (d *SANStorageDriver) RestoreSnapshot(snapConfig *storage.SnapshotConfig) error {
if d.Config.DebugTraceFlags["method"] {
fields := log.Fields{
"Method": "RestoreSnapshot",
"Type": "SANStorageDriver",
"snapshotName": snapConfig.InternalName,
"volumeName": snapConfig.VolumeInternalName,
}
log.WithFields(fields).Debug(">>>> RestoreSnapshot")
defer log.WithFields(fields).Debug("<<<< RestoreSnapshot")
}
return drivers.NewSnapshotsNotSupportedError(d.Name())
}
// DeleteSnapshot deletes a volume snapshot.
func (d *SANStorageDriver) DeleteSnapshot(snapConfig *storage.SnapshotConfig) error {
if d.Config.DebugTraceFlags["method"] {
fields := log.Fields{
"Method": "DeleteSnapshot",
"Type": "SANStorageDriver",
"snapshotName": snapConfig.InternalName,
"volumeName": snapConfig.VolumeInternalName,
}
log.WithFields(fields).Debug(">>>> DeleteSnapshot")
defer log.WithFields(fields).Debug("<<<< DeleteSnapshot")
}
return drivers.NewSnapshotsNotSupportedError(d.Name())
}
// CreateClone creates a new volume from the named volume, either by direct clone or from the named snapshot. The E-series volume plugin
// does not support cloning or snapshots, so this method always returns an error.
func (d *SANStorageDriver) CreateClone(volConfig *storage.VolumeConfig) error {
name := volConfig.InternalName
source := volConfig.CloneSourceVolumeInternal
snapshot := volConfig.CloneSourceSnapshot
if d.Config.DebugTraceFlags["method"] {
fields := log.Fields{
"Method": "CreateClone",
"Type": "SANStorageDriver",
"name": name,
"source": source,
"snapshot": snapshot,
}
log.WithFields(fields).Debug(">>>> CreateClone")
defer log.WithFields(fields).Debug("<<<< CreateClone")
}
return fmt.Errorf("cloning is not supported by backend type %s", d.Name())
}
func (d *SANStorageDriver) Import(volConfig *storage.VolumeConfig, originalName string) error {
return errors.New("import is not implemented")
}
func (d *SANStorageDriver) Rename(name string, newName string) error {
return errors.New("rename is not implemented")
}
// Get test for the existence of a volume
func (d *SANStorageDriver) Get(name string) error {
if d.Config.DebugTraceFlags["method"] {
fields := log.Fields{
"Method": "Get",
"Type": "SANStorageDriver",
"name": name,
}
log.WithFields(fields).Debug(">>>> Get")
defer log.WithFields(fields).Debug("<<<< Get")
}
_, err := d.getVolume(name)
if err != nil {
return err
}
return nil
}
func (d *SANStorageDriver) getVolume(name string) (api.VolumeEx, error) {
vol, err := d.API.GetVolume(name)
if err != nil {
return vol, fmt.Errorf("could not find volume %s: %v", name, err)
} else if !d.API.IsRefValid(vol.VolumeRef) {
return vol, fmt.Errorf("could not find volume %s", name)
}
log.WithField("volume", vol).Debug("Found volume.")
return vol, nil
}
// GetStorageBackendSpecs retrieve storage capabilities and register pools with specified backend.
func (d *SANStorageDriver) GetStorageBackendSpecs(backend *storage.Backend) error {
backend.Name = d.backendName()
virtual := len(d.virtualPools) > 0
for _, pool := range d.physicalPools {
pool.Backend = backend