This is an automated email from the ASF dual-hosted git repository. squakez pushed a commit to branch v3 in repository https://gitbox.apache.org/repos/asf/camel-k.git
commit 50f2c6b590638a28add85b6b8e3629421b1ed563 Author: 0z5a <[email protected]> AuthorDate: Sun Nov 15 14:21:50 2026 +0100 fix(trait): convert Quartz day-of-week numbers in cron schedules toKubernetesCronSchedule copied the day-of-week field of a Quartz expression (1-7 = SUN-SAT) into the CronJob schedule (0-6 = SUN-SAT), so numeric weekdays ran one day late and values such as 7, L or # produced schedules Kubernetes rejects. Shift days, ranges, lists and steps to the CronJob numbering, keep day names, and derive no CronJob when the field has no equivalent. Five-field expressions are unchanged. --- pkg/trait/cron.go | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++ pkg/trait/cron_test.go | 36 +++++++++++++++++++++++++++++++ 2 files changed, 94 insertions(+) diff --git a/pkg/trait/cron.go b/pkg/trait/cron.go index 5229a6707..38d66f9ac 100644 --- a/pkg/trait/cron.go +++ b/pkg/trait/cron.go @@ -50,6 +50,10 @@ const ( genericCronComponentFallbackScheme = "quartz" overriddenFromURI = "timer:camel-k-overridden-cron?delay=0&period=1&repeatCount=1" + + // Quartz numbers the days of the week from 1 (SUN) to 7 (SAT), + // while Kubernetes CronJob schedules use 0 (SUN) to 6 (SAT). + quartzFirstDayOfWeek = 1 ) type cronTrait struct { @@ -70,6 +74,9 @@ type cronExtractor func(string) (*cronInfo, error) var ( camelTimerPeriodMillis = regexp.MustCompile(`^[0-9]+$`) + cronStep = regexp.MustCompile(`^[1-9][0-9]*$`) + cronDayOfWeekNames = regexp.MustCompile(`(?i)^(SUN|MON|TUE|WED|THU|FRI|SAT)(-(SUN|MON|TUE|WED|THU|FRI|SAT))?$`) + quartzDayOfWeekNumbers = regexp.MustCompile(`^([1-7])(?:-([1-7]))?$`) supportedCamelComponents = map[string]cronExtractor{ "timer": timerToCronInfo, @@ -501,8 +508,11 @@ func cronEquivalent(cron1, cron2 string) bool { func toKubernetesCronSchedule(cron string) string { parts := strings.Split(cron, " ") + quartzFormat := false if len(parts) > 5 { + // Quartz format, starting with seconds + quartzFormat = true // drop seconds if they can be ignored if parts[0] == "0" { parts = parts[1:] @@ -517,6 +527,14 @@ func toKubernetesCronSchedule(cron string) string { } if len(parts) == 5 { + if quartzFormat { + dayOfWeek, ok := quartzToKubernetesDayOfWeek(parts[4]) + if !ok { + return "" + } + parts[4] = dayOfWeek + } + return strings.Join(parts, " ") } @@ -546,3 +564,43 @@ func (t *cronTrait) changeSourcesCronURI(e *Environment) (bool, error) { return anyRouteReplaced, nil } + +// quartzToKubernetesDayOfWeek converts a Quartz day-of-week field (1-7 or SUN-SAT) to the +// Kubernetes CronJob format (0-6 or SUN-SAT). It returns false if the field cannot be +// converted, e.g. when it uses Quartz specific characters such as "L" or "#". +func quartzToKubernetesDayOfWeek(field string) (string, bool) { + items := strings.Split(field, ",") + for i, item := range items { + base, step, hasStep := strings.Cut(item, "/") + if hasStep && !cronStep.MatchString(step) { + return "", false + } + if base == "*" || base == "?" || cronDayOfWeekNames.MatchString(base) { + continue + } + + days := quartzDayOfWeekNumbers.FindStringSubmatch(base) + if days == nil { + return "", false + } + converted := quartzToKubernetesDay(days[1]) + if days[2] != "" { + if days[2] < days[1] { + return "", false + } + converted += "-" + quartzToKubernetesDay(days[2]) + } + if hasStep { + converted += "/" + step + } + items[i] = converted + } + + return strings.Join(items, ","), true +} + +func quartzToKubernetesDay(day string) string { + value, _ := strconv.Atoi(day) + + return strconv.Itoa(value - quartzFirstDayOfWeek) +} diff --git a/pkg/trait/cron_test.go b/pkg/trait/cron_test.go index 19f30116b..ed1d4c132 100644 --- a/pkg/trait/cron_test.go +++ b/pkg/trait/cron_test.go @@ -144,6 +144,32 @@ func TestCronFromURI(t *testing.T) { { uri: "quartz:trigger?cron=0+0+0/4+*+*+?&fireNow=true", // invalid }, + { + uri: "quartz:trigger?cron=0+0+9+?+*+2", // Monday in Quartz + cron: "0 9 ? * 1", + components: "quartz", + }, + { + uri: "quartz:trigger?cron=0+0+9+?+*+2-6", // MON-FRI in Quartz + cron: "0 9 ? * 1-5", + components: "quartz", + }, + { + uri: "quartz:trigger?cron=0+0+9+?+*+1,7", // SUN and SAT in Quartz + cron: "0 9 ? * 0,6", + components: "quartz", + }, + { + uri: "quartz:trigger?cron=0+0+9+?+*+1/2", + cron: "0 9 ? * 0/2", + components: "quartz", + }, + { + uri: "quartz:trigger?cron=0+0+9+?+*+6L", // invalid + }, + { + uri: "quartz:trigger?cron=0+0+9+?+*+6%233", // invalid + }, // Cron only { @@ -169,6 +195,16 @@ func TestCronFromURI(t *testing.T) { cron: "0,6 0/4 * * MON-THU", components: "cron", }, + { + uri: "cron:tab?schedule=0+0+9+?+*+2-6", + cron: "0 9 ? * 1-5", + components: "cron", + }, + { + uri: "cron:tab?schedule=0 9 * * 1", + cron: "0 9 * * 1", + components: "cron", + }, { uri: "cron:tab?schedule=0+0+0/4+*+*+?+2020", // invalid },
