This repository has been archived by the owner on Apr 1, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.go
357 lines (295 loc) · 7.66 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
package utils
import (
"errors"
"fmt"
"math/rand"
"os"
"os/exec"
"reflect"
"regexp"
"strconv"
"strings"
"time"
"utils/log"
"github.com/container-storage-interface/spec/lib/go/csi"
"golang.org/x/sys/unix"
"k8s.io/apimachinery/pkg/api/resource"
)
const (
DoradoV6Version = "V600R003C00"
)
type VolumeMetrics struct {
Available *resource.Quantity
Capacity *resource.Quantity
InodesUsed *resource.Quantity
Inodes *resource.Quantity
InodesFree *resource.Quantity
Used *resource.Quantity
}
func PathExist(path string) (bool, error) {
_, err := os.Stat(path)
if err != nil {
if os.IsNotExist(err) {
return false, nil
} else {
return false, err
}
}
return true, nil
}
func ExecShellCmd(format string, args ...interface{}) (string, error) {
cmd := fmt.Sprintf(format, args...)
log.Infof("Gonna run shell cmd \"%s\".", cmd)
shCmd := exec.Command("/bin/sh", "-c", cmd)
output, err := shCmd.CombinedOutput()
if err != nil {
log.Warningf("Run shell cmd \"%s\" error: %s.", cmd, output)
return string(output), err
}
log.Infof("Shell cmd \"%s\" result:\n%s", cmd, output)
return string(output), nil
}
func GetLunName(name string) string {
if len(name) <= 31 {
return name
}
return name[:31]
}
func GetSnapshotName(name string) string {
if len(name) <= 31 {
return name
}
return name[:31]
}
func GetFusionStorageLunName(name string) string {
if len(name) <= 95 {
return name
}
return name[:95]
}
func GetFusionStorageSnapshotName(name string) string {
if len(name) <= 95 {
return name
}
return name[:95]
}
func GetFileSystemName(name string) string {
return strings.Replace(name, "-", "_", -1)
}
func GetFSSnapshotName(name string) string {
return strings.Replace(name, "-", "_", -1)
}
func GetSharePath(name string) string {
return "/" + strings.Replace(name, "-", "_", -1) + "/"
}
func GetFSSharePath(name string) string {
return "/" + strings.Replace(name, "-", "_", -1) + "/"
}
func GetHostName() (string, error) {
hostname, err := ExecShellCmd("hostname | xargs echo -n")
if err != nil {
return "", err
}
return hostname, nil
}
func GetPathTail(device string) string {
strs := strings.Split(device, "/")
if len(strs) > 0 {
return strs[len(strs)-1]
}
return ""
}
func GetBackendAndVolume(volumeId string) (string, string) {
var backend, volume string
splits := strings.SplitN(volumeId, "-", 2)
if len(splits) == 2 {
backend, volume = splits[0], splits[1]
} else {
backend, volume = "", splits[0]
}
log.Infof("Backend %s, volume %s", backend, volume)
return backend, volume
}
func SplitVolumeId(volumeId string) (string, string) {
splits := strings.SplitN(volumeId, ".", 2)
if len(splits) == 2 {
return splits[0], splits[1]
}
return splits[0], ""
}
func SplitSnapshotId(snapshotId string) (string, string, string) {
splits := strings.SplitN(snapshotId, ".", 3)
if len(splits) == 3 {
return splits[0], splits[1], splits[2]
}
return splits[0], "", ""
}
func MergeMap(args ...map[string]interface{}) map[string]interface{} {
newMap := make(map[string]interface{})
for _, arg := range args {
for k, v := range arg {
newMap[k] = v
}
}
return newMap
}
func WaitUntil(f func() (bool, error), timeout time.Duration, interval time.Duration) error {
done := make(chan error)
go func() {
timeout := time.After(timeout)
for {
condition, err := f()
if err != nil {
done <- err
return
}
if condition {
done <- nil
return
}
select {
case <-timeout:
done <- fmt.Errorf("Wait timeout")
return
default:
time.Sleep(interval)
}
}
}()
select {
case err := <-done:
return err
}
}
func RandomInt(n int) int {
rand.Seed(time.Now().UnixNano())
return rand.Intn(n)
}
func CopyMap(srcMap interface{}) map[string]interface{} {
copied := make(map[string]interface{})
if m, ok := srcMap.(map[string]string); ok {
for k, v := range m {
copied[k] = v
}
} else if m, ok := srcMap.(map[string]interface{}); ok {
for k, v := range m {
copied[k] = v
}
}
return copied
}
func StrToBool(str string) bool {
b, err := strconv.ParseBool(str)
if err != nil {
log.Warningf("Parse bool string %s error, return false")
return false
}
return b
}
func ReflectCall(obj interface{}, method string, args ...interface{}) []reflect.Value {
in := make([]reflect.Value, len(args))
for i, v := range args {
in[i] = reflect.ValueOf(v)
}
if v := reflect.ValueOf(obj).MethodByName(method); v.IsValid() {
return v.Call(in)
}
return nil
}
func IsDoradoV6(SystemInfo map[string]interface{}) bool {
versionInfo := SystemInfo["PRODUCTVERSION"].(string)
return versionInfo >= DoradoV6Version
}
func IsSupportFeature(features map[string]int, feature string) bool {
var support bool
status, exist := features[feature]
if exist {
support = status == 1 || status == 2
}
return support
}
func TransVolumeCapacity(size int64, unit int64) int64 {
newSize := RoundUpSize(size, unit)
return newSize
}
func RoundUpSize(volumeSizeBytes int64, allocationUnitBytes int64) int64 {
roundedUp := volumeSizeBytes / allocationUnitBytes
if volumeSizeBytes%allocationUnitBytes > 0 {
roundedUp++
}
return roundedUp
}
func GetAlua(alua map[string]interface{}, host string) map[string]interface{} {
if alua == nil {
return nil
}
for k, v := range alua {
if k == "*" {
continue
}
match, err := regexp.MatchString(k, host)
if err != nil {
log.Errorf("Regexp match error: %v", err)
} else if match {
return v.(map[string]interface{})
}
}
return alua["*"].(map[string]interface{})
}
func fsInfo(path string) (int64, int64, int64, int64, int64, int64, error) {
statfs := &unix.Statfs_t{}
err := unix.Statfs(path, statfs)
if err != nil {
return 0, 0, 0, 0, 0, 0, err
}
capacity := int64(statfs.Blocks) * int64(statfs.Bsize)
available := int64(statfs.Bavail) * int64(statfs.Bsize)
used := (int64(statfs.Blocks) - int64(statfs.Bfree)) * int64(statfs.Bsize)
inodes := int64(statfs.Files)
inodesFree := int64(statfs.Ffree)
inodesUsed := inodes - inodesFree
return inodes, inodesFree, inodesUsed, available, capacity, used, nil
}
func GetVolumeMetrics(path string) (*VolumeMetrics, error) {
volumeMetrics := &VolumeMetrics{}
inodes, inodesFree, inodesUsed, available, capacity, usage, err := fsInfo(path)
if err != nil {
return nil, fmt.Errorf("failed to get FsInfo, error %v", err)
}
volumeMetrics.Inodes = resource.NewQuantity(inodes, resource.BinarySI)
volumeMetrics.InodesFree = resource.NewQuantity(inodesFree, resource.BinarySI)
volumeMetrics.InodesUsed = resource.NewQuantity(inodesUsed, resource.BinarySI)
volumeMetrics.Available = resource.NewQuantity(available, resource.BinarySI)
volumeMetrics.Capacity = resource.NewQuantity(capacity, resource.BinarySI)
volumeMetrics.Used = resource.NewQuantity(usage, resource.BinarySI)
return volumeMetrics, nil
}
func GetLunUniqueId(protocol string, lun map[string]interface{}) (string, error){
if protocol == "roce" || protocol == "fc-nvme" {
tgtLunGuid, exist := lun["NGUID"].(string)
if !exist {
msg := fmt.Sprintf("The Lun info %s does not contain key NGUID", lun)
log.Errorln(msg)
return "", errors.New(msg)
}
return tgtLunGuid, nil
} else {
return lun["WWN"].(string), nil
}
}
func GetAccessModeType(accessMode csi.VolumeCapability_AccessMode_Mode) string {
switch accessMode {
case csi.VolumeCapability_AccessMode_SINGLE_NODE_WRITER:
return "ReadWrite"
case csi.VolumeCapability_AccessMode_SINGLE_NODE_READER_ONLY:
return "ReadOnly"
case csi.VolumeCapability_AccessMode_MULTI_NODE_READER_ONLY:
return "ReadOnly"
case csi.VolumeCapability_AccessMode_MULTI_NODE_SINGLE_WRITER:
return "ReadWrite"
case csi.VolumeCapability_AccessMode_MULTI_NODE_MULTI_WRITER:
return "ReadWrite"
default:
return ""
}
}