...

Source file src/github.com/chaos-mesh/chaos-mesh/pkg/ttlcontroller/ttlcontroller.go

Documentation: github.com/chaos-mesh/chaos-mesh/pkg/ttlcontroller

     1  // Copyright 2020 Chaos Mesh Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    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  // Controller defines the database ttl controller
    32  type Controller struct {
    33  	experiment core.ExperimentStore
    34  	event      core.EventStore
    35  	ttlconfig  *TTLconfig
    36  }
    37  
    38  // TTLconfig defines the ttl
    39  type TTLconfig struct {
    40  	// databaseTTLResyncPeriod defines the time interval to cleanup data in the database.
    41  	DatabaseTTLResyncPeriod time.Duration
    42  	// EventTTL defines the ttl of events
    43  	EventTTL time.Duration
    44  	// ArchiveExperimentTTL defines the ttl of archive experiments
    45  	ArchiveExperimentTTL time.Duration
    46  }
    47  
    48  // NewController returns a new database ttl controller
    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  // Register periodically calls function runWorker to delete the data.
    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  // runWorker is a long-running function that will call the
    69  // function in order to delete the events and archives.
    70  func (c *Controller) runWorker() {
    71  	log.Info("deleting expired data from the database")
    72  	c.event.DeleteByFinishTime(context.Background(), c.ttlconfig.EventTTL)
    73  	c.experiment.DeleteByFinishTime(context.Background(), c.ttlconfig.ArchiveExperimentTTL)
    74  }
    75