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

fix: attempt multiple time formats when parsing triggerAt #2205

Merged
merged 1 commit 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
10 changes: 6 additions & 4 deletions pkg/controllers/canary_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@ package controllers

import (
gocontext "context"
"fmt"
"time"

"github.com/flanksource/canary-checker/pkg/db"
"github.com/flanksource/canary-checker/pkg/utils"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

Expand Down Expand Up @@ -121,12 +123,12 @@ func (r *CanaryReconciler) Reconcile(parentCtx gocontext.Context, req ctrl.Reque
patch := client.MergeFrom(canaryForStatus.DeepCopy())

if val, ok := canary.Annotations["next-runtime"]; ok {
runAt, err := time.Parse(time.RFC3339, val)
if err != nil {
return ctrl.Result{}, err
runAt := utils.ParseTime(val)
if runAt == nil {
return ctrl.Result{}, fmt.Errorf("invalid next-runtime: %s", val)
}

if err := canaryJobs.TriggerAt(ctx, *dbCanary, runAt); err != nil {
if err := canaryJobs.TriggerAt(ctx, *dbCanary, *runAt); err != nil {
return ctrl.Result{Requeue: true, RequeueAfter: 2 * time.Minute}, err
}

Expand Down
20 changes: 20 additions & 0 deletions pkg/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,3 +152,23 @@ func FreePort() int {
address := listener.Addr().(*net.TCPAddr)
return address.Port
}

func ParseTime(t string) *time.Time {
formats := []string{
time.RFC3339,
time.RFC3339Nano,
time.ANSIC,
time.DateTime,
"2006-01-02T15:04:05", // ISO8601 without timezone
"2006-01-02 15:04:05", // MySQL datetime format
}

for _, format := range formats {
parsed, err := time.Parse(format, t)
if err == nil {
return &parsed
}
}

return nil
}
64 changes: 64 additions & 0 deletions pkg/utils/utils_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package utils

import (
"testing"
"time"

"github.com/samber/lo"
)

func TestParseTime(t *testing.T) {
testCases := []struct {
name string
input string
expected *time.Time
}{
{
name: "RFC3339",
input: "2023-04-05T15:04:05Z",
expected: lo.ToPtr(time.Date(2023, 4, 5, 15, 4, 5, 0, time.UTC)),
},
{
name: "RFC3339Nano",
input: "2023-04-05T15:04:05.999999999Z",
expected: lo.ToPtr(time.Date(2023, 4, 5, 15, 4, 5, 999999999, time.UTC)),
},
{
name: "ISO8601 with timezone",
input: "2023-04-05T15:04:05+02:00",
expected: lo.ToPtr(time.Date(2023, 4, 5, 15, 4, 5, 0, time.FixedZone("", 2*60*60))),
},
{
name: "ISO8601 without timezone",
input: "2023-04-05T15:04:05",
expected: lo.ToPtr(time.Date(2023, 4, 5, 15, 4, 5, 0, time.UTC)),
},
{
name: "MySQL datetime format",
input: "2023-04-05 15:04:05",
expected: lo.ToPtr(time.Date(2023, 4, 5, 15, 4, 5, 0, time.UTC)),
},
{
name: "Invalid format",
input: "not a valid time",
expected: nil,
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
result := ParseTime(tc.input)
if tc.expected == nil {
if result != nil {
t.Errorf("Expected nil, but got %v", result)
}
} else {
if result == nil {
t.Errorf("Expected %v, but got nil", tc.expected)
} else if !result.Equal(*tc.expected) {
t.Errorf("Expected %v, but got %v", tc.expected, result)
}
}
})
}
}
Loading