Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: frequency annotation helper #422

Merged
merged 7 commits into from
Oct 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion hauler-manifest.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,4 @@ metadata:
spec:
files:
- name: validatorctl
path: https://github.com/validator-labs/validatorctl/releases/download/v0.2.1/validator-linux-ARCH
path: https://github.com/validator-labs/validatorctl/releases/download/v0.2.2/validator-linux-ARCH
3 changes: 3 additions & 0 deletions pkg/constants/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,7 @@ const (

// ValidatorConfig is the name of the default validator config.
ValidatorConfig string = "validator-config"

// ReconciliationFrequencyAnnotation is the annotation key for reconciliation frequency.
ReconciliationFrequencyAnnotation = "validation.validator.labs/reconciliation-frequency"
)
31 changes: 31 additions & 0 deletions pkg/plugins/plugin_frequency.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package plugins

import (
"strconv"
"time"

"github.com/go-logr/logr"
"github.com/validator-labs/validator/pkg/constants"
ctrl "sigs.k8s.io/controller-runtime"
)

// FrequencyFromAnnotations calculates reconciliation frequency from annotations of the plugin custom resource.
// Defaults to 120 seconds if annotation is not found.
func FrequencyFromAnnotations(l logr.Logger, annotations map[string]string) ctrl.Result {
var frequency time.Duration
if secondsString, ok := annotations[constants.ReconciliationFrequencyAnnotation]; ok {
seconds, err := strconv.Atoi(secondsString)
if err != nil {
l.Error(err, "Failed to convert frequency annotation", "rescheduleSeconds", 120)
frequency = time.Second * 120
} else {
l.Info("Reconciliation frequency annotation found", "rescheduleSeconds", seconds)
frequency = time.Second * time.Duration(seconds)
}
} else {
l.Info("Frequency annotation not found: defaulting to 120 seconds")
frequency = time.Second * 120
}

return ctrl.Result{RequeueAfter: frequency}
}
55 changes: 55 additions & 0 deletions pkg/plugins/plugin_frequency_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package plugins

import (
"testing"
"time"

ctrl "sigs.k8s.io/controller-runtime"
)

func TestFrequencyFromAnnotations(t *testing.T) {

logger := ctrl.Log.WithName("TestFrequencyAnnotation")

testCases := []struct {
name string
annotations map[string]string
result ctrl.Result
}{
{
name: "CorrectAnnotationValue",
annotations: map[string]string{
"validation.validator.labs/reconciliation-frequency": "20",
},
result: ctrl.Result{
RequeueAfter: time.Duration(time.Second * 20),
},
},
{
name: "IncorrectAnnotationValue",
annotations: map[string]string{
"validation.validator.labs/reconciliation-frequency": "nonint",
},
result: ctrl.Result{
RequeueAfter: time.Duration(time.Second * 120),
},
},
{
name: "NoAnnotation",
annotations: map[string]string{},
result: ctrl.Result{
RequeueAfter: time.Duration(time.Second * 120),
},
},
}

for _, testCase := range testCases {
t.Run(testCase.name, func(t *testing.T) {
actual := FrequencyFromAnnotations(logger, testCase.annotations)
if actual != testCase.result {
t.Errorf("expected %v, got %v", testCase.result, actual)
}
})
}

}