forked from operator-framework/operator-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
service-monitor.go
93 lines (83 loc) · 2.95 KB
/
service-monitor.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
// Copyright 2018 The Operator-SDK Authors
//
// Licensed 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 metrics
import (
monitoringv1 "github.com/coreos/prometheus-operator/pkg/apis/monitoring/v1"
monclientv1 "github.com/coreos/prometheus-operator/pkg/client/versioned/typed/monitoring/v1"
"github.com/operator-framework/operator-sdk/pkg/k8sutil"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/discovery"
"k8s.io/client-go/rest"
)
// CreateServiceMonitors creates ServiceMonitors objects based on an array of Service objects.
// If CR ServiceMonitor is not registered in the Cluster it will not attempt at creating resources.
func CreateServiceMonitors(config *rest.Config, ns string, services []*v1.Service) ([]*monitoringv1.ServiceMonitor, error) {
// check if we can even create ServiceMonitors
exists, err := hasServiceMonitor(config)
if err != nil {
return nil, err
}
if !exists {
// ServiceMonitor was not registered, but we don't want to produce more errors just return.
return nil, nil
}
var serviceMonitors []*monitoringv1.ServiceMonitor
mclient := monclientv1.NewForConfigOrDie(config)
for _, s := range services {
sm := GenerateServiceMonitor(s)
smc, err := mclient.ServiceMonitors(ns).Create(sm)
if err != nil {
return serviceMonitors, err
}
serviceMonitors = append(serviceMonitors, smc)
}
return serviceMonitors, nil
}
// GenerateServiceMonitor generates a prometheus-operator ServiceMonitor object
// based on the passed Service object.
func GenerateServiceMonitor(s *v1.Service) *monitoringv1.ServiceMonitor {
labels := make(map[string]string)
for k, v := range s.ObjectMeta.Labels {
labels[k] = v
}
return &monitoringv1.ServiceMonitor{
TypeMeta: metav1.TypeMeta{
Kind: "ServiceMonitor",
APIVersion: "monitoring.coreos.com/v1",
},
ObjectMeta: metav1.ObjectMeta{
Name: s.ObjectMeta.Name,
Namespace: s.ObjectMeta.Namespace,
Labels: labels,
},
Spec: monitoringv1.ServiceMonitorSpec{
Selector: metav1.LabelSelector{
MatchLabels: labels,
},
Endpoints: []monitoringv1.Endpoint{
{
Port: s.Spec.Ports[0].Name,
},
},
},
}
}
// hasServiceMonitor checks if ServiceMonitor is registered in the cluster.
func hasServiceMonitor(config *rest.Config) (bool, error) {
dc := discovery.NewDiscoveryClientForConfigOrDie(config)
apiVersion := "monitoring.coreos.com/v1"
kind := "ServiceMonitor"
return k8sutil.ResourceExists(dc, apiVersion, kind)
}