/
job_backup.go
161 lines (130 loc) · 3.91 KB
/
job_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
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
/*
Copyright 2018 Pressinfra SRL
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package mysqlbackupcron
import (
"context"
"fmt"
"sync"
"time"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/wait"
"sigs.k8s.io/controller-runtime/pkg/client"
api "github.com/presslabs/mysql-operator/pkg/apis/mysql/v1alpha1"
)
var (
// polling time for backup to be completed
backupPollingTime = 5 * time.Second
// time to wait for a backup to be completed
backupWatchTimeout = time.Hour
)
// The job structure contains the context to schedule a backup
type job struct {
Name string
Namespace string
BackupRunning *bool
lock *sync.Mutex
c client.Client
BackupScheduleJobsHistoryLimit *int
}
func (j job) Run() {
backupName := fmt.Sprintf("%s-auto-backup-%s", j.Name, time.Now().Format("2006-01-02t15-04-05"))
backupKey := types.NamespacedName{Name: backupName, Namespace: j.Namespace}
log.Info("scheduled backup job started", "namespace", j.Namespace, "name", backupName)
if j.BackupScheduleJobsHistoryLimit != nil {
defer j.backupGC()
}
// Wrap backup creation to ensure that lock is released when backup is
// created
created := func() bool {
j.lock.Lock()
defer j.lock.Unlock()
if *j.BackupRunning {
log.Info("last scheduled backup still running! Can't initiate a new backup",
"cluster", fmt.Sprintf("%s/%s", j.Namespace, j.Name))
return false
}
tries := 0
for {
var err error
cluster := &api.MysqlBackup{
ObjectMeta: metav1.ObjectMeta{
Name: backupName,
Namespace: j.Namespace,
Labels: map[string]string{
"recurrent": "true",
},
},
Spec: api.MysqlBackupSpec{
ClusterName: j.Name,
},
}
if err = j.c.Create(context.TODO(), cluster); err == nil {
break
}
if tries > 5 {
log.Error(err, "fail to create backup, max tries exeded",
"cluster", j.Name, "retries", tries, "backup", backupName)
return false
}
log.Info("failed to create backup, retring", "backup", backupName,
"error", err, "tries", tries)
time.Sleep(5 * time.Second)
tries++
}
*j.BackupRunning = true
return true
}()
if !created {
return
}
defer func() {
j.lock.Lock()
defer j.lock.Unlock()
*j.BackupRunning = false
}()
err := wait.PollImmediate(backupPollingTime, backupWatchTimeout, func() (bool, error) {
backup := &api.MysqlBackup{}
if err := j.c.Get(context.TODO(), backupKey, backup); err != nil {
log.Info("failed to get backup", "backup", backupName, "error", err)
return false, nil
}
if backup.Status.Completed {
log.Info("backup finished", "backup", backup)
return true, nil
}
return false, nil
})
if err != nil {
log.Error(err, "waiting for backup to finish, failed",
"backup", backupName, "cluster", fmt.Sprintf("%s/%s", j.Namespace, j.Name))
}
}
func (j *job) backupGC() {
var err error
backupsList := &api.MysqlBackupList{}
selector := &client.ListOptions{}
selector = selector.InNamespace(j.Namespace).MatchingLabels(map[string]string{"recurrent": "true"})
if err = j.c.List(context.TODO(), selector, backupsList); err != nil {
log.Error(err, "failed getting backups", "selector", selector)
return
}
for i, backup := range backupsList.Items {
if i > *j.BackupScheduleJobsHistoryLimit {
// delete the backup
if err = j.c.Delete(context.TODO(), &backup); err != nil {
log.Error(err, "failed to delete a backup", "backup", backup)
}
}
}
}