You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by as...@apache.org on 2019/10/01 07:48:17 UTC
[camel-k] 03/05: fix(prometheus): Create a service if no one exists
already
This is an automated email from the ASF dual-hosted git repository.
astefanutti pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/camel-k.git
commit 64553ae05309806ebbdf999179a3edc4bcc425ee
Author: Antonin Stefanutti <an...@stefanutti.fr>
AuthorDate: Tue Sep 24 18:28:09 2019 +0200
fix(prometheus): Create a service if no one exists already
---
pkg/trait/prometheus.go | 47 ++++++++++++++++++++++---------------
pkg/trait/service.go | 61 ++++++++++++++++++++++++++++---------------------
2 files changed, 64 insertions(+), 44 deletions(-)
diff --git a/pkg/trait/prometheus.go b/pkg/trait/prometheus.go
index 7ff1db4..bbef5b8 100644
--- a/pkg/trait/prometheus.go
+++ b/pkg/trait/prometheus.go
@@ -94,32 +94,43 @@ func (t *prometheusTrait) Apply(e *Environment) (err error) {
container.Ports = append(container.Ports, *containerPort)
condition.Message += fmt.Sprintf("%s(%s/%d)", container.Name, containerPort.Name, containerPort.ContainerPort)
- // Add the service port
+ // Retrieve the service or create a new one if the service trait is enabled
+ serviceEnabled := false
service := e.Resources.GetServiceForIntegration(e.Integration)
if service == nil {
- condition.Status = corev1.ConditionFalse
- condition.Reason = v1alpha1.IntegrationConditionServiceNotAvailableReason
- } else {
+ trait := e.Catalog.GetTrait(serviceTraitID)
+ if serviceTrait, ok := trait.(*serviceTrait); ok {
+ serviceEnabled = serviceTrait.isEnabled()
+ }
+ if serviceEnabled {
+ // add a new service if not already created
+ service = getServiceFor(e)
+ e.Resources.Add(service)
+ }
+ }
+
+ // Add the service port and service monitor resource
+ // A better strategy may be needed when the Knative profile is active
+ if serviceEnabled {
servicePort := t.getServicePort()
service.Spec.Ports = append(service.Spec.Ports, *servicePort)
condition.Message += fmt.Sprintf("%s(%s/%d) -> ", service.Name, servicePort.Name, servicePort.Port)
- }
-
- e.Integration.Status.SetConditions(condition)
- if condition.Status == corev1.ConditionFalse {
- return nil
- }
-
- // Add the ServiceMonitor resource
- if t.ServiceMonitor {
- smt, err := t.getServiceMonitorFor(e)
- if err != nil {
- return err
+ // Add the ServiceMonitor resource
+ if t.ServiceMonitor {
+ smt, err := t.getServiceMonitorFor(e)
+ if err != nil {
+ return err
+ }
+ e.Resources.Add(smt)
}
- e.Resources.Add(smt)
+ } else {
+ condition.Status = corev1.ConditionFalse
+ condition.Reason = v1alpha1.IntegrationConditionServiceNotAvailableReason
}
+ e.Integration.Status.SetConditions(condition)
+
return nil
}
@@ -167,7 +178,7 @@ func (t *prometheusTrait) getServiceMonitorFor(e *Environment) (*monitoringv1.Se
},
Endpoints: []monitoringv1.Endpoint{
{
- Port: "prometheus",
+ Port: prometheusPortName,
},
},
},
diff --git a/pkg/trait/service.go b/pkg/trait/service.go
index ae204c0..3efb446 100644
--- a/pkg/trait/service.go
+++ b/pkg/trait/service.go
@@ -30,16 +30,23 @@ type serviceTrait struct {
Auto *bool `property:"auto"`
}
-const httpPortName = "http"
+const (
+ serviceTraitID = "service"
+ httpPortName = "http"
+)
func newServiceTrait() *serviceTrait {
return &serviceTrait{
- BaseTrait: newBaseTrait("service"),
+ BaseTrait: newBaseTrait(serviceTraitID),
}
}
+func (t *serviceTrait) isEnabled() bool {
+ return t.Enabled == nil || *t.Enabled
+}
+
func (t *serviceTrait) Configure(e *Environment) (bool, error) {
- if t.Enabled != nil && !*t.Enabled {
+ if !t.isEnabled() {
e.Integration.Status.SetCondition(
v1alpha1.IntegrationConditionServiceAvailable,
corev1.ConditionFalse,
@@ -83,32 +90,34 @@ func (t *serviceTrait) Configure(e *Environment) (bool, error) {
return true, nil
}
-func (t *serviceTrait) Apply(e *Environment) (err error) {
+func (t *serviceTrait) Apply(e *Environment) error {
svc := e.Resources.GetServiceForIntegration(e.Integration)
+ // add a new service if not already created
if svc == nil {
- svc := corev1.Service{
- TypeMeta: metav1.TypeMeta{
- Kind: "Service",
- APIVersion: "v1",
- },
- ObjectMeta: metav1.ObjectMeta{
- Name: e.Integration.Name,
- Namespace: e.Integration.Namespace,
- Labels: map[string]string{
- "camel.apache.org/integration": e.Integration.Name,
- },
+ svc = getServiceFor(e)
+ e.Resources.Add(svc)
+ }
+ return nil
+}
+
+func getServiceFor(e *Environment) *corev1.Service {
+ return &corev1.Service{
+ TypeMeta: metav1.TypeMeta{
+ Kind: "Service",
+ APIVersion: "v1",
+ },
+ ObjectMeta: metav1.ObjectMeta{
+ Name: e.Integration.Name,
+ Namespace: e.Integration.Namespace,
+ Labels: map[string]string{
+ "camel.apache.org/integration": e.Integration.Name,
},
- Spec: corev1.ServiceSpec{
- Ports: []corev1.ServicePort{},
- Selector: map[string]string{
- "camel.apache.org/integration": e.Integration.Name,
- },
+ },
+ Spec: corev1.ServiceSpec{
+ Ports: []corev1.ServicePort{},
+ Selector: map[string]string{
+ "camel.apache.org/integration": e.Integration.Name,
},
- }
-
- // add a new service if not already created
- e.Resources.Add(&svc)
+ },
}
-
- return nil
}