Skip to content

Commit

Permalink
fix(cron): Add documentation and test on multiple sources
Browse files Browse the repository at this point in the history
  • Loading branch information
gansheer committed Mar 7, 2024
1 parent 727e295 commit 94c60d0
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 3 deletions.
2 changes: 1 addition & 1 deletion docs/modules/ROOT/partials/apis/camel-k-crds.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion docs/modules/traits/pages/cron.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
2 changes: 1 addition & 1 deletion pkg/apis/camel/v1/trait/cron.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
//
Expand Down
80 changes: 80 additions & 0 deletions pkg/trait/cron_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 94c60d0

Please sign in to comment.