Skip to content

Commit

Permalink
Merge pull request #2 from TadasVainutis/feature/dynamic-timer-durati…
Browse files Browse the repository at this point in the history
…on-change

Changed log messages (about dynamic etcd backup timer changes) level …
  • Loading branch information
tvainutis authored Jan 29, 2020
2 parents 2a14f37 + 10a556a commit 3037b2f
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 16 deletions.
6 changes: 3 additions & 3 deletions pkg/controller/backup-operator/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func (b *Backup) run(ctx context.Context) {
}

func (b *Backup) onAdd(obj interface{}) {
b.logger.Infoln("------------------------------- onAdd ---------------------")
b.logger.Debugln("------------------------------- onAdd ---------------------")
key, err := cache.MetaNamespaceKeyFunc(obj)
if err != nil {
panic(err)
Expand All @@ -69,7 +69,7 @@ func (b *Backup) onAdd(obj interface{}) {
}

func (b *Backup) onUpdate(oldObj, newObj interface{}) {
b.logger.Infoln("------------------------------- onUpdate ---------------------")
b.logger.Debugln("------------------------------- onUpdate ---------------------")
key, err := cache.MetaNamespaceKeyFunc(newObj)
if err != nil {
panic(err)
Expand All @@ -78,7 +78,7 @@ func (b *Backup) onUpdate(oldObj, newObj interface{}) {
}

func (b *Backup) onDelete(obj interface{}) {
b.logger.Infoln("------------------------------- onDelete ---------------------")
b.logger.Debugln("------------------------------- onDelete ---------------------")
key, err := cache.DeletionHandlingMetaNamespaceKeyFunc(obj)
if err != nil {
panic(err)
Expand Down
26 changes: 13 additions & 13 deletions pkg/controller/backup-operator/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,23 +96,23 @@ func (b *Backup) processItem(key string) error {

var ticker *time.Ticker
var duration int64
b.logger.Infof("EtcdBackup name: %s", eb.Name)
b.logger.Debugf("EtcdBackup name: %s", eb.Name)
// Checking if etcdback status contains lastExecutionDate, if it doesn't then it meant that etcd periodic backup didn't fired already
if eb.Status.LastExecutionDate.IsZero() {
b.logger.Infoln("Calculating remaining periodic backup time, based on EtcdBackup crd creation date")
b.logger.Debugln("Calculating remaining periodic backup time, based on EtcdBackup crd creation date")

// duration = (Create date in seconds + backup interval in seconds) - current data in seconds
duration = int64(eb.CreationTimestamp.Time.Add(time.Duration(eb.Spec.BackupPolicy.BackupIntervalInSecond) * time.Second).Sub(time.Now()).Seconds())
ticker = time.NewTicker(
time.Duration(duration) * time.Second)
} else { // if it already exists and had some runs
b.logger.Infoln("Calculating remaining periodic backup time, based on EtcdBackup crd status lastExecutionDate")
} else { // if lastExecution already exists
b.logger.Debugln("Calculating remaining periodic backup time, based on EtcdBackup crd status lastExecutionDate")
currentDate := time.Now()
lastExec := eb.Status.LastExecutionDate.Time
timeDiff := int64(lastExec.Add(time.Duration(eb.Spec.BackupPolicy.BackupIntervalInSecond) * time.Second).Sub(currentDate).Seconds()) // Calculating new duration = (Create date + backup interval in seconds) - current date
b.logger.Infof("Statistics. Current date: %s \n", currentDate)
b.logger.Infof("LastExecutionDate: %s \n", lastExec)
b.logger.Infof("Time difference: %d \n", timeDiff)
b.logger.Debugf("Statistics. Current date: %s \n", currentDate)
b.logger.Debugf("LastExecutionDate: %s \n", lastExec)
b.logger.Debugf("Time difference: %d \n", timeDiff)
if timeDiff <= 0 {
duration = eb.Spec.BackupPolicy.BackupIntervalInSecond
ticker = time.NewTicker(
Expand All @@ -126,7 +126,7 @@ func (b *Backup) processItem(key string) error {
// Run new backup runner
ctx := context.Background()
ctx, cancel := context.WithCancel(ctx)
b.logger.Infof("Calculated Duration: %d \n", duration)
b.logger.Debugf("Calculated Duration: %d \n", duration)
go b.periodicRunnerFunc(ctx, ticker, eb, duration)

// Store cancel function for periodic
Expand All @@ -152,7 +152,7 @@ func (b *Backup) isChanged(eb *api.EtcdBackup) bool {
func (b *Backup) deletePeriodicBackupRunner(uid types.UID) bool {
backupRunner, exists := b.backupRunnerStore.Load(uid)
if exists {
b.logger.Infoln("--------------------------- Sending context kill signal to channel ---------------------")
b.logger.Debugln("--------------------------- Sending context kill signal to channel ---------------------")
backupRunner.(BackupRunner).cancelFunc()
b.backupRunnerStore.Delete(uid)
return true
Expand Down Expand Up @@ -201,7 +201,7 @@ func (b *Backup) periodicRunnerFunc(ctx context.Context, t *time.Ticker, eb *api
for {
select {
case <-ctx.Done():
b.logger.Infoln("--------------------- received context kill signal ---------------------")
b.logger.Debugln("--------------------- received context kill signal ---------------------")
return
case <-t.C:
var latestEb *api.EtcdBackup
Expand Down Expand Up @@ -233,9 +233,9 @@ func (b *Backup) periodicRunnerFunc(ctx context.Context, t *time.Ticker, eb *api

// If current duration of timer doesn't match expected duration that means we have to revert time to its old state
if currentDuration != latestEb.Spec.BackupPolicy.BackupIntervalInSecond {
b.logger.Infoln("------------------------------- Initializing new ticker ---------------------")
b.logger.Infof("Current timer duration: %d \n", currentDuration)
b.logger.Infof("Expected timer duration: %d \n", latestEb.Spec.BackupPolicy.BackupIntervalInSecond)
b.logger.Debugln("------------------------------- Initializing new ticker ---------------------")
b.logger.Debugf("Current timer duration: %d \n", currentDuration)
b.logger.Debugf("Expected timer duration: %d \n", latestEb.Spec.BackupPolicy.BackupIntervalInSecond)

t.Stop()
currentDuration = latestEb.Spec.BackupPolicy.BackupIntervalInSecond
Expand Down

0 comments on commit 3037b2f

Please sign in to comment.