This repository has been archived by the owner on Jan 3, 2024. It is now read-only.
forked from longhorn/longhorn-manager
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrecurringjob.go
89 lines (76 loc) · 2.44 KB
/
recurringjob.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
package manager
import (
"reflect"
"sort"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"github.com/longhorn/longhorn-manager/datastore"
longhorn "github.com/longhorn/longhorn-manager/k8s/pkg/apis/longhorn/v1beta2"
"github.com/longhorn/longhorn-manager/util"
)
func (m *VolumeManager) GetRecurringJob(name string) (*longhorn.RecurringJob, error) {
return m.ds.GetRecurringJob(name)
}
func (m *VolumeManager) ListRecurringJobsSorted() ([]*longhorn.RecurringJob, error) {
jobMap, err := m.ds.ListRecurringJobs()
if err != nil {
return []*longhorn.RecurringJob{}, err
}
jobs := make([]*longhorn.RecurringJob, len(jobMap))
jobNames, err := util.SortKeys(jobMap)
if err != nil {
return []*longhorn.RecurringJob{}, err
}
for i, name := range jobNames {
jobs[i] = jobMap[name]
}
return jobs, nil
}
func (m *VolumeManager) CreateRecurringJob(spec *longhorn.RecurringJobSpec) (*longhorn.RecurringJob, error) {
name := util.AutoCorrectName(spec.Name, datastore.NameMaximumLength)
job := &longhorn.RecurringJob{
ObjectMeta: metav1.ObjectMeta{
Name: name,
},
Spec: *spec,
}
job, err := m.ds.CreateRecurringJob(job)
if err != nil {
return nil, err
}
logrus.Infof("Created recurring job %v", name)
return job, nil
}
func (m *VolumeManager) UpdateRecurringJob(spec longhorn.RecurringJobSpec) (*longhorn.RecurringJob, error) {
var err error
defer func() {
err = errors.Wrapf(err, "unable to update %v recurring job", spec.Name)
}()
recurringJob, err := m.ds.GetRecurringJob(spec.Name)
if err != nil {
return nil, errors.Wrapf(err, "failed to get recurring job %v", spec.Name)
}
sort.Strings(recurringJob.Spec.Groups)
sort.Strings(spec.Groups)
if recurringJob.Spec.Cron == spec.Cron &&
reflect.DeepEqual(recurringJob.Spec.Groups, spec.Groups) &&
recurringJob.Spec.Retain == spec.Retain &&
recurringJob.Spec.Concurrency == spec.Concurrency &&
reflect.DeepEqual(recurringJob.Spec.Labels, spec.Labels) {
return recurringJob, nil
}
recurringJob.Spec.Cron = spec.Cron
recurringJob.Spec.Groups = spec.Groups
recurringJob.Spec.Retain = spec.Retain
recurringJob.Spec.Concurrency = spec.Concurrency
recurringJob.Spec.Labels = spec.Labels
return m.ds.UpdateRecurringJob(recurringJob)
}
func (m *VolumeManager) DeleteRecurringJob(name string) error {
if err := m.ds.DeleteRecurringJob(name); err != nil {
return err
}
logrus.Infof("Deleted recurring job %v", name)
return nil
}