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 b07472142 fix(trait): skip Quartz day-of-month syntax in cron schedules
b07472142 is described below
commit b07472142a339c224bac3d4f603d9c78f7d901ec
Author: Arpit Jain <[email protected]>
AuthorDate: Sun Nov 29 12:50:39 2026 -0500
fix(trait): skip Quartz day-of-month syntax in cron schedules
toKubernetesCronSchedule converts the day-of-week field of a Quartz
expression but copies the day-of-month field unchanged, so "L" for the
last day of the month and "W" for the nearest weekday reach the CronJob
schedule, which Kubernetes rejects. Check that field the same way and
derive no CronJob when it has no equivalent. Five-field expressions are
unchanged.
Co-Authored-By: Claude <[email protected]>
Signed-off-by: Arpit Jain <[email protected]>
---
pkg/trait/cron.go | 24 +++++++++++++-
pkg/trait/cron_test.go | 86 ++++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 109 insertions(+), 1 deletion(-)
diff --git a/pkg/trait/cron.go b/pkg/trait/cron.go
index cd0d6acc5..948d40967 100644
--- a/pkg/trait/cron.go
+++ b/pkg/trait/cron.go
@@ -77,6 +77,7 @@ var (
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]))?$`)
+ cronDayOfMonthNumbers =
regexp.MustCompile(`^([1-9]|[12][0-9]|3[01])(?:-([1-9]|[12][0-9]|3[01]))?$`)
supportedCamelComponents = map[string]cronExtractor{
"timer": timerToCronInfo,
@@ -524,7 +525,7 @@ func toKubernetesCronSchedule(cron string) string {
if len(parts) == 5 {
if quartzFormat {
dayOfWeek, ok := quartzToKubernetesDayOfWeek(parts[4])
- if !ok {
+ if !ok || !quartzDayOfMonthSupported(parts[2]) {
return ""
}
parts[4] = dayOfWeek
@@ -599,6 +600,27 @@ func quartzToKubernetesDayOfWeek(field string) (string,
bool) {
return strings.Join(items, ","), true
}
+// quartzDayOfMonthSupported reports whether a Quartz day-of-month field has a
Kubernetes
+// CronJob equivalent. Quartz accepts "L" for the last day of the month and
"W" for the
+// nearest weekday, on their own or combined, and the CronJob schedule parser
rejects both.
+// The numbering itself is the same in both formats, so nothing has to be
converted here.
+func quartzDayOfMonthSupported(field string) bool {
+ for item := range strings.SplitSeq(field, ",") {
+ base, step, hasStep := strings.Cut(item, "/")
+ if hasStep && !cronStep.MatchString(step) {
+ return false
+ }
+ if base == "*" || base == "?" {
+ continue
+ }
+ if !cronDayOfMonthNumbers.MatchString(base) {
+ return false
+ }
+ }
+
+ return true
+}
+
func quartzToKubernetesDay(day string) string {
value, _ := strconv.Atoi(day)
diff --git a/pkg/trait/cron_test.go b/pkg/trait/cron_test.go
index c4c5345be..fa1c1ed33 100644
--- a/pkg/trait/cron_test.go
+++ b/pkg/trait/cron_test.go
@@ -26,6 +26,7 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
+ appsv1 "k8s.io/api/apps/v1"
batchv1 "k8s.io/api/batch/v1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -170,6 +171,28 @@ func TestCronFromURI(t *testing.T) {
{
uri: "quartz:trigger?cron=0+0+9+?+*+6%233", // invalid
},
+ {
+ uri: "quartz:trigger?cron=0+0+9+15+*+%3F",
+ cron: "0 9 15 * ?",
+ components: "quartz",
+ },
+ {
+ uri: "quartz:trigger?cron=0+0+9+1,15+*+%3F",
+ cron: "0 9 1,15 * ?",
+ components: "quartz",
+ },
+ {
+ uri: "quartz:trigger?cron=0+0+9+L+*+%3F", // invalid
+ },
+ {
+ uri: "quartz:trigger?cron=0+0+9+LW+*+%3F", // invalid
+ },
+ {
+ uri: "quartz:trigger?cron=0+0+9+15W+*+%3F", // invalid
+ },
+ {
+ uri: "quartz:trigger?cron=0+0+9+L-3+*+%3F", // invalid
+ },
// Cron only
{
@@ -695,6 +718,69 @@ func TestCronWithTimeZone(t *testing.T) {
assert.EqualValues(t, *cronJob.Spec.TimeZone, "America/Sao_Paulo")
}
+func TestCronQuartzDayOfMonthWithoutKubernetesEquivalent(t *testing.T) {
+ catalog, err := camel.DefaultCatalog()
+ require.NoError(t, err)
+
+ client, _ := internal.NewFakeClient()
+ traitCatalog := NewCatalog(nil)
+
+ environment := Environment{
+ CamelCatalog: catalog,
+ Catalog: traitCatalog,
+ Client: client,
+ Integration: &v1.Integration{
+ ObjectMeta: metav1.ObjectMeta{
+ Name: "test",
+ Namespace: "ns",
+ },
+ Status: v1.IntegrationStatus{
+ Phase: v1.IntegrationPhaseDeploying,
+ },
+ Spec: v1.IntegrationSpec{
+ Sources: []v1.SourceSpec{
+ {
+ DataSpec: v1.DataSpec{
+ Name: "routes.java",
+ // L is the last day of
the month in Quartz and has no CronJob equivalent.
+ Content:
`from("quartz:trigger?cron=0 0 9 L * ?").to("log:test")`,
+ },
+ Language: v1.LanguageJavaSource,
+ },
+ },
+ Traits: v1.Traits{},
+ },
+ },
+ IntegrationKit: &v1.IntegrationKit{
+ Status: v1.IntegrationKitStatus{
+ Phase: v1.IntegrationKitPhaseReady,
+ },
+ },
+ Platform: pl,
+ EnvVars: make([]corev1.EnvVar, 0),
+ ExecutedTraits: make([]Trait, 0),
+ Resources: kubernetes.NewCollection(),
+ }
+ c, err := newFakeClient("ns")
+ require.NoError(t, err)
+
+ tc := NewCatalog(c)
+ _, _, err = tc.apply(&environment)
+ require.NoError(t, err)
+ assert.NotEmpty(t, environment.ExecutedTraits)
+
+ ct, _ := environment.GetTrait("cron").(*cronTrait)
+ assert.Nil(t, ct, "cron trait should not run for a schedule Kubernetes
cannot express")
+
+ cronJob := environment.Resources.GetCronJob(func(job *batchv1.CronJob)
bool { return true })
+ if cronJob != nil {
+ t.Fatalf("expected no CronJob, got schedule %q",
cronJob.Spec.Schedule)
+ }
+
+ deployment := environment.Resources.GetDeployment(func(d
*appsv1.Deployment) bool { return true })
+ assert.NotNil(t, deployment, "the integration should fall back to a
Deployment")
+}
+
func TestCronAuto(t *testing.T) {
catalog, err := camel.DefaultCatalog()
require.NoError(t, err)