You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by nf...@apache.org on 2021/03/25 14:08:15 UTC

[camel-k] branch master updated (f64b6b9 -> 1e8039d)

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

nferraro pushed a change to branch master
in repository https://gitbox.apache.org/repos/asf/camel-k.git.


    from f64b6b9  Kamel Dump command: Added log lines parameter
     new a134e48  Fix #2158: set a convention for object references
     new 6b506ae  Fix #2158: Add conversion to string and regen resources
     new 1e8039d  Fix #2158: fix bash completion and binding handling

The 3 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 deploy/traits.yaml                                 |   7 +-
 .../attachments/schema/integration-schema.json     |  26 ++--
 .../assets/attachments/schema/kamelet-schema.json  |  26 ++--
 docs/modules/traits/pages/knative.adoc             |   5 +-
 docs/modules/traits/pages/service-binding.adoc     |   2 +-
 pkg/cmd/completion_bash.go                         |   2 +-
 pkg/cmd/run.go                                     |   2 +-
 pkg/trait/service_binding.go                       |  58 ++++----
 pkg/util/reference/reference.go                    | 144 +++++++++++++++++++
 pkg/util/reference/reference_test.go               | 153 +++++++++++++++++++++
 10 files changed, 365 insertions(+), 60 deletions(-)
 create mode 100644 pkg/util/reference/reference.go
 create mode 100644 pkg/util/reference/reference_test.go

[camel-k] 02/03: Fix #2158: Add conversion to string and regen resources

Posted by nf...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

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

commit 6b506ae548d23d40abfb341bc22feaa6b56f2680
Author: nicolaferraro <ni...@gmail.com>
AuthorDate: Wed Mar 24 17:15:49 2021 +0100

    Fix #2158: Add conversion to string and regen resources
---
 deploy/traits.yaml                                 |  7 +++---
 .../attachments/schema/integration-schema.json     | 26 +++++++++++++++-------
 .../assets/attachments/schema/kamelet-schema.json  | 26 +++++++++++++++-------
 docs/modules/traits/pages/knative.adoc             |  5 ++---
 docs/modules/traits/pages/service-binding.adoc     |  2 +-
 pkg/util/reference/reference.go                    | 18 ++++++++++++++-
 pkg/util/reference/reference_test.go               | 22 ++++++++++++++++++
 7 files changed, 81 insertions(+), 25 deletions(-)

diff --git a/deploy/traits.yaml b/deploy/traits.yaml
index 62288dc..69abaf6 100755
--- a/deploy/traits.yaml
+++ b/deploy/traits.yaml
@@ -530,9 +530,8 @@ traits:
   - name: filter-source-channels
     type: bool
     description: Enables filtering on events based on the header "ce-knativehistory".
-      Since this is an experimental headerthat can be removed in a future version
-      of Knative, filtering is enabled only when the integration islistening from
-      more than 1 channel.
+      Since this header has been removed in newer versions ofKnative, filtering is
+      disabled by default.
   - name: camel-source-compat
     type: bool
     description: Enables Knative CamelSource pre 0.15 compatibility fixes (will be
@@ -808,7 +807,7 @@ traits:
       property.
   - name: service-bindings
     type: '[]string'
-    description: List of Provisioned Services and ServiceBindings in the form KIND.VERSION.GROUP/NAME[/NAMESPACE]
+    description: List of Provisioned Services and ServiceBindings in the form [[apigroup/]version:]kind:[namespace/]name
 - name: service
   platform: false
   profiles:
diff --git a/docs/modules/ROOT/assets/attachments/schema/integration-schema.json b/docs/modules/ROOT/assets/attachments/schema/integration-schema.json
index 14b7051..6e72017 100644
--- a/docs/modules/ROOT/assets/attachments/schema/integration-schema.json
+++ b/docs/modules/ROOT/assets/attachments/schema/integration-schema.json
@@ -1257,6 +1257,9 @@
         },
         {
           "properties": {
+            "break-on-shutdown": {
+              "type": "string"
+            },
             "copy": {
               "type": "string"
             },
@@ -3129,7 +3132,7 @@
         "include": {
           "type": "string"
         },
-        "json-view": {
+        "json-view-type-name": {
           "type": "string"
         },
         "module-class-names": {
@@ -3272,7 +3275,7 @@
         "include": {
           "type": "string"
         },
-        "json-view": {
+        "json-view-type-name": {
           "type": "string"
         },
         "library": {
@@ -4500,15 +4503,22 @@
       ]
     },
     "org.apache.camel.model.loadbalancer.CustomLoadBalancerDefinition": {
-      "type": "object",
-      "properties": {
-        "id": {
+      "anyOf": [
+        {
           "type": "string"
         },
-        "ref": {
-          "type": "string"
+        {
+          "type": "object",
+          "properties": {
+            "id": {
+              "type": "string"
+            },
+            "ref": {
+              "type": "string"
+            }
+          }
         }
-      }
+      ]
     },
     "org.apache.camel.model.loadbalancer.FailoverLoadBalancerDefinition": {
       "type": "object",
diff --git a/docs/modules/ROOT/assets/attachments/schema/kamelet-schema.json b/docs/modules/ROOT/assets/attachments/schema/kamelet-schema.json
index 3b2cb45..3c5802b 100644
--- a/docs/modules/ROOT/assets/attachments/schema/kamelet-schema.json
+++ b/docs/modules/ROOT/assets/attachments/schema/kamelet-schema.json
@@ -1192,6 +1192,9 @@
         },
         {
           "properties": {
+            "break-on-shutdown": {
+              "type": "string"
+            },
             "copy": {
               "type": "string"
             },
@@ -3064,7 +3067,7 @@
         "include": {
           "type": "string"
         },
-        "json-view": {
+        "json-view-type-name": {
           "type": "string"
         },
         "module-class-names": {
@@ -3207,7 +3210,7 @@
         "include": {
           "type": "string"
         },
-        "json-view": {
+        "json-view-type-name": {
           "type": "string"
         },
         "library": {
@@ -4435,15 +4438,22 @@
       ]
     },
     "org.apache.camel.model.loadbalancer.CustomLoadBalancerDefinition": {
-      "type": "object",
-      "properties": {
-        "id": {
+      "anyOf": [
+        {
           "type": "string"
         },
-        "ref": {
-          "type": "string"
+        {
+          "type": "object",
+          "properties": {
+            "id": {
+              "type": "string"
+            },
+            "ref": {
+              "type": "string"
+            }
+          }
         }
-      }
+      ]
     },
     "org.apache.camel.model.loadbalancer.FailoverLoadBalancerDefinition": {
       "type": "object",
diff --git a/docs/modules/traits/pages/knative.adoc b/docs/modules/traits/pages/knative.adoc
index 4b23f30..5f54e1b 100755
--- a/docs/modules/traits/pages/knative.adoc
+++ b/docs/modules/traits/pages/knative.adoc
@@ -66,9 +66,8 @@ Can contain simple event types or full Camel URIs (to use a specific broker).
 
 | knative.filter-source-channels
 | bool
-| Enables filtering on events based on the header "ce-knativehistory". Since this is an experimental header
-that can be removed in a future version of Knative, filtering is enabled only when the integration is
-listening from more than 1 channel.
+| Enables filtering on events based on the header "ce-knativehistory". Since this header has been removed in newer versions of
+Knative, filtering is disabled by default.
 
 | knative.camel-source-compat
 | bool
diff --git a/docs/modules/traits/pages/service-binding.adoc b/docs/modules/traits/pages/service-binding.adoc
index eafff8f..75c755c 100755
--- a/docs/modules/traits/pages/service-binding.adoc
+++ b/docs/modules/traits/pages/service-binding.adoc
@@ -28,7 +28,7 @@ The following configuration options are available:
 
 | service-binding.service-bindings
 | []string
-| List of Provisioned Services and ServiceBindings in the form KIND.VERSION.GROUP/NAME[/NAMESPACE]
+| List of Provisioned Services and ServiceBindings in the form [[apigroup/]version:]kind:[namespace/]name
 
 |===
 
diff --git a/pkg/util/reference/reference.go b/pkg/util/reference/reference.go
index 0458a89..dc79cd5 100644
--- a/pkg/util/reference/reference.go
+++ b/pkg/util/reference/reference.go
@@ -18,6 +18,7 @@ limitations under the License.
 package reference
 
 import (
+	"errors"
 	"fmt"
 	"regexp"
 	"unicode"
@@ -124,5 +125,20 @@ func (c *Converter) simpleDecodeString(str string) (corev1.ObjectReference, erro
 }
 
 func (c *Converter) ToString(ref corev1.ObjectReference) (string, error) {
-	return "", nil
+	if ref.Kind == "" {
+		return "", errors.New(`object reference is missing the "kind" field`)
+	}
+	if ref.Name == "" {
+		return "", errors.New(`object reference is missing the "name" field`)
+	}
+	res := ""
+	if ref.APIVersion != "" {
+		res += ref.APIVersion + ":"
+	}
+	res += ref.Kind + ":"
+	if ref.Namespace != "" {
+		res += ref.Namespace + "/"
+	}
+	res += ref.Name
+	return res, nil
 }
diff --git a/pkg/util/reference/reference_test.go b/pkg/util/reference/reference_test.go
index 5539602..f13c5d8 100644
--- a/pkg/util/reference/reference_test.go
+++ b/pkg/util/reference/reference_test.go
@@ -32,6 +32,7 @@ func TestExpressions(t *testing.T) {
 		name          string
 		error         bool
 		ref           corev1.ObjectReference
+		stringRef     string
 	}{
 		{
 			name:  "lowercase:source",
@@ -53,6 +54,7 @@ func TestExpressions(t *testing.T) {
 				APIVersion: "camel.apache.org/v1alpha1",
 				Name:       "source",
 			},
+			stringRef: "camel.apache.org/v1alpha1:Kamelet:source",
 		},
 		{
 			name: "ns1/source",
@@ -62,6 +64,17 @@ func TestExpressions(t *testing.T) {
 				Namespace:  "ns1",
 				Name:       "source",
 			},
+			stringRef: "camel.apache.org/v1alpha1:Kamelet:ns1/source",
+		},
+		{
+			name: "v1:Secret:ns1/scr2",
+			ref: corev1.ObjectReference{
+				Kind:       "Secret",
+				APIVersion: "v1",
+				Namespace:  "ns1",
+				Name:       "scr2",
+			},
+			stringRef: "v1:Secret:ns1/scr2",
 		},
 		{
 			name: "ksvc:service",
@@ -70,6 +83,7 @@ func TestExpressions(t *testing.T) {
 				APIVersion: "serving.knative.dev/v1",
 				Name:       "service",
 			},
+			stringRef: "serving.knative.dev/v1:Service:service",
 		},
 		{
 			name: "channel:ns3/ch2",
@@ -79,6 +93,7 @@ func TestExpressions(t *testing.T) {
 				Namespace:  "ns3",
 				Name:       "ch2",
 			},
+			stringRef: "messaging.knative.dev/v1:Channel:ns3/ch2",
 		},
 		{
 			name: "broker:default",
@@ -87,6 +102,7 @@ func TestExpressions(t *testing.T) {
 				APIVersion: "eventing.knative.dev/v1",
 				Name:       "default",
 			},
+			stringRef: "eventing.knative.dev/v1:Broker:default",
 		},
 		{
 			name: "PostgreSQL:ns1/db",
@@ -95,6 +111,7 @@ func TestExpressions(t *testing.T) {
 				Namespace: "ns1",
 				Name:      "db",
 			},
+			stringRef: "PostgreSQL:ns1/db",
 		},
 		{
 			name: "postgres.org/v1alpha1:PostgreSQL:ns1/db",
@@ -104,6 +121,7 @@ func TestExpressions(t *testing.T) {
 				Namespace:  "ns1",
 				Name:       "db",
 			},
+			stringRef: "postgres.org/v1alpha1:PostgreSQL:ns1/db",
 		},
 	}
 
@@ -122,8 +140,12 @@ func TestExpressions(t *testing.T) {
 			if tc.error {
 				assert.Error(t, err)
 			} else {
+				asString, err2 := converter.ToString(ref)
+				assert.NoError(t, err2)
+
 				assert.NoError(t, err)
 				assert.Equal(t, tc.ref, ref)
+				assert.Equal(t, tc.stringRef, asString)
 			}
 		})
 	}

[camel-k] 01/03: Fix #2158: set a convention for object references

Posted by nf...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

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

commit a134e488f7de828ea62cad32f02afdf438aa3ef2
Author: nicolaferraro <ni...@gmail.com>
AuthorDate: Wed Mar 24 17:03:39 2021 +0100

    Fix #2158: set a convention for object references
---
 pkg/cmd/run.go                       |   2 +-
 pkg/trait/service_binding.go         |  55 ++++++---------
 pkg/util/reference/reference.go      | 128 ++++++++++++++++++++++++++++++++++
 pkg/util/reference/reference_test.go | 131 +++++++++++++++++++++++++++++++++++
 4 files changed, 281 insertions(+), 35 deletions(-)

diff --git a/pkg/cmd/run.go b/pkg/cmd/run.go
index a77afa5..ca2c09f 100644
--- a/pkg/cmd/run.go
+++ b/pkg/cmd/run.go
@@ -73,7 +73,7 @@ func newCmdRun(rootCmdOptions *RootCmdOptions) (*cobra.Command, *runCmdOptions)
 	}
 
 	cmd.Flags().String("name", "", "The integration name")
-	cmd.Flags().StringArrayP("connect", "c", nil, "A ServiceBinding or Provisioned Service that the integration should bind to specified as KIND.VERSION.GROUP/NAME[/NAMESPACE]")
+	cmd.Flags().StringArrayP("connect", "c", nil, "A ServiceBinding or Provisioned Service that the integration should bind to, specified as [[apigroup/]version:]kind:[namespace/]name")
 	cmd.Flags().StringArrayP("dependency", "d", nil, "An external library that should be included. E.g. for Maven dependencies \"mvn:org.my/app:1.0\"")
 	cmd.Flags().BoolP("wait", "w", false, "Wait for the integration to be running")
 	cmd.Flags().StringP("kit", "k", "", "The kit used to run the integration")
diff --git a/pkg/trait/service_binding.go b/pkg/trait/service_binding.go
index d8ebb3a..10ff8ce 100644
--- a/pkg/trait/service_binding.go
+++ b/pkg/trait/service_binding.go
@@ -19,8 +19,8 @@ package trait
 
 import (
 	"fmt"
-	"strings"
 
+	"github.com/apache/camel-k/pkg/util/reference"
 	corev1 "k8s.io/api/core/v1"
 	k8serrors "k8s.io/apimachinery/pkg/api/errors"
 	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -38,7 +38,7 @@ import (
 // +camel-k:trait=service-binding
 type serviceBindingTrait struct {
 	BaseTrait `property:",squash"`
-	// List of Provisioned Services and ServiceBindings in the form KIND.VERSION.GROUP/NAME[/NAMESPACE]
+	// List of Provisioned Services and ServiceBindings in the form [[apigroup/]version:]kind:[namespace/]name
 	ServiceBindings []string `property:"service-bindings" json:"serviceBindings,omitempty"`
 }
 
@@ -173,33 +173,23 @@ func (t *serviceBindingTrait) getServiceBinding(e *Environment, name string) (sb
 
 func (t *serviceBindingTrait) parseProvisionedServices(e *Environment) ([]sb.Service, error) {
 	services := make([]sb.Service, 0)
+	converter := reference.NewConverter("")
 	for _, s := range t.ServiceBindings {
-		seg := strings.Split(s, "/")
-		if !(len(seg) == 3 || len(seg) == 2) {
-			return nil, fmt.Errorf("ServiceBinding: %s should be specified in the form KIND.VERSION.GROUP/NAME[/NAMESPACE]", s)
+		ref, err := converter.FromString(s)
+		if err != nil {
+			return services, err
 		}
-		gvk := seg[0]
-		index := strings.Index(gvk, ".")
-		kind := seg[0][0:index]
-		if kind == "ServiceBinding" {
-			continue
-		}
-		vg := seg[0][index+1 : len(gvk)]
-		index = strings.Index(vg, ".")
-		version := vg[0:index]
-		group := vg[index+1 : len(vg)]
-		name := seg[1]
 		namespace := e.Integration.Namespace
-		if len(seg) == 3 {
-			namespace = seg[2]
+		if ref.Namespace != "" {
+			namespace = ref.Namespace
 		}
 		service := sb.Service{
 			NamespacedRef: sb.NamespacedRef{
 				Ref: sb.Ref{
-					Group:   group,
-					Version: version,
-					Kind:    kind,
-					Name:    name,
+					Group:   ref.GroupVersionKind().Group,
+					Version: ref.GroupVersionKind().Version,
+					Kind:    ref.Kind,
+					Name:    ref.Name,
 				},
 				Namespace: &namespace,
 			},
@@ -211,23 +201,20 @@ func (t *serviceBindingTrait) parseProvisionedServices(e *Environment) ([]sb.Ser
 
 func (t *serviceBindingTrait) parseServiceBindings(e *Environment) ([]string, error) {
 	serviceBindings := make([]string, 0)
+	converter := reference.NewConverter("")
 	for _, s := range t.ServiceBindings {
-		seg := strings.Split(s, "/")
-		if !(len(seg) == 3 || len(seg) == 2) {
-			return nil, fmt.Errorf("ServiceBinding: %s should be specified in the form KIND.VERSION.GROUP/NAME[/NAMESPACE]", s)
+		ref, err := converter.FromString(s)
+		if err != nil {
+			return serviceBindings, err
 		}
-		gvk := seg[0]
-		index := strings.Index(gvk, ".")
-		kind := seg[0][0:index]
-		if kind == "ServiceBinding" {
-			vg := seg[0][index+1 : len(gvk)]
-			if vg != "v1alpha1.binding.operators.coreos.com" {
-				return nil, fmt.Errorf("ServiceBinding: %s VERSION.GROUP should be v1alpha1.binding.operators.coreos.com", s)
+		if ref.Kind == "ServiceBinding" {
+			if ref.GroupVersionKind().String() != sb.GroupVersion.String() {
+				return nil, fmt.Errorf("ServiceBinding: %q api version should be %q", s, sb.GroupVersion.String())
 			}
-			if len(seg) == 3 && seg[2] != e.Integration.Namespace {
+			if ref.Namespace != e.Integration.Namespace {
 				return nil, fmt.Errorf("ServiceBinding: %s should be in the same namespace %s as the integration", s, e.Integration.Namespace)
 			}
-			serviceBindings = append(serviceBindings, seg[1])
+			serviceBindings = append(serviceBindings, ref.Name)
 		}
 	}
 	return serviceBindings, nil
diff --git a/pkg/util/reference/reference.go b/pkg/util/reference/reference.go
new file mode 100644
index 0000000..0458a89
--- /dev/null
+++ b/pkg/util/reference/reference.go
@@ -0,0 +1,128 @@
+/*
+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 reference
+
+import (
+	"fmt"
+	"regexp"
+	"unicode"
+
+	camelv1alpha1 "github.com/apache/camel-k/pkg/apis/camel/v1alpha1"
+	corev1 "k8s.io/api/core/v1"
+	eventingv1 "knative.dev/eventing/pkg/apis/eventing/v1"
+	messagingv1 "knative.dev/eventing/pkg/apis/messaging/v1"
+	servingv1 "knative.dev/serving/pkg/apis/serving/v1"
+)
+
+const (
+	KameletPrefix = "kamelet:"
+)
+
+var (
+	simpleNameRegexp = regexp.MustCompile(`^(?:(?P<namespace>[a-z0-9-.]+)/)?(?P<name>[a-z0-9-.]+)$`)
+	fullNameRegexp   = regexp.MustCompile(`^(?:(?P<apiVersion>(?:[a-z0-9-.]+/)?(?:[a-z0-9-.]+)):)?(?P<kind>[A-Za-z0-9-.]+):(?:(?P<namespace>[a-z0-9-.]+)/)?(?P<name>[a-z0-9-.]+)$`)
+
+	templates = map[string]corev1.ObjectReference{
+		"kamelet": corev1.ObjectReference{
+			Kind:       "Kamelet",
+			APIVersion: camelv1alpha1.SchemeGroupVersion.String(),
+		},
+		"channel": corev1.ObjectReference{
+			Kind:       "Channel",
+			APIVersion: messagingv1.SchemeGroupVersion.String(),
+		},
+		"broker": corev1.ObjectReference{
+			Kind:       "Broker",
+			APIVersion: eventingv1.SchemeGroupVersion.String(),
+		},
+		"ksvc": corev1.ObjectReference{
+			Kind:       "Service",
+			APIVersion: servingv1.SchemeGroupVersion.String(),
+		},
+	}
+)
+
+type Converter struct {
+	defaultPrefix string
+}
+
+func NewConverter(defaultPrefix string) *Converter {
+	return &Converter{
+		defaultPrefix: defaultPrefix,
+	}
+}
+
+func (c *Converter) FromString(str string) (corev1.ObjectReference, error) {
+	ref, err := c.simpleDecodeString(str)
+	if err != nil {
+		return ref, err
+	}
+	c.expandReference(&ref)
+
+	if ref.Kind == "" || !unicode.IsUpper([]rune(ref.Kind)[0]) {
+		return corev1.ObjectReference{}, fmt.Errorf("invalid kind: %q", ref.Kind)
+	}
+	return ref, nil
+}
+
+func (c *Converter) expandReference(ref *corev1.ObjectReference) {
+	if template, ok := templates[ref.Kind]; ok {
+		if template.Kind != "" {
+			ref.Kind = template.Kind
+		}
+		if ref.APIVersion == "" && template.APIVersion != "" {
+			ref.APIVersion = template.APIVersion
+		}
+	}
+}
+
+func (c *Converter) simpleDecodeString(str string) (corev1.ObjectReference, error) {
+	fullName := str
+	if simpleNameRegexp.MatchString(str) {
+		fullName = c.defaultPrefix + str
+	}
+
+	if fullNameRegexp.MatchString(fullName) {
+		groupNames := fullNameRegexp.SubexpNames()
+		ref := corev1.ObjectReference{}
+		for _, match := range fullNameRegexp.FindAllStringSubmatch(fullName, -1) {
+			for idx, text := range match {
+				groupName := groupNames[idx]
+				switch groupName {
+				case "apiVersion":
+					ref.APIVersion = text
+				case "namespace":
+					ref.Namespace = text
+				case "kind":
+					ref.Kind = text
+				case "name":
+					ref.Name = text
+				}
+			}
+		}
+		return ref, nil
+	}
+	if c.defaultPrefix != "" {
+		return corev1.ObjectReference{}, fmt.Errorf(`name %q does not match either "[[apigroup/]version:]kind:[namespace/]name" or "[namespace/]name"`, str)
+	}
+	return corev1.ObjectReference{}, fmt.Errorf(`name %q does not match format "[[apigroup/]version:]kind:[namespace/]name"`, str)
+}
+
+func (c *Converter) ToString(ref corev1.ObjectReference) (string, error) {
+	return "", nil
+}
diff --git a/pkg/util/reference/reference_test.go b/pkg/util/reference/reference_test.go
new file mode 100644
index 0000000..5539602
--- /dev/null
+++ b/pkg/util/reference/reference_test.go
@@ -0,0 +1,131 @@
+/*
+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 reference
+
+import (
+	"fmt"
+	"testing"
+
+	"github.com/stretchr/testify/assert"
+	corev1 "k8s.io/api/core/v1"
+)
+
+func TestExpressions(t *testing.T) {
+	emptyPrefix := ""
+	tests := []struct {
+		defaultPrefix *string
+		name          string
+		error         bool
+		ref           corev1.ObjectReference
+	}{
+		{
+			name:  "lowercase:source",
+			error: true,
+		},
+		{
+			name:  "PostgreSQL/ns/name",
+			error: true,
+		},
+		{
+			defaultPrefix: &emptyPrefix,
+			name:          "source",
+			error:         true,
+		},
+		{
+			name: "source",
+			ref: corev1.ObjectReference{
+				Kind:       "Kamelet",
+				APIVersion: "camel.apache.org/v1alpha1",
+				Name:       "source",
+			},
+		},
+		{
+			name: "ns1/source",
+			ref: corev1.ObjectReference{
+				Kind:       "Kamelet",
+				APIVersion: "camel.apache.org/v1alpha1",
+				Namespace:  "ns1",
+				Name:       "source",
+			},
+		},
+		{
+			name: "ksvc:service",
+			ref: corev1.ObjectReference{
+				Kind:       "Service",
+				APIVersion: "serving.knative.dev/v1",
+				Name:       "service",
+			},
+		},
+		{
+			name: "channel:ns3/ch2",
+			ref: corev1.ObjectReference{
+				Kind:       "Channel",
+				APIVersion: "messaging.knative.dev/v1",
+				Namespace:  "ns3",
+				Name:       "ch2",
+			},
+		},
+		{
+			name: "broker:default",
+			ref: corev1.ObjectReference{
+				Kind:       "Broker",
+				APIVersion: "eventing.knative.dev/v1",
+				Name:       "default",
+			},
+		},
+		{
+			name: "PostgreSQL:ns1/db",
+			ref: corev1.ObjectReference{
+				Kind:      "PostgreSQL",
+				Namespace: "ns1",
+				Name:      "db",
+			},
+		},
+		{
+			name: "postgres.org/v1alpha1:PostgreSQL:ns1/db",
+			ref: corev1.ObjectReference{
+				APIVersion: "postgres.org/v1alpha1",
+				Kind:       "PostgreSQL",
+				Namespace:  "ns1",
+				Name:       "db",
+			},
+		},
+	}
+
+	for i, tc := range tests {
+		t.Run(fmt.Sprintf("%d-%s", i, tc.name), func(t *testing.T) {
+
+			var converter *Converter
+			if tc.defaultPrefix != nil {
+				converter = NewConverter(*tc.defaultPrefix)
+			} else {
+				// Using kamelet: prefix by default in the tests
+				converter = NewConverter(KameletPrefix)
+			}
+
+			ref, err := converter.FromString(tc.name)
+			if tc.error {
+				assert.Error(t, err)
+			} else {
+				assert.NoError(t, err)
+				assert.Equal(t, tc.ref, ref)
+			}
+		})
+	}
+
+}

[camel-k] 03/03: Fix #2158: fix bash completion and binding handling

Posted by nf...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

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

commit 1e8039dfc36228150134db5c534b0fe647b8409d
Author: nicolaferraro <ni...@gmail.com>
AuthorDate: Wed Mar 24 23:56:38 2021 +0100

    Fix #2158: fix bash completion and binding handling
---
 pkg/cmd/completion_bash.go   | 2 +-
 pkg/trait/service_binding.go | 5 ++++-
 2 files changed, 5 insertions(+), 2 deletions(-)

diff --git a/pkg/cmd/completion_bash.go b/pkg/cmd/completion_bash.go
index 6d554c1..7da2b7b 100644
--- a/pkg/cmd/completion_bash.go
+++ b/pkg/cmd/completion_bash.go
@@ -108,7 +108,7 @@ __kamel_kubectl_get_servicebinding() {
                version=$(echo ${resource} | cut -d'/' -f 3)
                group=$(echo ${resource} | cut -d'/' -f 2)
                kind=$(echo ${resource} | cut -d'/' -f 1)
-               services_list="${services_list} ${kind}.${version}.${group}/${name}"
+               services_list="${services_list} ${group}/${version}:${kind}:${name}"
             done
             COMPREPLY=( $( compgen -W "${services_list[*]}" -- "$cur" ) )
         fi
diff --git a/pkg/trait/service_binding.go b/pkg/trait/service_binding.go
index 10ff8ce..611a387 100644
--- a/pkg/trait/service_binding.go
+++ b/pkg/trait/service_binding.go
@@ -207,8 +207,11 @@ func (t *serviceBindingTrait) parseServiceBindings(e *Environment) ([]string, er
 		if err != nil {
 			return serviceBindings, err
 		}
+		if ref.Namespace == "" {
+			ref.Namespace = e.Integration.Namespace
+		}
 		if ref.Kind == "ServiceBinding" {
-			if ref.GroupVersionKind().String() != sb.GroupVersion.String() {
+			if ref.GroupVersionKind().GroupVersion().String() != sb.GroupVersion.String() {
 				return nil, fmt.Errorf("ServiceBinding: %q api version should be %q", s, sb.GroupVersion.String())
 			}
 			if ref.Namespace != e.Integration.Namespace {