-
Notifications
You must be signed in to change notification settings - Fork 2
Better cron expressions #5
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -21,10 +21,10 @@ type RestartSchedule struct { | |||||||||||||||||
| Status RestartScheduleStatus `json:"status,omitempty"` | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| type RestartScheduleSpec struct { | ||||||||||||||||||
| // +kubebuilder:validation:Required | ||||||||||||||||||
| // +kubebuilder:validation:Pattern=`^(\d+|\*)(/\d+)?(\s+(\d+|\*)(/\d+)?){4}$` | ||||||||||||||||||
| Schedule string `json:"schedule"` | ||||||||||||||||||
| type RestartScheduleSpec struct { | ||||||||||||||||||
| // +kubebuilder:validation:Required | ||||||||||||||||||
| // +kubebuilder:validation:Pattern=`^((TZ=|CRON_TZ=)[A-Za-z/_]+(\s+))?(([0-9]+|\*)([-,/]([0-9]+|\*))*\s+){4}([0-9]+|\*)([-,/]([0-9]+|\*))*$` | ||||||||||||||||||
| Schedule string `json:"schedule"` | ||||||||||||||||||
|
Comment on lines
+24
to
+27
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There are a couple of issues on these lines:
Suggested change
|
||||||||||||||||||
|
|
||||||||||||||||||
| // +kubebuilder:validation:Required | ||||||||||||||||||
| TargetRef TargetRef `json:"targetRef"` | ||||||||||||||||||
|
|
||||||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,6 +2,7 @@ package controller | |
|
|
||
| import ( | ||
| "context" | ||
| "regexp" | ||
| "sync" | ||
| "testing" | ||
| "time" | ||
|
|
@@ -31,24 +32,46 @@ func TestCronScheduleValidation(t *testing.T) { | |
| _ = v1alpha1.AddToScheme(s) | ||
| _ = appsv1.AddToScheme(s) | ||
|
|
||
| validSchedule := "0 * * * *" | ||
| invalidSchedule := "invalid cron" | ||
|
|
||
| tests := []struct { | ||
| name string | ||
| schedule string | ||
| shouldError bool | ||
| }{ | ||
| { | ||
| name: "Valid schedule", | ||
| schedule: validSchedule, | ||
| schedule: "0 * * * *", | ||
| shouldError: false, | ||
| }, | ||
| { | ||
| name: "Invalid schedule", | ||
| schedule: invalidSchedule, | ||
| schedule: "invalid cron", | ||
| shouldError: true, | ||
| }, | ||
| { | ||
| name: "Step syntax - every 5 minutes", | ||
| schedule: "*/5 * * * *", | ||
| shouldError: false, | ||
| }, | ||
| { | ||
| name: "Step syntax - every 3 hours", | ||
| schedule: "0 */3 * * *", | ||
| shouldError: false, | ||
| }, | ||
| { | ||
| name: "Range syntax", | ||
| schedule: "0 9-17 * * *", | ||
| shouldError: false, | ||
| }, | ||
| { | ||
| name: "List syntax", | ||
| schedule: "0 9,12,15,18 * * *", | ||
| shouldError: false, | ||
| }, | ||
| { | ||
| name: "Range with step", | ||
| schedule: "0 0-23/2 * * *", | ||
| shouldError: false, | ||
| }, | ||
| } | ||
|
|
||
| for _, tt := range tests { | ||
|
|
@@ -63,6 +86,103 @@ func TestCronScheduleValidation(t *testing.T) { | |
| } | ||
| } | ||
|
|
||
| func TestCronRegexSchemaValidation(t *testing.T) { | ||
| // This is the regex from types.go | ||
| pattern := `^((TZ=|CRON_TZ=)[A-Za-z/_]+(\s+))?(([0-9]+|\*)([-,/]([0-9]+|\*))*\s+){4}([0-9]+|\*)([-,/]([0-9]+|\*))*$` | ||
| re, err := regexp.Compile(pattern) | ||
| assert.NoError(t, err) | ||
|
|
||
| tests := []struct { | ||
| name string | ||
| input string | ||
| should bool | ||
| }{ | ||
| // Valid expressions | ||
| { | ||
| name: "Basic cron expression", | ||
| input: "0 * * * *", | ||
| should: true, | ||
| }, | ||
| { | ||
| name: "Step syntax - every 5 minutes", | ||
| input: "*/5 * * * *", | ||
| should: true, | ||
| }, | ||
| { | ||
| name: "Step syntax - every 3 hours", | ||
| input: "0 */3 * * *", | ||
| should: true, | ||
| }, | ||
| { | ||
| name: "Range syntax", | ||
| input: "0 9-17 * * *", | ||
| should: true, | ||
| }, | ||
| { | ||
| name: "List syntax", | ||
| input: "0 9,12,15,18 * * *", | ||
| should: true, | ||
| }, | ||
| { | ||
| name: "Range with step", | ||
| input: "0 0-23/2 * * *", | ||
| should: true, | ||
| }, | ||
| { | ||
| name: "Every minute", | ||
| input: "* * * * *", | ||
| should: true, | ||
| }, | ||
| { | ||
| name: "With TZ prefix", | ||
| input: "TZ=UTC 0 2 * * *", | ||
| should: true, | ||
| }, | ||
| { | ||
| name: "With CRON_TZ prefix", | ||
| input: "CRON_TZ=America/New_York 0 2 * * *", | ||
| should: true, | ||
| }, | ||
| { | ||
| name: "With TZ and step syntax", | ||
| input: "TZ=Europe/London */5 * * * *", | ||
| should: true, | ||
| }, | ||
| { | ||
| name: "With CRON_TZ and complex expression", | ||
| input: "CRON_TZ=Asia/Tokyo 0 */3 1-15 * 1-5", | ||
| should: true, | ||
| }, | ||
| // Invalid expressions | ||
| { | ||
| name: "Missing fields", | ||
| input: "0 * * *", | ||
| should: false, | ||
| }, | ||
| { | ||
| name: "Too many fields", | ||
| input: "0 * * * * *", | ||
| should: false, | ||
| }, | ||
| { | ||
| name: "Invalid text", | ||
| input: "invalid cron expression", | ||
| should: false, | ||
| }, | ||
|
Comment on lines
+156
to
+171
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's great that you've added tests for the new regex. To make this test suite even more robust and to document the known limitations of the regex, consider adding test cases for cron strings that are structurally invalid but are currently passed by the regex. This will make it clear what the CRD validation allows vs. what the controller's parser will ultimately reject. For example: // ... inside tests slice
{
name: "Structurally invalid: multiple hyphens in one field",
input: "0 1-2-3 * * *",
should: true, // The regex currently accepts this, though cron parsers will not.
},
{
name: "Structurally invalid: multiple slashes in one field",
input: "*/2/3 * * * *",
should: true, // The regex currently accepts this, though cron parsers will not.
},These tests would pass with the current regex, correctly demonstrating its permissiveness and serving as documentation. |
||
| } | ||
|
|
||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| matches := re.MatchString(tt.input) | ||
| if tt.should { | ||
| assert.True(t, matches, "expected %q to match regex", tt.input) | ||
| } else { | ||
| assert.False(t, matches, "expected %q to NOT match regex", tt.input) | ||
| } | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestTargetNamespaceResolution(t *testing.T) { | ||
| s := runtime.NewScheme() | ||
| _ = v1alpha1.AddToScheme(s) | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The new regex pattern is a great improvement and covers many more valid cron expressions. However, it's a bit too permissive and may accept some invalid formats that the underlying
robfig/cronparser will reject. For example, a field like1-2-3would be accepted by this regex, but is not a valid cron field part. While the controller will eventually reject this, tightening the regex would provide users with earlier feedback fromkubectl apply. The part([-,/]([0-9]+|\*))*allows for repeated separators like-or/in a single field.