This is an automated email from the ASF dual-hosted git repository.
lburgazzoli pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/camel-k.git
The following commit(s) were added to refs/heads/master by this push:
new 4ac6992 Route trait does not work if route.auto is set to false #767
4ac6992 is described below
commit 4ac699206c670e597f3453ae000e213fc56faf2b
Author: lburgazzoli <[email protected]>
AuthorDate: Thu Jun 27 18:22:16 2019 +0200
Route trait does not work if route.auto is set to false #767
---
pkg/trait/route.go | 22 ++++++--------
pkg/trait/route_test.go | 77 ++++++++++++++++++++++++++++++++++---------------
2 files changed, 63 insertions(+), 36 deletions(-)
diff --git a/pkg/trait/route.go b/pkg/trait/route.go
index ab7c5f4..2d53872 100644
--- a/pkg/trait/route.go
+++ b/pkg/trait/route.go
@@ -18,7 +18,6 @@ limitations under the License.
package trait
import (
- "errors"
"reflect"
"github.com/apache/camel-k/pkg/apis/camel/v1alpha1"
@@ -31,7 +30,6 @@ 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"`
@@ -39,7 +37,8 @@ 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
+
+ service *corev1.Service
}
func newRouteTrait() *routeTrait {
@@ -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
@@ -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 &&
@@ -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 {
diff --git a/pkg/trait/route_test.go b/pkg/trait/route_test.go
index ca45334..48310fd 100644
--- a/pkg/trait/route_test.go
+++ b/pkg/trait/route_test.go
@@ -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"
@@ -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)
@@ -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{
@@ -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)
@@ -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{
@@ -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)