-
Notifications
You must be signed in to change notification settings - Fork 139
/
util.go
424 lines (359 loc) · 13.6 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
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
package csi
import (
"context"
"encoding/json"
"fmt"
"io"
"os"
"path"
"strconv"
"strings"
"time"
"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/mount-utils"
utilexec "k8s.io/utils/exec"
longhornclient "github.com/longhorn/longhorn-manager/client"
longhorn "github.com/longhorn/longhorn-manager/k8s/pkg/apis/longhorn/v1beta2"
"github.com/longhorn/longhorn-manager/types"
)
const (
// defaultStaleReplicaTimeout set to 48 hours (2880 minutes)
defaultStaleReplicaTimeout = 2880
defaultForceUmountTimeout = 30 * time.Second
tempTestMountPointValidStatusFile = ".longhorn-volume-mount-point-test.tmp"
)
// NewForcedParamsExec creates a osExecutor that allows for adding additional params to later occurring Run calls
func NewForcedParamsExec(cmdParamMapping map[string]string) utilexec.Interface {
return &forcedParamsOsExec{
exec: utilexec.New(),
cmdParamMapping: cmdParamMapping,
}
}
type forcedParamsOsExec struct {
exec utilexec.Interface
cmdParamMapping map[string]string
}
type volumeFilesystemStatistics struct {
availableBytes int64
totalBytes int64
usedBytes int64
availableInodes int64
totalInodes int64
usedInodes int64
}
func (e *forcedParamsOsExec) Command(cmd string, args ...string) utilexec.Cmd {
var params []string
if value := e.cmdParamMapping[cmd]; value != "" {
// we prepend the user params, since options are conventionally before the final args
// command [-option(s)] [argument(s)]
params = append(params, strings.Split(value, " ")...)
}
params = append(params, args...)
return e.exec.Command(cmd, params...)
}
func (e *forcedParamsOsExec) CommandContext(ctx context.Context, cmd string, args ...string) utilexec.Cmd {
return e.exec.CommandContext(ctx, cmd, args...)
}
func (e *forcedParamsOsExec) LookPath(file string) (string, error) {
return e.exec.LookPath(file)
}
func updateVolumeParamsForBackingImage(volumeParameters map[string]string, backingImageParameters map[string]string) {
BackingImageInfoFields := []string{
longhorn.BackingImageParameterName,
longhorn.BackingImageParameterDataSourceType,
longhorn.BackingImageParameterChecksum,
}
for _, v := range BackingImageInfoFields {
volumeParameters[v] = backingImageParameters[v]
delete(backingImageParameters, v)
}
backingImageParametersStr, _ := json.Marshal(backingImageParameters)
volumeParameters[longhorn.BackingImageParameterDataSourceParameters] = string(backingImageParametersStr)
}
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(longhorn.AccessModeReadWriteMany)
} else {
vol.AccessMode = string(longhorn.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(longhorn.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 encrypted, ok := volOptions["encrypted"]; ok {
isEncrypted, err := strconv.ParseBool(encrypted)
if err != nil {
return nil, errors.Wrap(err, "Invalid parameter encrypted")
}
vol.Encrypted = isEncrypted
}
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 replicaAutoBalance, ok := volOptions["replicaAutoBalance"]; ok {
err := types.ValidateReplicaAutoBalance(longhorn.ReplicaAutoBalance(replicaAutoBalance))
if err != nil {
return nil, errors.Wrap(err, "Invalid parameter replicaAutoBalance")
}
vol.ReplicaAutoBalance = replicaAutoBalance
}
if locality, ok := volOptions["dataLocality"]; ok {
if err := types.ValidateDataLocality(longhorn.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 unmapMarkSnapChainRemoved, ok := volOptions["unmapMarkSnapChainRemoved"]; ok {
if err := types.ValidateUnmapMarkSnapChainRemoved(longhorn.UnmapMarkSnapChainRemoved(unmapMarkSnapChainRemoved)); err != nil {
return nil, errors.Wrap(err, "Invalid parameter unmapMarkSnapChainRemoved")
}
vol.UnmapMarkSnapChainRemoved = unmapMarkSnapChainRemoved
}
if replicaSoftAntiAffinity, ok := volOptions["replicaSoftAntiAffinity"]; ok {
if err := types.ValidateReplicaSoftAntiAffinity(longhorn.ReplicaSoftAntiAffinity(replicaSoftAntiAffinity)); err != nil {
return nil, errors.Wrap(err, "Invalid parameter replicaSoftAntiAffinity")
}
vol.ReplicaSoftAntiAffinity = replicaSoftAntiAffinity
}
if replicaZoneSoftAntiAffinity, ok := volOptions["replicaZoneSoftAntiAffinity"]; ok {
if err := types.ValidateReplicaZoneSoftAntiAffinity(longhorn.ReplicaZoneSoftAntiAffinity(replicaZoneSoftAntiAffinity)); err != nil {
return nil, errors.Wrap(err, "Invalid parameter replicaZoneSoftAntiAffinity")
}
vol.ReplicaZoneSoftAntiAffinity = replicaZoneSoftAntiAffinity
}
if fromBackup, ok := volOptions["fromBackup"]; ok {
vol.FromBackup = fromBackup
}
if dataSource, ok := volOptions["dataSource"]; ok {
vol.DataSource = dataSource
}
if backingImage, ok := volOptions[longhorn.BackingImageParameterName]; ok {
vol.BackingImage = backingImage
}
recurringJobSelector := []longhornclient.VolumeRecurringJob{}
if jsonRecurringJobSelector, ok := volOptions["recurringJobSelector"]; ok {
err := json.Unmarshal([]byte(jsonRecurringJobSelector), &recurringJobSelector)
if err != nil {
return nil, errors.Wrap(err, "invalid json format of recurringJobSelector")
}
vol.RecurringJobSelector = recurringJobSelector
}
if diskSelector, ok := volOptions["diskSelector"]; ok {
vol.DiskSelector = strings.Split(diskSelector, ",")
}
if nodeSelector, ok := volOptions["nodeSelector"]; ok {
vol.NodeSelector = strings.Split(nodeSelector, ",")
}
vol.BackendStoreDriver = string(longhorn.BackendStoreDriverTypeV1)
if driver, ok := volOptions["backendStoreDriver"]; ok {
vol.BackendStoreDriver = driver
}
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 syncMountPointDirectory(targetPath string) error {
d, err := os.OpenFile(targetPath, os.O_SYNC, 0750)
if err != nil {
return err
}
defer d.Close()
if _, err := d.Readdirnames(1); err != nil {
if !errors.Is(err, io.EOF) {
return err
}
}
// it would not always return `Input/Output Error` or `read-only file system` errors if we only use ReadDir() or Readdirnames() without an I/O operation
// an I/O operation will make the targetPath mount point invalid immediately
tempFile := path.Join(targetPath, tempTestMountPointValidStatusFile)
f, err := os.OpenFile(tempFile, os.O_CREATE|os.O_RDWR|os.O_SYNC|os.O_TRUNC, 0666)
if err != nil {
return err
}
defer func() {
if err := f.Close(); err != nil {
logrus.WithError(err).Warnf("Failed to close %v", tempFile)
}
if err := os.Remove(tempFile); err != nil {
logrus.WithError(err).Warnf("Failed to remove %v", tempFile)
}
}()
if _, err := f.WriteString(tempFile); err != nil {
return err
}
return nil
}
// ensureMountPoint evaluates whether a path is a valid mountPoint
// in case the targetPath does not exists it will create a path and return false
// in case where the mount point exists but is corrupt, the mount point will be cleaned up and a error is returned
// the underlying implementation utilizes mounter.IsLikelyNotMountPoint so it cannot detect bind mounts
func ensureMountPoint(targetPath string, mounter mount.Interface) (bool, error) {
logrus.Debugf("Trying to ensure mount point %v", targetPath)
notMnt, err := mount.IsNotMountPoint(mounter, targetPath)
if os.IsNotExist(err) {
return false, os.MkdirAll(targetPath, 0750)
}
IsCorruptedMnt := mount.IsCorruptedMnt(err)
if !IsCorruptedMnt {
logrus.Debugf("Mount point %v try opening and syncing dir to make sure it's healthy", targetPath)
if err := syncMountPointDirectory(targetPath); err != nil {
logrus.WithError(err).Warnf("Mount point %v was identified as corrupt by opening and syncing", targetPath)
IsCorruptedMnt = true
}
}
if IsCorruptedMnt {
unmountErr := unmount(targetPath, mounter)
if unmountErr != nil {
return false, fmt.Errorf("failed to unmount corrupt mount point %v umount error: %v eval error: %v",
targetPath, unmountErr, err)
}
return false, fmt.Errorf("unmounted existing corrupt mount point %v", targetPath)
}
return !notMnt, err
}
func unmount(targetPath string, mounter mount.Interface) error {
var err error
forceUnmounter, ok := mounter.(mount.MounterForceUnmounter)
if ok {
logrus.Debugf("Trying to force unmount potential mount point %v", targetPath)
err = forceUnmounter.UnmountWithForce(targetPath, defaultForceUmountTimeout)
} else {
logrus.Debugf("Trying to unmount potential mount point %v", targetPath)
err = mounter.Unmount(targetPath)
}
if err == nil {
return nil
}
if strings.Contains(err.Error(), "not mounted") ||
strings.Contains(err.Error(), "no mount point specified") {
logrus.Infof("No need for unmount not a mount point %v", targetPath)
return nil
}
return err
}
// cleanupMountPoint ensures all mount layers for the targetPath are unmounted and the mount directory is removed
func cleanupMountPoint(targetPath string, mounter mount.Interface) error {
// we just try to unmount since the path check would get stuck for nfs mounts
logrus.Infof("Trying to cleanup mount point %v", targetPath)
if err := unmount(targetPath, mounter); err != nil {
logrus.WithError(err).Warn("Failed to unmount during cleanup")
return err
}
logrus.Infof("Cleaned up mount point %v", targetPath)
return mount.CleanupMountPoint(targetPath, mounter, true)
}
// 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
}
// 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 {
err = f.Close()
return err
}
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(longhorn.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
}