forked from rancher/rancher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ensure.go
69 lines (61 loc) · 1.82 KB
/
ensure.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
package servicemonitor
import (
"fmt"
"strings"
monitoringv1 "github.com/coreos/prometheus-operator/pkg/client/monitoring/v1"
util "github.com/rancher/rancher/pkg/controllers/user/workload"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/runtime"
)
func (c *MetricsServiceController) ensureService(key string, svc *corev1.Service) (runtime.Object, error) {
if svc == nil || svc.DeletionTimestamp != nil {
parts := strings.Split(key, "/")
sms, err := c.smLister.List(parts[0], labels.NewSelector())
if err != nil {
return svc, err
}
for _, sm := range sms {
c.smClient.Controller().Enqueue(sm.Namespace, sm.Name)
}
return svc, nil
}
if _, ok := svc.Annotations[metricsServiceLabel]; !ok {
return svc, nil
}
var owner *monitoringv1.ServiceMonitor
var err error
for _, o := range svc.OwnerReferences {
if o.Kind == "ServiceMonitor" {
owner, err = c.smLister.Get(svc.Namespace, o.Name)
if err != nil {
return svc, err
}
}
}
ports := GetServicePortsFromEndpoint(owner.Spec.Endpoints)
if !util.ArePortsEqual(ports, svc.Spec.Ports) {
c.smClient.Controller().Enqueue(owner.Namespace, owner.Name)
}
return svc, nil
}
func (c *MetricsServiceController) ensureServiceMonitor(key string, obj *monitoringv1.ServiceMonitor) (runtime.Object, error) {
if obj == nil || obj.DeletionTimestamp != nil {
parts := strings.Split(key, "/")
return obj, c.workloadLister.EnqueueAllWorkloads(parts[0])
}
var workload *util.Workload
var err error
for _, owner := range obj.OwnerReferences {
key := fmt.Sprintf("%s:%s:%s", strings.ToLower(owner.Kind), obj.Namespace, owner.Name)
workload, err = c.workloadLister.GetByWorkloadID(key)
if err != nil {
continue
}
}
if workload == nil {
return obj, nil
}
c.workloadLister.EnqueueWorkload(workload)
return obj, nil
}