You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by lb...@apache.org on 2019/07/18 16:47:24 UTC

[camel-k] branch master updated: route trait assumes target port name is always 'http' #837

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


The following commit(s) were added to refs/heads/master by this push:
     new b03fe86  route trait assumes target port name is always 'http' #837
b03fe86 is described below

commit b03fe8609a0bd1f42218fd11fb665cee6e2e5546
Author: lburgazzoli <lb...@gmail.com>
AuthorDate: Thu Jul 18 11:48:42 2019 +0200

    route trait assumes target port name is always 'http' #837
---
 pkg/trait/route.go      |  8 +++++++-
 pkg/trait/route_test.go | 35 +++++++++++++++++++++++++++++++++++
 2 files changed, 42 insertions(+), 1 deletion(-)

diff --git a/pkg/trait/route.go b/pkg/trait/route.go
index c3eb320..f761a48 100644
--- a/pkg/trait/route.go
+++ b/pkg/trait/route.go
@@ -80,6 +80,12 @@ func (t *routeTrait) Configure(e *Environment) (bool, error) {
 }
 
 func (t *routeTrait) Apply(e *Environment) error {
+	servicePortName := httpPortName
+	dt := e.Catalog.GetTrait(containerTraitID)
+	if dt != nil {
+		servicePortName = dt.(*containerTrait).ServicePortName
+	}
+
 	route := routev1.Route{
 		TypeMeta: metav1.TypeMeta{
 			Kind:       "Route",
@@ -91,7 +97,7 @@ func (t *routeTrait) Apply(e *Environment) error {
 		},
 		Spec: routev1.RouteSpec{
 			Port: &routev1.RoutePort{
-				TargetPort: intstr.FromString("http"),
+				TargetPort: intstr.FromString(servicePortName),
 			},
 			To: routev1.RouteTargetReference{
 				Kind: "Service",
diff --git a/pkg/trait/route_test.go b/pkg/trait/route_test.go
index 0f81683..0d270fc 100644
--- a/pkg/trait/route_test.go
+++ b/pkg/trait/route_test.go
@@ -104,6 +104,7 @@ func TestRoute_Default(t *testing.T) {
 
 	assert.Nil(t, err)
 	assert.NotEmpty(t, environment.ExecutedTraits)
+	assert.NotNil(t, environment.GetTrait(ID("container")))
 	assert.NotNil(t, environment.GetTrait(ID("route")))
 
 	route := environment.Resources.GetRoute(func(r *routev1.Route) bool {
@@ -112,6 +113,8 @@ func TestRoute_Default(t *testing.T) {
 
 	assert.NotNil(t, route)
 	assert.Nil(t, route.Spec.TLS)
+	assert.NotNil(t, route.Spec.Port)
+	assert.Equal(t, httpPortName, route.Spec.Port.TargetPort.StrVal)
 }
 
 func TestRoute_Disabled(t *testing.T) {
@@ -166,3 +169,35 @@ func TestRoute_TLS(t *testing.T) {
 	assert.NotNil(t, route.Spec.TLS)
 	assert.Equal(t, routev1.TLSTerminationEdge, route.Spec.TLS.Termination)
 }
+
+func TestRoute_WithCustomServicePort(t *testing.T) {
+	name := xid.New().String()
+	environment := createTestRouteEnvironment(t, name)
+	environment.Integration.Spec.Traits = map[string]v1alpha1.TraitSpec{
+		containerTraitID: {
+			Configuration: map[string]string{
+				"service-port-name": "my-port",
+			},
+		},
+	}
+
+	traitsCatalog := environment.Catalog
+	err := traitsCatalog.apply(environment)
+
+	assert.Nil(t, err)
+	assert.NotEmpty(t, environment.ExecutedTraits)
+	assert.NotNil(t, environment.GetTrait(ID("container")))
+	assert.NotNil(t, environment.GetTrait(ID("route")))
+
+	route := environment.Resources.GetRoute(func(r *routev1.Route) bool {
+		return r.ObjectMeta.Name == name
+	})
+
+	assert.NotNil(t, route)
+	assert.NotNil(t, route.Spec.Port)
+	assert.Equal(
+		t,
+		environment.Integration.Spec.Traits[containerTraitID].Configuration["service-port-name"],
+		route.Spec.Port.TargetPort.StrVal,
+	)
+}