Skip to content

Commit

Permalink
Fix #2361: refactor code
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolaferraro authored and orpiske committed Jun 11, 2021
1 parent 8f5248a commit f892f2c
Show file tree
Hide file tree
Showing 7 changed files with 120 additions and 49 deletions.
24 changes: 5 additions & 19 deletions pkg/cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ limitations under the License.
package cmd

import (
"bytes"
"context"
"encoding/json"
"fmt"
Expand All @@ -31,6 +30,7 @@ import (
"strings"
"syscall"

"github.com/apache/camel-k/pkg/util/property"
"github.com/magiconair/properties"
"github.com/mitchellh/mapstructure"
"github.com/pkg/errors"
Expand Down Expand Up @@ -585,9 +585,9 @@ func (o *runCmdOptions) updateIntegrationCode(c client.Client, sources []string,
return nil, err
}
for _, k := range props.Keys() {
_, ok := props.Get(k)
v, ok := props.Get(k)
if ok {
entry, err := toPropertyEntry(props, k)
entry, err := property.EncodePropertyFileEntry(k, v)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -793,7 +793,8 @@ func isLocalAndFileExists(fileName string) (bool, error) {

func addIntegrationProperties(props *properties.Properties, spec *v1.IntegrationSpec) error {
for _, k := range props.Keys() {
entry, err := toPropertyEntry(props, k)
v, _ := props.Get(k)
entry, err := property.EncodePropertyFileEntry(k, v)
if err != nil {
return err
}
Expand All @@ -817,21 +818,6 @@ func loadPropertyFile(fileName string) (*properties.Properties, error) {
return p, nil
}

func toPropertyEntry(props *properties.Properties, key string) (string, error) {
value, _ := props.Get(key)
p := properties.NewProperties()
p.DisableExpansion = true
if _, _, err := p.Set(key, value); err != nil {
return "", err
}
buf := new(bytes.Buffer)
if _, err := p.Write(buf, properties.UTF8); err != nil {
return "", err
}
pair := strings.TrimSuffix(buf.String(), "\n")
return pair, nil
}

func resolvePodTemplate(ctx context.Context, templateSrc string, spec *v1.IntegrationSpec) (err error) {
// check if template is set
if templateSrc == "" {
Expand Down
17 changes: 14 additions & 3 deletions pkg/cmd/run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,7 @@ a=b
#d=c\=e
#ignore=me
f=g:h
i=j\nk
`

func TestAddPropertyFile(t *testing.T) {
Expand All @@ -267,11 +268,12 @@ func TestAddPropertyFile(t *testing.T) {
properties, err := extractProperties("file:" + tmpFile.Name())
assert.Nil(t, err)
assert.Nil(t, addIntegrationProperties(properties, &spec))
assert.Equal(t, 2, len(spec.Configuration))
assert.Equal(t, 3, len(spec.Configuration))
assert.Equal(t, `a = b`, spec.Configuration[0].Value)
//assert.Equal(t, `c\=d=e`, spec.Configuration[1].Value)
//assert.Equal(t, `d=c\=e`, spec.Configuration[2].Value)
//assert.Equal(t, `c\=d = e`, spec.Configuration[1].Value)
//assert.Equal(t, `d = c\=e`, spec.Configuration[2].Value)
assert.Equal(t, `f = g:h`, spec.Configuration[1].Value)
assert.Equal(t, `i = j\nk`, spec.Configuration[2].Value)
}

func TestRunPropertyFileFlag(t *testing.T) {
Expand All @@ -293,6 +295,15 @@ func TestRunPropertyFileFlag(t *testing.T) {
assert.Equal(t, tmpFile.Name(), runCmdOptions.PropertyFiles[0])
}

func TestRunProperty(t *testing.T) {
spec := v1.IntegrationSpec{}
properties, err := extractProperties(`key=value\nnewline`)
assert.Nil(t, err)
assert.Nil(t, addIntegrationProperties(properties, &spec))
assert.Equal(t, 1, len(spec.Configuration))
assert.Equal(t, `key = value\nnewline`, spec.Configuration[0].Value)
}

func TestRunResourceFlag(t *testing.T) {
runCmdOptions, rootCmd, _ := initializeRunCmdOptions(t)
_, err := test.ExecuteCommand(rootCmd, cmdRun,
Expand Down
33 changes: 26 additions & 7 deletions pkg/controller/kameletbinding/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ package kameletbinding
import (
"context"
"encoding/json"
"fmt"
"sort"

v1 "github.com/apache/camel-k/pkg/apis/camel/v1"
Expand All @@ -31,6 +30,7 @@ import (
"github.com/apache/camel-k/pkg/util/bindings"
"github.com/apache/camel-k/pkg/util/knative"
"github.com/apache/camel-k/pkg/util/kubernetes"
"github.com/apache/camel-k/pkg/util/property"
"github.com/pkg/errors"
k8serrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -125,10 +125,21 @@ func createIntegrationFor(ctx context.Context, c client.Client, kameletbinding *
}
}

configureBinding(&it, from)
configureBinding(&it, steps...)
configureBinding(&it, to)
configureBinding(&it, errorHandler)
if err := configureBinding(&it, from); err != nil {
return nil, err
}

if err := configureBinding(&it, steps...); err != nil {
return nil, err
}

if err := configureBinding(&it, to); err != nil {
return nil, err
}

if err := configureBinding(&it, errorHandler); err != nil {
return nil, err
}

if it.Spec.Configuration != nil {
sort.SliceStable(it.Spec.Configuration, func(i, j int) bool {
Expand Down Expand Up @@ -178,7 +189,7 @@ func createIntegrationFor(ctx context.Context, c client.Client, kameletbinding *
return &it, nil
}

func configureBinding(integration *v1.Integration, bindings ...*bindings.Binding) {
func configureBinding(integration *v1.Integration, bindings ...*bindings.Binding) error {
for _, b := range bindings {
if b == nil {
continue
Expand All @@ -190,12 +201,20 @@ func configureBinding(integration *v1.Integration, bindings ...*bindings.Binding
integration.Spec.Traits[k] = v
}
for k, v := range b.ApplicationProperties {
entry, err := property.EncodePropertyFileEntry(k, v)

if err != nil {
return err
}

integration.Spec.Configuration = append(integration.Spec.Configuration, v1.ConfigurationSpec{
Type: "property",
Value: fmt.Sprintf("%s=%s", k, v),
Value: entry,
})
}
}

return nil
}

func determineProfile(ctx context.Context, c client.Client, binding *v1alpha1.KameletBinding) (v1.TraitProfile, error) {
Expand Down
8 changes: 4 additions & 4 deletions pkg/trait/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ package trait
import (
"fmt"
"sort"
"strings"

"github.com/apache/camel-k/pkg/util/property"
corev1 "k8s.io/api/core/v1"

v1 "github.com/apache/camel-k/pkg/apis/camel/v1"
Expand Down Expand Up @@ -156,12 +156,12 @@ func (t *builderTrait) builderTask(e *Environment) (*v1.BuilderTask, error) {
// User provided Maven properties
if t.Properties != nil {
for _, v := range t.Properties {
split := strings.SplitN(v, "=", 2)
if len(split) != 2 {
key, value := property.SplitPropertyFileEntry(v)
if len(key) == 0 || len(value) == 0 {
return nil, fmt.Errorf("maven property must have key=value format, it was %v", v)
}

task.Maven.Properties[split[0]] = split[1]
task.Maven.Properties[key] = value
}
}

Expand Down
10 changes: 2 additions & 8 deletions pkg/trait/trait_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,14 @@ limitations under the License.
package trait

import (
"bytes"
"context"
"fmt"
"path"
"sort"
"strconv"
"strings"

"github.com/magiconair/properties"
"github.com/apache/camel-k/pkg/util/property"
"github.com/pkg/errors"
appsv1 "k8s.io/api/apps/v1"
"k8s.io/api/batch/v1beta1"
Expand Down Expand Up @@ -368,15 +367,10 @@ func (e *Environment) DetermineCatalogNamespace() string {

func (e *Environment) computeApplicationProperties() (*corev1.ConfigMap, error) {
// application properties
props := properties.LoadMap(e.ApplicationProperties)
props.DisableExpansion = true
props.Sort()
buf := new(bytes.Buffer)
_, err := props.Write(buf, properties.UTF8)
applicationProperties, err := property.EncodePropertyFile(e.ApplicationProperties)
if err != nil {
return nil, errors.Wrapf(err, "could not compute application properties")
}
applicationProperties := buf.String()

if applicationProperties != "" {
return &corev1.ConfigMap{
Expand Down
10 changes: 2 additions & 8 deletions pkg/trait/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"sort"
"strings"

"github.com/apache/camel-k/pkg/util/property"
user "github.com/mitchellh/go-homedir"
"github.com/scylladb/go-set/strset"

Expand Down Expand Up @@ -93,14 +94,7 @@ func collectConfigurationPairs(configurationType string, configurable ...v1.Conf

for _, entry := range entries {
if entry.Type == configurationType {
pair := strings.SplitN(entry.Value, "=", 2)
var k, v string
if len(pair) >= 1 {
k = strings.TrimSpace(pair[0])
}
if len(pair) == 2 {
v = strings.TrimSpace(pair[1])
}
k, v := property.SplitPropertyFileEntry(entry.Value)
if k == "" {
continue
}
Expand Down
67 changes: 67 additions & 0 deletions pkg/util/property/property.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package property

import (
"bytes"
"strings"

"github.com/magiconair/properties"
"github.com/pkg/errors"
)

// EncodePropertyFileEntry converts the given key/value pair into a .properties file entry
func EncodePropertyFileEntry(key, value string) (string, error) {
p := properties.NewProperties()
p.DisableExpansion = true
if _, _, err := p.Set(key, value); err != nil {
return "", err
}
buf := new(bytes.Buffer)
if _, err := p.Write(buf, properties.UTF8); err != nil {
return "", err
}
pair := strings.TrimSuffix(buf.String(), "\n")
return pair, nil
}

// EncodePropertyFile encodes a property map into a .properties file
func EncodePropertyFile(sourceProperties map[string]string) (string, error) {
props := properties.LoadMap(sourceProperties)
props.DisableExpansion = true
props.Sort()
buf := new(bytes.Buffer)
_, err := props.Write(buf, properties.UTF8)
if err != nil {
return "", errors.Wrapf(err, "could not compute application properties")
}
return buf.String(), nil
}

// SplitPropertyFileEntry splits an encoded property into key/value pair, without decoding the content
func SplitPropertyFileEntry(entry string) (string, string) {
pair := strings.SplitN(entry, "=", 2)
var k, v string
if len(pair) >= 1 {
k = strings.TrimSpace(pair[0])
}
if len(pair) == 2 {
v = strings.TrimSpace(pair[1])
}
return k, v
}

0 comments on commit f892f2c

Please sign in to comment.