Skip to content

Commit

Permalink
fix(cmd): run does not expand camel properties
Browse files Browse the repository at this point in the history
Closes #4163
  • Loading branch information
squakez committed Mar 30, 2023
1 parent f8dfa01 commit ed00e1a
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 24 deletions.
38 changes: 14 additions & 24 deletions pkg/cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -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
}
Expand All @@ -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 {
Expand Down Expand Up @@ -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)
Expand Down
64 changes: 64 additions & 0 deletions pkg/cmd/run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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}")
}

0 comments on commit ed00e1a

Please sign in to comment.