Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prometheus trait initial implementation #364

Merged
merged 1 commit into from
Jan 23, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 15 additions & 4 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions Gopkg.toml
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,7 @@ required = [
[[prune.project]]
name = "k8s.io/code-generator"
non-go = false

[[constraint]]
name = "github.com/coreos/prometheus-operator"
version = "0.27.0"
15 changes: 15 additions & 0 deletions docs/traits.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,21 @@ The following is a list of common traits that can be configured by the end users
+
It's disabled by default.

| prometheus
| Kubernetes, OpenShift
| Exposes the integration with a Service and a ServiceMonitor resources so that the Prometheus endpoint can be scraped.
+
+
It's disabled by default.

[cols="m,"]
!===

! prometheus.port
! To configure a different Prometheus endpoint port (default `9778`).

!===

|=======================


Expand Down
8 changes: 8 additions & 0 deletions pkg/apis/addtoscheme_monitoring.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package apis

import "github.com/apache/camel-k/pkg/util/monitoring"

func init() {
// Register the types with the Scheme so the components can map objects to GroupVersionKinds and back
AddToSchemes = append(AddToSchemes, monitoring.AddToScheme)
}
5 changes: 5 additions & 0 deletions pkg/trait/catalog.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ type Catalog struct {
tService Trait
tRoute Trait
tIngress Trait
tPrometheus Trait
tOwner Trait
tImages Trait
tBuilder Trait
Expand All @@ -60,6 +61,7 @@ func NewCatalog(ctx context.Context, c client.Client) *Catalog {
tService: newServiceTrait(),
tRoute: newRouteTrait(),
tIngress: newIngressTrait(),
tPrometheus: newPrometheusTrait(),
tOwner: newOwnerTrait(),
tImages: newImagesTrait(),
tBuilder: newBuilderTrait(),
Expand Down Expand Up @@ -91,6 +93,7 @@ func (c *Catalog) allTraits() []Trait {
c.tService,
c.tRoute,
c.tIngress,
c.tPrometheus,
c.tOwner,
c.tImages,
c.tBuilder,
Expand All @@ -116,6 +119,7 @@ func (c *Catalog) traitsFor(environment *Environment) []Trait {
c.tDeployment,
c.tService,
c.tRoute,
c.tPrometheus,
c.tOwner,
}
case v1alpha1.TraitProfileKubernetes:
Expand All @@ -131,6 +135,7 @@ func (c *Catalog) traitsFor(environment *Environment) []Trait {
c.tDeployment,
c.tService,
c.tIngress,
c.tPrometheus,
c.tOwner,
}
case v1alpha1.TraitProfileKnative:
Expand Down
122 changes: 122 additions & 0 deletions pkg/trait/prometheus.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
/*
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/apis/camel/v1alpha1"
monitoringv1 "github.com/coreos/prometheus-operator/pkg/apis/monitoring/v1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

type prometheusTrait struct {
BaseTrait `property:",squash"`

Port int `property:"port"`
}

func newPrometheusTrait() *prometheusTrait {
return &prometheusTrait{
BaseTrait: BaseTrait{
id: ID("prometheus"),
},
Port: 9779,
}
}

func (t *prometheusTrait) Configure(e *Environment) (bool, error) {
if t.Enabled == nil || !*t.Enabled {
return false, nil
}

if !e.IntegrationInPhase(v1alpha1.IntegrationPhaseDeploying) {
return false, nil
}

return true, nil
}

func (t *prometheusTrait) Apply(e *Environment) (err error) {
// TODO: update the existing integration service instead of
// creating an extra service dedicated to Prometheus
svc := t.getServiceFor(e)
e.Resources.Add(svc)
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
}

func (t *prometheusTrait) getServiceMonitorFor(e *Environment) *monitoringv1.ServiceMonitor {
smt := monitoringv1.ServiceMonitor{
TypeMeta: metav1.TypeMeta{
Kind: "ServiceMonitor",
APIVersion: "monitoring.coreos.com/v1",
},
ObjectMeta: metav1.ObjectMeta{
Name: e.Integration.Name + "-prometheus",
Namespace: e.Integration.Namespace,
Labels: map[string]string{
// TODO: add the ability to configure additional labels
"camel.apache.org/integration": e.Integration.Name,
},
},
Spec: monitoringv1.ServiceMonitorSpec{
Selector: metav1.LabelSelector{
MatchLabels: map[string]string{
"camel.apache.org/integration": e.Integration.Name,
},
},
Endpoints: []monitoringv1.Endpoint{
monitoringv1.Endpoint{
Port: "prometheus",
},
},
},
}
return &smt
}
47 changes: 47 additions & 0 deletions pkg/util/monitoring/register.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
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 monitoring

import (
monitoringv1 "github.com/coreos/prometheus-operator/pkg/apis/monitoring/v1"
"github.com/sirupsen/logrus"
"k8s.io/apimachinery/pkg/runtime"
)

type registerFunction func(*runtime.Scheme) error

// AddToScheme adds monitoring types to the scheme
func AddToScheme(scheme *runtime.Scheme) error {
var err error

err = doAdd(monitoringv1.AddToScheme, scheme, err)

return err
}

func doAdd(addToScheme registerFunction, scheme *runtime.Scheme, err error) error {
callErr := addToScheme(scheme)
if callErr != nil {
logrus.Error("Error while registering monitoring types", callErr)
}

if err == nil {
return callErr
}
return err
}
Loading