forked from percona/percona-server-mongodb-operator
/
backup.go
72 lines (63 loc) · 1.55 KB
/
backup.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
package backup
import (
"context"
"github.com/pkg/errors"
client "sigs.k8s.io/controller-runtime/pkg/client"
api "github.com/percona/percona-server-mongodb-operator/pkg/apis/psmdb/v1"
)
type JobType int
const (
TypeBackup JobType = iota
TypeRestore
)
type Job struct {
Name string
Type JobType
}
// HasActiveJobs returns true if there are running backups or restores
// in given cluster and namestpace
func HasActiveJobs(cl client.Client, cluster, namespace string, current Job) (bool, error) {
bcps := &api.PerconaServerMongoDBBackupList{}
err := cl.List(context.TODO(),
&client.ListOptions{
Namespace: namespace,
},
bcps,
)
if err != nil {
return false, errors.Wrap(err, "get backup list")
}
for _, b := range bcps.Items {
if b.Name == current.Name && current.Type == TypeBackup {
continue
}
if b.Spec.PSMDBCluster == cluster &&
b.Status.State != api.BackupStateReady &&
b.Status.State != api.BackupStateError &&
b.Status.State != api.BackupStateWaiting {
return true, nil
}
}
rstrs := &api.PerconaServerMongoDBRestoreList{}
err = cl.List(context.TODO(),
&client.ListOptions{
Namespace: namespace,
},
rstrs,
)
if err != nil {
return false, errors.Wrap(err, "get restore list")
}
for _, r := range rstrs.Items {
if r.Name == current.Name && current.Type == TypeRestore {
continue
}
if r.Spec.ClusterName == cluster &&
r.Status.State != api.RestoreStateReady &&
r.Status.State != api.RestoreStateError &&
r.Status.State != api.RestoreStateWaiting {
return true, nil
}
}
return false, nil
}