Skip to content

Commit

Permalink
Improve error handling while parsing Prometheus service monitor labels
Browse files Browse the repository at this point in the history
  • Loading branch information
astefanutti authored and lburgazzoli committed Jan 31, 2019
1 parent 3c2c9ca commit ec8f3b8
Showing 1 changed file with 25 additions and 9 deletions.
34 changes: 25 additions & 9 deletions pkg/trait/prometheus.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,13 @@ limitations under the License.
package trait

import (
"strings"
"errors"
"fmt"
"strconv"
"strings"

"github.com/apache/camel-k/pkg/apis/camel/v1alpha1"
"github.com/apache/camel-k/pkg/util/envvar"
"github.com/pkg/errors"

corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand All @@ -36,7 +37,7 @@ type prometheusTrait struct {
BaseTrait `property:",squash"`

Labels string `property:"labels"`
Port int `property:"port"`
Port int `property:"port"`
}

const prometheusPortName = "prometheus"
Expand Down Expand Up @@ -106,14 +107,20 @@ func (t *prometheusTrait) Apply(e *Environment) (err error) {
})

// Add the ServiceMonitor resource
smt := t.getServiceMonitorFor(e)
smt, err := t.getServiceMonitorFor(e)
if err != nil {
return err
}
e.Resources.Add(smt)

return nil
}

func (t *prometheusTrait) getServiceMonitorFor(e *Environment) *monitoringv1.ServiceMonitor {
labels := parseLabels(t.Labels)
func (t *prometheusTrait) getServiceMonitorFor(e *Environment) (*monitoringv1.ServiceMonitor, error) {
labels, err := parseLabels(t.Labels)
if err != nil {
return nil, err
}
labels["camel.apache.org/integration"] = e.Integration.Name

smt := monitoringv1.ServiceMonitor{
Expand All @@ -139,14 +146,23 @@ func (t *prometheusTrait) getServiceMonitorFor(e *Environment) *monitoringv1.Ser
},
},
}
return &smt
return &smt, nil
}

func parseLabels(labels string) map[string]string {
func parseLabels(labels string) (map[string]string, error) {
m := make(map[string]string)

if len(labels) == 0 {
return m, nil
}

for _, label := range strings.Split(labels, ",") {
kv := strings.Split(label, "=")
if len(kv) != 2 {
return nil, fmt.Errorf("invalid label [%s] in labels [%s]", label, labels)
}
m[kv[0]] = kv[1]
}
return m

return m, nil
}

0 comments on commit ec8f3b8

Please sign in to comment.