Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Backport 1.12.x] fix(cmd): run does not expand camel properties #4202

Merged
merged 2 commits into from
Mar 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 14 additions & 24 deletions pkg/cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -700,11 +700,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 @@ -744,14 +744,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 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 @@ -761,30 +765,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 @@ -872,12 +859,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 @@ -710,3 +710,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}")
}