You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by lb...@apache.org on 2018/10/08 14:19:44 UTC

[camel-k] 04/14: Added route trait

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

commit 56a7b4db16e660a93a1106888704b5ad72cce6bd
Author: nferraro <ni...@gmail.com>
AuthorDate: Thu Oct 4 13:49:11 2018 +0200

    Added route trait
---
 pkg/trait/base.go                 | 12 +++++++----
 pkg/trait/route.go                | 42 ++++++++++++++++++++++++++++++++++++---
 pkg/trait/service.go              | 12 +++++------
 pkg/util/kubernetes/collection.go |  9 +++++++++
 4 files changed, 62 insertions(+), 13 deletions(-)

diff --git a/pkg/trait/base.go b/pkg/trait/base.go
index d831d79..6ba4dfc 100644
--- a/pkg/trait/base.go
+++ b/pkg/trait/base.go
@@ -59,7 +59,9 @@ func (*baseTrait) getConfigMapFor(e Environment) *corev1.ConfigMap {
 		ObjectMeta: metav1.ObjectMeta{
 			Name:      e.Integration.Name,
 			Namespace: e.Integration.Namespace,
-			Labels:    e.Integration.Labels,
+			Labels: map[string]string{
+				"camel.apache.org/integration": e.Integration.Name,
+			},
 			Annotations: map[string]string{
 				"camel.apache.org/source.language": string(e.Integration.Spec.Source.Language),
 				"camel.apache.org/source.name":     e.Integration.Spec.Source.Name,
@@ -112,9 +114,11 @@ func (*baseTrait) getDeploymentFor(e Environment) *appsv1.Deployment {
 			APIVersion: appsv1.SchemeGroupVersion.String(),
 		},
 		ObjectMeta: metav1.ObjectMeta{
-			Name:        e.Integration.Name,
-			Namespace:   e.Integration.Namespace,
-			Labels:      e.Integration.Labels,
+			Name:      e.Integration.Name,
+			Namespace: e.Integration.Namespace,
+			Labels: map[string]string{
+				"camel.apache.org/integration": e.Integration.Name,
+			},
 			Annotations: e.Integration.Annotations,
 		},
 		Spec: appsv1.DeploymentSpec{
diff --git a/pkg/trait/route.go b/pkg/trait/route.go
index 681c8b1..1906442 100644
--- a/pkg/trait/route.go
+++ b/pkg/trait/route.go
@@ -20,6 +20,9 @@ package trait
 import (
 	"github.com/apache/camel-k/pkg/util/kubernetes"
 	routev1 "github.com/openshift/api/route/v1"
+	corev1 "k8s.io/api/core/v1"
+	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
+	"k8s.io/apimachinery/pkg/util/intstr"
 )
 
 type routeTrait struct {
@@ -30,9 +33,42 @@ func (*routeTrait) ID() ID {
 }
 
 func (e *routeTrait) Customize(environment Environment, resources *kubernetes.Collection) (bool, error) {
-	return true, nil
+	var service *corev1.Service
+	resources.VisitService(func(s *corev1.Service) {
+		if s.ObjectMeta.Labels != nil {
+			if intName, ok := s.ObjectMeta.Labels["camel.apache.org/integration"]; ok && intName == environment.Integration.Name {
+				service = s
+			}
+		}
+	})
+
+	if service != nil {
+		resources.Add(e.getRouteFor(environment, service))
+		return true, nil
+	}
+
+	return false, nil
 }
 
-func (*routeTrait) getRouteFor(e Environment) *routev1.Route {
-	return nil
+func (*routeTrait) getRouteFor(e Environment, service *corev1.Service) *routev1.Route {
+	route := routev1.Route{
+		TypeMeta: metav1.TypeMeta{
+			Kind:       "Route",
+			APIVersion: routev1.SchemeGroupVersion.String(),
+		},
+		ObjectMeta: metav1.ObjectMeta{
+			Name:      service.Name,
+			Namespace: service.Namespace,
+		},
+		Spec: routev1.RouteSpec{
+			Port: &routev1.RoutePort{
+				TargetPort: intstr.FromString("http"),
+			},
+			To: routev1.RouteTargetReference{
+				Kind: "Service",
+				Name: service.Name,
+			},
+		},
+	}
+	return &route
 }
diff --git a/pkg/trait/service.go b/pkg/trait/service.go
index 3576f94..a6438d8 100644
--- a/pkg/trait/service.go
+++ b/pkg/trait/service.go
@@ -58,7 +58,9 @@ func (*serviceTrait) getServiceFor(e Environment) *corev1.Service {
 		ObjectMeta: metav1.ObjectMeta{
 			Name:      e.Integration.Name,
 			Namespace: e.Integration.Namespace,
-			Labels:    e.Integration.Labels,
+			Labels: map[string]string{
+				"camel.apache.org/integration": e.Integration.Name,
+			},
 		},
 		Spec: corev1.ServiceSpec{
 			Ports: []corev1.ServicePort{
@@ -66,11 +68,9 @@ func (*serviceTrait) getServiceFor(e Environment) *corev1.Service {
 					Name:     "http",
 					Port:     80,
 					Protocol: corev1.ProtocolTCP,
-					TargetPort: intstr.IntOrString{
-						// TODO discovering the real port is hard - maybe we should just set 8080 as conventional port in the doc
-						// or allow users to configure it in the trait configuration section
-						IntVal: 8080,
-					},
+					// TODO discovering the real port is hard - maybe we should just set 8080 as conventional port in the doc
+					// or allow users to configure it in the trait configuration section
+					TargetPort: intstr.FromInt(8080),
 				},
 			},
 			Selector: map[string]string{
diff --git a/pkg/util/kubernetes/collection.go b/pkg/util/kubernetes/collection.go
index 91cb7ee..0b5f217 100644
--- a/pkg/util/kubernetes/collection.go
+++ b/pkg/util/kubernetes/collection.go
@@ -64,6 +64,15 @@ func (c *Collection) VisitConfigMap(visitor func(*corev1.ConfigMap)) {
 	})
 }
 
+// VisitService executes the visitor function on all Service resources
+func (c *Collection) VisitService(visitor func(*corev1.Service)) {
+	c.Visit(func(res runtime.Object) {
+		if conv, ok := res.(*corev1.Service); ok {
+			visitor(conv)
+		}
+	})
+}
+
 // VisitMetaObject executes the visitor function on all meta.Object resources
 func (c *Collection) VisitMetaObject(visitor func(metav1.Object)) {
 	c.Visit(func(res runtime.Object) {