From c5fcefac9efcac18fc14d2873c9672f5a76a4ecf Mon Sep 17 00:00:00 2001 From: lburgazzoli Date: Thu, 27 Jun 2019 18:22:16 +0200 Subject: [PATCH] Route trait does not work if route.auto is set to false #767 --- pkg/trait/route.go | 42 ++++++++++++++---- pkg/trait/route_test.go | 96 +++++++++++++++++++++++++++++++++-------- 2 files changed, 113 insertions(+), 25 deletions(-) diff --git a/pkg/trait/route.go b/pkg/trait/route.go index ab7c5f4919..1a6854dc39 100644 --- a/pkg/trait/route.go +++ b/pkg/trait/route.go @@ -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" @@ -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 { @@ -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 } @@ -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 && @@ -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 { diff --git a/pkg/trait/route_test.go b/pkg/trait/route_test.go index ca45334fd4..dba1a99804 100644 --- a/pkg/trait/route_test.go +++ b/pkg/trait/route_test.go @@ -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", + }, }, }, - }), + ), } } @@ -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) +}