Skip to content

Commit

Permalink
Route trait does not work if route.auto is set to false #767
Browse files Browse the repository at this point in the history
  • Loading branch information
lburgazzoli committed Jun 27, 2019
1 parent 3a4c882 commit c5fcefa
Show file tree
Hide file tree
Showing 2 changed files with 113 additions and 25 deletions.
42 changes: 34 additions & 8 deletions pkg/trait/route.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,11 @@ package trait

import (
"errors"
"fmt"
"reflect"

"github.com/apache/camel-k/pkg/util/kubernetes"

"github.com/apache/camel-k/pkg/apis/camel/v1alpha1"

routev1 "github.com/openshift/api/route/v1"
Expand All @@ -39,7 +42,10 @@ type routeTrait struct {
TLSCACertificate string `property:"tls-ca-certificate"`
TLSDestinationCACertificate string `property:"tls-destination-ca-certificate"`
TLSInsecureEdgeTerminationPolicy string `property:"tls-insecure-edge-termination-policy"`
service *corev1.Service
ServiceName string `property:"service-name"`
ServiceNamespace string `property:"service-namespace"`

service *corev1.Service
}

func newRouteTrait() *routeTrait {
Expand All @@ -57,17 +63,34 @@ func (t *routeTrait) Configure(e *Environment) (bool, error) {
return false, nil
}

if t.Auto == nil || *t.Auto {
if t.Auto != nil && !*t.Auto && t.ServiceName == "" {
return false, errors.New("cannot apply route trait as auto configuration is turned off and no service name defined")
}

if t.ServiceName != "" {
ns := e.Integration.Namespace
if t.ServiceNamespace != "" {
ns = t.ServiceNamespace
}

s, err := kubernetes.GetService(e.C, e.Client, t.ServiceName, ns)
if err != nil {
return false, err
}
if s == nil {
return false, fmt.Errorf("cannot apply route trait: unable to lookup service %s/%s", ns, t.ServiceName)
}

t.service = s
}

if t.service == nil && (t.Auto == nil || *t.Auto) {
t.service = t.getTargetService(e)
if t.service == nil {
return false, nil
}
}

if t.service == nil {
return false, errors.New("cannot apply route trait: no target service")
}

return true, nil
}

Expand All @@ -80,7 +103,9 @@ func (t *routeTrait) Apply(e *Environment) error {
return nil
}

func (t *routeTrait) getTargetService(e *Environment) (service *corev1.Service) {
func (t *routeTrait) getTargetService(e *Environment) *corev1.Service {
var service *corev1.Service

e.Resources.VisitService(func(s *corev1.Service) {
if s.ObjectMeta.Labels != nil {
if s.ObjectMeta.Labels["camel.apache.org/integration"] == e.Integration.Name &&
Expand All @@ -92,7 +117,8 @@ func (t *routeTrait) getTargetService(e *Environment) (service *corev1.Service)
}
}
})
return

return service
}

func (t *routeTrait) getRouteFor(service *corev1.Service) *routev1.Route {
Expand Down
96 changes: 79 additions & 17 deletions pkg/trait/route_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,26 +68,28 @@ func createTestRouteEnvironment(t *testing.T) *Environment {
EnvVars: make([]corev1.EnvVar, 0),
ExecutedTraits: make([]Trait, 0),
Classpath: strset.New(),
Resources: kubernetes.NewCollection(&corev1.Service{
TypeMeta: metav1.TypeMeta{
Kind: "Service",
APIVersion: "v1",
},
ObjectMeta: metav1.ObjectMeta{
Name: "test-i",
Namespace: "test-ns",
Labels: map[string]string{
"camel.apache.org/integration": "test-i",
"camel.apache.org/service.type": ServiceTypeUser,
Resources: kubernetes.NewCollection(
&corev1.Service{
TypeMeta: metav1.TypeMeta{
Kind: "Service",
APIVersion: "v1",
},
},
Spec: corev1.ServiceSpec{
Ports: []corev1.ServicePort{},
Selector: map[string]string{
"camel.apache.org/integration": "test-i",
ObjectMeta: metav1.ObjectMeta{
Name: "test-i",
Namespace: "test-ns",
Labels: map[string]string{
"camel.apache.org/integration": "test-i",
"camel.apache.org/service.type": ServiceTypeUser,
},
},
Spec: corev1.ServiceSpec{
Ports: []corev1.ServicePort{},
Selector: map[string]string{
"camel.apache.org/integration": "test-i",
},
},
},
}),
),
}
}

Expand Down Expand Up @@ -135,3 +137,63 @@ func TestRoute_TLS(t *testing.T) {
assert.NotNil(t, route.Spec.TLS)
assert.Equal(t, routev1.TLSTerminationEdge, route.Spec.TLS.Termination)
}

func TestRoute_DisabledAuto(t *testing.T) {
environment := createTestRouteEnvironment(t)
traitsCatalog := environment.Catalog

c, err := test.NewFakeClient(
&corev1.Service{
TypeMeta: metav1.TypeMeta{
Kind: "Service",
APIVersion: "v1",
},
ObjectMeta: metav1.ObjectMeta{
Name: "test-i-named",
Namespace: "test-ns",
},
},
)

assert.Nil(t, err)

environment.C = context.TODO()
environment.Client = c
environment.Integration.Spec.Traits = map[string]v1alpha1.TraitSpec{
"route": {
Configuration: map[string]string{
"auto": "false",
"service-name": "test-i-named",
},
},
}

err = traitsCatalog.apply(environment)

assert.Nil(t, err)
assert.NotEmpty(t, environment.ExecutedTraits)
assert.NotNil(t, environment.GetTrait(ID("route")))

route := environment.Resources.GetRoute(func(r *routev1.Route) bool {
return r.ObjectMeta.Name == "test-i-named"
})

assert.NotNil(t, route)
}

func TestRoute_WrongConfiguration(t *testing.T) {
environment := createTestRouteEnvironment(t)
traitsCatalog := environment.Catalog

environment.Integration.Spec.Traits = map[string]v1alpha1.TraitSpec{
"route": {
Configuration: map[string]string{
"auto": "false",
},
},
}

err := traitsCatalog.apply(environment)

assert.NotNil(t, err)
}

0 comments on commit c5fcefa

Please sign in to comment.