You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by as...@apache.org on 2020/10/02 11:27:53 UTC

[camel-k] branch master updated: fix: Adapt Knative service definition to new KnativeEnvironment definition

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

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


The following commit(s) were added to refs/heads/master by this push:
     new a08cb00  fix: Adapt Knative service definition to new KnativeEnvironment definition
a08cb00 is described below

commit a08cb00629aee9f6acfe855e45cd0f5b967e616f
Author: Antonin Stefanutti <an...@stefanutti.fr>
AuthorDate: Thu Oct 1 10:17:21 2020 +0200

    fix: Adapt Knative service definition to new KnativeEnvironment definition
---
 pkg/apis/camel/v1/knative/types.go         | 18 ++++++++----------
 pkg/apis/camel/v1/knative/types_support.go | 17 +----------------
 pkg/trait/knative.go                       | 18 +++++++++++++++++-
 pkg/trait/knative_test.go                  | 15 +++++++--------
 4 files changed, 33 insertions(+), 35 deletions(-)

diff --git a/pkg/apis/camel/v1/knative/types.go b/pkg/apis/camel/v1/knative/types.go
index 430c223..c4147d6 100644
--- a/pkg/apis/camel/v1/knative/types.go
+++ b/pkg/apis/camel/v1/knative/types.go
@@ -33,11 +33,14 @@ func NewCamelEnvironment() CamelEnvironment {
 
 // CamelServiceDefinition defines the parameters to connect to Knative service. It's also used for exposed services
 type CamelServiceDefinition struct {
-	ServiceType CamelServiceType  `json:"type"`
-	Name        string            `json:"name"`
-	Host        string            `json:"host,omitempty"`
-	Port        *int              `json:"port,omitempty"`
-	Metadata    map[string]string `json:"metadata,omitempty"`
+	ServiceType CamelServiceType `json:"type"`
+	Name        string           `json:"name"`
+	// Deprecated: use URL instead
+	Host string `json:"host,omitempty"`
+	// Deprecated: use URL instead
+	Port     *int              `json:"port,omitempty"`
+	URL      string            `json:"url,omitempty"`
+	Metadata map[string]string `json:"metadata,omitempty"`
 }
 
 // CamelEndpointKind --
@@ -73,11 +76,6 @@ func (s CamelServiceType) ResourceDescription(subject string) string {
 // Meta Options
 const (
 	CamelMetaServicePath = "service.path"
-	CamelMetaServiceID   = "service.id"
-	CamelMetaServiceName = "service.name"
-	CamelMetaServiceHost = "service.host"
-	CamelMetaServicePort = "service.port"
-	CamelMetaServiceZone = "service.zone"
 
 	CamelMetaKnativeKind       = "knative.kind"
 	CamelMetaKnativeAPIVersion = "knative.apiVersion"
diff --git a/pkg/apis/camel/v1/knative/types_support.go b/pkg/apis/camel/v1/knative/types_support.go
index 796b89d..f986774 100644
--- a/pkg/apis/camel/v1/knative/types_support.go
+++ b/pkg/apis/camel/v1/knative/types_support.go
@@ -20,19 +20,15 @@ package knative
 import (
 	"encoding/json"
 	"net/url"
-	"strconv"
 )
 
 // BuildCamelServiceDefinition creates a CamelServiceDefinition from a given URL
 func BuildCamelServiceDefinition(name string, endpointKind CamelEndpointKind, serviceType CamelServiceType,
 	serviceURL url.URL, apiVersion, kind string) (CamelServiceDefinition, error) {
 
-	port := 80
-
 	definition := CamelServiceDefinition{
 		Name:        name,
-		Host:        serviceURL.Host,
-		Port:        &port,
+		URL:         serviceURL.String(),
 		ServiceType: serviceType,
 		Metadata: map[string]string{
 			CamelMetaEndpointKind:      string(endpointKind),
@@ -40,17 +36,6 @@ func BuildCamelServiceDefinition(name string, endpointKind CamelEndpointKind, se
 			CamelMetaKnativeKind:       kind,
 		},
 	}
-	portStr := serviceURL.Port()
-	if portStr != "" {
-		port, err := strconv.Atoi(portStr)
-		if err != nil {
-			return CamelServiceDefinition{}, err
-		}
-		definition.Port = &port
-	}
-	if serviceURL.Path != "" {
-		definition.Metadata[CamelMetaServicePath] = serviceURL.Path
-	}
 
 	return definition, nil
 }
diff --git a/pkg/trait/knative.go b/pkg/trait/knative.go
index 5ff9c46..ed41b6b 100644
--- a/pkg/trait/knative.go
+++ b/pkg/trait/knative.go
@@ -21,15 +21,16 @@ import (
 	"fmt"
 	"net/url"
 	"reflect"
+	"strconv"
 	"strings"
 
-	"github.com/apache/camel-k/pkg/util/kubernetes"
 	v1 "github.com/apache/camel-k/pkg/apis/camel/v1"
 	knativeapi "github.com/apache/camel-k/pkg/apis/camel/v1/knative"
 	"github.com/apache/camel-k/pkg/metadata"
 	"github.com/apache/camel-k/pkg/util"
 	"github.com/apache/camel-k/pkg/util/envvar"
 	knativeutil "github.com/apache/camel-k/pkg/util/knative"
+	"github.com/apache/camel-k/pkg/util/kubernetes"
 	"github.com/pkg/errors"
 	corev1 "k8s.io/api/core/v1"
 	k8serrors "k8s.io/apimachinery/pkg/api/errors"
@@ -243,6 +244,21 @@ func (t *knativeTrait) Apply(e *Environment) error {
 			}
 		}
 
+		// Convert deprecated Host and Port fields to URL field
+		// Can be removed once CamelSource controller migrate to the new API
+		for i, service := range env.Services {
+			if service.URL == "" {
+				URL := "http://" + service.Host
+				if service.Port != nil {
+					URL = URL + ":" + strconv.Itoa(*service.Port)
+				}
+				service.URL = URL
+				service.Host = ""
+				service.Port = nil
+				env.Services[i] = service
+			}
+		}
+
 		if err := t.configureChannels(e, &env); err != nil {
 			return err
 		}
diff --git a/pkg/trait/knative_test.go b/pkg/trait/knative_test.go
index 63586c9..6bea953 100644
--- a/pkg/trait/knative_test.go
+++ b/pkg/trait/knative_test.go
@@ -123,28 +123,28 @@ func TestKnativeEnvConfigurationFromTrait(t *testing.T) {
 
 	cSource1 := ne.FindService("channel-source-1", knativeapi.CamelEndpointKindSource, knativeapi.CamelServiceTypeChannel, "messaging.knative.dev/v1beta1", "Channel")
 	assert.NotNil(t, cSource1)
-	assert.Empty(t, cSource1.Host)
+	assert.Empty(t, cSource1.URL)
 
 	cSink1 := ne.FindService("channel-sink-1", knativeapi.CamelEndpointKindSink, knativeapi.CamelServiceTypeChannel, "messaging.knative.dev/v1beta1", "Channel")
 	assert.NotNil(t, cSink1)
-	assert.Equal(t, "channel-sink-1.host", cSink1.Host)
+	assert.Equal(t, "http://channel-sink-1.host/", cSink1.URL)
 
 	eSource1 := ne.FindService("endpoint-source-1", knativeapi.CamelEndpointKindSource, knativeapi.CamelServiceTypeEndpoint, "serving.knative.dev/v1", "Service")
 	assert.NotNil(t, eSource1)
-	assert.Empty(t, eSource1.Host)
+	assert.Empty(t, eSource1.URL)
 
 	eSink1 := ne.FindService("endpoint-sink-1", knativeapi.CamelEndpointKindSink, knativeapi.CamelServiceTypeEndpoint, "serving.knative.dev/v1", "Service")
 	assert.NotNil(t, eSink1)
-	assert.Equal(t, "endpoint-sink-1.host", eSink1.Host)
+	assert.Equal(t, "http://endpoint-sink-1.host/", eSink1.URL)
 	eSink2 := ne.FindService("endpoint-sink-2", knativeapi.CamelEndpointKindSink, knativeapi.CamelServiceTypeEndpoint, "serving.knative.dev/v1", "Service")
 	assert.NotNil(t, eSink2)
-	assert.Equal(t, "endpoint-sink-2.host", eSink2.Host)
+	assert.Equal(t, "http://endpoint-sink-2.host/", eSink2.URL)
 
 	eEventSource := ne.FindService("default", knativeapi.CamelEndpointKindSource, knativeapi.CamelServiceTypeEvent, "eventing.knative.dev/v1beta1", "Broker")
 	assert.NotNil(t, eEventSource)
 	eEventSink := ne.FindService("default", knativeapi.CamelEndpointKindSink, knativeapi.CamelServiceTypeEvent, "eventing.knative.dev/v1beta1", "Broker")
 	assert.NotNil(t, eEventSink)
-	assert.Equal(t, "broker-default.host", eEventSink.Host)
+	assert.Equal(t, "http://broker-default.host/", eEventSink.URL)
 }
 
 func TestKnativeEnvConfigurationFromSource(t *testing.T) {
@@ -243,8 +243,7 @@ func TestKnativeEnvConfigurationFromSource(t *testing.T) {
 
 	source := ne.FindService("s3fileMover1", knativeapi.CamelEndpointKindSource, knativeapi.CamelServiceTypeEndpoint, "serving.knative.dev/v1", "Service")
 	assert.NotNil(t, source)
-	assert.Empty(t, source.Host)
-	assert.Nil(t, source.Port)
+	assert.Empty(t, source.URL)
 	assert.Empty(t, source.Metadata[knativeapi.CamelMetaKnativeReply])
 
 	channel := ne.FindService("channel-source-1", knativeapi.CamelEndpointKindSource, knativeapi.CamelServiceTypeChannel, "", "")