This is an automated email from the ASF dual-hosted git repository.

pcongiusti pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel-k.git


The following commit(s) were added to refs/heads/main by this push:
     new 9c058daa1 chore(knative): configure via properties
9c058daa1 is described below

commit 9c058daa180106fb5b770558f9b553b7bfb9d2ef
Author: Pasquale Congiusti <pasquale.congiu...@gmail.com>
AuthorDate: Tue Feb 27 14:49:44 2024 +0100

    chore(knative): configure via properties
---
 docs/modules/ROOT/partials/apis/camel-k-crds.adoc |   1 -
 docs/modules/traits/pages/knative.adoc            |   1 -
 pkg/apis/camel/v1/knative/types.go                |   1 +
 pkg/apis/camel/v1/knative/types_support.go        |  41 +++++
 pkg/apis/camel/v1/knative/types_support_test.go   |  61 ++++++
 pkg/apis/camel/v1/trait/knative.go                |   1 -
 pkg/trait/knative.go                              | 215 +++++++++-------------
 pkg/trait/knative_service_test.go                 |   2 -
 pkg/trait/knative_test.go                         | 133 ++++++-------
 9 files changed, 257 insertions(+), 199 deletions(-)

diff --git a/docs/modules/ROOT/partials/apis/camel-k-crds.adoc 
b/docs/modules/ROOT/partials/apis/camel-k-crds.adoc
index e8e5bae6e..574e7c98c 100644
--- a/docs/modules/ROOT/partials/apis/camel-k-crds.adoc
+++ b/docs/modules/ROOT/partials/apis/camel-k-crds.adoc
@@ -7183,7 +7183,6 @@ Automatically deploy the integration as Knative service 
when all conditions hold
 The Knative trait automatically discovers addresses of Knative resources and 
inject them into the
 running integration.
 
-The full Knative configuration is injected in the CAMEL_KNATIVE_CONFIGURATION 
in JSON format.
 The Camel Knative component will then use the full configuration to configure 
the routes.
 
 The trait is enabled by default when the Knative profile is active.
diff --git a/docs/modules/traits/pages/knative.adoc 
b/docs/modules/traits/pages/knative.adoc
index ef2af7007..b82ac44a0 100755
--- a/docs/modules/traits/pages/knative.adoc
+++ b/docs/modules/traits/pages/knative.adoc
@@ -4,7 +4,6 @@
 The Knative trait automatically discovers addresses of Knative resources and 
inject them into the
 running integration.
 
-The full Knative configuration is injected in the CAMEL_KNATIVE_CONFIGURATION 
in JSON format.
 The Camel Knative component will then use the full configuration to configure 
the routes.
 
 The trait is enabled by default when the Knative profile is active.
diff --git a/pkg/apis/camel/v1/knative/types.go 
b/pkg/apis/camel/v1/knative/types.go
index 01e781225..2fb6b9665 100644
--- a/pkg/apis/camel/v1/knative/types.go
+++ b/pkg/apis/camel/v1/knative/types.go
@@ -38,6 +38,7 @@ type CamelServiceDefinition struct {
        URL         string            `json:"url,omitempty"`
        Path        string            `json:"path,omitempty"`
        Metadata    map[string]string `json:"metadata,omitempty"`
+       SinkBinding bool              `json:"sinkBinding,omitempty"`
 }
 
 // CamelEndpointKind -- .
diff --git a/pkg/apis/camel/v1/knative/types_support.go 
b/pkg/apis/camel/v1/knative/types_support.go
index 0c7dcc870..d4ebf4b39 100644
--- a/pkg/apis/camel/v1/knative/types_support.go
+++ b/pkg/apis/camel/v1/knative/types_support.go
@@ -19,6 +19,7 @@ package knative
 
 import (
        "encoding/json"
+       "fmt"
        "net/url"
 )
 
@@ -41,6 +42,46 @@ func BuildCamelServiceDefinition(name string, endpointKind 
CamelEndpointKind, se
        return definition, nil
 }
 
+// SetSinkBinding marks one of the service as SinkBinding.
+func (env *CamelEnvironment) SetSinkBinding(name string, endpointKind 
CamelEndpointKind, serviceType CamelServiceType, apiVersion, kind string) {
+       for i, svc := range env.Services {
+               svc := svc
+               if svc.Name == name &&
+                       svc.Metadata[CamelMetaEndpointKind] == 
string(endpointKind) &&
+                       svc.ServiceType == serviceType &&
+                       (apiVersion == "" || 
svc.Metadata[CamelMetaKnativeAPIVersion] == apiVersion) &&
+                       (kind == "" || svc.Metadata[CamelMetaKnativeKind] == 
kind) {
+                       svc.SinkBinding = true
+                       env.Services[i] = svc
+               }
+       }
+}
+
+// ToCamelProperties returns the application properties representation of the 
services.
+func (env *CamelEnvironment) ToCamelProperties() map[string]string {
+       mappedServices := make(map[string]string)
+       for i, service := range env.Services {
+               resource := 
fmt.Sprintf("camel.component.knative.environment.resources[%d]", i)
+               mappedServices[fmt.Sprintf("%s.name", resource)] = service.Name
+               mappedServices[fmt.Sprintf("%s.type", resource)] = 
string(service.ServiceType)
+               mappedServices[fmt.Sprintf("%s.objectKind", resource)] = 
service.Metadata[CamelMetaKnativeKind]
+               mappedServices[fmt.Sprintf("%s.objectApiVersion", resource)] = 
service.Metadata[CamelMetaKnativeAPIVersion]
+               mappedServices[fmt.Sprintf("%s.endpointKind", resource)] = 
service.Metadata[CamelMetaEndpointKind]
+               mappedServices[fmt.Sprintf("%s.reply", resource)] = 
service.Metadata[CamelMetaKnativeReply]
+               if service.ServiceType == CamelServiceTypeEvent {
+                       mappedServices[fmt.Sprintf("%s.objectName", resource)] 
= service.Metadata[CamelMetaKnativeName]
+               }
+               if service.SinkBinding {
+                       mappedServices[fmt.Sprintf("%s.url", resource)] = 
"${K_SINK}"
+                       mappedServices[fmt.Sprintf("%s.ceOverrides", resource)] 
= "${K_CE_OVERRIDES}"
+               } else {
+                       mappedServices[fmt.Sprintf("%s.url", resource)] = 
service.URL
+                       mappedServices[fmt.Sprintf("%s.path", resource)] = 
service.Path
+               }
+       }
+       return mappedServices
+}
+
 // Serialize serializes a CamelEnvironment.
 func (env *CamelEnvironment) Serialize() (string, error) {
        res, err := json.Marshal(env)
diff --git a/pkg/apis/camel/v1/knative/types_support_test.go 
b/pkg/apis/camel/v1/knative/types_support_test.go
new file mode 100644
index 000000000..44e204c27
--- /dev/null
+++ b/pkg/apis/camel/v1/knative/types_support_test.go
@@ -0,0 +1,61 @@
+/*
+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 knative
+
+import (
+       "net/url"
+       "testing"
+
+       "github.com/stretchr/testify/assert"
+)
+
+func TestSetSinkBinding(t *testing.T) {
+       camelEnv := NewCamelEnvironment()
+       svc1, err := BuildCamelServiceDefinition(
+               "test",
+               CamelEndpointKindSink,
+               CamelServiceTypeChannel,
+               url.URL{},
+               "apiVersion",
+               "InMemoryChannel",
+       )
+       assert.Nil(t, err)
+       camelEnv.Services = append(camelEnv.Services, svc1)
+       svc := camelEnv.FindService("test",
+               CamelEndpointKindSink,
+               CamelServiceTypeChannel,
+               "apiVersion",
+               "InMemoryChannel",
+       )
+       assert.NotNil(t, svc)
+       assert.False(t, svc.SinkBinding)
+       camelEnv.SetSinkBinding("test",
+               CamelEndpointKindSink,
+               CamelServiceTypeChannel,
+               "apiVersion",
+               "InMemoryChannel",
+       )
+       svc = camelEnv.FindService("test",
+               CamelEndpointKindSink,
+               CamelServiceTypeChannel,
+               "apiVersion",
+               "InMemoryChannel",
+       )
+       assert.NotNil(t, svc)
+       assert.True(t, svc.SinkBinding)
+}
diff --git a/pkg/apis/camel/v1/trait/knative.go 
b/pkg/apis/camel/v1/trait/knative.go
index 4495c6a48..b8ce7fe46 100644
--- a/pkg/apis/camel/v1/trait/knative.go
+++ b/pkg/apis/camel/v1/trait/knative.go
@@ -20,7 +20,6 @@ package trait
 // The Knative trait automatically discovers addresses of Knative resources 
and inject them into the
 // running integration.
 //
-// The full Knative configuration is injected in the 
CAMEL_KNATIVE_CONFIGURATION in JSON format.
 // The Camel Knative component will then use the full configuration to 
configure the routes.
 //
 // The trait is enabled by default when the Knative profile is active.
diff --git a/pkg/trait/knative.go b/pkg/trait/knative.go
index 91bb7b7bc..0775be5ec 100644
--- a/pkg/trait/knative.go
+++ b/pkg/trait/knative.go
@@ -39,7 +39,7 @@ import (
        traitv1 "github.com/apache/camel-k/v2/pkg/apis/camel/v1/trait"
        "github.com/apache/camel-k/v2/pkg/metadata"
        "github.com/apache/camel-k/v2/pkg/util"
-       "github.com/apache/camel-k/v2/pkg/util/envvar"
+       "github.com/apache/camel-k/v2/pkg/util/camel"
        knativeutil "github.com/apache/camel-k/v2/pkg/util/knative"
        "github.com/apache/camel-k/v2/pkg/util/kubernetes"
 )
@@ -77,101 +77,51 @@ func (t *knativeTrait) Configure(e *Environment) (bool, 
*TraitCondition, error)
                return false, nil, nil
        }
        if pointer.BoolDeref(t.Auto, true) {
+               sources, err := kubernetes.ResolveIntegrationSources(e.Ctx, 
e.Client, e.Integration, e.Resources)
+               if err != nil {
+                       return false, nil, err
+               }
                if len(t.ChannelSources) == 0 {
-                       items := make([]string, 0)
-                       sources, err := 
kubernetes.ResolveIntegrationSources(e.Ctx, e.Client, e.Integration, 
e.Resources)
+                       items, err := filterMetaItems(e.CamelCatalog, sources, 
knativeapi.CamelServiceTypeChannel, "from")
                        if err != nil {
                                return false, nil, err
                        }
-                       if err := metadata.Each(e.CamelCatalog, sources, func(_ 
int, meta metadata.IntegrationMetadata) bool {
-                               items = append(items, 
knativeutil.FilterURIs(meta.FromURIs, knativeapi.CamelServiceTypeChannel)...)
-                               return true
-                       }); err != nil {
-                               return false, nil, err
-                       }
-
                        t.ChannelSources = items
-                       sort.Strings(t.ChannelSources)
                }
                if len(t.ChannelSinks) == 0 {
-                       items := make([]string, 0)
-                       sources, err := 
kubernetes.ResolveIntegrationSources(e.Ctx, e.Client, e.Integration, 
e.Resources)
+                       items, err := filterMetaItems(e.CamelCatalog, sources, 
knativeapi.CamelServiceTypeChannel, "to")
                        if err != nil {
                                return false, nil, err
                        }
-                       if err := metadata.Each(e.CamelCatalog, sources, func(_ 
int, meta metadata.IntegrationMetadata) bool {
-                               items = append(items, 
knativeutil.FilterURIs(meta.ToURIs, knativeapi.CamelServiceTypeChannel)...)
-                               return true
-                       }); err != nil {
-                               return false, nil, err
-                       }
-
                        t.ChannelSinks = items
-                       sort.Strings(t.ChannelSinks)
                }
                if len(t.EndpointSources) == 0 {
-                       items := make([]string, 0)
-                       sources, err := 
kubernetes.ResolveIntegrationSources(e.Ctx, e.Client, e.Integration, 
e.Resources)
+                       items, err := filterMetaItems(e.CamelCatalog, sources, 
knativeapi.CamelServiceTypeEndpoint, "from")
                        if err != nil {
                                return false, nil, err
                        }
-                       if err := metadata.Each(e.CamelCatalog, sources, func(_ 
int, meta metadata.IntegrationMetadata) bool {
-                               items = append(items, 
knativeutil.FilterURIs(meta.FromURIs, knativeapi.CamelServiceTypeEndpoint)...)
-                               return true
-                       }); err != nil {
-                               return false, nil, err
-                       }
-
                        t.EndpointSources = items
-                       sort.Strings(t.EndpointSources)
                }
                if len(t.EndpointSinks) == 0 {
-                       items := make([]string, 0)
-                       sources, err := 
kubernetes.ResolveIntegrationSources(e.Ctx, e.Client, e.Integration, 
e.Resources)
+                       items, err := filterMetaItems(e.CamelCatalog, sources, 
knativeapi.CamelServiceTypeEndpoint, "to")
                        if err != nil {
                                return false, nil, err
                        }
-                       if err := metadata.Each(e.CamelCatalog, sources, func(_ 
int, meta metadata.IntegrationMetadata) bool {
-                               items = append(items, 
knativeutil.FilterURIs(meta.ToURIs, knativeapi.CamelServiceTypeEndpoint)...)
-                               return true
-                       }); err != nil {
-                               return false, nil, err
-                       }
-
                        t.EndpointSinks = items
-                       sort.Strings(t.EndpointSinks)
                }
                if len(t.EventSources) == 0 {
-                       items := make([]string, 0)
-                       sources, err := 
kubernetes.ResolveIntegrationSources(e.Ctx, e.Client, e.Integration, 
e.Resources)
+                       items, err := filterMetaItems(e.CamelCatalog, sources, 
knativeapi.CamelServiceTypeEvent, "from")
                        if err != nil {
                                return false, nil, err
                        }
-                       if err := metadata.Each(e.CamelCatalog, sources, func(_ 
int, meta metadata.IntegrationMetadata) bool {
-                               items = append(items, 
knativeutil.FilterURIs(meta.FromURIs, knativeapi.CamelServiceTypeEvent)...)
-                               return true
-                       }); err != nil {
-                               return false, nil, err
-                       }
-
                        t.EventSources = items
-                       sort.Strings(t.EventSources)
                }
                if len(t.EventSinks) == 0 {
-                       items := make([]string, 0)
-                       sources, err := 
kubernetes.ResolveIntegrationSources(e.Ctx, e.Client, e.Integration, 
e.Resources)
+                       items, err := filterMetaItems(e.CamelCatalog, sources, 
knativeapi.CamelServiceTypeEvent, "to")
                        if err != nil {
                                return false, nil, err
                        }
-                       if err := metadata.Each(e.CamelCatalog, sources, func(_ 
int, meta metadata.IntegrationMetadata) bool {
-                               items = append(items, 
knativeutil.FilterURIs(meta.ToURIs, knativeapi.CamelServiceTypeEvent)...)
-                               return true
-                       }); err != nil {
-                               return false, nil, err
-                       }
-
                        t.EventSinks = items
-                       sort.Strings(t.EventSinks)
                }
                if t.FilterSourceChannels == nil {
                        // Filtering is no longer used by default
@@ -186,6 +136,25 @@ func (t *knativeTrait) Configure(e *Environment) (bool, 
*TraitCondition, error)
        return true, nil, nil
 }
 
+func filterMetaItems(catalog *camel.RuntimeCatalog, sources []v1.SourceSpec, 
cst knativeapi.CamelServiceType, uriType string) ([]string, error) {
+       items := make([]string, 0)
+       if err := metadata.Each(catalog, sources, func(_ int, meta 
metadata.IntegrationMetadata) bool {
+               var uris []string
+               if uriType == "from" {
+                       uris = meta.FromURIs
+               } else if uriType == "to" {
+                       uris = meta.ToURIs
+               }
+               items = append(items, knativeutil.FilterURIs(uris, cst)...)
+               return true
+       }); err != nil {
+               return nil, err
+       }
+
+       sort.Strings(items)
+       return items, nil
+}
+
 func (t *knativeTrait) Apply(e *Environment) error {
        if e.IntegrationInPhase(v1.IntegrationPhaseInitialization) {
                util.StringSliceUniqueAdd(&e.Integration.Status.Capabilities, 
v1.CapabilityKnative)
@@ -211,7 +180,6 @@ func (t *knativeTrait) Apply(e *Environment) error {
                                return err
                        }
                }
-
                if err := t.configureChannels(e, &env); err != nil {
                        return err
                }
@@ -224,13 +192,12 @@ func (t *knativeTrait) Apply(e *Environment) error {
                if err := t.configureSinkBinding(e, &env); err != nil {
                        return err
                }
-
-               conf, err := env.Serialize()
-               if err != nil {
-                       return fmt.Errorf("unable to fetch environment 
configuration: %w", err)
+               if e.ApplicationProperties == nil {
+                       e.ApplicationProperties = make(map[string]string)
+               }
+               for k, v := range env.ToCamelProperties() {
+                       e.ApplicationProperties[k] = v
                }
-
-               envvar.SetVal(&e.EnvVars, "CAMEL_KNATIVE_CONFIGURATION", conf)
        }
 
        return nil
@@ -272,25 +239,23 @@ func (t *knativeTrait) configureChannels(e *Environment, 
env *knativeapi.CamelEn
                return err
        }
 
-       if !pointer.BoolDeref(t.SinkBinding, false) {
-               // Sinks
-               err = t.ifServiceMissingDo(e, env, t.ChannelSinks, 
knativeapi.CamelServiceTypeChannel, knativeapi.CamelEndpointKindSink,
-                       func(ref *corev1.ObjectReference, serviceURI string, 
urlProvider func() (*url.URL, error)) error {
-                               loc, err := urlProvider()
-                               if err != nil {
-                                       return err
-                               }
-                               svc, err := 
knativeapi.BuildCamelServiceDefinition(ref.Name, 
knativeapi.CamelEndpointKindSink,
-                                       knativeapi.CamelServiceTypeChannel, 
*loc, ref.APIVersion, ref.Kind)
-                               if err != nil {
-                                       return err
-                               }
-                               env.Services = append(env.Services, svc)
-                               return nil
-                       })
-               if err != nil {
-                       return err
-               }
+       // Sinks
+       err = t.ifServiceMissingDo(e, env, t.ChannelSinks, 
knativeapi.CamelServiceTypeChannel, knativeapi.CamelEndpointKindSink,
+               func(ref *corev1.ObjectReference, serviceURI string, 
urlProvider func() (*url.URL, error)) error {
+                       loc, err := urlProvider()
+                       if err != nil {
+                               return err
+                       }
+                       svc, err := 
knativeapi.BuildCamelServiceDefinition(ref.Name, 
knativeapi.CamelEndpointKindSink,
+                               knativeapi.CamelServiceTypeChannel, *loc, 
ref.APIVersion, ref.Kind)
+                       if err != nil {
+                               return err
+                       }
+                       env.Services = append(env.Services, svc)
+                       return nil
+               })
+       if err != nil {
+               return err
        }
 
        return nil
@@ -332,24 +297,22 @@ func (t *knativeTrait) configureEndpoints(e *Environment, 
env *knativeapi.CamelE
        }
 
        // Sinks
-       if !pointer.BoolDeref(t.SinkBinding, false) {
-               err := t.ifServiceMissingDo(e, env, t.EndpointSinks, 
knativeapi.CamelServiceTypeEndpoint, knativeapi.CamelEndpointKindSink,
-                       func(ref *corev1.ObjectReference, serviceURI string, 
urlProvider func() (*url.URL, error)) error {
-                               loc, err := urlProvider()
-                               if err != nil {
-                                       return err
-                               }
-                               svc, err := 
knativeapi.BuildCamelServiceDefinition(ref.Name, 
knativeapi.CamelEndpointKindSink,
-                                       knativeapi.CamelServiceTypeEndpoint, 
*loc, ref.APIVersion, ref.Kind)
-                               if err != nil {
-                                       return err
-                               }
-                               env.Services = append(env.Services, svc)
-                               return nil
-                       })
-               if err != nil {
-                       return err
-               }
+       err := t.ifServiceMissingDo(e, env, t.EndpointSinks, 
knativeapi.CamelServiceTypeEndpoint, knativeapi.CamelEndpointKindSink,
+               func(ref *corev1.ObjectReference, serviceURI string, 
urlProvider func() (*url.URL, error)) error {
+                       loc, err := urlProvider()
+                       if err != nil {
+                               return err
+                       }
+                       svc, err := 
knativeapi.BuildCamelServiceDefinition(ref.Name, 
knativeapi.CamelEndpointKindSink,
+                               knativeapi.CamelServiceTypeEndpoint, *loc, 
ref.APIVersion, ref.Kind)
+                       if err != nil {
+                               return err
+                       }
+                       env.Services = append(env.Services, svc)
+                       return nil
+               })
+       if err != nil {
+               return err
        }
 
        return nil
@@ -391,24 +354,22 @@ func (t *knativeTrait) configureEvents(e *Environment, 
env *knativeapi.CamelEnvi
        }
 
        // Sinks
-       if !pointer.BoolDeref(t.SinkBinding, false) {
-               err = t.ifServiceMissingDo(e, env, t.EventSinks, 
knativeapi.CamelServiceTypeEvent, knativeapi.CamelEndpointKindSink,
-                       func(ref *corev1.ObjectReference, serviceURI string, 
urlProvider func() (*url.URL, error)) error {
-                               loc, err := urlProvider()
-                               if err != nil {
-                                       return err
-                               }
-                               svc, err := 
knativeapi.BuildCamelServiceDefinition(ref.Name, 
knativeapi.CamelEndpointKindSink,
-                                       knativeapi.CamelServiceTypeEvent, *loc, 
ref.APIVersion, ref.Kind)
-                               if err != nil {
-                                       return err
-                               }
-                               env.Services = append(env.Services, svc)
-                               return nil
-                       })
-               if err != nil {
-                       return err
-               }
+       err = t.ifServiceMissingDo(e, env, t.EventSinks, 
knativeapi.CamelServiceTypeEvent, knativeapi.CamelEndpointKindSink,
+               func(ref *corev1.ObjectReference, serviceURI string, 
urlProvider func() (*url.URL, error)) error {
+                       loc, err := urlProvider()
+                       if err != nil {
+                               return err
+                       }
+                       svc, err := 
knativeapi.BuildCamelServiceDefinition(ref.Name, 
knativeapi.CamelEndpointKindSink,
+                               knativeapi.CamelServiceTypeEvent, *loc, 
ref.APIVersion, ref.Kind)
+                       if err != nil {
+                               return err
+                       }
+                       env.Services = append(env.Services, svc)
+                       return nil
+               })
+       if err != nil {
+               return err
        }
 
        return nil
@@ -454,12 +415,8 @@ func (t *knativeTrait) configureSinkBinding(e 
*Environment, env *knativeapi.Came
        }
 
        err := t.withServiceDo(false, e, env, services, serviceType, 
knativeapi.CamelEndpointKindSink, func(ref *corev1.ObjectReference, serviceURI 
string, _ func() (*url.URL, error)) error {
-               
e.ApplicationProperties["camel.k.customizer.sinkbinding.enabled"] = "true"
-               e.ApplicationProperties["camel.k.customizer.sinkbinding.name"] 
= ref.Name
-               e.ApplicationProperties["camel.k.customizer.sinkbinding.type"] 
= string(serviceType)
-               e.ApplicationProperties["camel.k.customizer.sinkbinding.kind"] 
= ref.Kind
-               
e.ApplicationProperties["camel.k.customizer.sinkbinding.api-version"] = 
ref.APIVersion
-
+               // Mark the service which will be used as SinkBinding
+               env.SetSinkBinding(ref.Name, knativeapi.CamelEndpointKindSink, 
serviceType, ref.APIVersion, ref.Kind)
                if e.IntegrationInPhase(v1.IntegrationPhaseDeploying, 
v1.IntegrationPhaseRunning) {
                        e.PostStepProcessors = append(e.PostStepProcessors, 
func(e *Environment) error {
                                sinkBindingInjected := false
diff --git a/pkg/trait/knative_service_test.go 
b/pkg/trait/knative_service_test.go
index 6367c13f6..559ecbe4c 100644
--- a/pkg/trait/knative_service_test.go
+++ b/pkg/trait/knative_service_test.go
@@ -35,7 +35,6 @@ import (
        v1 "github.com/apache/camel-k/v2/pkg/apis/camel/v1"
        traitv1 "github.com/apache/camel-k/v2/pkg/apis/camel/v1/trait"
        "github.com/apache/camel-k/v2/pkg/util/camel"
-       "github.com/apache/camel-k/v2/pkg/util/envvar"
        "github.com/apache/camel-k/v2/pkg/util/gzip"
        "github.com/apache/camel-k/v2/pkg/util/kubernetes"
        "github.com/apache/camel-k/v2/pkg/util/test"
@@ -124,7 +123,6 @@ func TestKnativeService(t *testing.T) {
        require.NoError(t, err)
        assert.NotEmpty(t, environment.ExecutedTraits)
        assert.NotNil(t, environment.GetTrait("knative"))
-       assert.NotNil(t, envvar.Get(environment.EnvVars, 
"CAMEL_KNATIVE_CONFIGURATION"))
        assert.Equal(t, 4, environment.Resources.Size())
 
        s := environment.Resources.GetKnativeService(func(service 
*serving.Service) bool {
diff --git a/pkg/trait/knative_test.go b/pkg/trait/knative_test.go
index 0d5a339fb..7684cb618 100644
--- a/pkg/trait/knative_test.go
+++ b/pkg/trait/knative_test.go
@@ -18,6 +18,9 @@ limitations under the License.
 package trait
 
 import (
+       "fmt"
+       "net/url"
+       "regexp"
        "testing"
 
        "github.com/stretchr/testify/assert"
@@ -38,7 +41,6 @@ import (
        traitv1 "github.com/apache/camel-k/v2/pkg/apis/camel/v1/trait"
        "github.com/apache/camel-k/v2/pkg/client"
        "github.com/apache/camel-k/v2/pkg/util/camel"
-       "github.com/apache/camel-k/v2/pkg/util/envvar"
        k8sutils "github.com/apache/camel-k/v2/pkg/util/kubernetes"
        "github.com/apache/camel-k/v2/pkg/util/test"
 )
@@ -117,11 +119,7 @@ func TestKnativeEnvConfigurationFromTrait(t *testing.T) {
        err = tr.Apply(&environment)
        assert.Nil(t, err)
 
-       kc := envvar.Get(environment.EnvVars, "CAMEL_KNATIVE_CONFIGURATION")
-       assert.NotNil(t, kc)
-
-       ne := knativeapi.NewCamelEnvironment()
-       err = ne.Deserialize(kc.Value)
+       ne, err := fromCamelProperties(environment.ApplicationProperties)
        assert.Nil(t, err)
 
        cSource1 := ne.FindService("channel-source-1", 
knativeapi.CamelEndpointKindSource, knativeapi.CamelServiceTypeChannel, 
"messaging.knative.dev/v1", "Channel")
@@ -239,11 +237,7 @@ func TestKnativeEnvConfigurationFromSource(t *testing.T) {
        err = tr.Apply(&environment)
        assert.Nil(t, err)
 
-       kc := envvar.Get(environment.EnvVars, "CAMEL_KNATIVE_CONFIGURATION")
-       assert.NotNil(t, kc)
-
-       ne := knativeapi.NewCamelEnvironment()
-       err = ne.Deserialize(kc.Value)
+       ne, err := fromCamelProperties(environment.ApplicationProperties)
        assert.Nil(t, err)
 
        source := ne.FindService("s3fileMover1", 
knativeapi.CamelEndpointKindSource, knativeapi.CamelServiceTypeEndpoint, 
"serving.knative.dev/v1", "Service")
@@ -353,60 +347,6 @@ func TestKnativePlatformHttpDependencies(t *testing.T) {
        }
 }
 
-func TestKnativeConfigurationSorting(t *testing.T) {
-       // unsorted on purpose
-       sources := []v1.SourceSpec{
-               {
-                       DataSpec: v1.DataSpec{
-                               Name:    "s1.groovy",
-                               Content: 
`from('knative:channel/channel-source-2').log('${body}')`,
-                       },
-                       Language: v1.LanguageGroovy,
-               },
-               {
-                       DataSpec: v1.DataSpec{
-                               Name:    "s2.groovy",
-                               Content: 
`from('knative:channel/channel-source-4').log('${body}')`,
-                       },
-                       Language: v1.LanguageGroovy,
-               },
-               {
-                       DataSpec: v1.DataSpec{
-                               Name:    "s3.groovy",
-                               Content: 
`from('knative:channel/channel-source-1').log('${body}')`,
-                       },
-                       Language: v1.LanguageGroovy,
-               },
-               {
-                       DataSpec: v1.DataSpec{
-                               Name:    "s4.groovy",
-                               Content: 
`from('knative:channel/channel-source-3').log('${body}')`,
-                       },
-                       Language: v1.LanguageGroovy,
-               },
-       }
-
-       environment := NewFakeEnvironment(t, v1.SourceSpec{})
-       environment.Integration.Status.Phase = v1.IntegrationPhaseRunning
-       environment.Integration.Spec.Sources = sources
-
-       c, err := SortChannelFakeClient("ns")
-       assert.Nil(t, err)
-       tc := NewCatalog(c)
-       err = tc.Configure(&environment)
-       assert.Nil(t, err)
-       conditions, _ := tc.apply(&environment)
-       assert.Empty(t, conditions)
-       // no matter if there is any other trait error
-       camelEnv := knativeapi.NewCamelEnvironment()
-       err = camelEnv.Deserialize(envvar.Get(environment.EnvVars, 
"CAMEL_KNATIVE_CONFIGURATION").Value)
-       assert.Nil(t, err)
-       assert.Equal(t, "channel-source-1", camelEnv.Services[0].Name)
-       assert.Equal(t, "channel-source-2", camelEnv.Services[1].Name)
-       assert.Equal(t, "channel-source-3", camelEnv.Services[2].Name)
-       assert.Equal(t, "channel-source-4", camelEnv.Services[3].Name)
-}
-
 func NewFakeEnvironment(t *testing.T, source v1.SourceSpec) Environment {
        t.Helper()
 
@@ -709,3 +649,66 @@ func SortChannelFakeClient(namespace string) 
(client.Client, error) {
                },
        )
 }
+
+func TestKnativeSinkBinding(t *testing.T) {
+       source := v1.SourceSpec{
+               DataSpec: v1.DataSpec{
+                       Name:    "sink.groovy",
+                       Content: 
`from('timer:foo').to('knative:channel/channel-sink-1?apiVersion=messaging.knative.dev%2Fv1&kind=Channel')`,
+               },
+               Language: v1.LanguageGroovy,
+       }
+
+       environment := NewFakeEnvironment(t, source)
+       environment.Integration.Status.Phase = v1.IntegrationPhaseDeploying
+
+       c, err := NewFakeClient("ns")
+       assert.Nil(t, err)
+
+       tc := NewCatalog(c)
+
+       err = tc.Configure(&environment)
+       assert.Nil(t, err)
+
+       _, err = tc.apply(&environment)
+       assert.Nil(t, err)
+       baseProp := "camel.component.knative.environment.resources[0]"
+       assert.Equal(t, "channel-sink-1", 
environment.ApplicationProperties[baseProp+".name"])
+       assert.Equal(t, "${K_SINK}", 
environment.ApplicationProperties[baseProp+".url"])
+       assert.Equal(t, "${K_CE_OVERRIDES}", 
environment.ApplicationProperties[baseProp+".ceOverrides"])
+       assert.Equal(t, "channel", 
environment.ApplicationProperties[baseProp+".type"])
+       assert.Equal(t, "Channel", 
environment.ApplicationProperties[baseProp+".objectKind"])
+       assert.Equal(t, "messaging.knative.dev/v1", 
environment.ApplicationProperties[baseProp+".objectApiVersion"])
+       assert.Equal(t, "sink", 
environment.ApplicationProperties[baseProp+".endpointKind"])
+}
+
+// fromCamelProperties deserialize from properties to environment.
+func fromCamelProperties(appProps map[string]string) 
(*knativeapi.CamelEnvironment, error) {
+       env := knativeapi.NewCamelEnvironment()
+       re := 
regexp.MustCompile(`camel.component.knative.environment.resources\[(\d+)\].name`)
+       for k, v := range appProps {
+               if re.MatchString(k) {
+                       index := re.FindStringSubmatch(k)[1]
+                       baseComp := 
fmt.Sprintf("camel.component.knative.environment.resources[%s]", index)
+                       url, err := url.Parse(appProps[fmt.Sprintf("%s.url", 
baseComp)])
+                       if err != nil {
+                               return nil, err
+                       }
+                       svc, err := knativeapi.BuildCamelServiceDefinition(
+                               v,
+                               
knativeapi.CamelEndpointKind(appProps[fmt.Sprintf("%s.endpointKind", 
baseComp)]),
+                               
knativeapi.CamelServiceType(appProps[fmt.Sprintf("%s.type", baseComp)]),
+                               *url,
+                               appProps[fmt.Sprintf("%s.objectApiVersion", 
baseComp)],
+                               appProps[fmt.Sprintf("%s.objectKind", 
baseComp)],
+                       )
+                       if err != nil {
+                               return nil, err
+                       }
+                       svc.Metadata[knativeapi.CamelMetaKnativeReply] = 
appProps[fmt.Sprintf("%s.reply", baseComp)]
+                       env.Services = append(env.Services, svc)
+               }
+       }
+
+       return &env, nil
+}

Reply via email to