-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
191 lines (171 loc) · 4.78 KB
/
utils.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
package servicemonitor
import (
"encoding/json"
"fmt"
"reflect"
"sort"
"strings"
monitoringv1 "github.com/coreos/prometheus-operator/pkg/client/monitoring/v1"
util "github.com/rancher/rancher/pkg/controllers/user/workload"
rmonitoringv1 "github.com/rancher/types/apis/monitoring.coreos.com/v1"
v3 "github.com/rancher/types/apis/project.cattle.io/v3"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/util/intstr"
)
func filterRancherLabels(l map[string]string) labels.Set {
rtn := map[string]string{}
for k, v := range l {
if !strings.Contains(k, "cattle.io/") {
rtn[k] = v
}
}
return labels.Set(rtn)
}
func getWorkloadOwnerReference(w *util.Workload) metav1.OwnerReference {
controller := true
return metav1.OwnerReference{
APIVersion: w.APIVersion,
Kind: w.Kind,
Name: w.Name,
UID: w.UUID,
Controller: &controller,
}
}
func getMetricsFromWorkload(w *util.Workload) ([]v3.WorkloadMetric, error) {
data, ok := w.TemplateSpec.Annotations[metricsAnnotation]
if !ok {
return nil, nil
}
var metrics []v3.WorkloadMetric
if err := json.Unmarshal([]byte(data), &metrics); err != nil {
return nil, err
}
return metrics, nil
}
func getServiceMonitorFromWorkload(w *util.Workload) (*monitoringv1.ServiceMonitor, error) {
metrics, err := getMetricsFromWorkload(w)
if err != nil {
return nil, err
}
if len(metrics) == 0 {
return nil, nil
}
workloadTargetAnnotation, err := util.IDAnnotationToString(w.Key)
if err != nil {
return nil, err
}
rtn := &monitoringv1.ServiceMonitor{
ObjectMeta: metav1.ObjectMeta{
OwnerReferences: []metav1.OwnerReference{getWorkloadOwnerReference(w)},
Namespace: w.Namespace,
Annotations: map[string]string{
util.WorkloadAnnotation: workloadTargetAnnotation,
},
Name: w.Name,
},
Spec: monitoringv1.ServiceMonitorSpec{},
}
for _, metric := range metrics {
portName := fmt.Sprintf("%s%d", "metrics", metric.Port)
intstrPort := intstr.FromInt(int(metric.Port))
endpoint := monitoringv1.Endpoint{
Port: portName,
TargetPort: &intstrPort,
Path: metric.Path,
Scheme: metric.Schema,
TLSConfig: &monitoringv1.TLSConfig{
InsecureSkipVerify: true,
},
}
if endpoint.Path == "" {
endpoint.Path = "/metrics"
}
rtn.Spec.Endpoints = append(rtn.Spec.Endpoints, endpoint)
}
return rtn, nil
}
func getWorkloadFromOwners(namespace string, owners []metav1.OwnerReference, lister rmonitoringv1.ServiceMonitorLister) (*monitoringv1.ServiceMonitor, error) {
for _, owner := range owners {
if !*owner.Controller || owner.Kind != "ServiceMonitor" {
continue
}
return lister.Get(namespace, owner.Name)
}
return nil, nil
}
func areServiceMonitorEqual(a, b *monitoringv1.ServiceMonitor) bool {
sort.Sort(EndpointSorter(a.Spec.Endpoints))
sort.Sort(EndpointSorter(b.Spec.Endpoints))
if len(a.Spec.Endpoints) != len(b.Spec.Endpoints) {
return false
}
for i := 0; i < len(a.Spec.Endpoints); i++ {
aEndpoint := a.Spec.Endpoints[i]
bEndpoint := b.Spec.Endpoints[i]
if aEndpoint.Port != bEndpoint.Port ||
aEndpoint.Path != bEndpoint.Path ||
aEndpoint.Scheme != bEndpoint.Scheme {
return false
}
}
for _, annotation := range []string{util.WorkloadAnnotation, servicesAnnotation} {
adata := a.Annotations[annotation]
bdata := b.Annotations[annotation]
if adata == bdata && adata == "" {
continue
}
var aarray, barray []string
if err := json.Unmarshal([]byte(adata), &aarray); err != nil {
return false
}
if err := json.Unmarshal([]byte(bdata), &barray); err != nil {
return false
}
sort.Strings(aarray)
sort.Strings(barray)
if !reflect.DeepEqual(aarray, barray) {
return false
}
}
return true
}
type EndpointSorter []monitoringv1.Endpoint
func (e EndpointSorter) Len() int {
return len(e)
}
func (e EndpointSorter) Swap(i, j int) {
e[i], e[j] = e[j], e[i]
}
func (e EndpointSorter) Less(i, j int) bool {
return getEndpointString(e[i]) < getEndpointString(e[j])
}
func getEndpointString(e monitoringv1.Endpoint) string {
return fmt.Sprintf("%s%s%s", e.Scheme, e.Port, e.Path)
}
func getStringSliceFromAnnotation(obj metav1.ObjectMeta, key string) []string {
annotaiton, ok := obj.Annotations[key]
if !ok {
return []string{}
}
var rtn []string
json.Unmarshal([]byte(annotaiton), &rtn)
return rtn
}
func GetServicePortsFromEndpoint(endpoints []monitoringv1.Endpoint) []corev1.ServicePort {
PortMap := map[string]bool{}
var rtn []corev1.ServicePort
for _, endpoint := range endpoints {
if _, ok := PortMap[endpoint.Port]; ok {
continue
}
rtn = append(rtn, corev1.ServicePort{
Name: endpoint.Port,
Port: endpoint.TargetPort.IntVal,
TargetPort: *endpoint.TargetPort,
})
PortMap[endpoint.Port] = true
}
return rtn
}