Skip to content

Commit

Permalink
Merge pull request #73 from Kuadrant/GH-35
Browse files Browse the repository at this point in the history
GH-35 randomize validation time
  • Loading branch information
maleck13 authored Apr 15, 2024
2 parents 84f6a59 + f3e5c35 commit b5a7642
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 3 deletions.
21 changes: 21 additions & 0 deletions internal/common/helper.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package common

import (
"time"

"k8s.io/apimachinery/pkg/util/rand"
)

// RandomizeDuration randomizes duration for a given variance.
// variance is expected to be of a format 0.1 for 10%, 0.5 for 50% and so on
func RandomizeDuration(variance float64, duration time.Duration) time.Duration {
// we won't go smaller than a second - using milliseconds to have a relatively big number to randomize
millisecond := float64(duration.Milliseconds())

upperLimit := millisecond * (1.0 + variance)
lowerLimit := millisecond * (1.0 - variance)

return time.Millisecond * time.Duration(rand.Int63nRange(
int64(lowerLimit),
int64(upperLimit)))
}
41 changes: 41 additions & 0 deletions internal/common/helper_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package common

import (
"testing"
"time"
)

func TestRandomizeDuration(t *testing.T) {
testIterations := 100

tests := []struct {
name string
variance float64
duration time.Duration
}{
{
name: "returns valid duration in range",
variance: 0.5,
duration: time.Second * 5,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
i := 0
for i < testIterations {
if got := RandomizeDuration(tt.variance, tt.duration); !isValidVariance(tt.duration, got, tt.variance) {
t.Errorf("RandomizeDuration() invalid randomization; got = %v", got.String())
}
i++
}
})
}
}

func isValidVariance(duration, randomizedDuration time.Duration, variance float64) bool {
upperLimit := float64(duration.Milliseconds()) + float64(duration.Milliseconds())*variance
lowerLimmit := float64(duration.Milliseconds()) - float64(duration.Milliseconds())*variance

return float64(randomizedDuration.Milliseconds()) > lowerLimmit &&
float64(randomizedDuration.Milliseconds()) < upperLimit
}
12 changes: 9 additions & 3 deletions internal/controller/dnsrecord_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,19 @@ import (
externaldnsprovider "sigs.k8s.io/external-dns/provider"

"github.com/kuadrant/dns-operator/api/v1alpha1"
"github.com/kuadrant/dns-operator/internal/common"
externaldnsplan "github.com/kuadrant/dns-operator/internal/external-dns/plan"
"github.com/kuadrant/dns-operator/internal/provider"
)

const (
DNSRecordFinalizer = "kuadrant.io/dns-record"
DNSRecordFinalizer = "kuadrant.io/dns-record"
validationRequeueVariance = 0.5
)

var (
defaultRequeueTime time.Duration
validationRequeueTime = time.Second * 5
validationRequeueTime = time.Millisecond * 5000
noRequeueDuration = time.Duration(0)
validFor time.Duration
reconcileStart = metav1.Time{}
Expand All @@ -69,6 +71,10 @@ func (r *DNSRecordReconciler) Reconcile(ctx context.Context, req ctrl.Request) (
logger := log.FromContext(ctx)

reconcileStart = metav1.Now()

// randomize validation reconcile delay
validationRequeueTime = common.RandomizeDuration(validationRequeueVariance, validationRequeueTime)

previous := &v1alpha1.DNSRecord{}
err := r.Client.Get(ctx, client.ObjectKey{Namespace: req.Namespace, Name: req.Name}, previous)
if err != nil {
Expand Down Expand Up @@ -138,7 +144,7 @@ func (r *DNSRecordReconciler) updateStatus(ctx context.Context, previous, curren
if apierrors.IsConflict(updateError) {
return ctrl.Result{Requeue: true}, nil
}
return ctrl.Result{}, updateError
return ctrl.Result{RequeueAfter: requeueAfter}, updateError
}
return ctrl.Result{RequeueAfter: requeueAfter}, nil
}
Expand Down

0 comments on commit b5a7642

Please sign in to comment.