Skip to content
Open
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
4 changes: 2 additions & 2 deletions charts/restart-operator/Chart.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ apiVersion: v2
name: restart-operator
description: A Helm chart for Kubernetes restart-operator
type: application
version: 0.1.1-alpha1
version: 0.1.1-alpha2
appVersion: "0.1.0"
keywords:
- kubernetes
Expand All @@ -23,4 +23,4 @@ annotations:
artifacthub.io/crds: |
- kind: RestartSchedule
version: v1alpha1
name: restartschedules.restart-operator.k8s
name: restartschedules.restart-operator.k8s
2 changes: 1 addition & 1 deletion charts/restart-operator/templates/crd.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ spec:
schedule:
type: string
description: "Schedule in Cron format"
pattern: "^(\\d+|\\*)(/\\d+)?(\\s+(\\d+|\\*)(/\\d+)?){4}$"
pattern: "^((TZ=|CRON_TZ=)[A-Za-z/_]+(\\s+))?(([0-9]+|\\*)([-,/]([0-9]+|\\*))*\\s+){4}([0-9]+|\\*)([-,/]([0-9]+|\\*))*$"

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

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/cron parser will reject. For example, a field like 1-2-3 would 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 from kubectl apply. The part ([-,/]([0-9]+|\*))* allows for repeated separators like - or / in a single field.

targetRef:
type: object
required:
Expand Down
13 changes: 10 additions & 3 deletions config/samples/restartschedule_sample.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,16 @@ metadata:
name: deployment-restart-sample
namespace: default
spec:
# Every minute (5 fields standard format: minute, hour, day of month, month, day of week)
schedule: "* * * * *"
# Restart at 2 AM every day (5 fields standard format: minute, hour, day of month, month, day of week)
# Supported formats:
# - Basic: "0 2 * * *" (at 2 AM daily)
# - Step syntax: "*/5 * * * *" (every 5 minutes)
# - Ranges: "0 9-17 * * *" (hourly from 9 AM to 5 PM)
# - Lists: "0 9,12,15,18 * * *" (at 9 AM, noon, 3 PM, 6 PM)
# - Timezone: "TZ=America/New_York 0 2 * * *" (at 2 AM in New York timezone)
# - CRON_TZ: "CRON_TZ=UTC 0 2 * * *" (at 2 AM UTC)
schedule: "0 2 * * *"
targetRef:
kind: Deployment
name: example-deployment
# namespace: default # Optional, defaults to the same namespace as RestartSchedule
# namespace: default # Optional, defaults to the same namespace as RestartSchedule
8 changes: 4 additions & 4 deletions pkg/apis/v1alpha1/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

There are a couple of issues on these lines:

  1. Formatting: Line 24 is indented with a tab, which is inconsistent with Go's standard formatting (gofmt). The type keyword should not be indented, and fields within the struct should be indented consistently.
  2. Regex Permissiveness: The regex on line 26, while a good improvement, is a bit too permissive. It will accept some invalid cron strings (e.g., 1-2-3 in a field), which the controller's cron parser will later reject. This is the same issue as in the CRD definition.
Suggested change
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"`
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"`


// +kubebuilder:validation:Required
TargetRef TargetRef `json:"targetRef"`
Expand Down
130 changes: 125 additions & 5 deletions pkg/controller/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package controller

import (
"context"
"regexp"
"sync"
"testing"
"time"
Expand Down Expand Up @@ -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 {
Expand All @@ -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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

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)
Expand Down