From 94c60d024617b4533ad8a7119d7ae0b09fc07b6c Mon Sep 17 00:00:00 2001 From: Gaelle Fournier Date: Thu, 7 Mar 2024 16:12:52 +0100 Subject: [PATCH] fix(cron): Add documentation and test on multiple sources Closes #5158 --- .../ROOT/partials/apis/camel-k-crds.adoc | 2 +- docs/modules/traits/pages/cron.adoc | 2 +- pkg/apis/camel/v1/trait/cron.go | 2 +- pkg/trait/cron_test.go | 80 +++++++++++++++++++ 4 files changed, 83 insertions(+), 3 deletions(-) diff --git a/docs/modules/ROOT/partials/apis/camel-k-crds.adoc b/docs/modules/ROOT/partials/apis/camel-k-crds.adoc index e012f68257..49674b263a 100644 --- a/docs/modules/ROOT/partials/apis/camel-k-crds.adoc +++ b/docs/modules/ROOT/partials/apis/camel-k-crds.adoc @@ -6490,7 +6490,7 @@ require to be activated at specific hours of the day or with a periodic delay of For such tasks, the cron trait can materialize the integration as a Kubernetes CronJob instead of a standard deployment, in order to save resources when the integration does not need to be executed. -Integrations that start from the following components are evaluated by the cron trait: `timer`, `cron`, `quartz`. +Integrations that start from the following components are evaluated by the cron trait: `timer`, `cron`, `quartz`. The trait does support multiple evaluated components only if they have the same schedule, else it will fallback to Camel implementation instead of instanciating a Kubernetes CronJob. WARNING: In case of native build-mode defined in xref:traits:quarkus.adoc[quarkus] trait, the component can't be customized. diff --git a/docs/modules/traits/pages/cron.adoc b/docs/modules/traits/pages/cron.adoc index 2250e33057..6b5cbb0980 100755 --- a/docs/modules/traits/pages/cron.adoc +++ b/docs/modules/traits/pages/cron.adoc @@ -8,7 +8,7 @@ require to be activated at specific hours of the day or with a periodic delay of For such tasks, the cron trait can materialize the integration as a Kubernetes CronJob instead of a standard deployment, in order to save resources when the integration does not need to be executed. -Integrations that start from the following components are evaluated by the cron trait: `timer`, `cron`, `quartz`. +Integrations that start from the following components are evaluated by the cron trait: `timer`, `cron`, `quartz`. The trait does support multiple evaluated components only if they have the same schedule, else it will fallback to Camel implementation instead of instanciating a Kubernetes CronJob. WARNING: In case of native build-mode defined in xref:traits:quarkus.adoc[quarkus] trait, the component can't be customized. diff --git a/pkg/apis/camel/v1/trait/cron.go b/pkg/apis/camel/v1/trait/cron.go index 55f2eb4967..e1e1773491 100644 --- a/pkg/apis/camel/v1/trait/cron.go +++ b/pkg/apis/camel/v1/trait/cron.go @@ -24,7 +24,7 @@ package trait // For such tasks, the cron trait can materialize the integration as a Kubernetes CronJob instead of a standard deployment, // in order to save resources when the integration does not need to be executed. // -// Integrations that start from the following components are evaluated by the cron trait: `timer`, `cron`, `quartz`. +// Integrations that start from the following components are evaluated by the cron trait: `timer`, `cron`, `quartz`. The trait does support multiple evaluated components only if they have the same schedule, else it will fallback to Camel implementation instead of instanciating a Kubernetes CronJob. // // WARNING: In case of native build-mode defined in xref:traits:quarkus.adoc[quarkus] trait, the component can't be customized. // diff --git a/pkg/trait/cron_test.go b/pkg/trait/cron_test.go index c390b29e5e..13492bc588 100644 --- a/pkg/trait/cron_test.go +++ b/pkg/trait/cron_test.go @@ -298,6 +298,86 @@ func TestCronDeps(t *testing.T) { assert.Contains(t, environment.Integration.Status.Dependencies, "mvn:org.apache.camel.k:camel-k-cron") } +func TestCronMultipleScheduleFallback(t *testing.T) { + catalog, err := camel.DefaultCatalog() + assert.Nil(t, err) + + client, _ := test.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.IntegrationPhaseInitialization, + }, + Spec: v1.IntegrationSpec{ + Profile: v1.TraitProfileKnative, + Sources: []v1.SourceSpec{ + { + DataSpec: v1.DataSpec{ + Name: "routes.java", + Content: `from("cron:tab?schedule=0 0/2 * * ?").to("log:test")`, + }, + Language: v1.LanguageJavaSource, + }, + { + DataSpec: v1.DataSpec{ + Name: "routes2.java", + Content: `from("cron:tab?schedule=0 0/3 * * ?").to("log:test")`, + }, + Language: v1.LanguageJavaSource, + }, + }, + Traits: v1.Traits{}, + }, + }, + IntegrationKit: &v1.IntegrationKit{ + Status: v1.IntegrationKitStatus{ + Phase: v1.IntegrationKitPhaseReady, + }, + }, + Platform: &v1.IntegrationPlatform{ + Spec: v1.IntegrationPlatformSpec{ + Cluster: v1.IntegrationPlatformClusterOpenShift, + Build: v1.IntegrationPlatformBuildSpec{ + PublishStrategy: v1.IntegrationPlatformBuildPublishStrategyS2I, + Registry: v1.RegistrySpec{Address: "registry"}, + RuntimeVersion: catalog.Runtime.Version, + }, + Profile: v1.TraitProfileKnative, + }, + Status: v1.IntegrationPlatformStatus{ + Phase: v1.IntegrationPlatformPhaseReady, + }, + }, + EnvVars: make([]corev1.EnvVar, 0), + ExecutedTraits: make([]Trait, 0), + Resources: kubernetes.NewCollection(), + } + environment.Platform.ResyncStatusFullConfig() + + c, err := NewFakeClient("ns") + assert.Nil(t, err) + + tc := NewCatalog(c) + conditions, err := tc.apply(&environment) + + assert.Nil(t, err) + assert.Empty(t, conditions) + assert.NotEmpty(t, environment.ExecutedTraits) + + ct, _ := environment.GetTrait("cron").(*cronTrait) + assert.NotNil(t, ct) + assert.NotNil(t, ct.Fallback, "Should apply Fallback since non equivalent scheduling for mutiple cron compatible component") +} + func TestCronDepsFallback(t *testing.T) { catalog, err := camel.DefaultCatalog() require.NoError(t, err)