This is an automated email from the ASF dual-hosted git repository. lburgazzoli pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/camel-k.git
commit 38dce53521e61a0118af2f8f6c5be5c820fef11f Author: nferraro <ni.ferr...@gmail.com> AuthorDate: Mon Dec 24 09:37:21 2018 +0100 Add option to provide initial configuration to Knative --- pkg/apis/camel/v1alpha1/knative/types_support.go | 21 +++++++++++++++++++-- pkg/trait/knative.go | 23 +++++++++++++++++------ 2 files changed, 36 insertions(+), 8 deletions(-) diff --git a/pkg/apis/camel/v1alpha1/knative/types_support.go b/pkg/apis/camel/v1alpha1/knative/types_support.go index 49df4c3..97c6fe7 100644 --- a/pkg/apis/camel/v1alpha1/knative/types_support.go +++ b/pkg/apis/camel/v1alpha1/knative/types_support.go @@ -54,12 +54,29 @@ func BuildCamelServiceDefinition(name string, serviceType CamelServiceType, rawu return &definition, nil } - // Serialize serializes a CamelEnvironment -func (env CamelEnvironment) Serialize() (string, error) { +func (env *CamelEnvironment) Serialize() (string, error) { res, err := json.Marshal(env) if err != nil { return "", err } return string(res), nil } + +// Deserialize deserializes a camel environment into this struct +func (env *CamelEnvironment) Deserialize(str string) error { + if err := json.Unmarshal([]byte(str), env); err != nil { + return err + } + return nil +} + +// ContainsService tells if the environment contains a service with the given name and type +func (env *CamelEnvironment) ContainsService(name string, serviceType CamelServiceType) bool { + for _, svc := range env.Services { + if svc.Name == name && svc.ServiceType == serviceType { + return true + } + } + return false +} diff --git a/pkg/trait/knative.go b/pkg/trait/knative.go index 60010b4..977d6ad 100644 --- a/pkg/trait/knative.go +++ b/pkg/trait/knative.go @@ -45,12 +45,13 @@ const ( ) type knativeTrait struct { - BaseTrait `property:",squash"` - Sources string `property:"sources"` - Sinks string `property:"sinks"` - MinScale *int `property:"minScale"` - MaxScale *int `property:"maxScale"` - Auto *bool `property:"auto"` + BaseTrait `property:",squash"` + Configuration string `property:"configuration"` + Sources string `property:"sources"` + Sinks string `property:"sinks"` + MinScale *int `property:"minScale"` + MaxScale *int `property:"maxScale"` + Auto *bool `property:"auto"` } func newKnativeTrait() *knativeTrait { @@ -293,9 +294,16 @@ func (t *knativeTrait) getConfigurationSerialized(e *Environment) (string, error func (t *knativeTrait) getConfiguration(e *Environment) (knativeapi.CamelEnvironment, error) { env := knativeapi.NewCamelEnvironment() + if t.Configuration != "" { + env.Deserialize(t.Configuration) + } + // Sources sourceChannels := t.getConfiguredSourceChannels() for _, ch := range sourceChannels { + if env.ContainsService(ch, knativeapi.CamelServiceTypeChannel) { + continue + } svc := knativeapi.CamelServiceDefinition{ Name: ch, Host: "0.0.0.0", @@ -311,6 +319,9 @@ func (t *knativeTrait) getConfiguration(e *Environment) (knativeapi.CamelEnviron // Sinks sinkChannels := t.getConfiguredSinkChannels() for _, ch := range sinkChannels { + if env.ContainsService(ch, knativeapi.CamelServiceTypeChannel) { + continue + } channel, err := t.retrieveChannel(e.Integration.Namespace, ch) if err != nil { return env, err