Skip to content

Commit

Permalink
route trait assumes target port name is always 'http' apache#837
Browse files Browse the repository at this point in the history
  • Loading branch information
lburgazzoli committed Jul 18, 2019
1 parent c558322 commit 3253b53
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
8 changes: 7 additions & 1 deletion pkg/trait/route.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand Down
35 changes: 35 additions & 0 deletions pkg/trait/route_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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) {
Expand Down Expand Up @@ -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,
)
}

0 comments on commit 3253b53

Please sign in to comment.