/
backup_handler.go
311 lines (275 loc) · 9.39 KB
/
backup_handler.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
package main
import (
"context"
"fmt"
"time"
"github.com/cenkalti/backoff"
cbv1alpha1 "github.com/ryo-watanabe/k8s-snap/pkg/apis/clustersnapshot/v1alpha1"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/runtime"
"k8s.io/client-go/tools/cache"
"k8s.io/klog"
//"github.com/ryo-watanabe/k8s-snap/pkg/cluster"
//"github.com/ryo-watanabe/k8s-snap/pkg/objectstore"
)
// runWorker is a long-running function that will continually call the
// processNextWorkItem function in order to read and process a message on the
// workqueue.
func (c *Controller) runSnapshotWorker() {
for c.processNextSnapshotItem(false) {
}
}
func (c *Controller) runSnapshotQueuer() {
for c.processNextSnapshotItem(true) {
}
}
// processNextWorkItem will read a single work item off the workqueue and
// attempt to process it, by calling the syncHandler.
func (c *Controller) processNextSnapshotItem(queueonly bool) bool {
// Process snapshot queue
obj, shutdown := c.snapshotQueue.Get()
if shutdown {
return false
}
err := func(obj interface{}) error {
defer c.snapshotQueue.Done(obj)
var key string
var ok bool
if key, ok = obj.(string); !ok {
c.snapshotQueue.Forget(obj)
runtime.HandleError(fmt.Errorf("expected string in workqueue but got %#v", obj))
return nil
}
if err := c.snapshotSyncHandler(key, queueonly); err != nil {
c.snapshotQueue.AddRateLimited(key)
return fmt.Errorf("error syncing '%s': %s, requeuing", key, err.Error())
}
c.snapshotQueue.Forget(obj)
klog.V(4).Infof("Successfully synced '%s'", key)
return nil
}(obj)
if err != nil {
runtime.HandleError(err)
return true
}
return true
}
func retryNotify(err error, wait time.Duration) {
klog.Infof("Retrying after %.2f seconds with error : %s", wait.Seconds(), err.Error())
}
// snapshotSyncHandler compares the actual state with the desired, and attempts to
// converge the two. It then updates the Status block of the Snapshot resource
// with the current status of the resource.
func (c *Controller) snapshotSyncHandler(key string, queueonly bool) error {
//getOptions := metav1.GetOptions{IncludeUninitialized: false}
// context for snapshot
ctx := context.TODO()
// Convert the namespace/name string into a distinct namespace and name
namespace, name, err := cache.SplitMetaNamespaceKey(key)
if err != nil {
runtime.HandleError(fmt.Errorf("invalid resource key: %s", key))
return nil
}
// Get the Snapshot resource with this namespace/name.
snapshot, err := c.snapshotLister.Snapshots(namespace).Get(name)
if err != nil {
if errors.IsNotFound(err) {
// When deleting a snapshot, exit sync handler here.
return nil
}
return err
}
// controller stopped wwhile taking the snapshot
if snapshot.Status.Phase == "InProgress" {
// check timestamp just in case
retryend := metav1.NewTime(snapshot.ObjectMeta.CreationTimestamp.Add(
time.Duration(c.maxretryelapsedsec+1) * time.Second))
nowTime := metav1.NewTime(time.Now())
if retryend.Before(&nowTime) {
snapshot, err = c.updateSnapshotStatus(ctx, snapshot, "Failed", "Controller stopped while taking the snapshot")
if err != nil {
return err
}
}
}
// do snapshot
if !queueonly && snapshot.Status.Phase == "InQueue" {
snapshot, err = c.updateSnapshotStatus(ctx, snapshot, "InProgress", "")
if err != nil {
return err
}
// bucket
bucket, err := c.getBucket(ctx, c.namespace, snapshot.Spec.ObjectstoreConfig,
c.kubeclientset, c.cbclientset, c.insecure)
if err != nil {
snapshot, err = c.updateSnapshotStatus(ctx, snapshot, "Failed", err.Error())
if err != nil {
return err
}
return nil
}
klog.Infof("- Objectstore Config name:%s endpoint:%s bucket:%s",
bucket.GetName(), bucket.GetEndpoint(), bucket.GetBucketName())
// do snapshot with backoff retry
b := backoff.NewExponentialBackOff()
b.MaxElapsedTime = time.Duration(c.maxretryelapsedsec) * time.Second
b.RandomizationFactor = 0.2
b.Multiplier = 2.0
b.InitialInterval = 2 * time.Second
operationSnapshot := func() error {
return c.clusterCmd.Snapshot(ctx, snapshot)
}
err = backoff.RetryNotify(operationSnapshot, b, retryNotify)
if err != nil {
snapshot, err = c.updateSnapshotStatus(ctx, snapshot, "Failed", err.Error())
if err != nil {
return err
}
return nil
}
// upload snapshot with backoff retry
b.Reset()
operationUpload := func() error {
return c.clusterCmd.UploadSnapshot(snapshot, bucket)
}
err = backoff.RetryNotify(operationUpload, b, retryNotify)
if err != nil {
snapshot, err = c.updateSnapshotStatus(ctx, snapshot, "Failed", err.Error())
if err != nil {
return err
}
return nil
}
snapshot, err = c.updateSnapshotStatus(ctx, snapshot, "Completed", "")
if err != nil {
return err
}
}
nowTime := metav1.NewTime(time.Now())
// initialize
if snapshot.Status.Phase == "" {
// Check AvailableUntil
if snapshot.Spec.AvailableUntil.IsZero() {
// Check TTL string
if snapshot.Spec.TTL.Duration == 0 {
snapshot.Spec.TTL.Duration = 24 * 30 * time.Hour
}
} else if snapshot.Spec.AvailableUntil.Before(&nowTime) {
snapshot, err = c.updateSnapshotStatus(ctx, snapshot, "Failed", "AvailableUntil is set as past.")
if err != nil {
return err
}
// When the snapshot failed, exit sync handler here.
return nil
}
snapshot, err = c.updateSnapshotStatus(ctx, snapshot, "InQueue", "")
if err != nil {
return err
}
}
// expiration for failed snapshot
if snapshot.Status.Phase == "Failed" && snapshot.Status.AvailableUntil.IsZero() {
if !snapshot.Spec.AvailableUntil.IsZero() {
snapshot.Status.AvailableUntil = snapshot.Spec.AvailableUntil
snapshot.Status.TTL.Duration = snapshot.Status.AvailableUntil.Time.Sub(snapshot.ObjectMeta.CreationTimestamp.Time)
} else {
snapshot.Status.AvailableUntil = metav1.NewTime(
snapshot.ObjectMeta.CreationTimestamp.Add(snapshot.Spec.TTL.Duration))
snapshot.Status.TTL = snapshot.Spec.TTL
}
snapshot, err = c.updateSnapshotStatus(ctx, snapshot, snapshot.Status.Phase, snapshot.Status.Reason)
if err != nil {
return err
}
}
// expiration edited
if snapshot.Status.Phase == "Completed" || snapshot.Status.Phase == "Failed" {
if !snapshot.Spec.AvailableUntil.IsZero() && !snapshot.Spec.AvailableUntil.Equal(&snapshot.Status.AvailableUntil) {
snapshot.Status.AvailableUntil = snapshot.Spec.AvailableUntil
snapshot, err = c.updateSnapshotStatus(ctx, snapshot, snapshot.Status.Phase, snapshot.Status.Reason)
if err != nil {
return err
}
}
}
// delete expired
if !snapshot.Status.AvailableUntil.IsZero() && snapshot.Status.AvailableUntil.Before(&nowTime) {
err := c.cbclientset.ClustersnapshotV1alpha1().Snapshots(c.namespace).Delete(ctx, name, metav1.DeleteOptions{})
if err != nil {
snapshot, err = c.updateSnapshotStatus(ctx, snapshot, "Failed", err.Error())
if err != nil {
return err
}
}
klog.Infof("snapshot:%s expired - deleted", name)
// When the snapshot deleted, exit sync handler here.
return nil
}
c.recorder.Event(snapshot, corev1.EventTypeNormal, "Synced", "Snapshot synced successfully")
return nil
}
func (c *Controller) updateSnapshotStatus(ctx context.Context, snapshot *cbv1alpha1.Snapshot,
phase, reason string) (*cbv1alpha1.Snapshot, error) {
snapshotCopy := snapshot.DeepCopy()
snapshotCopy.Status.Phase = phase
snapshotCopy.Status.Reason = reason
klog.Infof("snapshot:%s status %s => %s : %s", snapshot.ObjectMeta.Name, snapshot.Status.Phase, phase, reason)
snapshot, err := c.cbclientset.ClustersnapshotV1alpha1().Snapshots(snapshot.Namespace).Update(
ctx, snapshotCopy, metav1.UpdateOptions{})
if err != nil {
return snapshot, fmt.Errorf("Failed to update snapshot status for %s : %s", snapshot.ObjectMeta.Name, err.Error())
}
return snapshot, err
}
// enqueueSnapshot takes a Snapshot resource and converts it into a namespace/name
// string which is then put onto the work queue. This method should *not* be
// passed resources of any type other than Snapshot.
func (c *Controller) enqueueSnapshot(obj interface{}) {
var key string
var err error
//klog.Info("snapshot enqueued : %#v", obj)
// queue only snapshots in our namespace
meta, err := meta.Accessor(obj)
if err != nil {
runtime.HandleError(fmt.Errorf("object has no meta: %v", err))
return
}
if meta.GetNamespace() != c.namespace {
return
}
if key, err = cache.MetaNamespaceKeyFunc(obj); err != nil {
runtime.HandleError(err)
return
}
c.snapshotQueue.AddRateLimited(key)
}
// Delete snapshot files on objectstore when Snapshot resource deleted
func (c *Controller) deleteSnapshot(obj interface{}) {
// convert object into Snapshot and get info for deleting
snapshot, ok := obj.(*cbv1alpha1.Snapshot)
if !ok {
klog.Warningf("Delete snapshot: Invalid object passed: %#v", obj)
return
}
// delete only snapshots in our namespace
if snapshot.ObjectMeta.GetNamespace() != c.namespace {
return
}
// context for delete snapshot
ctx := context.TODO()
bucket, err := c.getBucket(ctx, c.namespace, snapshot.Spec.ObjectstoreConfig,
c.kubeclientset, c.cbclientset, c.insecure)
if err != nil {
runtime.HandleError(err)
return
}
// Delete snapshot data.
klog.Infof("Deleting snapshot %s data from objectstore %s", snapshot.ObjectMeta.Name, snapshot.Spec.ObjectstoreConfig)
err = bucket.Delete(snapshot.ObjectMeta.Name + ".tgz")
if err != nil {
runtime.HandleError(err)
}
}