Skip to content

Commit

Permalink
fetch fullsnapshotinterval from the etcd spec fullsnapshotschedule
Browse files Browse the repository at this point in the history
  • Loading branch information
anveshreddy18 committed Jun 26, 2024
1 parent edaf8e9 commit 715260b
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 4 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ require (
github.com/prometheus/client_model v0.4.0 // indirect
github.com/prometheus/common v0.44.0 // indirect
github.com/prometheus/procfs v0.10.1 // indirect
github.com/robfig/cron v1.2.0 // indirect
github.com/robfig/cron/v3 v3.0.1 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/sirupsen/logrus v1.9.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,8 @@ github.com/prometheus/procfs v0.10.1 h1:kYK1Va/YMlutzCGazswoHKo//tZVlFpKYh+Pymzi
github.com/prometheus/procfs v0.10.1/go.mod h1:nwNm2aOCAYw8uTR/9bWRREkZFxAUcWzPHWJq+XBB/FM=
github.com/rakyll/gotest v0.0.6 h1:hBTqkO3jiuwYW/M9gL4bu0oTYcm8J6knQAAPUsJsz1I=
github.com/rakyll/gotest v0.0.6/go.mod h1:SkoesdNCWmiD4R2dljIUcfSnNdVZ12y8qK4ojDkc2Sc=
github.com/robfig/cron v1.2.0 h1:ZjScXvvxeQ63Dbyxy76Fj3AT3Ut0aKsyd2/tl3DTMuQ=
github.com/robfig/cron v1.2.0/go.mod h1:JGuDeoQd7Z6yL4zQhZ3OPEVHB7fL6Ka6skscFHfmt2k=
github.com/robfig/cron/v3 v3.0.1 h1:WdRxkvbJztn8LMz/QEvLN5sBU+xKpSqwwUO1Pjr4qDs=
github.com/robfig/cron/v3 v3.0.1/go.mod h1:eQICP3HwyT7UooqI/z+Ov+PtYAWygg1TEWWzGIFLtro=
github.com/rogpeppe/fastuuid v0.0.0-20150106093220-6724a57986af/go.mod h1:XWv6SoW27p1b0cqNHllgS5HIMJraePCO15w5zCzIWYg=
Expand Down
16 changes: 12 additions & 4 deletions internal/health/condition/check_backup_ready.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"time"

druidv1alpha1 "github.com/gardener/etcd-druid/api/v1alpha1"
"github.com/gardener/etcd-druid/internal/utils"
coordinationv1 "k8s.io/api/coordination/v1"

"k8s.io/apimachinery/pkg/types"
Expand Down Expand Up @@ -54,19 +55,26 @@ func (f *fullSnapshotBackupReadyCheck) Check(ctx context.Context, etcd druidv1al

//Fetch snapshot leases
var (
fullSnapErr error
fullSnapLease = &coordinationv1.Lease{}
fullSnapErr error
fullSnapLease = &coordinationv1.Lease{}
fullSnapshotInterval = 24 * time.Hour
err error
)
fullSnapErr = f.cl.Get(ctx, types.NamespacedName{Name: getFullSnapLeaseName(&etcd), Namespace: etcd.ObjectMeta.Namespace}, fullSnapLease)
fullLeaseRenewTime := fullSnapLease.Spec.RenewTime
fullLeaseCreationTime := fullSnapLease.ObjectMeta.CreationTimestamp

if etcd.Spec.Backup.FullSnapshotSchedule != nil {
if fullSnapshotInterval, err = utils.ComputeScheduleInterval(*etcd.Spec.Backup.FullSnapshotSchedule); err != nil {
return result
}
}
//Set status to Unknown if errors in fetching full snapshot lease
if fullSnapErr != nil {
return result
}
if fullLeaseRenewTime == nil {
if time.Since(fullLeaseCreationTime.Time) < 24*time.Hour {
if time.Since(fullLeaseCreationTime.Time) < fullSnapshotInterval {
return result
} else {
result.status = druidv1alpha1.ConditionFalse
Expand All @@ -75,7 +83,7 @@ func (f *fullSnapshotBackupReadyCheck) Check(ctx context.Context, etcd druidv1al
return result
}
} else {
if time.Since(fullLeaseRenewTime.Time) < 24*time.Hour {
if time.Since(fullLeaseRenewTime.Time) < fullSnapshotInterval {
result.status = druidv1alpha1.ConditionTrue
result.reason = SnapshotUploadedOnSchedule
result.message = fmt.Sprintf("Full snapshot uploaded successfully %v ago", time.Since(fullLeaseRenewTime.Time))
Expand Down
18 changes: 18 additions & 0 deletions internal/utils/miscellaneous.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ import (
"fmt"
"maps"
"strings"
"time"

"github.com/robfig/cron"
"sigs.k8s.io/controller-runtime/pkg/client"
)

Expand Down Expand Up @@ -76,3 +78,19 @@ func IfConditionOr[T any](condition bool, trueVal, falseVal T) T {
func PointerOf[T any](val T) *T {
return &val
}

// ComputeScheduleInterval computes the interval between two activations for the given cron schedule.
// Assumes that every cron activation is at equal intervals apart, based on cron schedules such as
// "once every X hours", "once every Y days", "at 1:00pm on every Tuesday", etc.
// TODO: write a new function to accurately compute the previous activation time from the cron schedule
// in order to compute when the previous activation of the cron schedule was supposed to have occurred,
// instead of relying on the assumption that all the cron activations are evenly spaced.
func ComputeScheduleInterval(cronSchedule string) (time.Duration, error) {
schedule, err := cron.ParseStandard(cronSchedule)
if err != nil {
return 0, err
}
nextScheduledTime := schedule.Next(time.Now())
nextNextScheduledTime := schedule.Next(nextScheduledTime)
return nextNextScheduledTime.Sub(nextScheduledTime), nil
}

0 comments on commit 715260b

Please sign in to comment.