diff --git a/pkg/cmd/run.go b/pkg/cmd/run.go index 1ce6e82393..88cc6a42d2 100644 --- a/pkg/cmd/run.go +++ b/pkg/cmd/run.go @@ -699,11 +699,11 @@ func (o *runCmdOptions) convertOptionsToTraits(cmd *cobra.Command, c client.Clie return err } - if err := o.applyProperties(c); err != nil { + if err := o.applyProperties(c, o.Properties, "camel.properties"); err != nil { return err } - if err := o.applyBuildProperties(c); err != nil { + if err := o.applyProperties(c, o.BuildProperties, "builder.properties"); err != nil { return err } @@ -743,14 +743,18 @@ func convertToTrait(value, traitParameter string) string { return fmt.Sprintf("%s=%s", traitParameter, value) } -func (o *runCmdOptions) applyProperties(c client.Client) error { - props, err := o.mergePropertiesWithPrecedence(c, o.Properties) +func (o *runCmdOptions) applyProperties(c client.Client, items []string, traitName string) error { + if items == nil || len(items) == 0 { + return nil + } + props, err := o.mergePropertiesWithPrecedence(c, items) if err != nil { return err } for _, key := range props.Keys() { - kv := fmt.Sprintf("%s=%s", key, props.GetString(key, "")) - propsTraits, err := o.convertToTraitParameter(c, kv, "camel.properties") + val, _ := props.Get(key) + kv := fmt.Sprintf("%s=%s", key, val) + propsTraits, err := o.convertToTraitParameter(c, kv, traitName) if err != nil { return err } @@ -760,30 +764,13 @@ func (o *runCmdOptions) applyProperties(c client.Client) error { return nil } -func (o *runCmdOptions) applyBuildProperties(c client.Client) error { - // convert each build configuration to a builder trait property - buildProps, err := o.mergePropertiesWithPrecedence(c, o.BuildProperties) - if err != nil { - return err - } - for _, key := range buildProps.Keys() { - kv := fmt.Sprintf("%s=%s", key, buildProps.GetString(key, "")) - buildPropsTraits, err := o.convertToTraitParameter(c, kv, "builder.properties") - if err != nil { - return err - } - o.Traits = append(o.Traits, buildPropsTraits...) - } - - return nil -} - func (o *runCmdOptions) convertToTraitParameter(c client.Client, value, traitParameter string) ([]string, error) { traits := make([]string, 0) props, err := o.extractProperties(c, value) if err != nil { return nil, err } + props.DisableExpansion = true for _, k := range props.Keys() { v, ok := props.Get(k) if ok { @@ -871,12 +858,15 @@ func (o *runCmdOptions) GetIntegrationName(sources []string) string { func (o *runCmdOptions) mergePropertiesWithPrecedence(c client.Client, items []string) (*properties.Properties, error) { loPrecedenceProps := properties.NewProperties() + loPrecedenceProps.DisableExpansion = true hiPrecedenceProps := properties.NewProperties() + hiPrecedenceProps.DisableExpansion = true for _, item := range items { prop, err := o.extractProperties(c, item) if err != nil { return nil, err } + prop.DisableExpansion = true // We consider file, secret and config map props to have a lower priority versus single properties if strings.HasPrefix(item, "file:") || strings.HasPrefix(item, "secret:") || strings.HasPrefix(item, "configmap:") { loPrecedenceProps.Merge(prop) diff --git a/pkg/cmd/run_test.go b/pkg/cmd/run_test.go index 04de9df311..5322319c80 100644 --- a/pkg/cmd/run_test.go +++ b/pkg/cmd/run_test.go @@ -709,3 +709,67 @@ func TestIntegrationServiceAccountName(t *testing.T) { assert.Nil(t, err) assert.Contains(t, output, "serviceAccountName: my-service-account") } + +func TestFileProperties(t *testing.T) { + var tmpFile1 *os.File + var err error + if tmpFile1, err = os.CreateTemp("", "camel-k-*.properties"); err != nil { + t.Error(err) + } + + assert.Nil(t, tmpFile1.Close()) + assert.Nil(t, os.WriteFile(tmpFile1.Name(), []byte(` + key=${value} + #key2=value2 + my.key=value + `), 0o400)) + + var tmpFile *os.File + if tmpFile, err = os.CreateTemp("", "camel-k-"); err != nil { + t.Error(err) + } + + assert.Nil(t, tmpFile.Close()) + assert.Nil(t, os.WriteFile(tmpFile.Name(), []byte(TestSrcContent), 0o400)) + _, runCmd, _ := initializeRunCmdOptionsWithOutput(t) + output, err := test.ExecuteCommand(runCmd, cmdRun, tmpFile.Name(), + "-p", "file:"+tmpFile1.Name(), + "-o", "yaml", + ) + assert.Nil(t, err) + assert.NotContains(t, output, "#key2") + assert.Contains(t, output, "my.key = value") + assert.Contains(t, output, "key = ${value}") +} + +func TestPropertyShouldNotExpand(t *testing.T) { + var tmpFile1 *os.File + var err error + if tmpFile1, err = os.CreateTemp("", "camel-k-*.properties"); err != nil { + t.Error(err) + } + + assert.Nil(t, tmpFile1.Close()) + assert.Nil(t, os.WriteFile(tmpFile1.Name(), []byte(` + key=${value} + `), 0o400)) + + var tmpFile *os.File + if tmpFile, err = os.CreateTemp("", "camel-k-"); err != nil { + t.Error(err) + } + + assert.Nil(t, tmpFile.Close()) + assert.Nil(t, os.WriteFile(tmpFile.Name(), []byte(TestSrcContent), 0o400)) + _, runCmd, _ := initializeRunCmdOptionsWithOutput(t) + output, err := test.ExecuteCommand(runCmd, cmdRun, tmpFile.Name(), + "-o", "yaml", + "-p", "runtimeProp=${value}", + "--build-property", "buildProp=${value}", + "-p", "file:"+tmpFile1.Name(), + ) + assert.Nil(t, err) + assert.Contains(t, output, "runtimeProp = ${value}") + assert.Contains(t, output, "buildProp = ${value}") + assert.Contains(t, output, "key = ${value}") +}