-
Notifications
You must be signed in to change notification settings - Fork 0
/
clustereventscleanup.go
62 lines (54 loc) · 1.66 KB
/
clustereventscleanup.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
package clusterevents
import (
"context"
"time"
"github.com/rancher/rancher/pkg/cluster/utils"
"github.com/rancher/types/apis/management.cattle.io/v3"
"github.com/rancher/types/config"
"github.com/sirupsen/logrus"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
)
const (
syncInterval = 1 * time.Hour
TTL = 24 * time.Hour
)
type Cleaner struct {
clusterEvents v3.ClusterEventInterface
clusterEventsLister v3.ClusterEventLister
}
func Register(ctx context.Context, management *config.ManagementContext) {
c := &Cleaner{
clusterEvents: management.Management.ClusterEvents(""),
clusterEventsLister: management.Management.ClusterEvents("").Controller().Lister(),
}
go c.sync(ctx, syncInterval)
}
func (c *Cleaner) sync(ctx context.Context, syncInterval time.Duration) {
for range utils.TickerContext(ctx, syncInterval) {
err := c.cleanup()
if err != nil {
logrus.Errorf("Error running cluster events cleanup thread %v", err)
}
}
}
func (c *Cleaner) cleanup() error {
logrus.Infof("Running cluster events cleanup")
events, err := c.clusterEventsLister.List("", labels.NewSelector())
if err != nil {
return err
}
for _, event := range events {
created := event.CreationTimestamp.Time
if time.Now().Sub(created) >= TTL {
logrus.Debugf("Cleaninig up cluster event %s", event.Message)
err := c.clusterEvents.Delete(event.Name, &metav1.DeleteOptions{})
if err != nil {
// just log the error, retry will happen as a part of the next run
logrus.Errorf("Error deleting cluster event %s: %v", event.Message, err)
}
}
}
logrus.Infof("Done running cluster events cleanup")
return nil
}