This is an automated email from the ASF dual-hosted git repository.

squakez pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel-k.git


The following commit(s) were added to refs/heads/main by this push:
     new faaa10747 fix(trait): convert Quartz day-of-week numbers in cron 
schedules
faaa10747 is described below

commit faaa10747f9220bc6331b36e368bec100abd2563
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 d52435f0a..cd0d6acc5 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
 
 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,
@@ -496,8 +503,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:]
@@ -512,6 +522,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 675e52a80..c4c5345be 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
                },

Reply via email to