-
Notifications
You must be signed in to change notification settings - Fork 1
/
reaper.go
112 lines (100 loc) · 2.7 KB
/
reaper.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
/*
Copyright 2019 The saiki Authors
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 reaper
import (
"context"
"time"
"github.com/trinchan/saiki/pkg/controller/policy"
"github.com/trinchan/saiki/pkg/controller/purger"
"github.com/trinchan/saiki/pkg/store"
"k8s.io/klog"
)
type Reaper interface {
Stop()
}
type Reap struct {
s store.Storer
policy policy.BackupPolicy
p purger.Purger
reapInterval *time.Ticker
ctx context.Context
}
func New(ctx context.Context, policy policy.BackupPolicy, s store.Storer, p purger.Purger, interval time.Duration) *Reap {
reapInterval := time.NewTicker(interval)
f := &Reap{
s: s,
policy: policy,
p: p,
reapInterval: reapInterval,
ctx: ctx,
}
go func() {
klog.Infof("starting reaper")
// TODO wait group to wait for flush all on Close()?
for range f.reapInterval.C {
if err := f.reap(ctx); err != nil {
reapErrorsCounter.Inc()
klog.Infof("error reaping: %v", err)
}
}
}()
return f
}
func (r *Reap) Stop() {
if r != nil && r.reapInterval != nil {
r.reapInterval.Stop()
}
}
func (r *Reap) reap(ctx context.Context) error {
reapStart := time.Now()
klog.Infof("reap cycle starting")
state, err := r.s.State(ctx, store.StateOptions{
AllNamespaces: true,
AllRevisions: true,
})
if err != nil {
return err
}
var revsToReap store.RevisionList
count := 0
for _, revisions := range state {
for i := range revisions {
count++
rev := revisions[i]
// never purge the last revision of an object unless it's deleted
if i == (len(revisions)-1) && !rev.Deleted() {
continue
}
rule := r.policy.RuleFor(rev)
if rule == nil {
klog.Warningf("no rule defined for revision - will not reap: %s", rev)
continue
}
reapThreshold := time.Now().Add(-rule.RPOPolicy.PurgeOlderThan.Duration())
if reapThreshold.After(rev.RevisionTimestamp()) {
revsToReap = append(revsToReap, rev)
}
}
}
defer func() {
klog.Infof("reaper processed %d objects and queued %d objects for purge in %s", count, len(revsToReap), time.Since(reapStart))
}()
if len(revsToReap) == 0 {
return nil
}
for _, rev := range revsToReap {
reapCounter.Inc()
r.p.Queue(rev)
}
return nil
}