/
recurring_job.go
967 lines (839 loc) · 27.3 KB
/
recurring_job.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
package app
import (
"context"
"encoding/json"
"fmt"
"math/rand"
"os"
"sort"
"strconv"
"strings"
"sync"
"time"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"github.com/urfave/cli"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/rest"
longhornclient "github.com/longhorn/longhorn-manager/client"
"github.com/longhorn/longhorn-manager/engineapi"
longhorn "github.com/longhorn/longhorn-manager/k8s/pkg/apis/longhorn/v1beta1"
lhclientset "github.com/longhorn/longhorn-manager/k8s/pkg/client/clientset/versioned"
"github.com/longhorn/longhorn-manager/types"
"github.com/longhorn/longhorn-manager/util"
)
const (
FlagSnapshotName = "snapshot-name"
FlagGroups = "groups"
FlagLabels = "labels"
FlagRetain = "retain"
FlagConcurrent = "concurrent"
FlagBackup = "backup"
HTTPClientTimout = 1 * time.Minute
SnapshotPurgeStatusInterval = 5 * time.Second
WaitInterval = 5 * time.Second
DetachingWaitInterval = 10 * time.Second
VolumeAttachTimeout = 300 // 5 minutes
BackupProcessStartTimeout = 90 // 1.5 minutes
jobTypeSnapshot = string("snapshot")
jobTypeBackup = string("backup")
)
type Job struct {
logger logrus.FieldLogger
lhClient lhclientset.Interface
namespace string
volumeName string
snapshotName string
retain int
jobType string
labels map[string]string
api *longhornclient.RancherClient
}
func RecurringJobCmd() cli.Command {
return cli.Command{
Name: "recurring-job",
Flags: []cli.Flag{
cli.StringFlag{
Name: FlagManagerURL,
Usage: "Longhorn manager API URL",
},
},
Action: func(c *cli.Context) {
if err := recurringJob(c); err != nil {
logrus.Fatalf("Error taking snapshot: %v", err)
}
},
}
}
func recurringJob(c *cli.Context) error {
logger := logrus.StandardLogger()
var err error
var managerURL string = c.String(FlagManagerURL)
if managerURL == "" {
return fmt.Errorf("require %v", FlagManagerURL)
}
if c.NArg() != 1 {
return errors.New("job name is required")
}
jobName := c.Args()[0]
namespace := os.Getenv(types.EnvPodNamespace)
if namespace == "" {
return fmt.Errorf("cannot detect pod namespace, environment variable %v is missing", types.EnvPodNamespace)
}
lhClient, err := getLonghornClientset()
if err != nil {
return errors.Wrap(err, "unable to get clientset")
}
recurringJob, err := lhClient.LonghornV1beta1().RecurringJobs(namespace).Get(context.TODO(), jobName, metav1.GetOptions{})
if err != nil {
logrus.WithError(err).Errorf("Failed to get recurring job %v.", jobName)
return nil
}
var jobGroups []string = recurringJob.Spec.Groups
var jobTask string = string(recurringJob.Spec.Task)
var jobRetain int = recurringJob.Spec.Retain
var jobConcurrent int = recurringJob.Spec.Concurrency
jobLabelMap := map[string]string{}
if recurringJob.Spec.Labels != nil {
jobLabelMap = recurringJob.Spec.Labels
}
jobLabelMap[types.RecurringJobLabel] = recurringJob.Name
labelJSON, err := json.Marshal(jobLabelMap)
if err != nil {
return errors.Wrap(err, "Failed to get JSON encoding for labels")
}
var doBackup bool = false
if jobTask == string(types.RecurringJobTypeBackup) {
doBackup = true
}
allowDetachedSetting := types.SettingNameAllowRecurringJobWhileVolumeDetached
allowDetached, err := getSettingAsBoolean(allowDetachedSetting, namespace, lhClient)
if err != nil {
return errors.Wrapf(err, "unable to get %v setting", allowDetachedSetting)
}
logger.Debugf("Setting %v is %v", allowDetachedSetting, allowDetached)
volumes, err := getVolumesBySelector(types.LonghornLabelRecurringJob, jobName, namespace, lhClient)
if err != nil {
return err
}
filteredVolumes := []string{}
filterVolumesForJob(allowDetached, volumes, &filteredVolumes)
for _, jobGroup := range jobGroups {
volumes, err := getVolumesBySelector(types.LonghornLabelRecurringJobGroup, jobGroup, namespace, lhClient)
if err != nil {
return err
}
filterVolumesForJob(allowDetached, volumes, &filteredVolumes)
}
logger.Infof("Found %v volumes with recurring job %v", len(filteredVolumes), jobName)
var wg sync.WaitGroup
concurrentLimiter := make(chan struct{}, jobConcurrent)
defer wg.Wait()
for _, volumeName := range filteredVolumes {
wg.Add(1)
go func(volumeName string) {
concurrentLimiter <- struct{}{}
defer func() {
<-concurrentLimiter
wg.Done()
}()
log := logger.WithFields(logrus.Fields{
"job": jobName,
"volume": volumeName,
"task": jobTask,
"retain": jobRetain,
"concurrent": jobConcurrent,
"groups": strings.Join(jobGroups, ","),
"labels": string(labelJSON),
})
log.Info("Creating job")
snapshotName := types.GetCronJobNameForRecurringJob(jobName) + "-" + util.RandomID()
job, err := NewJob(
logger,
managerURL,
volumeName,
snapshotName,
jobLabelMap,
jobRetain,
doBackup)
if err != nil {
log.WithError(err).Error("failed to create new job for volume")
return
}
err = job.run()
if err != nil {
log.WithError(err).Errorf("failed to run job for volume")
return
}
log.Info("Created job")
}(volumeName)
}
return nil
}
func NewJob(logger logrus.FieldLogger, managerURL, volumeName, snapshotName string, labels map[string]string, retain int, backup bool) (*Job, error) {
namespace := os.Getenv(types.EnvPodNamespace)
if namespace == "" {
return nil, fmt.Errorf("Cannot detect pod namespace, environment variable %v is missing", types.EnvPodNamespace)
}
config, err := rest.InClusterConfig()
if err != nil {
return nil, errors.Wrap(err, "unable to get client config")
}
lhClient, err := lhclientset.NewForConfig(config)
if err != nil {
return nil, errors.Wrap(err, "unable to get clientset")
}
clientOpts := &longhornclient.ClientOpts{
Url: managerURL,
Timeout: HTTPClientTimout,
}
apiClient, err := longhornclient.NewRancherClient(clientOpts)
if err != nil {
return nil, errors.Wrapf(err, "could not create longhorn-manager api client")
}
// must at least retain 1 of course
if retain == 0 {
retain = 1
}
jobType := jobTypeSnapshot
if backup {
jobType = jobTypeBackup
}
logger = logger.WithFields(logrus.Fields{
"namespace": namespace,
"volumeName": volumeName,
"snapshotName": snapshotName,
"labels": labels,
"retain": retain,
"jobType": jobType,
})
return &Job{
logger: logger,
lhClient: lhClient,
namespace: namespace,
volumeName: volumeName,
snapshotName: snapshotName,
labels: labels,
retain: retain,
jobType: jobType,
api: apiClient,
}, nil
}
// handleVolumeDetachment decides whether the current recurring job should detach the volume.
// It should detach the volume when:
// 1. The volume is attached by this recurring job
// 2. The volume state is VolumeStateAttached
func (job *Job) handleVolumeDetachment() {
volumeAPI := job.api.Volume
volumeName := job.volumeName
jobName, _ := job.labels[types.RecurringJobLabel]
if jobName == "" {
job.logger.Warn("Missing RecurringJob label")
return
}
for {
volume, err := volumeAPI.ById(volumeName)
if err == nil {
if volume.LastAttachedBy != jobName {
return
}
// !volume.DisableFrontend condition makes sure that volume is detached by this recurring job,
// not by the auto-reattachment feature.
if volume.State == string(types.VolumeStateDetached) && !volume.DisableFrontend {
job.logger.Infof("Volume %v is detached", volumeName)
return
}
if volume.State == string(types.VolumeStateAttached) {
job.logger.Infof("Attempting to detach volume %v from all nodes", volumeName)
if _, err := volumeAPI.ActionDetach(volume, &longhornclient.DetachInput{HostId: ""}); err != nil {
job.logger.WithError(err).Info("Volume detach request failed")
}
}
} else {
job.logger.WithError(err).Infof("Could not get volume %v", volumeName)
}
time.Sleep(DetachingWaitInterval)
}
}
func (job *Job) run() (err error) {
job.logger.Info("job starts running")
volumeAPI := job.api.Volume
volumeName := job.volumeName
jobName, _ := job.labels[types.RecurringJobLabel]
if jobName == "" {
return fmt.Errorf("missing RecurringJob label")
}
volume, err := volumeAPI.ById(volumeName)
if err != nil {
return errors.Wrapf(err, "could not get volume %v", volumeName)
}
if len(volume.Controllers) > 1 {
return fmt.Errorf("cannot run job for volume %v that is using %v engines", volume.Name, len(volume.Controllers))
}
defer job.handleVolumeDetachment()
if volume.State != string(types.VolumeStateAttached) && volume.State != string(types.VolumeStateDetached) {
return fmt.Errorf("volume %v is in an invalid state for recurring job: %v. Volume must be in state Attached or Detached", volumeName, volume.State)
}
if volume.State == string(types.VolumeStateDetached) {
// Find a random ready node to attach the volume
// For load balancing purpose, the node need to be random
nodeToAttach, err := job.findARandomReadyNode(volume)
if err != nil {
return errors.Wrapf(err, "cannot do auto attaching for volume %v", volumeName)
}
// Automatically attach the volume
// Disable the volume's frontend make sure that pod cannot use the volume during the recurring job.
// This is necessary so that we can safely detach the volume when finishing the job.
job.logger.Infof("Automatically attach volume %v to node %v", volumeName, nodeToAttach)
if volume, err = volumeAPI.ActionAttach(volume, &longhornclient.AttachInput{
DisableFrontend: true,
HostId: nodeToAttach,
AttachedBy: jobName,
}); err != nil {
return err
}
volume, err = job.waitForVolumeState(string(types.VolumeStateAttached), VolumeAttachTimeout)
if err != nil {
return err
}
job.logger.Infof("Volume %v is in state %v", volumeName, volume.State)
}
if job.jobType == jobTypeBackup {
job.logger.Infof("Running recurring backup for volume %v", volumeName)
return job.doRecurringBackup()
}
job.logger.Infof("Running recurring snapshot for volume %v", volumeName)
return job.doRecurringSnapshot()
}
func (job *Job) doRecurringSnapshot() (err error) {
defer func() {
if err == nil {
job.logger.Info("Finish running recurring snapshot")
}
}()
volumeAPI := job.api.Volume
volumeName := job.volumeName
volume, err := volumeAPI.ById(volumeName)
if err != nil {
return errors.Wrapf(err, "could not get volume %v", volumeName)
}
shouldDo, err := job.shouldDoRecurringSnapshot(volume)
if err != nil {
return err
}
if !shouldDo {
job.logger.Infof("Skipping taking snapshot because the volume %v doesn't have new data in volume-head", volumeName)
return nil
}
err = job.doSnapshot()
if err != nil {
return err
}
return job.doSnapshotCleanup(false)
}
func (job *Job) doSnapshot() (err error) {
volumeAPI := job.api.Volume
volumeName := job.volumeName
volume, err := volumeAPI.ById(volumeName)
if err != nil {
return errors.Wrapf(err, "could not get volume %v", volumeName)
}
if _, err := volumeAPI.ActionSnapshotCreate(volume, &longhornclient.SnapshotInput{
Labels: job.labels,
Name: job.snapshotName,
}); err != nil {
return err
}
job.logger.Infof("Created the snapshot %v", job.snapshotName)
return nil
}
func (job *Job) doSnapshotCleanup(backupDone bool) (err error) {
volumeAPI := job.api.Volume
volumeName := job.volumeName
volume, err := volumeAPI.ById(volumeName)
if err != nil {
return errors.Wrapf(err, "could not get volume %v", volumeName)
}
collection, err := volumeAPI.ActionSnapshotList(volume)
if err != nil {
return err
}
cleanupSnapshotNames := job.listSnapshotNamesForCleanup(collection.Data, backupDone)
for _, snapshot := range cleanupSnapshotNames {
if _, err := volumeAPI.ActionSnapshotDelete(volume, &longhornclient.SnapshotInput{
Name: snapshot,
}); err != nil {
return err
}
job.logger.Debugf("Cleaned up snapshot %v for %v", snapshot, volumeName)
}
if len(cleanupSnapshotNames) > 0 {
if _, err := volumeAPI.ActionSnapshotPurge(volume); err != nil {
return err
}
for {
done := true
errorList := map[string]string{}
volume, err := volumeAPI.ById(volumeName)
if err != nil {
return err
}
for _, status := range volume.PurgeStatus {
if status.IsPurging {
done = false
break
}
if status.Error != "" {
errorList[status.Replica] = status.Error
}
}
if done {
if len(errorList) != 0 {
for replica, errMsg := range errorList {
job.logger.Warnf("error purging snapshots on replica %v: %v", replica, errMsg)
}
job.logger.Warnf("encountered one or more errors while purging snapshots")
}
return nil
}
time.Sleep(SnapshotPurgeStatusInterval)
}
}
return nil
}
// shouldDoRecurringSnapshot return whether we should take a new snapshot in the current running of the job
func (job *Job) shouldDoRecurringSnapshot(volume *longhornclient.Volume) (bool, error) {
volumeHeadSize, err := job.getVolumeHeadSize(volume)
if err != nil {
return false, err
}
// If volume-head has new data, we need to take snapshot
if volumeHeadSize > 0 {
return true, nil
}
return false, nil
}
type NameWithTimestamp struct {
Name string
Timestamp time.Time
}
// getLastSnapshot return the last snapshot of the volume exclude the volume-head
// return nil, nil if volume doesn't have any snapshot other than the volume-head
func (job *Job) getLastSnapshot(volume *longhornclient.Volume) (*longhornclient.Snapshot, error) {
volumeHead, err := job.api.Volume.ActionSnapshotGet(volume, &longhornclient.SnapshotInput{
Name: engineapi.VolumeHeadName,
})
if err != nil {
return nil, errors.Wrapf(err, "could not get volume-head for volume %v", job.volumeName)
}
if volumeHead.Parent == "" {
return nil, nil
}
lastSnapshot, err := job.api.Volume.ActionSnapshotGet(volume, &longhornclient.SnapshotInput{
Name: volumeHead.Parent,
})
if err != nil {
return nil, errors.Wrapf(err, "could not get parent of volume-head for volume %v", job.volumeName)
}
return lastSnapshot, nil
}
// getVolumeHeadSize return the size of volume-head snapshot
func (job *Job) getVolumeHeadSize(volume *longhornclient.Volume) (int64, error) {
volumeHead, err := job.api.Volume.ActionSnapshotGet(volume, &longhornclient.SnapshotInput{
Name: engineapi.VolumeHeadName,
})
if err != nil {
return 0, errors.Wrapf(err, "could not get volume-head for volume %v", job.volumeName)
}
volumeHeadSize, err := strconv.ParseInt(volumeHead.Size, 10, 64)
if err != nil {
return 0, err
}
return volumeHeadSize, nil
}
func (job *Job) listSnapshotNamesForCleanup(snapshots []longhornclient.Snapshot, backupDone bool) []string {
jobLabel, found := job.labels[types.RecurringJobLabel]
if !found {
return []string{}
}
// Only consider deleting the snapshots that were created by our current job
snapshots = filterSnapshotsWithLabel(snapshots, types.RecurringJobLabel, jobLabel)
if job.jobType == jobTypeSnapshot {
return filterExpiredItems(snapshotsToNameWithTimestamps(snapshots), job.retain)
}
// For the recurring backup job, only keep the snapshot of the last backup and the current snapshot
retainingSnapshots := map[string]struct{}{job.snapshotName: struct{}{}}
if !backupDone {
lastBackup, err := job.getLastBackup()
if err == nil && lastBackup != nil {
retainingSnapshots[lastBackup.SnapshotName] = struct{}{}
}
}
return snapshotsToNames(filterSnapshotsNotInTargets(snapshots, retainingSnapshots))
}
func (job *Job) doRecurringBackup() (err error) {
defer func() {
if err == nil {
job.logger.Info("Finish running recurring backup")
}
}()
backupAPI := job.api.BackupVolume
volumeAPI := job.api.Volume
snapshot := job.snapshotName
volumeName := job.volumeName
defer func() {
err = errors.Wrapf(err, "failed to complete backupAndCleanup for %v", volumeName)
}()
volume, err := volumeAPI.ById(volumeName)
if err != nil {
return errors.Wrapf(err, "could not get volume %v", volumeName)
}
shouldDo, err := job.shouldDoRecurringBackup(volume)
if err != nil {
return err
}
if !shouldDo {
job.logger.Infof("Skipping taking backup because volume %v is either empty or doesn't have new data since the last backup", volumeName)
return nil
}
if err := job.doSnapshot(); err != nil {
return err
}
if err := job.doSnapshotCleanup(false); err != nil {
return err
}
if _, err := volumeAPI.ActionSnapshotBackup(volume, &longhornclient.SnapshotInput{
Labels: job.labels,
Name: snapshot,
}); err != nil {
return err
}
if err := job.waitForBackupProcessStart(BackupProcessStartTimeout); err != nil {
return err
}
// Wait for backup creation complete
for {
volume, err := volumeAPI.ById(volumeName)
if err != nil {
return err
}
hasRunningController := false // controller is the engine process
for _, controller := range volume.Controllers {
if controller.Running {
hasRunningController = true
break
}
}
if !hasRunningController {
return fmt.Errorf("the volume %v is detached on the middle of the backup process", volumeName)
}
var info *longhornclient.BackupStatus
for _, status := range volume.BackupStatus {
if status.Snapshot == snapshot {
info = &status
break
}
}
if info == nil {
return fmt.Errorf("cannot find the status of the backup for snapshot %v. It might because the engine has restarted", snapshot)
}
complete := false
switch info.State {
case engineapi.BackupStateComplete:
complete = true
job.logger.Debugf("Complete creating backup %v", info.BackupURL)
case engineapi.BackupStateInProgress:
job.logger.Debugf("Creating backup %v, current progress %v", info.BackupURL, info.Progress)
case engineapi.BackupStateError:
return fmt.Errorf("failed to create backup %v: %v", info.BackupURL, info.Error)
default:
return fmt.Errorf("invalid state %v for backup %v", info.State, info.BackupURL)
}
if complete {
break
}
time.Sleep(WaitInterval)
}
defer func() {
if err != nil {
job.logger.Warnf("created backup successfully but errored on cleanup for %v: %v", volumeName, err)
err = nil
}
}()
backupVolume, err := backupAPI.ById(volumeName)
if err != nil {
return err
}
backups, err := backupAPI.ActionBackupList(backupVolume)
if err != nil {
return err
}
cleanupBackups := job.listBackupsForCleanup(backups.Data)
for _, backup := range cleanupBackups {
if _, err := backupAPI.ActionBackupDelete(backupVolume, &longhornclient.BackupInput{
Name: backup,
}); err != nil {
return fmt.Errorf("Cleaned up backup %v failed for %v: %v", backup, volumeName, err)
}
job.logger.Debugf("Cleaned up backup %v for %v", backup, volumeName)
}
if err := job.doSnapshotCleanup(true); err != nil {
return err
}
return nil
}
// waitForBackupProcessStart timeout in second
// Return nil if the backup progress has started; error if error or timeout
func (job *Job) waitForBackupProcessStart(timeout int) error {
volumeAPI := job.api.Volume
volumeName := job.volumeName
snapshot := job.snapshotName
for i := 0; i < timeout; i++ {
volume, err := volumeAPI.ById(volumeName)
if err != nil {
return err
}
for _, status := range volume.BackupStatus {
if status.Snapshot == snapshot {
return nil
}
}
time.Sleep(WaitInterval)
}
return fmt.Errorf("timeout waiting for the backup of the snapshot %v of volume %v to start", snapshot, volumeName)
}
// shouldDoRecurringBackup return whether the recurring backup should take place
// We should do recurring backup if there is new data since the last backup
func (job *Job) shouldDoRecurringBackup(volume *longhornclient.Volume) (bool, error) {
volumeHeadSize, err := job.getVolumeHeadSize(volume)
if err != nil {
return false, err
}
// If volume-head has new data, we need to do backup
if volumeHeadSize > 0 {
return true, nil
}
lastBackup, err := job.getLastBackup()
if err != nil {
return false, err
}
lastSnapshot, err := job.getLastSnapshot(volume)
if err != nil {
return false, err
}
if lastSnapshot == nil {
return false, nil
}
if lastBackup != nil && lastBackup.SnapshotName == lastSnapshot.Name {
return false, nil
}
return true, nil
}
// getLastBackup return the last backup of the volume
// return nil, nil if volume doesn't have any backup
func (job *Job) getLastBackup() (*longhornclient.Backup, error) {
var err error
defer func() {
err = errors.Wrapf(err, "failed to get last backup for %v", job.volumeName)
}()
volume, err := job.api.Volume.ById(job.volumeName)
if err != nil {
return nil, err
}
if volume.LastBackup == "" {
return nil, nil
}
backupVolume, err := job.api.BackupVolume.ById(job.volumeName)
if err != nil {
return nil, err
}
return job.api.BackupVolume.ActionBackupGet(backupVolume, &longhornclient.BackupInput{
Name: volume.LastBackup,
})
}
func (job *Job) listBackupsForCleanup(backups []longhornclient.Backup) []string {
sts := []NameWithTimestamp{}
// only remove backups that where created by our current job
jobLabel, found := job.labels[types.RecurringJobLabel]
if !found {
return []string{}
}
for _, backup := range backups {
backupLabel, found := backup.Labels[types.RecurringJobLabel]
if found && jobLabel == backupLabel {
t, err := time.Parse(time.RFC3339, backup.Created)
if err != nil {
job.logger.Errorf("Fail to parse datetime %v for backup %v",
backup.Created, backup)
continue
}
sts = append(sts, NameWithTimestamp{
Name: backup.Name,
Timestamp: t,
})
}
}
return filterExpiredItems(sts, job.retain)
}
func (job *Job) GetVolume(name string) (*longhorn.Volume, error) {
return job.lhClient.LonghornV1beta1().Volumes(job.namespace).Get(context.TODO(), name, metav1.GetOptions{})
}
func (job *Job) UpdateVolumeStatus(v *longhorn.Volume) (*longhorn.Volume, error) {
return job.lhClient.LonghornV1beta1().Volumes(job.namespace).UpdateStatus(context.TODO(), v, metav1.UpdateOptions{})
}
// waitForVolumeState timeout in second
func (job *Job) waitForVolumeState(state string, timeout int) (*longhornclient.Volume, error) {
volumeAPI := job.api.Volume
volumeName := job.volumeName
for i := 0; i < timeout; i++ {
volume, err := volumeAPI.ById(volumeName)
if err == nil {
if volume.State == state {
return volume, nil
}
}
time.Sleep(1 * time.Second)
}
return nil, fmt.Errorf("timeout waiting for volume %v to be in state %v", volumeName, state)
}
// findARandomReadyNode return a random ready node to attach the volume
// return error if there is no ready node
func (job *Job) findARandomReadyNode(v *longhornclient.Volume) (string, error) {
nodeCollection, err := job.api.Node.List(longhornclient.NewListOpts())
if err != nil {
return "", err
}
engineImage, err := job.api.EngineImage.ById(types.GetEngineImageChecksumName(v.CurrentImage))
if err != nil {
return "", err
}
if engineImage.State != types.EngineImageStateDeployed && engineImage.State != types.EngineImageStateDeploying {
return "", fmt.Errorf("error: the volume's engine image %v is in state: %v", engineImage.Name, engineImage.State)
}
var readyNodeList []string
for _, node := range nodeCollection.Data {
if readyConditionInterface, ok := node.Conditions[types.NodeConditionTypeReady]; ok {
// convert the interface to json
jsonString, err := json.Marshal(readyConditionInterface)
if err != nil {
return "", err
}
readyCondition := types.Condition{}
if err := json.Unmarshal(jsonString, &readyCondition); err != nil {
return "", err
}
if readyCondition.Status == types.ConditionStatusTrue && engineImage.NodeDeploymentMap[node.Name] {
readyNodeList = append(readyNodeList, node.Name)
}
}
}
if len(readyNodeList) == 0 {
return "", fmt.Errorf("cannot find a ready node")
}
return readyNodeList[rand.Intn(len(readyNodeList))], nil
}
func filterSnapshots(snapshots []longhornclient.Snapshot, predicate func(snapshot longhornclient.Snapshot) bool) []longhornclient.Snapshot {
filtered := []longhornclient.Snapshot{}
for _, snapshot := range snapshots {
if predicate(snapshot) {
filtered = append(filtered, snapshot)
}
}
return filtered
}
// filterSnapshotsWithLabel return snapshots that have LabelKey and LabelValue
func filterSnapshotsWithLabel(snapshots []longhornclient.Snapshot, labelKey, labelValue string) []longhornclient.Snapshot {
return filterSnapshots(snapshots, func(snapshot longhornclient.Snapshot) bool {
snapshotLabelValue, found := snapshot.Labels[labelKey]
return found && labelValue == snapshotLabelValue
})
}
// filterSnapshotsNotInTargets returns snapshots that are not in the Targets
func filterSnapshotsNotInTargets(snapshots []longhornclient.Snapshot, targets map[string]struct{}) []longhornclient.Snapshot {
return filterSnapshots(snapshots, func(snapshot longhornclient.Snapshot) bool {
if _, ok := targets[snapshot.Name]; !ok {
return true
}
return false
})
}
// filterExpiredItems returns a list of names from the input sts excluding the latest retainCount names
func filterExpiredItems(nts []NameWithTimestamp, retainCount int) []string {
sort.Slice(nts, func(i, j int) bool {
return nts[i].Timestamp.Before(nts[j].Timestamp)
})
ret := []string{}
for i := 0; i < len(nts)-retainCount; i++ {
ret = append(ret, nts[i].Name)
}
return ret
}
func snapshotsToNameWithTimestamps(snapshots []longhornclient.Snapshot) []NameWithTimestamp {
result := []NameWithTimestamp{}
for _, snapshot := range snapshots {
t, err := time.Parse(time.RFC3339, snapshot.Created)
if err != nil {
logrus.Errorf("Fail to parse datetime %v for snapshot %v",
snapshot.Created, snapshot.Name)
continue
}
result = append(result, NameWithTimestamp{
Name: snapshot.Name,
Timestamp: t,
})
}
return result
}
func snapshotsToNames(snapshots []longhornclient.Snapshot) []string {
result := []string{}
for _, snapshot := range snapshots {
result = append(result, snapshot.Name)
}
return result
}
func filterVolumesForJob(allowDetached bool, volumes []longhorn.Volume, filterNames *[]string) {
logger := logrus.StandardLogger()
for _, volume := range volumes {
// skip duplicates
if util.Contains(*filterNames, volume.Name) {
continue
}
if volume.Status.Robustness != types.VolumeRobustnessFaulted &&
(volume.Status.State == types.VolumeStateAttached || allowDetached) {
*filterNames = append(*filterNames, volume.Name)
continue
}
logger.Debugf("Cannot create job for %v volume in state %v", volume.Name, volume.Status.State)
}
}
func getVolumesBySelector(recurringJobType, recurringJobName, namespace string, client *lhclientset.Clientset) ([]longhorn.Volume, error) {
logger := logrus.StandardLogger()
label := fmt.Sprintf("%s=%s",
types.GetRecurringJobLabelKey(recurringJobType, recurringJobName), types.LonghornLabelValueEnabled)
logger.Debugf("Get volumes from label %v", label)
volumes, err := client.LonghornV1beta1().Volumes(namespace).List(context.TODO(), metav1.ListOptions{
LabelSelector: label,
})
if err != nil {
return nil, err
}
return volumes.Items, nil
}
func getSettingAsBoolean(name types.SettingName, namespace string, client *lhclientset.Clientset) (bool, error) {
obj, err := client.LonghornV1beta1().Settings(namespace).Get(context.TODO(), string(name), metav1.GetOptions{})
if err != nil {
return false, err
}
value, err := strconv.ParseBool(obj.Value)
if err != nil {
return false, err
}
return value, nil
}
func getLonghornClientset() (*lhclientset.Clientset, error) {
config, err := rest.InClusterConfig()
if err != nil {
return nil, errors.Wrap(err, "unable to get client config")
}
return lhclientset.NewForConfig(config)
}