forked from NetApp/trident
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.go
1040 lines (872 loc) · 28.9 KB
/
utils.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 2022 NetApp, Inc. All Rights Reserved.
package utils
import (
"bytes"
"context"
"crypto/rand"
"fmt"
"io/ioutil"
"net"
"net/http"
"net/url"
"os"
"reflect"
"regexp"
"sort"
"strconv"
"strings"
"time"
"go.uber.org/multierr"
"golang.org/x/text/cases"
"golang.org/x/text/language"
. "github.com/netapp/trident/logging"
)
const (
// Linux is a constant value for the runtime.GOOS that represents the Linux OS
Linux = "linux"
// Windows is a constant value for the runtime.GOOS that represents the Windows OS
Windows = "windows"
// Darwin is a constant value for the runtime.GOOS that represents Apple MacOS
Darwin = "darwin"
PrepPending NodePrepStatus = "pending"
PrepRunning NodePrepStatus = "running"
PrepCompleted NodePrepStatus = "completed"
PrepFailed NodePrepStatus = "failed"
PrepOutdated NodePrepStatus = "outdated"
PrepPreConfigured NodePrepStatus = "preconfigured"
Centos = "centos"
RHEL = "rhel"
Ubuntu = "ubuntu"
Debian = "debian"
REDACTED = "<REDACTED>"
// NAS protocols
SMB = "smb"
// Path separator
WindowsPathSeparator = `\`
UnixPathSeparator = "/"
deviceOperationsTimeout = 5 * time.Second
)
var (
NFSVersionMajorRegex = regexp.MustCompile(`^(nfsvers|vers)=(?P<major>\d)$`)
NFSVersionMajorMinorRegex = regexp.MustCompile(`^(nfsvers|vers)=(?P<major>\d)\.(?P<minor>\d)$`)
NFSVersionMinorRegex = regexp.MustCompile(`^minorversion=(?P<minor>\d)$`)
)
// ///////////////////////////////////////////////////////////////////////////
//
// Binary units
//
// ///////////////////////////////////////////////////////////////////////////
type sizeUnit2 []string
func (s sizeUnit2) Len() int {
return len(s)
}
func (s sizeUnit2) Swap(i, j int) {
s[i], s[j] = s[j], s[i]
}
func (s sizeUnit2) Less(i, j int) bool {
return len(s[i]) > len(s[j])
}
var (
lookupTable2 = make(map[string]int)
units2 = sizeUnit2{}
)
func init() {
// populate the lookup table for binary suffixes
lookupTable2["k"] = 1
lookupTable2["ki"] = 1
lookupTable2["kib"] = 1
lookupTable2["m"] = 2
lookupTable2["mi"] = 2
lookupTable2["mib"] = 2
lookupTable2["g"] = 3
lookupTable2["gi"] = 3
lookupTable2["gib"] = 3
lookupTable2["t"] = 4
lookupTable2["ti"] = 4
lookupTable2["tib"] = 4
lookupTable2["p"] = 5
lookupTable2["pi"] = 5
lookupTable2["pib"] = 5
lookupTable2["e"] = 6
lookupTable2["ei"] = 6
lookupTable2["eib"] = 6
lookupTable2["z"] = 7
lookupTable2["zi"] = 7
lookupTable2["zib"] = 7
lookupTable2["y"] = 8
lookupTable2["yi"] = 8
lookupTable2["yib"] = 8
// The slice of units is used to ensure that they are accessed by suffix from longest to
// shortest, i.e. match 'tib' before matching 'b'.
for unit := range lookupTable2 {
units2 = append(units2, unit)
}
sort.Sort(units2)
}
// ///////////////////////////////////////////////////////////////////////////
//
// SI units
//
// ///////////////////////////////////////////////////////////////////////////
type sizeUnit10 []string
func (s sizeUnit10) Len() int {
return len(s)
}
func (s sizeUnit10) Swap(i, j int) {
s[i], s[j] = s[j], s[i]
}
func (s sizeUnit10) Less(i, j int) bool {
return len(s[i]) > len(s[j])
}
var (
lookupTable10 = make(map[string]int)
units10 = sizeUnit10{}
)
func init() {
// populate the lookup table for SI suffixes
lookupTable10["b"] = 0
lookupTable10["bytes"] = 0
lookupTable10["kb"] = 1
lookupTable10["mb"] = 2
lookupTable10["gb"] = 3
lookupTable10["tb"] = 4
lookupTable10["pb"] = 5
lookupTable10["eb"] = 6
lookupTable10["zb"] = 7
lookupTable10["yb"] = 8
// The slice of units is used to ensure that they are accessed by suffix from longest to
// shortest, i.e. match 'tib' before matching 'b'.
for unit := range lookupTable10 {
units10 = append(units10, unit)
}
sort.Sort(units10)
}
// Pow is an integer version of exponentiation; existing builtin is float, we needed an int version.
func Pow(x int64, y int) int64 {
if y == 0 {
return 1
}
result := x
for n := 1; n < y; n++ {
result = result * x
}
return result
}
// ConvertSizeToBytes converts size to bytes; see also https://en.wikipedia.org/wiki/Kilobyte
func ConvertSizeToBytes(s string) (string, error) {
// make lowercase so units detection always works
s = strings.TrimSpace(strings.ToLower(s))
// first look for binary units
for _, unit := range units2 {
if strings.HasSuffix(s, unit) {
s = strings.TrimSuffix(s, unit)
if i, err := strconv.ParseInt(s, 10, 0); err != nil {
return "", fmt.Errorf("invalid size value '%s': %v", s, err)
} else {
i = i * Pow(1024, lookupTable2[unit])
s = strconv.FormatInt(i, 10)
return s, nil
}
}
}
// fall back to SI units
for _, unit := range units10 {
if strings.HasSuffix(s, unit) {
s = strings.TrimSuffix(s, unit)
if i, err := strconv.ParseInt(s, 10, 0); err != nil {
return "", fmt.Errorf("invalid size value '%s': %v", s, err)
} else {
i = i * Pow(1000, lookupTable10[unit])
s = strconv.FormatInt(i, 10)
return s, nil
}
}
}
// no valid units found, so ensure the value is a number
if _, err := strconv.ParseUint(s, 10, 64); err != nil {
return "", fmt.Errorf("invalid size value '%s': %v", s, err)
}
return s, nil
}
// GetVolumeSizeBytes determines the size, in bytes, of a volume from the "size" opt value. If "size" has a units
// suffix, that is handled here. If there are no units, the default is GiB. If size is not in opts, the specified
// default value is parsed identically and used instead.
func GetVolumeSizeBytes(ctx context.Context, opts map[string]string, defaultVolumeSize string) (uint64, error) {
usingDefaultSize := false
usingDefaultUnits := false
// Use the size if specified, else use the configured default size
size := GetV(opts, "size", "")
if size == "" {
size = defaultVolumeSize
usingDefaultSize = true
}
// Default to GiB if no units are present
if !sizeHasUnits(size) {
size += "G"
usingDefaultUnits = true
}
// Ensure the size is valid
sizeBytesStr, err := ConvertSizeToBytes(size)
if err != nil {
return 0, err
}
sizeBytes, _ := strconv.ParseUint(sizeBytesStr, 10, 64)
Logc(ctx).WithFields(LogFields{
"sizeBytes": sizeBytes,
"size": size,
"usingDefaultSize": usingDefaultSize,
"usingDefaultUnits": usingDefaultUnits,
}).Debug("Determined volume size.")
return sizeBytes, nil
}
// sizeHasUnits checks whether a size string includes a units suffix.
func sizeHasUnits(size string) bool {
// make lowercase so units detection always works
size = strings.TrimSpace(strings.ToLower(size))
for _, unit := range units2 {
if strings.HasSuffix(size, unit) {
return true
}
}
for _, unit := range units10 {
if strings.HasSuffix(size, unit) {
return true
}
}
return false
}
// VolumeSizeWithinTolerance checks to see if requestedSize is within the delta of the currentSize.
// If within the delta true is returned. If not within the delta and requestedSize is less than the
// currentSize false is returned.
func VolumeSizeWithinTolerance(requestedSize, currentSize, delta int64) bool {
sizeDiff := requestedSize - currentSize
if sizeDiff < 0 {
sizeDiff = -sizeDiff
}
if sizeDiff <= delta {
return true
}
return false
}
// GetV takes a map, key(s), and a defaultValue; will return the value of the key or defaultValue if none is set.
// If keys is a string of key values separated by "|", then each key is tried in turn. This allows compatibility
// with deprecated values, i.e. "fstype|fileSystemType".
func GetV(opts map[string]string, keys, defaultValue string) string {
for _, key := range strings.Split(keys, "|") {
// Try key first, then do a case-insensitive search
if value, ok := opts[key]; ok {
return value
} else {
for k, v := range opts {
if strings.EqualFold(k, key) {
return v
}
}
}
}
return defaultValue
}
// RandomString returns a string of the specified length consisting only of alphabetic characters.
func RandomString(strSize int) string {
chars := "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
bytes := make([]byte, strSize)
_, err := rand.Read(bytes)
if err != nil {
Logc(context.Background()).WithError(err).Error("Unable to generate random bytes")
}
for i, b := range bytes {
bytes[i] = chars[b%byte(len(chars))]
}
return string(bytes)
}
// StringInSlice checks whether a string is in a list of strings
func StringInSlice(s string, list []string) bool {
for _, item := range list {
if item == s {
return true
}
}
return false
}
func LogHTTPRequest(request *http.Request, requestBody []byte, driverName string, redactBody, isDriverLog bool) {
header := ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"
footer := "--------------------------------------------------------------------------------"
ctx := request.Context()
ctx = GenerateRequestContextForLayer(ctx, LogLayerUtils)
requestURL, err := url.Parse(request.URL.String())
if err != nil {
if isDriverLog {
Logd(ctx, driverName, true).WithError(err).Errorf("Unable to parse URL '%s'", request.URL.String())
} else {
Logc(ctx).WithError(err).Errorf("Unable to parse URL '%s'", request.URL.String())
}
}
requestURL.User = nil
headers := make(map[string][]string)
for k, v := range request.Header {
headers[k] = v
}
delete(headers, "Authorization")
delete(headers, "Api-Key")
delete(headers, "Secret-Key")
var body string
if requestBody == nil {
body = "<nil>"
} else if redactBody {
body = REDACTED
} else {
body = string(requestBody)
}
if isDriverLog {
Logd(ctx, driverName, true).Tracef("\n%s\n%s %s\nHeaders: %v\nBody: %s\n%s",
header, request.Method, requestURL, headers, body, footer)
} else {
Logc(ctx).Tracef("\n%s\n%s %s\nHeaders: %v\nBody: %s\n%s",
header, request.Method, requestURL, headers, body, footer)
}
}
func LogHTTPResponse(
ctx context.Context, response *http.Response, responseBody []byte, driverName string, redactBody, isDriverLog bool,
) {
ctx = GenerateRequestContextForLayer(ctx, LogLayerUtils)
header := "<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<"
footer := "================================================================================"
headers := make(map[string][]string)
for k, v := range response.Header {
headers[k] = v
}
delete(headers, "Authorization")
delete(headers, "Api-Key")
delete(headers, "Secret-Key")
var body string
if responseBody == nil {
body = "<nil>"
} else if redactBody {
body = REDACTED
} else {
body = string(responseBody)
}
if isDriverLog {
Logd(ctx, driverName, true).Tracef("\n%s\nStatus: %s\nHeaders: %v\nBody: %s\n%s", header,
response.Status, headers, body, footer)
} else {
Logc(ctx).Tracef("\n%s\nStatus: %s\nHeaders: %v\nBody: %s\n%s", header,
response.Status, headers, body, footer)
}
}
type HTTPError struct {
Status string
StatusCode int
}
func (e HTTPError) Error() string {
return fmt.Sprintf("HTTP error: %s", e.Status)
}
func NewHTTPError(response *http.Response) *HTTPError {
if response.StatusCode < 300 {
return nil
}
return &HTTPError{response.Status, response.StatusCode}
}
// SliceContains checks to see if a slice (genericSlice) contains an item (genericElement)
func SliceContains(genericSlice, genericElement interface{}) bool {
slice, elem := reflect.ValueOf(genericSlice), reflect.ValueOf(genericElement)
if slice.Kind() != reflect.Slice && slice.Kind() != reflect.Array {
return false
}
if slice.Len() > 1 {
if slice.Index(0).Type() != elem.Type() {
return false
}
}
for i := 0; i < slice.Len(); i++ {
if elem.Interface() == slice.Index(i).Interface() {
return true
}
}
return false
}
// SliceContainsElements checks to see if a slice (genericSlice) contains a list of all/some items (genericElements)
func SliceContainsElements(genericSlice, genericElements interface{}) (bool, bool) {
slice, elem := reflect.ValueOf(genericSlice), reflect.ValueOf(genericElements)
if slice.Kind() != reflect.Slice && slice.Kind() != reflect.Array {
return false, false
}
if elem.Kind() != reflect.Slice && elem.Kind() != reflect.Array {
return false, false
}
containAll := true
containSome := false
for i := 0; i < elem.Len(); i++ {
if SliceContains(genericSlice, elem.Index(i).Interface()) {
containSome = true
} else {
containAll = false
}
}
if !containSome {
containAll = false
}
return containAll, containSome
}
// SliceContainsString checks to see if a []string contains a string
func SliceContainsString(slice []string, s string) bool {
return SliceContainsStringConditionally(slice, s, func(val1, val2 string) bool { return val1 == val2 })
}
// SliceContainsStringCaseInsensitive is SliceContainsString but case insensitive
func SliceContainsStringCaseInsensitive(slice []string, s string) bool {
matchFunc := func(main, val string) bool {
return strings.EqualFold(main, val)
}
return SliceContainsStringConditionally(slice, s, matchFunc)
}
// SliceContainsStringConditionally checks to see if a []string contains a string based on certain criteria
func SliceContainsStringConditionally(slice []string, s string, fn func(string, string) bool) bool {
for _, item := range slice {
if fn(item, s) {
return true
}
}
return false
}
// RemoveStringFromSlice removes a string from a []string
func RemoveStringFromSlice(slice []string, s string) (result []string) {
return RemoveStringFromSliceConditionally(slice, s, func(val1, val2 string) bool { return val1 == val2 })
}
// RemoveStringFromSliceConditionally removes a string from a []string if it meets certain criteria
func RemoveStringFromSliceConditionally(slice []string, s string, fn func(string, string) bool) (result []string) {
for _, item := range slice {
if fn(s, item) {
continue
}
result = append(result, item)
}
return
}
// SplitImageDomain accepts a container image name and splits off the domain portion, if any.
func SplitImageDomain(name string) (domain, remainder string) {
i := strings.IndexRune(name, '/')
if i == -1 || (!strings.ContainsAny(name[:i], ".:") && name[:i] != "localhost") {
domain, remainder = "", name
} else {
domain, remainder = name[:i], name[i+1:]
}
return
}
// ReplaceImageRegistry accepts a container image name and a registry name (FQDN[:port]) and
// returns the same image name with the supplied registry.
func ReplaceImageRegistry(image, registry string) string {
_, remainder := SplitImageDomain(image)
if registry == "" {
return remainder
}
return registry + "/" + remainder
}
// ValidateCIDRs checks if a list of CIDR blocks are valid and returns a multi error containing all errors
// which may occur during the parsing process.
func ValidateCIDRs(ctx context.Context, cidrs []string) error {
var err error
// needed to capture all issues within the CIDR set
errors := make([]error, 0)
for _, cidr := range cidrs {
_, _, err := net.ParseCIDR(cidr)
if err != nil {
errors = append(errors, err)
Logc(ctx).WithError(err).Error("Found an invalid CIDR.")
}
}
if len(errors) != 0 {
err = multierr.Combine(errors...)
return err
}
return err
}
// FilterIPs takes a list of IPs and CIDRs and returns the sorted list of IPs that are contained by one or more of the
// CIDRs
func FilterIPs(ctx context.Context, ips, cidrs []string) ([]string, error) {
networks := make([]*net.IPNet, len(cidrs))
filteredIPs := make([]string, 0)
for i, cidr := range cidrs {
_, ipNet, err := net.ParseCIDR(cidr)
if err != nil {
err = fmt.Errorf("error parsing CIDR; %v", err)
Logc(ctx).WithField("CIDR", cidr).Error(err)
return nil, err
}
networks[i] = ipNet
}
for _, ip := range ips {
parsedIP := net.ParseIP(ip)
for _, network := range networks {
fields := LogFields{
"IP": ip,
"Network": network.String(),
}
if network.Contains(parsedIP) {
Logc(ctx).WithFields(fields).Debug("IP found in network.")
filteredIPs = append(filteredIPs, ip)
break
} else {
Logc(ctx).WithFields(fields).Debug("IP not found in network.")
}
}
}
sort.Strings(filteredIPs)
return filteredIPs, nil
}
func GetYAMLTagWithSpaceCount(text, tagName string) (string, int) {
// This matches pattern in a multiline string of type " {something}\n"
tagsWithIndentationRegex := regexp.MustCompile(`(?m)^[\t ]*{` + tagName + `}$\n`)
tag := tagsWithIndentationRegex.FindStringSubmatch(text)
// Since we have two of `()` in the pattern, we want to use the tag identified by the second `()`.
if len(tag) > 0 {
tagWithSpaces := tagsWithIndentationRegex.FindString(text)
indentation := CountSpacesBeforeText(tagWithSpaces)
return tagWithSpaces, indentation
}
return "", 0
}
func CountSpacesBeforeText(text string) int {
return len(text) - len(strings.TrimLeft(text, " \t"))
}
// GetFindMultipathValue returns the value of find_multipaths
// Returned values:
// no (or off): Create a multipath device for every path that is not explicitly disabled
// yes (or on): Create a device if one of some conditions are met
// other possible values: smart, greedy, strict
func GetFindMultipathValue(text string) string {
// This matches pattern in a multiline string of type " find_multipaths: yes"
tagsWithIndentationRegex := regexp.MustCompile(`(?m)^[\t ]*find_multipaths[\t ]*["|']?(?P<tagName>[\w-_]+)["|']?[\t ]*$`)
tag := tagsWithIndentationRegex.FindStringSubmatch(text)
// Since we have two of `()` in the pattern, we want to use the tag identified by the second `()`.
if len(tag) > 1 {
if tag[1] == "off" {
return "no"
} else if tag[1] == "on" {
return "yes"
}
return tag[1]
}
return ""
}
// AreMountOptionsInList returns true if any of the options are in mountOptions
func AreMountOptionsInList(mountOptions string, optionList []string) bool {
if mountOptions == "" || len(optionList) == 0 {
return false
}
mountOptionsSlice := strings.Split(strings.TrimPrefix(mountOptions, "-o"), ",")
for _, mountOptionItem := range mountOptionsSlice {
if SliceContainsString(optionList, mountOptionItem) {
return true
}
}
return false
}
// SanitizeMountOptions removes the options provided from the string list and spaces around options
func SanitizeMountOptions(mountOptions string, removeMountOptions []string) string {
if mountOptions == "" || len(removeMountOptions) == 0 {
return mountOptions
}
sanitized := make([]string, 0)
for _, mountOption := range strings.Split(mountOptions, ",") {
trimmedMountOption := strings.TrimSpace(mountOption)
if !SliceContainsString(removeMountOptions, trimmedMountOption) {
sanitized = append(sanitized, trimmedMountOption)
}
}
return strings.Join(sanitized, ",")
}
// GetNFSVersionFromMountOptions accepts a set of mount options, a default NFS version, and a list of
// supported NFS versions, and it returns the NFS version specified by the mount options, or the default
// if none is found, plus an error (if any). If a set of supported versions is supplied, and the returned
// version isn't in it, this method returns an error; otherwise it returns nil.
func GetNFSVersionFromMountOptions(mountOptions, defaultVersion string, supportedVersions []string) (string, error) {
major := ""
minor := ""
// Strip off -o prefix if present
mountOptions = strings.TrimPrefix(mountOptions, "-o ")
// Check each mount option using the three mutually-exclusive regular expressions. Last option wins.
for _, mountOption := range strings.Split(mountOptions, ",") {
mountOption = strings.TrimSpace(mountOption)
if matchGroups := GetRegexSubmatches(NFSVersionMajorMinorRegex, mountOption); matchGroups != nil {
major = matchGroups["major"]
minor = matchGroups["minor"]
continue
}
if matchGroups := GetRegexSubmatches(NFSVersionMajorRegex, mountOption); matchGroups != nil {
major = matchGroups["major"]
continue
}
if matchGroups := GetRegexSubmatches(NFSVersionMinorRegex, mountOption); matchGroups != nil {
minor = matchGroups["minor"]
continue
}
}
version := ""
// Assemble version string from major/minor values.
if major == "" {
// Minor doesn't matter if major isn't set.
version = ""
} else if minor == "" {
// Major works by itself if minor isn't set.
version = major
} else {
version = major + "." + minor
}
// Handle default & supported versions.
if version == "" {
return defaultVersion, nil
} else if supportedVersions == nil || SliceContainsString(supportedVersions, version) {
return version, nil
} else {
return version, fmt.Errorf("unsupported NFS version: %s", version)
}
}
// GetNFSVersionMountOptions accepts a set of mount options, identifies
// and returns all of the NFS version mount options.
func GetNFSVersionMountOptions(mountOptions string) []string {
// Strip off -o prefix if present
NFSMountOptions := make([]string, 0)
// Strip off -o prefix if present
mountOptions = strings.TrimPrefix(mountOptions, "-o ")
// Check each mount option using the three mutually-exclusive regular expressions. Last option wins.
for _, mountOption := range strings.Split(mountOptions, ",") {
mountOption = strings.TrimSpace(mountOption)
if matchGroups := GetRegexSubmatches(NFSVersionMajorMinorRegex, mountOption); matchGroups != nil {
NFSMountOptions = append(NFSMountOptions, mountOption)
continue
}
if matchGroups := GetRegexSubmatches(NFSVersionMajorRegex, mountOption); matchGroups != nil {
NFSMountOptions = append(NFSMountOptions, mountOption)
continue
}
if matchGroups := GetRegexSubmatches(NFSVersionMinorRegex, mountOption); matchGroups != nil {
NFSMountOptions = append(NFSMountOptions, mountOption)
continue
}
}
return NFSMountOptions
}
// SetNFSVersionMountOptions removes any existing NFS version mount option and sets passed NFS version mount options.
func SetNFSVersionMountOptions(mountOptions, newNFSVersionMountOption string) string {
// Strip off -o prefix if present or any "," at the end
mountOptions = strings.TrimPrefix(mountOptions, "-o ")
mountOptions = strings.TrimSuffix(mountOptions, ",")
NFSMountOptions := GetNFSVersionMountOptions(mountOptions)
// Remove any of the NFS mount options already set
if len(NFSMountOptions) != 0 {
mountOptions = SanitizeMountOptions(mountOptions, NFSMountOptions)
}
// Set the new NFS version mount option
return AppendToStringList(mountOptions, newNFSVersionMountOption, ",")
}
// GetRegexSubmatches accepts a regular expression with one or more groups and returns a map
// of the group matches found in the supplied string.
func GetRegexSubmatches(r *regexp.Regexp, s string) map[string]string {
match := r.FindStringSubmatch(s)
if match == nil {
return nil
}
paramsMap := make(map[string]string)
for i, name := range r.SubexpNames() {
if i > 0 && i <= len(match) {
paramsMap[name] = match[i]
}
}
return paramsMap
}
// Detect if code is running in a container or not
func RunningInContainer() bool {
return os.Getenv("CSI_ENDPOINT") != ""
}
func Max(x, y int64) int64 {
if x > y {
return x
}
return y
}
// SplitString is same as strings.Split except it returns a nil ([]string(nil)) of size 0 instead of
// string slice with an empty string (size 1) when string to be split is empty.
func SplitString(_ context.Context, s, sep string) []string {
if s == "" {
return nil
}
return strings.Split(s, sep)
}
// ReplaceAtIndex returns a string with the rune at the specified index replaced
func ReplaceAtIndex(in string, r rune, index int) (string, error) {
if index < 0 || index >= len(in) {
return in, fmt.Errorf("index '%d' out of bounds for string '%s'", index, in)
}
out := []rune(in)
out[index] = r
return string(out), nil
}
// MinInt64 returns the lower of the two integers specified
func MinInt64(a, b int64) int64 {
if a < b {
return a
}
return b
}
func ValidateOctalUnixPermissions(perms string) error {
permsRegex := regexp.MustCompile(`^[0-7]{4}$`)
if !permsRegex.MatchString(perms) {
return fmt.Errorf("%s is not a valid octal unix permissions value", perms)
}
return nil
}
func GenerateVolumePublishName(volumeID, nodeID string) string {
return fmt.Sprintf(volumeID + "." + nodeID)
}
// ToStringRedacted identifies attributes of a struct, stringifies them such that they can be consumed by the
// struct's stringer interface, and redacts elements specified in the redactList.
func ToStringRedacted(structPointer interface{}, redactList []string, configVal interface{}) (out string) {
defer func() {
if r := recover(); r != nil {
Log().Errorf("Panic in utils#ToStringRedacted; err: %v", r)
out = "<panic>"
}
}()
elements := reflect.ValueOf(structPointer).Elem()
var output strings.Builder
for i := 0; i < elements.NumField(); i++ {
fieldName := elements.Type().Field(i).Name
switch {
case fieldName == "Config" && configVal != nil:
output.WriteString(fmt.Sprintf("%v:%v ", fieldName, configVal))
case SliceContainsString(redactList, fieldName):
output.WriteString(fmt.Sprintf("%v:%v ", fieldName, REDACTED))
default:
output.WriteString(fmt.Sprintf("%v:%#v ", fieldName, elements.Field(i)))
}
}
out = output.String()
return
}
func RedactSecretsFromString(stringToSanitize string, replacements map[string]string, useRegex bool) string {
compileError := "regex matching the secret could not compile, so the entire string has been redacted"
for key, value := range replacements {
if useRegex {
pattern, err := regexp.Compile(key)
if err != nil {
return compileError
}
stringToSanitize = pattern.ReplaceAllString(stringToSanitize, value)
} else {
stringToSanitize = strings.ReplaceAll(stringToSanitize, key, value)
}
}
return stringToSanitize
}
// GetVerifiedBlockFsType retrieves and verifies the filesystem type for a BlockOnFile protocol
func GetVerifiedBlockFsType(filesystemType string) (string, error) {
fsTypeSplit := strings.Split(filesystemType, "/")
if len(fsTypeSplit) != 2 {
return "", fmt.Errorf("unable to get filesystem type from '%v'", filesystemType)
}
if fsTypeSplit[0] != "nfs" {
return "", fmt.Errorf("unrecognized fileSystemType option: %s", filesystemType)
}
return VerifyFilesystemSupport(fsTypeSplit[1])
}
// VerifyFilesystemSupport checks for a supported file system type
func VerifyFilesystemSupport(fs string) (string, error) {
fstype := strings.ToLower(fs)
switch fstype {
case fsXfs, fsExt3, fsExt4, fsRaw:
return fstype, nil
default:
return "", fmt.Errorf("unsupported fileSystemType option: %s", fstype)
}
}
// AppendToStringList appends an item to a string list with a seperator
func AppendToStringList(stringList, newItem, sep string) string {
stringListItems := SplitString(context.TODO(), stringList, sep)
if len(stringListItems) == 0 {
return newItem
}
stringListItems = append(stringListItems, newItem)
return strings.Join(stringListItems, sep)
}
// parseHostportIP returns just the IP address part of the given input IP address and strips any port information
func parseHostportIP(hostport string) string {
ipAddress := ""
if IPv6Check(hostport) {
// this is an IPv6 address, remove port value and add square brackets around the IP address
if hostport[0] == '[' {
ipAddress = strings.Split(hostport, "]")[0] + "]"
} else {
// assumption here is that without the square brackets its only IP address without port information
ipAddress = "[" + hostport + "]"
}
} else {
ipAddress = strings.Split(hostport, ":")[0]
}
return ipAddress
}
// ensureHostportFormatted ensures IPv6 hostport is in correct format
func ensureHostportFormatted(hostport string) string {
// If this is an IPv6 address, ensure IP address is enclosed in square
// brackets, as in "[::1]:80".
if IPv6Check(hostport) && hostport[0] != '[' {
// assumption here is that without the square brackets its only IP address without port information
return "[" + hostport + "]"
}
return hostport
}
func IPv6Check(ip string) bool {
return strings.Count(ip, ":") >= 2
}
// ConsistentRead repeatedly reads a file until it gets the same content twice.
// This is useful when reading files in /proc that are larger than page size
// and kernel may modify them between individual read() syscalls.
func ConsistentRead(filename string, attempts int) ([]byte, error) {
oldContent, err := ioutil.ReadFile(filename)
if err != nil {
return nil, err
}
for i := 0; i < attempts; i++ {
newContent, err := ioutil.ReadFile(filename)
if err != nil {
return nil, err
}
if bytes.Equal(oldContent, newContent) {
return newContent, nil
}
// Files are different, continue reading
oldContent = newContent