...
1
2
3
4
5
6
7
8
9
10
11
12
13
14 package ttlcontroller
15
16 import (
17 "context"
18 "time"
19
20 "github.com/chaos-mesh/chaos-mesh/pkg/core"
21
22 runtimeutil "k8s.io/apimachinery/pkg/util/runtime"
23 "k8s.io/apimachinery/pkg/util/wait"
24 ctrl "sigs.k8s.io/controller-runtime"
25 )
26
27 var (
28 log = ctrl.Log.WithName("database ttl controller")
29 )
30
31
32 type Controller struct {
33 experiment core.ExperimentStore
34 event core.EventStore
35 ttlconfig *TTLconfig
36 }
37
38
39 type TTLconfig struct {
40
41 DatabaseTTLResyncPeriod time.Duration
42
43 EventTTL time.Duration
44
45 ArchiveExperimentTTL time.Duration
46 }
47
48
49 func NewController(
50 experiment core.ExperimentStore,
51 event core.EventStore,
52 ttlc *TTLconfig,
53 ) *Controller {
54 return &Controller{
55 experiment: experiment,
56 event: event,
57 ttlconfig: ttlc,
58 }
59 }
60
61
62 func Register(c *Controller, controllerRuntimeStopCh <-chan struct{}) {
63 defer runtimeutil.HandleCrash()
64 log.Info("starting database TTL controller")
65 go wait.Until(c.runWorker, c.ttlconfig.DatabaseTTLResyncPeriod, controllerRuntimeStopCh)
66 }
67
68
69
70 func (c *Controller) runWorker() {
71 log.Info("deleting expired data from the database")
72 c.event.DeleteByCreateTime(context.Background(), c.ttlconfig.EventTTL)
73 c.experiment.DeleteByFinishTime(context.Background(), c.ttlconfig.ArchiveExperimentTTL)
74 }
75