camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From lburgazz...@apache.org
Subject [camel-k] 03/14: Added service trait
Date Mon, 08 Oct 2018 14:19:43 GMT
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 8052da5abed8460b7fb0ca7aa375c52172edc0f1
Author: nferraro <ni.ferraro@gmail.com>
AuthorDate: Thu Oct 4 13:15:51 2018 +0200

    Added service trait
---
 pkg/trait/base.go                 | 21 +++------
 pkg/trait/catalog.go              |  8 ++--
 pkg/trait/{expose.go => route.go} | 15 ++++---
 pkg/trait/service.go              | 92 +++++++++++++++++++++++++++++++++++++++
 pkg/trait/trait.go                |  4 --
 5 files changed, 113 insertions(+), 27 deletions(-)

diff --git a/pkg/trait/base.go b/pkg/trait/base.go
index 69a46b9..d831d79 100644
--- a/pkg/trait/base.go
+++ b/pkg/trait/base.go
@@ -35,17 +35,8 @@ func (*baseTrait) ID() ID {
 }
 
 func (d *baseTrait) Customize(environment Environment, resources *kubernetes.Collection)
(bool, error) {
-	var cm *corev1.ConfigMap
-	var err error
-	if cm, err = d.getConfigMapFor(environment); err != nil {
-		return false, err
-	}
-	resources.Add(cm)
-	var depl *appsv1.Deployment
-	if depl, err = d.getDeploymentFor(environment); err != nil {
-		return false, err
-	}
-	resources.Add(depl)
+	resources.Add(d.getConfigMapFor(environment))
+	resources.Add(d.getDeploymentFor(environment))
 	return true, nil
 }
 
@@ -55,7 +46,7 @@ func (d *baseTrait) Customize(environment Environment, resources *kubernetes.Col
 //
 // **********************************
 
-func (*baseTrait) getConfigMapFor(e Environment) (*corev1.ConfigMap, error) {
+func (*baseTrait) getConfigMapFor(e Environment) *corev1.ConfigMap {
 	// combine properties of integration with context, integration
 	// properties have the priority
 	properties := CombineConfigurationAsMap("property", e.Context, e.Integration)
@@ -80,7 +71,7 @@ func (*baseTrait) getConfigMapFor(e Environment) (*corev1.ConfigMap, error)
{
 		},
 	}
 
-	return &cm, nil
+	return &cm
 }
 
 // **********************************
@@ -89,7 +80,7 @@ func (*baseTrait) getConfigMapFor(e Environment) (*corev1.ConfigMap, error)
{
 //
 // **********************************
 
-func (*baseTrait) getDeploymentFor(e Environment) (*appsv1.Deployment, error) {
+func (*baseTrait) getDeploymentFor(e Environment) *appsv1.Deployment {
 	sourceName := strings.TrimPrefix(e.Integration.Spec.Source.Name, "/")
 
 	// combine environment of integration with context, integration
@@ -240,5 +231,5 @@ func (*baseTrait) getDeploymentFor(e Environment) (*appsv1.Deployment,
error) {
 	deployment.Spec.Template.Spec.Volumes = vols
 	deployment.Spec.Template.Spec.Containers[0].VolumeMounts = mnts
 
-	return &deployment, nil
+	return &deployment
 }
diff --git a/pkg/trait/catalog.go b/pkg/trait/catalog.go
index 3325860..a969012 100644
--- a/pkg/trait/catalog.go
+++ b/pkg/trait/catalog.go
@@ -23,8 +23,9 @@ import (
 )
 
 var (
-	tExpose = &exposeTrait{}
 	tBase = &baseTrait{}
+	tService = &serviceTrait{}
+	tRoute = &routeTrait{}
 	tOwner = &ownerTrait{}
 )
 
@@ -34,13 +35,14 @@ func CustomizersFor(environment Environment) Customizer {
 	case v1alpha1.IntegrationPlatformClusterOpenShift:
 		return compose(
 			tBase,
-			tExpose,
+			tService,
+			tRoute,
 			tOwner,
 		)
 	case v1alpha1.IntegrationPlatformClusterKubernetes:
 		return compose(
 			tBase,
-			tExpose,
+			tService,
 			tOwner,
 		)
 		// case Knative: ...
diff --git a/pkg/trait/expose.go b/pkg/trait/route.go
similarity index 72%
rename from pkg/trait/expose.go
rename to pkg/trait/route.go
index 2dac116..681c8b1 100644
--- a/pkg/trait/expose.go
+++ b/pkg/trait/route.go
@@ -19,15 +19,20 @@ package trait
 
 import (
 	"github.com/apache/camel-k/pkg/util/kubernetes"
+	routev1 "github.com/openshift/api/route/v1"
 )
 
-type exposeTrait struct {
+type routeTrait struct {
 }
 
-func (*exposeTrait) ID() ID {
-	return ID("expose")
+func (*routeTrait) ID() ID {
+	return ID("route")
 }
 
-func (*exposeTrait) Customize(environment Environment, resources *kubernetes.Collection)
(bool, error) {
-	return false, nil
+func (e *routeTrait) Customize(environment Environment, resources *kubernetes.Collection)
(bool, error) {
+	return true, nil
+}
+
+func (*routeTrait) getRouteFor(e Environment) *routev1.Route {
+	return nil
 }
diff --git a/pkg/trait/service.go b/pkg/trait/service.go
new file mode 100644
index 0000000..3576f94
--- /dev/null
+++ b/pkg/trait/service.go
@@ -0,0 +1,92 @@
+/*
+Licensed to the Apache Software Foundation (ASF) under one or more
+contributor license agreements.  See the NOTICE file distributed with
+this work for additional information regarding copyright ownership.
+The ASF licenses this file to You under the Apache License, Version 2.0
+(the "License"); you may not use this file except in compliance with
+the License.  You may obtain a copy of the License at
+
+   http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing, software
+distributed under the License is distributed on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+See the License for the specific language governing permissions and
+limitations under the License.
+*/
+
+package trait
+
+import (
+	"github.com/apache/camel-k/pkg/util/kubernetes"
+	corev1 "k8s.io/api/core/v1"
+	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
+	"k8s.io/apimachinery/pkg/util/intstr"
+)
+
+var webComponents = map[string]bool{
+	"camel:servlet":     true,
+	"camel:undertow":    true,
+	"camel:jetty":       true,
+	"camel:netty-http":  true,
+	"camel:netty4-http": true,
+	// TODO find a better way to discover need for exposure
+	// maybe using the resolved classpath of the context instead of the requested dependencies
+}
+
+type serviceTrait struct {
+}
+
+func (*serviceTrait) ID() ID {
+	return ID("service")
+}
+
+func (e *serviceTrait) Customize(environment Environment, resources *kubernetes.Collection)
(bool, error) {
+	if !e.requiresService(environment) {
+		return false, nil
+	}
+	resources.Add(e.getServiceFor(environment))
+	return true, nil
+}
+
+func (*serviceTrait) getServiceFor(e Environment) *corev1.Service {
+	svc := corev1.Service{
+		TypeMeta: metav1.TypeMeta{
+			Kind:       "Service",
+			APIVersion: "v1",
+		},
+		ObjectMeta: metav1.ObjectMeta{
+			Name:      e.Integration.Name,
+			Namespace: e.Integration.Namespace,
+			Labels:    e.Integration.Labels,
+		},
+		Spec: corev1.ServiceSpec{
+			Ports: []corev1.ServicePort{
+				{
+					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,
+					},
+				},
+			},
+			Selector: map[string]string{
+				"camel.apache.org/integration": e.Integration.Name,
+			},
+		},
+	}
+
+	return &svc
+}
+
+func (*serviceTrait) requiresService(environment Environment) bool {
+	for _, dep := range environment.Integration.Spec.Dependencies {
+		if decision, present := webComponents[dep]; present {
+			return decision
+		}
+	}
+	return false
+}
diff --git a/pkg/trait/trait.go b/pkg/trait/trait.go
index 90e4d29..d9c372f 100644
--- a/pkg/trait/trait.go
+++ b/pkg/trait/trait.go
@@ -19,7 +19,6 @@ package trait
 
 import (
 	"github.com/apache/camel-k/pkg/apis/camel/v1alpha1"
-	"github.com/apache/camel-k/pkg/discover"
 	"github.com/apache/camel-k/pkg/platform"
 	"github.com/apache/camel-k/pkg/util/kubernetes"
 )
@@ -29,7 +28,6 @@ type Environment struct {
 	Platform            *v1alpha1.IntegrationPlatform
 	Context             *v1alpha1.IntegrationContext
 	Integration         *v1alpha1.Integration
-	Dependencies        []string
 	ExecutedCustomizers []ID
 }
 
@@ -43,13 +41,11 @@ func NewEnvironment(integration *v1alpha1.Integration) (*Environment,
error) {
 	if err != nil {
 		return nil, err
 	}
-	dependencies := discover.Dependencies(integration.Spec.Source)
 
 	return &Environment{
 		Platform:            pl,
 		Context:             ctx,
 		Integration:         integration,
-		Dependencies:        dependencies,
 		ExecutedCustomizers: make([]ID, 0),
 	}, nil
 }


Mime
View raw message