Repository: apache/camel-k (go)

Issue:
route trait assumes target port name is always 'http'

Pull request description:
route trait assumes target port name is always 'http'
Fixes #837

Patch:
diff --git a/pkg/trait/route.go b/pkg/trait/route.go
index c3eb320c5..f761a489a 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",


Test patch:
diff --git a/pkg/trait/route_test.go b/pkg/trait/route_test.go
index 0f81683d5..0d270fc11 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,
+	)
+}