-
Notifications
You must be signed in to change notification settings - Fork 140
/
util.go
290 lines (248 loc) · 8.58 KB
/
util.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
package csi
import (
"encoding/json"
"fmt"
"os"
"strconv"
"strings"
"github.com/container-storage-interface/spec/lib/go/csi"
"github.com/pkg/errors"
"github.com/robfig/cron"
"github.com/sirupsen/logrus"
"golang.org/x/sys/unix"
"k8s.io/kubernetes/pkg/util/mount"
longhornclient "github.com/longhorn/longhorn-manager/client"
"github.com/longhorn/longhorn-manager/types"
)
const (
// defaultStaleReplicaTimeout set to 48 hours (2880 minutes)
defaultStaleReplicaTimeout = 2880
)
// NewForcedParamsOsExec creates a osExecutor that allows for adding additional params to later occurring Run calls
func NewForcedParamsOsExec(cmdParamMapping map[string]string) mount.Exec {
return &forcedParamsOsExec{
osExec: mount.NewOSExec(),
cmdParamMapping: cmdParamMapping,
}
}
type forcedParamsOsExec struct {
osExec mount.Exec
cmdParamMapping map[string]string
}
type volumeFilesystemStatistics struct {
availableBytes int64
totalBytes int64
usedBytes int64
availableInodes int64
totalInodes int64
usedInodes int64
}
func (e *forcedParamsOsExec) Run(cmd string, args ...string) ([]byte, error) {
var params []string
if param := e.cmdParamMapping[cmd]; param != "" {
// we prepend the user params, since options are conventionally before the final args
// command [-option(s)] [argument(s)]
params = append(params, param)
}
params = append(params, args...)
return e.osExec.Run(cmd, params...)
}
func getVolumeOptions(volOptions map[string]string) (*longhornclient.Volume, error) {
vol := &longhornclient.Volume{}
if staleReplicaTimeout, ok := volOptions["staleReplicaTimeout"]; ok {
srt, err := strconv.Atoi(staleReplicaTimeout)
if err != nil {
return nil, errors.Wrap(err, "Invalid parameter staleReplicaTimeout")
}
vol.StaleReplicaTimeout = int64(srt)
}
if vol.StaleReplicaTimeout <= 0 {
vol.StaleReplicaTimeout = defaultStaleReplicaTimeout
}
if share, ok := volOptions["share"]; ok {
isShared, err := strconv.ParseBool(share)
if err != nil {
return nil, errors.Wrap(err, "Invalid parameter share")
}
if isShared {
vol.AccessMode = string(types.AccessModeReadWriteMany)
} else {
vol.AccessMode = string(types.AccessModeReadWriteOnce)
}
}
if migratable, ok := volOptions["migratable"]; ok {
isMigratable, err := strconv.ParseBool(migratable)
if err != nil {
return nil, errors.Wrap(err, "Invalid parameter migratable")
}
if isMigratable && vol.AccessMode != string(types.AccessModeReadWriteMany) {
logrus.Infof("Cannot mark volume %v as migratable, "+
"since access mode is not RWX proceeding with RWO non migratable volume creation.", vol.Name)
volOptions["migratable"] = strconv.FormatBool(false)
isMigratable = false
}
vol.Migratable = isMigratable
}
if numberOfReplicas, ok := volOptions["numberOfReplicas"]; ok {
nor, err := strconv.Atoi(numberOfReplicas)
if err != nil || nor < 0 {
return nil, errors.Wrap(err, "Invalid parameter numberOfReplicas")
}
vol.NumberOfReplicas = int64(nor)
}
if locality, ok := volOptions["dataLocality"]; ok {
if err := types.ValidateDataLocality(types.DataLocality(locality)); err != nil {
return nil, errors.Wrap(err, "Invalid parameter dataLocality")
}
vol.DataLocality = locality
}
if revisionCounterDisabled, ok := volOptions["disableRevisionCounter"]; ok {
revCounterDisabled, err := strconv.ParseBool(revisionCounterDisabled)
if err != nil {
return nil, errors.Wrap(err, "Invalid parameter disableRevisionCounter")
}
vol.RevisionCounterDisabled = revCounterDisabled
}
if fromBackup, ok := volOptions["fromBackup"]; ok {
vol.FromBackup = fromBackup
}
if backingImage, ok := volOptions["backingImage"]; ok {
vol.BackingImage = backingImage
}
if jsonRecurringJobs, ok := volOptions["recurringJobs"]; ok {
recurringJobs, err := parseJSONRecurringJobs(jsonRecurringJobs)
if err != nil {
return nil, errors.Wrap(err, "Invalid parameter recurringJobs")
}
vol.RecurringJobs = recurringJobs
}
if diskSelector, ok := volOptions["diskSelector"]; ok {
vol.DiskSelector = strings.Split(diskSelector, ",")
}
if nodeSelector, ok := volOptions["nodeSelector"]; ok {
vol.NodeSelector = strings.Split(nodeSelector, ",")
}
return vol, nil
}
func parseJSONRecurringJobs(jsonRecurringJobs string) ([]longhornclient.RecurringJob, error) {
recurringJobs := []longhornclient.RecurringJob{}
err := json.Unmarshal([]byte(jsonRecurringJobs), &recurringJobs)
if err != nil {
return nil, fmt.Errorf("invalid json format of recurringJobs: %v %v", jsonRecurringJobs, err)
}
for _, recurringJob := range recurringJobs {
if _, err := cron.ParseStandard(recurringJob.Cron); err != nil {
return nil, fmt.Errorf("invalid cron format(%v): %v", recurringJob.Cron, err)
}
}
return recurringJobs, nil
}
func isLikelyNotMountPointAttach(targetpath string) (bool, error) {
notMnt, err := mount.New("").IsLikelyNotMountPoint(targetpath)
if err != nil {
if os.IsNotExist(err) {
err = os.MkdirAll(targetpath, 0750)
if err == nil {
notMnt = true
}
}
}
return notMnt, err
}
func isLikelyNotMountPointDetach(targetpath string) (bool, error) {
notMnt, err := mount.New("").IsLikelyNotMountPoint(targetpath)
if err != nil {
if os.IsNotExist(err) {
return notMnt, fmt.Errorf("targetpath not found")
}
}
return notMnt, err
}
// Should be similar to the detect function in `util` package
// For csi plugins, util.DetectFileSystem is not available since we cannot use NSExecutor in the workloads
func detectFileSystem(devicePath string) (string, error) {
mounter := &mount.SafeFormatAndMount{Interface: mount.New(""), Exec: mount.NewOSExec()}
output, err := mounter.Run("blkid", devicePath)
if err != nil {
return "", errors.Wrapf(err, "failed to get the file system info from device %v, maybe there is no Linux file system on the volume", devicePath)
}
items := strings.Split(string(output), " ")
if len(items) < 3 {
return "", fmt.Errorf("failed to detect the file system from device %v, invalid output of command blkid", devicePath)
}
return strings.Trim(strings.TrimPrefix(strings.TrimSpace(items[2]), "TYPE="), "\""), nil
}
// isBlockDevice return true if volumePath file is a block device, false otherwise.
func isBlockDevice(volumePath string) (bool, error) {
var stat unix.Stat_t
// See https://man7.org/linux/man-pages/man2/stat.2.html for details
err := unix.Stat(volumePath, &stat)
if err != nil {
return false, err
}
// See https://man7.org/linux/man-pages/man7/inode.7.html for detail
if (stat.Mode & unix.S_IFMT) == unix.S_IFBLK {
return true, nil
}
return false, nil
}
func getFilesystemStatistics(volumePath string) (*volumeFilesystemStatistics, error) {
var statfs unix.Statfs_t
// See http://man7.org/linux/man-pages/man2/statfs.2.html for details.
err := unix.Statfs(volumePath, &statfs)
if err != nil {
return nil, err
}
volStats := &volumeFilesystemStatistics{
availableBytes: int64(statfs.Bavail) * int64(statfs.Bsize),
totalBytes: int64(statfs.Blocks) * int64(statfs.Bsize),
usedBytes: (int64(statfs.Blocks) - int64(statfs.Bfree)) * int64(statfs.Bsize),
availableInodes: int64(statfs.Ffree),
totalInodes: int64(statfs.Files),
usedInodes: int64(statfs.Files) - int64(statfs.Ffree),
}
return volStats, nil
}
// makeDir creates a new directory.
// If pathname already exists as a directory, no error is returned.
// If pathname already exists as a file, an error is returned.
func makeDir(pathname string) error {
err := os.MkdirAll(pathname, os.FileMode(0755))
if err != nil {
if !os.IsExist(err) {
return err
}
}
return nil
}
// makeFile creates an empty file.
// If pathname already exists, whether a file or directory, no error is returned.
func makeFile(pathname string) error {
f, err := os.OpenFile(pathname, os.O_CREATE, os.FileMode(0644))
if f != nil {
f.Close()
}
if err != nil {
if !os.IsExist(err) {
return err
}
}
return nil
}
//requiresSharedAccess checks if the volume is requested to be multi node capable
// a volume that is already in shared access mode, must be used via shared access
// even if single node access is requested.
func requiresSharedAccess(vol *longhornclient.Volume, cap *csi.VolumeCapability) bool {
isSharedVolume := false
if vol != nil {
isSharedVolume = vol.AccessMode == string(types.AccessModeReadWriteMany) || vol.Migratable
}
mode := csi.VolumeCapability_AccessMode_UNKNOWN
if cap != nil {
mode = cap.AccessMode.Mode
}
return isSharedVolume ||
mode == csi.VolumeCapability_AccessMode_MULTI_NODE_READER_ONLY ||
mode == csi.VolumeCapability_AccessMode_MULTI_NODE_SINGLE_WRITER ||
mode == csi.VolumeCapability_AccessMode_MULTI_NODE_MULTI_WRITER
}