Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Route trait does not work if route.auto is set to false #783

Merged
merged 1 commit into from
Jul 1, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 9 additions & 13 deletions pkg/trait/route.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ limitations under the License.
package trait

import (
"errors"
"reflect"

"github.com/apache/camel-k/pkg/apis/camel/v1alpha1"
Expand All @@ -31,15 +30,15 @@ import (

type routeTrait struct {
BaseTrait `property:",squash"`
Auto *bool `property:"auto"`
Host string `property:"host"`
TLSTermination string `property:"tls-termination"`
TLSCertificate string `property:"tls-certificate"`
TLSKey string `property:"tls-key"`
TLSCACertificate string `property:"tls-ca-certificate"`
TLSDestinationCACertificate string `property:"tls-destination-ca-certificate"`
TLSInsecureEdgeTerminationPolicy string `property:"tls-insecure-edge-termination-policy"`
service *corev1.Service

service *corev1.Service
}

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

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

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

return true, nil
Expand All @@ -80,7 +73,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 +87,8 @@ func (t *routeTrait) getTargetService(e *Environment) (service *corev1.Service)
}
}
})
return

return service
}

func (t *routeTrait) getRouteFor(service *corev1.Service) *routev1.Route {
Expand Down
77 changes: 54 additions & 23 deletions pkg/trait/route_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import (
"context"
"testing"

"github.com/rs/xid"

"github.com/scylladb/go-set/strset"

"github.com/apache/camel-k/pkg/apis/camel/v1alpha1"
Expand All @@ -34,7 +36,7 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

func createTestRouteEnvironment(t *testing.T) *Environment {
func createTestRouteEnvironment(t *testing.T, name string) *Environment {
catalog, err := test.DefaultCatalog()
assert.Nil(t, err)

Expand All @@ -43,7 +45,7 @@ func createTestRouteEnvironment(t *testing.T) *Environment {
Catalog: NewCatalog(context.TODO(), nil),
Integration: &v1alpha1.Integration{
ObjectMeta: metav1.ObjectMeta{
Name: "test-i",
Name: name,
Namespace: "test-ns",
},
Status: v1alpha1.IntegrationStatus{
Expand All @@ -68,31 +70,34 @@ 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: name,
Namespace: "test-ns",
Labels: map[string]string{
"camel.apache.org/integration": name,
"camel.apache.org/service.type": ServiceTypeUser,
},
},
Spec: corev1.ServiceSpec{
Ports: []corev1.ServicePort{},
Selector: map[string]string{
"camel.apache.org/integration": name,
},
},
},
}),
),
}
}

func TestRoute_Default(t *testing.T) {
environment := createTestRouteEnvironment(t)
name := xid.New().String()
environment := createTestRouteEnvironment(t, name)
traitsCatalog := environment.Catalog

err := traitsCatalog.apply(environment)
Expand All @@ -102,15 +107,41 @@ func TestRoute_Default(t *testing.T) {
assert.NotNil(t, environment.GetTrait(ID("route")))

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

assert.NotNil(t, route)
assert.Nil(t, route.Spec.TLS)
}

func TestRoute_Disabled(t *testing.T) {
name := xid.New().String()
environment := createTestRouteEnvironment(t, name)
environment.Integration.Spec.Traits = map[string]v1alpha1.TraitSpec{
"route": {
Configuration: map[string]string{
"enabled": "false",
},
},
}

traitsCatalog := environment.Catalog
err := traitsCatalog.apply(environment)

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

route := environment.Resources.GetRoute(func(r *routev1.Route) bool {
return r.ObjectMeta.Name == name
})

assert.Nil(t, route)
}

func TestRoute_TLS(t *testing.T) {
environment := createTestRouteEnvironment(t)
name := xid.New().String()
environment := createTestRouteEnvironment(t, name)
traitsCatalog := environment.Catalog

environment.Integration.Spec.Traits = map[string]v1alpha1.TraitSpec{
Expand All @@ -128,7 +159,7 @@ func TestRoute_TLS(t *testing.T) {
assert.NotNil(t, environment.GetTrait(ID("route")))

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

assert.NotNil(t, route)
Expand Down