-
Notifications
You must be signed in to change notification settings - Fork 1
/
federation_controller.go
529 lines (464 loc) · 15.5 KB
/
federation_controller.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
/*
Copyright AppsCode Inc. and Contributors
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 servicemonitor
import (
"context"
"fmt"
"reflect"
"sort"
"strings"
"go.openviz.dev/grafana-tools/pkg/detector"
"github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring"
monitoringv1 "github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring/v1"
core "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/util/errors"
"k8s.io/apimachinery/pkg/util/intstr"
"k8s.io/client-go/rest"
"k8s.io/klog/v2"
cu "kmodules.xyz/client-go/client"
clustermeta "kmodules.xyz/client-go/cluster"
meta_util "kmodules.xyz/client-go/meta"
mona "kmodules.xyz/monitoring-agent-api/api/v1"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/builder"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/handler"
"sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/predicate"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
)
// FederationReconciler reconciles a ServiceMonitor object
type FederationReconciler struct {
cfg *rest.Config
kc client.Client
scheme *runtime.Scheme
d detector.Detector
}
func NewFederationReconciler(cfg *rest.Config, kc client.Client, d detector.Detector) *FederationReconciler {
return &FederationReconciler{
cfg: cfg,
kc: kc,
scheme: kc.Scheme(),
d: d,
}
}
// Reconcile is part of the main kubernetes reconciliation loop which aims to
// move the current state of the cluster closer to the desired state.
// TODO(user): Modify the Reconcile function to compare the state specified by
// the ServiceMonitor object against the actual cluster state, and then
// perform operations to make the cluster state reflect the state specified by
// the user.
//
// For more details, check Reconcile and its Result here:
// - https://pkg.go.dev/sigs.k8s.io/controller-runtime@v0.16.0/pkg/reconcile
func (r *FederationReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
log := log.FromContext(ctx)
var svcMon monitoringv1.ServiceMonitor
if err := r.kc.Get(ctx, req.NamespacedName, &svcMon); err != nil {
log.Error(err, "unable to fetch ServiceMonitor")
// we'll ignore not-found errors, since they can't be fixed by an immediate
// requeue (we'll need to wait for a new notification), and we can get them
// on deleted requests.
return ctrl.Result{}, client.IgnoreNotFound(err)
}
// has federate label
val, found := svcMon.Labels[mona.PrometheusKey]
if !found || val != mona.PrometheusValueFederated {
return ctrl.Result{}, nil
}
var promList monitoringv1.PrometheusList
if err := r.kc.List(context.TODO(), &promList); err != nil {
log.Error(err, "unable to list Prometheus")
return ctrl.Result{}, client.IgnoreNotFound(err)
}
prometheuses := promList.Items
if len(prometheuses) == 0 {
return ctrl.Result{}, nil
}
sort.Slice(prometheuses, func(i, j int) bool {
if prometheuses[i].Namespace != prometheuses[j].Namespace {
return prometheuses[i].Namespace < prometheuses[j].Namespace
}
return prometheuses[i].Name < prometheuses[j].Name
})
if ready, err := r.d.Ready(); !ready {
return ctrl.Result{}, err
}
if !r.d.Federated() {
// non rancher
err := r.updateServiceMonitorLabels(prometheuses[0], &svcMon)
if err != nil {
log.Error(err, "failed to apply label")
}
return ctrl.Result{}, err
}
// services
srcServices := map[client.ObjectKey]core.Service{}
svcSel, err := metav1.LabelSelectorAsSelector(&svcMon.Spec.Selector)
if err != nil {
return ctrl.Result{}, err
}
for _, ns := range svcMon.Spec.NamespaceSelector.MatchNames {
var svcList core.ServiceList
err = r.kc.List(context.TODO(), &svcList, client.InNamespace(ns), client.MatchingLabelsSelector{
Selector: svcSel,
})
if err != nil {
return ctrl.Result{}, err
}
for _, svc := range svcList.Items {
srcServices[client.ObjectKeyFromObject(&svc)] = svc
}
}
// secret
var srcSecrets []core.Secret
for i := range svcMon.Spec.Endpoints {
e := svcMon.Spec.Endpoints[i]
if e.TLSConfig != nil &&
e.TLSConfig.CA.Secret != nil &&
e.TLSConfig.CA.Secret.Name != "" {
key := client.ObjectKey{
Name: e.TLSConfig.CA.Secret.Name,
Namespace: svcMon.Namespace,
}
var srcSecret core.Secret
err := r.kc.Get(context.TODO(), key, &srcSecret)
if err != nil {
return ctrl.Result{}, err
}
srcSecrets = append(srcSecrets, srcSecret)
}
}
var errList []error
for _, prom := range promList.Items {
isDefault := r.d.IsDefault(client.ObjectKeyFromObject(prom))
if !isDefault && prom.Namespace == req.Namespace {
err := fmt.Errorf("federated service monitor can't be in the same namespace with project Prometheus %s/%s", prom.Namespace, prom.Namespace)
log.Error(err, "bad service monitor")
return ctrl.Result{}, nil // don't retry until svcmon changes
}
if isDefault {
if err := r.updateServiceMonitorLabels(prom, &svcMon); err != nil {
errList = append(errList, err)
}
} else {
targetSvcMon, err := r.copyServiceMonitor(prom, &svcMon)
if err != nil {
errList = append(errList, err)
}
for _, srcSvc := range srcServices {
if err := r.copyService(&srcSvc, targetSvcMon); err != nil {
errList = append(errList, err)
}
var srcEP core.Endpoints
err = r.kc.Get(context.TODO(), client.ObjectKeyFromObject(&srcSvc), &srcEP)
if err != nil {
errList = append(errList, err)
} else {
if err := r.copyEndpoints(&srcSvc, &srcEP, targetSvcMon); err != nil {
errList = append(errList, err)
}
}
}
for _, srcSecret := range srcSecrets {
if err := r.copySecret(&srcSecret, targetSvcMon); err != nil {
errList = append(errList, err)
}
}
}
}
return ctrl.Result{}, errors.NewAggregate(errList)
}
func (r *FederationReconciler) updateServiceMonitorLabels(prom *monitoringv1.Prometheus, src *monitoringv1.ServiceMonitor) error {
vt, err := cu.CreateOrPatch(context.TODO(), r.kc, src, func(in client.Object, createOp bool) client.Object {
obj := in.(*monitoringv1.ServiceMonitor)
labels, _ := meta_util.LabelsForLabelSelector(prom.Spec.ServiceMonitorSelector)
obj.Labels = meta_util.OverwriteKeys(obj.Labels, labels)
return obj
})
if err != nil {
return err
}
klog.Infof("%s ServiceMonitor %s/%s", vt, src.Namespace, src.Name)
return nil
}
func (r *FederationReconciler) copyServiceMonitor(prom *monitoringv1.Prometheus, src *monitoringv1.ServiceMonitor) (*monitoringv1.ServiceMonitor, error) {
sel, err := metav1.LabelSelectorAsSelector(prom.Spec.ServiceMonitorNamespaceSelector)
if err != nil {
return nil, err
}
var nsList core.NamespaceList
err = r.kc.List(context.TODO(), &nsList, client.MatchingLabelsSelector{
Selector: sel,
})
if err != nil {
return nil, err
}
namespaces := make([]string, 0, len(nsList.Items))
namespaces = append(namespaces, "") // for non-namespaced resources
for _, ns := range nsList.Items {
if ns.Name == fmt.Sprintf("cattle-project-%s", ns.Labels[clustermeta.LabelKeyRancherFieldProjectId]) {
continue
}
namespaces = append(namespaces, ns.Name)
}
sort.Strings(namespaces)
target := monitoringv1.ServiceMonitor{
ObjectMeta: metav1.ObjectMeta{
Name: src.Name,
Namespace: prom.Namespace,
},
}
vt, err := cu.CreateOrPatch(context.TODO(), r.kc, &target, func(in client.Object, createOp bool) client.Object {
obj := in.(*monitoringv1.ServiceMonitor)
ref := metav1.NewControllerRef(prom, schema.GroupVersionKind{
Group: monitoring.GroupName,
Version: monitoringv1.Version,
Kind: "Prometheus",
})
obj.OwnerReferences = []metav1.OwnerReference{*ref}
labels, _ := meta_util.LabelsForLabelSelector(prom.Spec.ServiceMonitorSelector)
obj.Labels = meta_util.OverwriteKeys(obj.Labels, labels)
delete(obj.Labels, mona.PrometheusKey)
obj.Spec = *src.Spec.DeepCopy()
keepNSMetrics := monitoringv1.RelabelConfig{
Action: "keep",
SourceLabels: []monitoringv1.LabelName{"namespace"},
Regex: "(" + strings.Join(namespaces, "|") + ")",
}
for i := range obj.Spec.Endpoints {
e := obj.Spec.Endpoints[i]
e.HonorLabels = true // keep original labels
if len(e.MetricRelabelConfigs) == 0 || !reflect.DeepEqual(keepNSMetrics, *e.MetricRelabelConfigs[0]) {
e.MetricRelabelConfigs = append([]*monitoringv1.RelabelConfig{
&keepNSMetrics,
}, e.MetricRelabelConfigs...)
}
obj.Spec.Endpoints[i] = e
}
return obj
})
if err != nil {
return nil, err
}
klog.Infof("%s ServiceMonitor %s/%s", vt, target.Namespace, target.Name)
return &target, nil
}
func (r *FederationReconciler) copySecret(src *core.Secret, targetSvcMon *monitoringv1.ServiceMonitor) error {
target := core.Secret{
ObjectMeta: metav1.ObjectMeta{
Name: src.Name,
Namespace: targetSvcMon.Namespace,
},
Immutable: nil,
Data: nil,
StringData: nil,
Type: "",
}
vt, err := cu.CreateOrPatch(context.TODO(), r.kc, &target, func(in client.Object, createOp bool) client.Object {
obj := in.(*core.Secret)
ref := metav1.NewControllerRef(targetSvcMon, schema.GroupVersionKind{
Group: monitoring.GroupName,
Version: monitoringv1.Version,
Kind: "ServiceMonitor",
})
obj.OwnerReferences = []metav1.OwnerReference{*ref}
obj.Immutable = src.Immutable
obj.Data = src.Data
obj.Type = src.Type
return obj
})
if err != nil {
return err
}
klog.Infof("%s Secret %s/%s", vt, target.Namespace, target.Name)
return nil
}
func (r *FederationReconciler) copyService(src *core.Service, targetSvcMon *monitoringv1.ServiceMonitor) error {
target := core.Service{
ObjectMeta: metav1.ObjectMeta{
Name: src.Name,
Namespace: targetSvcMon.Namespace,
},
}
vt, err := cu.CreateOrPatch(context.TODO(), r.kc, &target, func(in client.Object, createOp bool) client.Object {
obj := in.(*core.Service)
ref := metav1.NewControllerRef(targetSvcMon, schema.GroupVersionKind{
Group: monitoring.GroupName,
Version: monitoringv1.Version,
Kind: "ServiceMonitor",
})
obj.OwnerReferences = []metav1.OwnerReference{*ref}
obj.Labels = meta_util.OverwriteKeys(obj.Labels, src.Labels)
obj.Annotations = meta_util.OverwriteKeys(obj.Annotations, src.Annotations)
obj.Spec.Type = core.ServiceTypeClusterIP
for _, port := range src.Spec.Ports {
obj.Spec.Ports = UpsertServicePort(obj.Spec.Ports, port)
}
return obj
})
if err != nil {
return err
}
klog.Infof("%s Service %s/%s", vt, target.Namespace, target.Name)
return nil
}
func UpsertServicePort(ports []core.ServicePort, x core.ServicePort) []core.ServicePort {
for i, port := range ports {
if port.Name == x.Name {
port.Port = x.Port
port.TargetPort = intstr.FromInt(int(x.Port))
ports[i] = port
return ports
}
}
return append(ports, core.ServicePort{
Name: x.Name,
Port: x.Port,
TargetPort: intstr.FromInt(int(x.Port)),
})
}
func (r *FederationReconciler) copyEndpoints(srcSvc *core.Service, srcEP *core.Endpoints, targetSvcMon *monitoringv1.ServiceMonitor) error {
target := core.Endpoints{
ObjectMeta: metav1.ObjectMeta{
Name: srcEP.Name,
Namespace: targetSvcMon.Namespace,
},
}
vt, err := cu.CreateOrPatch(context.TODO(), r.kc, &target, func(in client.Object, createOp bool) client.Object {
obj := in.(*core.Endpoints)
ref := metav1.NewControllerRef(targetSvcMon, schema.GroupVersionKind{
Group: monitoring.GroupName,
Version: monitoringv1.Version,
Kind: "ServiceMonitor",
})
obj.OwnerReferences = []metav1.OwnerReference{*ref}
obj.Labels = meta_util.OverwriteKeys(obj.Labels, srcEP.Labels)
obj.Annotations = meta_util.OverwriteKeys(obj.Annotations, srcEP.Annotations)
for i, srcSubNet := range srcEP.Subsets {
if i >= len(obj.Subsets) {
obj.Subsets = append(obj.Subsets, core.EndpointSubset{})
}
obj.Subsets[i].Addresses = []core.EndpointAddress{
{
IP: srcSvc.Spec.ClusterIP,
},
}
for _, port := range srcSubNet.Ports {
obj.Subsets[i].Ports = UpsertEndpointPort(obj.Subsets[i].Ports, core.EndpointPort{
Name: port.Name,
Port: ServicePortForName(srcSvc, port.Name),
})
}
}
return obj
})
if err != nil {
return err
}
klog.Infof("%s Endpoints %s/%s", vt, target.Namespace, target.Name)
return nil
}
func ServicePortForName(svc *core.Service, portName string) int32 {
for _, port := range svc.Spec.Ports {
if port.Name == portName {
return port.Port
}
}
panic(fmt.Errorf("service %s/%s has no port with name %s", svc.Namespace, svc.Name, portName))
}
func UpsertEndpointPort(ports []core.EndpointPort, x core.EndpointPort) []core.EndpointPort {
for i, port := range ports {
if port.Name == x.Name {
port.Port = x.Port
ports[i] = port
return ports
}
}
return append(ports, core.EndpointPort{
Name: x.Name,
Port: x.Port,
})
}
// service -> []serviceMonitors
func (r *FederationReconciler) ServiceMonitorsForService(ctx context.Context, obj client.Object) []reconcile.Request {
var list monitoringv1.ServiceMonitorList
err := r.kc.List(ctx, &list, client.MatchingLabels{
mona.PrometheusKey: mona.PrometheusValueFederated,
})
if err != nil {
klog.Error(err)
return nil
}
var req []reconcile.Request
for _, svcMon := range list.Items {
nsMatches := svcMon.Spec.NamespaceSelector.Any ||
contains(svcMon.Spec.NamespaceSelector.MatchNames, obj.GetNamespace())
if !nsMatches {
continue
}
sel, err := metav1.LabelSelectorAsSelector(&svcMon.Spec.Selector)
if err != nil {
return nil
}
if sel.Matches(labels.Set(obj.GetLabels())) {
req = append(req, reconcile.Request{NamespacedName: client.ObjectKeyFromObject(svcMon)})
}
}
return req
}
// Prometheus -> serviceMonitor
func ServiceMonitorsForPrometheus(kc client.Client, labels map[string]string) func(_ context.Context, _ client.Object) []reconcile.Request {
return func(ctx context.Context, _ client.Object) []reconcile.Request {
var list monitoringv1.ServiceMonitorList
err := kc.List(ctx, &list, client.MatchingLabels(labels))
if err != nil {
klog.Error(err)
return nil
}
var req []reconcile.Request
for _, svcMon := range list.Items {
req = append(req, reconcile.Request{NamespacedName: client.ObjectKeyFromObject(svcMon)})
}
return req
}
}
func contains(arr []string, x string) bool {
for _, s := range arr {
if s == x {
return true
}
}
return false
}
// SetupWithManager sets up the controller with the Manager.
func (r *FederationReconciler) SetupWithManager(mgr ctrl.Manager) error {
return ctrl.NewControllerManagedBy(mgr).
For(&monitoringv1.ServiceMonitor{}, builder.WithPredicates(predicate.NewPredicateFuncs(func(obj client.Object) bool {
val, found := obj.GetLabels()[mona.PrometheusKey]
return found && val == mona.PrometheusValueFederated
}))).
Watches(
&core.Service{},
handler.EnqueueRequestsFromMapFunc(r.ServiceMonitorsForService)).
Watches(
&monitoringv1.Prometheus{},
handler.EnqueueRequestsFromMapFunc(ServiceMonitorsForPrometheus(r.kc, map[string]string{
mona.PrometheusKey: mona.PrometheusValueFederated,
}))).
Complete(r)
}