Skip to content

Commit

Permalink
fix: attempt multiple time formats when parsing triggerAt
Browse files Browse the repository at this point in the history
  • Loading branch information
adityathebe authored and moshloop committed Oct 8, 2024
1 parent 2170c60 commit 8c1d5c1
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 4 deletions.
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)
}
}
})
}
}

0 comments on commit 8c1d5c1

Please sign in to comment.