This is an automated email from the ASF dual-hosted git repository. pcongiusti pushed a commit to branch main in repository https://gitbox.apache.org/repos/asf/camel-k.git
commit 030c21d2020c350ff93ae41d78ef46ffd0229bf3 Author: Pasquale Congiusti <[email protected]> AuthorDate: Tue Jan 20 08:24:56 2026 +0100 fix(trait): don't remove yaml route parameter Closes #6392 --- pkg/trait/cron.go | 30 +++++++++++++++++++----------- pkg/util/source/inspector_yaml.go | 3 ++- pkg/util/source/inspector_yaml_test.go | 27 +++++++++++++++++++++++++++ 3 files changed, 48 insertions(+), 12 deletions(-) diff --git a/pkg/trait/cron.go b/pkg/trait/cron.go index 7c8abe152..87088b050 100644 --- a/pkg/trait/cron.go +++ b/pkg/trait/cron.go @@ -184,6 +184,8 @@ func (t *cronTrait) autoConfigure(e *Environment) error { } func (t *cronTrait) Apply(e *Environment) error { + replaced := false + var err error //nolint: nestif if e.IntegrationInPhase(v1.IntegrationPhaseInitialization) { util.StringSliceUniqueAdd(&e.Integration.Status.Capabilities, v1.CapabilityCron) @@ -198,7 +200,8 @@ func (t *cronTrait) Apply(e *Environment) error { return nil } // Will change the "from" URI in order to execute the task just once - if err := t.changeSourcesCronURI(e); err != nil { + replaced, err = t.changeSourcesCronURI(e) + if err != nil { return err } cronComponentArtifact := e.CamelCatalog.GetArtifactByScheme("timer") @@ -217,17 +220,19 @@ func (t *cronTrait) Apply(e *Environment) error { cronJob := t.getCronJobFor(e) e.Resources.Add(cronJob) + conditionMessage := "CronJob name is %s" + cronJob.Name + if replaced { + conditionMessage += "; notice that the routes \"from\" parameter were changed to " + + "\"" + overriddenFromURI + "\" in order to be able to trigger the Camel application as a CronJob." + } e.Integration.Status.SetCondition( v1.IntegrationConditionCronJobAvailable, corev1.ConditionTrue, v1.IntegrationConditionCronJobAvailableReason, - fmt.Sprintf( - "CronJob name is %s. Notice that the routes \"from\" parameter was changed to "+ - "\"%s\" in order to be able to trigger the Camel application as a CronJob.", - cronJob.Name, - overriddenFromURI, - )) + conditionMessage, + ) + } return nil @@ -530,17 +535,20 @@ func checkedStringToUint64(str string) uint64 { // changeSourcesCronURI is in charge to change the value of the from route with a component that executes // the workload just once. -func (t *cronTrait) changeSourcesCronURI(e *Environment) error { +func (t *cronTrait) changeSourcesCronURI(e *Environment) (bool, error) { + anyRouteReplaced := false for _, src := range e.Integration.AllSources() { dslInspector := source.InspectorForLanguage(e.CamelCatalog, src.InferLanguage()) replaced, err := dslInspector.ReplaceFromURI(&src, overriddenFromURI) + if err != nil { + return false, fmt.Errorf("wasn't able to replace cron uri trigger in source %s", src.Name) + } if replaced { + anyRouteReplaced = replaced // replace generated source e.Integration.Status.AddOrReplaceGeneratedSources(src) - } else if err != nil { - return fmt.Errorf("wasn't able to replace cron uri trigger in source %s", src.Name) } } - return nil + return anyRouteReplaced, nil } diff --git a/pkg/util/source/inspector_yaml.go b/pkg/util/source/inspector_yaml.go index e7e75099c..efb43114f 100644 --- a/pkg/util/source/inspector_yaml.go +++ b/pkg/util/source/inspector_yaml.go @@ -264,10 +264,11 @@ func (i YAMLInspector) ReplaceFromURI(source *v1.SourceSpec, newFromURI string) return false, nil } } - delete(from, "parameters") + // only replace those components that can be turned into a cron job oldURI, ok := from["uri"].(string) if ok && (strings.HasPrefix(oldURI, "timer") || strings.HasPrefix(oldURI, "cron") || strings.HasPrefix(oldURI, "quartz")) { from["uri"] = newFromURI + delete(from, "parameters") } } diff --git a/pkg/util/source/inspector_yaml_test.go b/pkg/util/source/inspector_yaml_test.go index af688756e..770da30a9 100644 --- a/pkg/util/source/inspector_yaml_test.go +++ b/pkg/util/source/inspector_yaml_test.go @@ -770,6 +770,33 @@ func TestYAMLFromWithMultiRouteReplaceURI(t *testing.T) { assert.Equal(t, expectedYamlFromMultiCronReplacement, sourceSpec.Content) } +const yamlNoChanges = `- from: + parameters: + host: some value + steps: + - to: direct:hello + uri: sftp +` + +func TestYAMLDontReplaceURI(t *testing.T) { + inspector := newTestYAMLInspector(t) + + sourceSpec := &v1.SourceSpec{ + DataSpec: v1.DataSpec{ + Name: "test.yaml", + Content: yamlNoChanges, + }, + } + replaced, err := inspector.ReplaceFromURI( + sourceSpec, + "direct:newURI?hello=world", + ) + assert.Nil(t, err) + assert.False(t, replaced) + // Assert no changes + assert.Equal(t, yamlNoChanges, sourceSpec.Content) +} + func TestYAMLRESTContractFirst(t *testing.T) { yamlContractFirst := ` - rest:
