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 03f8af7  Service and Prometheus traits aggregate into a single service 
per integration
03f8af7 is described below

commit 03f8af7f7df12567f20a0087ff18db02f7d2df4d
Author: Antonin Stefanutti <anto...@stefanutti.fr>
AuthorDate: Fri Jan 25 12:29:33 2019 +0100

    Service and Prometheus traits aggregate into a single service per 
integration
---
 pkg/trait/prometheus.go | 56 +++++++++++++++++++------------------------------
 pkg/trait/service.go    | 30 ++++++++++++++++----------
 2 files changed, 40 insertions(+), 46 deletions(-)

diff --git a/pkg/trait/prometheus.go b/pkg/trait/prometheus.go
index a0c983d..3e71587 100644
--- a/pkg/trait/prometheus.go
+++ b/pkg/trait/prometheus.go
@@ -61,43 +61,28 @@ func (t *prometheusTrait) Apply(e *Environment) (err error) 
{
        // Configure the Prometheus Java agent
        envvar.SetVal(&e.EnvVars, "AB_PROMETHEUS_PORT", strconv.Itoa(t.Port))
 
-       // TODO: update the existing integration service instead of
-       // creating an extra service dedicated to Prometheus
-       svc := t.getServiceFor(e)
-       e.Resources.Add(svc)
+       // Expose the Prometheus endpoint
+       // Either update the existing service added by previously executed 
traits
+       // (e.g. the service trait) or add a new service resource
+       svc := e.Resources.GetService(func (svc *corev1.Service) bool {
+               return svc.Name == e.Integration.Name
+       })
+       if svc == nil {
+               svc = getServiceFor(e)
+               e.Resources.Add(svc)
+       }
+       port := corev1.ServicePort{
+               Name:     "prometheus",
+               Port:     int32(t.Port),
+               Protocol: corev1.ProtocolTCP,
+       }
+       svc.Spec.Ports = append(svc.Spec.Ports, port)
+
+       // Add the ServiceMonitor resource
        smt := t.getServiceMonitorFor(e)
        e.Resources.Add(smt)
-       return nil
-}
 
-func (t *prometheusTrait) getServiceFor(e *Environment) *corev1.Service {
-       svc := corev1.Service{
-               TypeMeta: metav1.TypeMeta{
-                       Kind:       "Service",
-                       APIVersion: "v1",
-               },
-               ObjectMeta: metav1.ObjectMeta{
-                       Name:      e.Integration.Name + "-prometheus",
-                       Namespace: e.Integration.Namespace,
-                       Labels: map[string]string{
-                               "camel.apache.org/integration": 
e.Integration.Name,
-                       },
-               },
-               Spec: corev1.ServiceSpec{
-                       Ports: []corev1.ServicePort{
-                               {
-                                       Name:     "prometheus",
-                                       Port:     int32(t.Port),
-                                       Protocol: corev1.ProtocolTCP,
-                               },
-                       },
-                       Selector: map[string]string{
-                               "camel.apache.org/integration": 
e.Integration.Name,
-                       },
-               },
-       }
-
-       return &svc
+       return nil
 }
 
 func (t *prometheusTrait) getServiceMonitorFor(e *Environment) 
*monitoringv1.ServiceMonitor {
@@ -107,11 +92,12 @@ func (t *prometheusTrait) getServiceMonitorFor(e 
*Environment) *monitoringv1.Ser
                        APIVersion: "monitoring.coreos.com/v1",
                },
                ObjectMeta: metav1.ObjectMeta{
-                       Name:      e.Integration.Name + "-prometheus",
+                       Name:      e.Integration.Name,
                        Namespace: e.Integration.Namespace,
                        Labels: map[string]string{
                                // TODO: add the ability to configure 
additional labels
                                "camel.apache.org/integration": 
e.Integration.Name,
+                               "team": "fuse",
                        },
                },
                Spec: monitoringv1.ServiceMonitorSpec{
diff --git a/pkg/trait/service.go b/pkg/trait/service.go
index d04a0dd..043444c 100644
--- a/pkg/trait/service.go
+++ b/pkg/trait/service.go
@@ -65,12 +65,27 @@ func (t *serviceTrait) Configure(e *Environment) (bool, 
error) {
 }
 
 func (t *serviceTrait) Apply(e *Environment) (err error) {
-       svc := t.getServiceFor(e)
-       e.Resources.Add(svc)
+       // Either update the existing service added by previously executed 
traits
+       // (e.g. the prometheus trait) or add a new service resource
+       svc := e.Resources.GetService(func(svc *corev1.Service) bool {
+               return svc.Name == e.Integration.Name
+       })
+       if svc == nil {
+               svc = getServiceFor(e)
+               e.Resources.Add(svc)
+       }
+       port := corev1.ServicePort{
+               Name:       "http",
+               Port:       80,
+               Protocol:   corev1.ProtocolTCP,
+               TargetPort: intstr.FromInt(t.Port),
+       }
+       svc.Spec.Ports = append(svc.Spec.Ports, port)
+
        return nil
 }
 
-func (t *serviceTrait) getServiceFor(e *Environment) *corev1.Service {
+func getServiceFor(e *Environment) *corev1.Service {
        svc := corev1.Service{
                TypeMeta: metav1.TypeMeta{
                        Kind:       "Service",
@@ -84,14 +99,7 @@ func (t *serviceTrait) getServiceFor(e *Environment) 
*corev1.Service {
                        },
                },
                Spec: corev1.ServiceSpec{
-                       Ports: []corev1.ServicePort{
-                               {
-                                       Name:       "http",
-                                       Port:       80,
-                                       Protocol:   corev1.ProtocolTCP,
-                                       TargetPort: intstr.FromInt(t.Port),
-                               },
-                       },
+                       Ports: []corev1.ServicePort{},
                        Selector: map[string]string{
                                "camel.apache.org/integration": 
e.Integration.Name,
                        },

Reply via email to