-
Notifications
You must be signed in to change notification settings - Fork 458
/
promOperator.go
457 lines (404 loc) · 15.7 KB
/
promOperator.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
// Copyright The OpenTelemetry 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 watcher
import (
"context"
"fmt"
"log/slog"
"os"
"time"
"github.com/blang/semver/v4"
gokitlog "github.com/go-kit/log"
"github.com/go-kit/log/level"
"github.com/go-logr/logr"
monitoringv1 "github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring/v1"
promv1alpha1 "github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring/v1alpha1"
"github.com/prometheus-operator/prometheus-operator/pkg/assets"
monitoringclient "github.com/prometheus-operator/prometheus-operator/pkg/client/versioned"
"github.com/prometheus-operator/prometheus-operator/pkg/informers"
"github.com/prometheus-operator/prometheus-operator/pkg/k8sutil"
"github.com/prometheus-operator/prometheus-operator/pkg/listwatch"
"github.com/prometheus-operator/prometheus-operator/pkg/operator"
"github.com/prometheus-operator/prometheus-operator/pkg/prometheus"
prometheusgoclient "github.com/prometheus/client_golang/prometheus"
promconfig "github.com/prometheus/prometheus/config"
kubeDiscovery "github.com/prometheus/prometheus/discovery/kubernetes"
"gopkg.in/yaml.v2"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/cache"
"k8s.io/client-go/util/retry"
allocatorconfig "github.com/open-telemetry/opentelemetry-operator/cmd/otel-allocator/config"
)
const (
resyncPeriod = 5 * time.Minute
minEventInterval = time.Second * 5
)
func NewPrometheusCRWatcher(ctx context.Context, logger logr.Logger, cfg allocatorconfig.Config) (*PrometheusCRWatcher, error) {
// TODO: Remove this after go 1.23 upgrade
promLogger := level.NewFilter(gokitlog.NewLogfmtLogger(os.Stderr), level.AllowWarn())
slogger := slog.New(logr.ToSlogHandler(logger))
var resourceSelector *prometheus.ResourceSelector
mClient, err := monitoringclient.NewForConfig(cfg.ClusterConfig)
if err != nil {
return nil, err
}
clientset, err := kubernetes.NewForConfig(cfg.ClusterConfig)
if err != nil {
return nil, err
}
factory := informers.NewMonitoringInformerFactories(map[string]struct{}{v1.NamespaceAll: {}}, map[string]struct{}{}, mClient, allocatorconfig.DefaultResyncTime, nil) //TODO decide what strategy to use regarding namespaces
monitoringInformers, err := getInformers(factory)
if err != nil {
return nil, err
}
// we want to use endpointslices by default
serviceDiscoveryRole := monitoringv1.ServiceDiscoveryRole("EndpointSlice")
// TODO: We should make these durations configurable
prom := &monitoringv1.Prometheus{
Spec: monitoringv1.PrometheusSpec{
CommonPrometheusFields: monitoringv1.CommonPrometheusFields{
ScrapeInterval: monitoringv1.Duration(cfg.PrometheusCR.ScrapeInterval.String()),
PodMonitorSelector: cfg.PrometheusCR.PodMonitorSelector,
PodMonitorNamespaceSelector: cfg.PrometheusCR.PodMonitorNamespaceSelector,
ServiceMonitorSelector: cfg.PrometheusCR.ServiceMonitorSelector,
ServiceMonitorNamespaceSelector: cfg.PrometheusCR.ServiceMonitorNamespaceSelector,
ScrapeConfigSelector: cfg.PrometheusCR.ScrapeConfigSelector,
ScrapeConfigNamespaceSelector: cfg.PrometheusCR.ScrapeConfigNamespaceSelector,
ProbeSelector: cfg.PrometheusCR.ProbeSelector,
ProbeNamespaceSelector: cfg.PrometheusCR.ProbeNamespaceSelector,
ServiceDiscoveryRole: &serviceDiscoveryRole,
},
},
}
generator, err := prometheus.NewConfigGenerator(promLogger, prom, true)
if err != nil {
return nil, err
}
store := assets.NewStoreBuilder(clientset.CoreV1(), clientset.CoreV1())
promRegisterer := prometheusgoclient.NewRegistry()
operatorMetrics := operator.NewMetrics(promRegisterer)
eventRecorderFactory := operator.NewEventRecorderFactory(false)
eventRecorder := eventRecorderFactory(clientset, "target-allocator")
var nsMonInf cache.SharedIndexInformer
getNamespaceInformerErr := retry.OnError(retry.DefaultRetry,
func(err error) bool {
logger.Error(err, "Retrying namespace informer creation in promOperator CRD watcher")
return true
}, func() error {
nsMonInf, err = getNamespaceInformer(ctx, map[string]struct{}{v1.NamespaceAll: {}}, promLogger, clientset, operatorMetrics)
return err
})
if getNamespaceInformerErr != nil {
logger.Error(getNamespaceInformerErr, "Failed to create namespace informer in promOperator CRD watcher")
return nil, getNamespaceInformerErr
}
resourceSelector, err = prometheus.NewResourceSelector(slogger, prom, store, nsMonInf, operatorMetrics, eventRecorder)
if err != nil {
logger.Error(err, "Failed to create resource selector in promOperator CRD watcher")
}
return &PrometheusCRWatcher{
logger: slogger,
kubeMonitoringClient: mClient,
k8sClient: clientset,
informers: monitoringInformers,
nsInformer: nsMonInf,
stopChannel: make(chan struct{}),
eventInterval: minEventInterval,
configGenerator: generator,
kubeConfigPath: cfg.KubeConfigFilePath,
podMonitorNamespaceSelector: cfg.PrometheusCR.PodMonitorNamespaceSelector,
serviceMonitorNamespaceSelector: cfg.PrometheusCR.ServiceMonitorNamespaceSelector,
scrapeConfigNamespaceSelector: cfg.PrometheusCR.ScrapeConfigNamespaceSelector,
probeNamespaceSelector: cfg.PrometheusCR.ProbeNamespaceSelector,
resourceSelector: resourceSelector,
store: store,
}, nil
}
type PrometheusCRWatcher struct {
logger *slog.Logger
kubeMonitoringClient monitoringclient.Interface
k8sClient kubernetes.Interface
informers map[string]*informers.ForResource
nsInformer cache.SharedIndexInformer
eventInterval time.Duration
stopChannel chan struct{}
configGenerator *prometheus.ConfigGenerator
kubeConfigPath string
podMonitorNamespaceSelector *metav1.LabelSelector
serviceMonitorNamespaceSelector *metav1.LabelSelector
scrapeConfigNamespaceSelector *metav1.LabelSelector
probeNamespaceSelector *metav1.LabelSelector
resourceSelector *prometheus.ResourceSelector
store *assets.StoreBuilder
}
func getNamespaceInformer(ctx context.Context, allowList map[string]struct{}, promOperatorLogger gokitlog.Logger, clientset kubernetes.Interface, operatorMetrics *operator.Metrics) (cache.SharedIndexInformer, error) {
kubernetesVersion, err := clientset.Discovery().ServerVersion()
if err != nil {
return nil, err
}
kubernetesSemverVersion, err := semver.ParseTolerant(kubernetesVersion.String())
if err != nil {
return nil, err
}
lw, _, err := listwatch.NewNamespaceListWatchFromClient(
ctx,
promOperatorLogger,
kubernetesSemverVersion,
clientset.CoreV1(),
clientset.AuthorizationV1().SelfSubjectAccessReviews(),
allowList,
map[string]struct{}{},
)
if err != nil {
return nil, err
}
return cache.NewSharedIndexInformer(
operatorMetrics.NewInstrumentedListerWatcher(lw),
&v1.Namespace{}, resyncPeriod, cache.Indexers{},
), nil
}
// getInformers returns a map of informers for the given resources.
func getInformers(factory informers.FactoriesForNamespaces) (map[string]*informers.ForResource, error) {
serviceMonitorInformers, err := informers.NewInformersForResource(factory, monitoringv1.SchemeGroupVersion.WithResource(monitoringv1.ServiceMonitorName))
if err != nil {
return nil, err
}
podMonitorInformers, err := informers.NewInformersForResource(factory, monitoringv1.SchemeGroupVersion.WithResource(monitoringv1.PodMonitorName))
if err != nil {
return nil, err
}
probeInformers, err := informers.NewInformersForResource(factory, monitoringv1.SchemeGroupVersion.WithResource(monitoringv1.ProbeName))
if err != nil {
return nil, err
}
scrapeConfigInformers, err := informers.NewInformersForResource(factory, promv1alpha1.SchemeGroupVersion.WithResource(promv1alpha1.ScrapeConfigName))
if err != nil {
return nil, err
}
return map[string]*informers.ForResource{
monitoringv1.ServiceMonitorName: serviceMonitorInformers,
monitoringv1.PodMonitorName: podMonitorInformers,
monitoringv1.ProbeName: probeInformers,
promv1alpha1.ScrapeConfigName: scrapeConfigInformers,
}, nil
}
// Watch wrapped informers and wait for an initial sync.
func (w *PrometheusCRWatcher) Watch(upstreamEvents chan Event, upstreamErrors chan error) error {
success := true
// this channel needs to be buffered because notifications are asynchronous and neither producers nor consumers wait
notifyEvents := make(chan struct{}, 1)
if w.nsInformer != nil {
go w.nsInformer.Run(w.stopChannel)
if ok := w.WaitForNamedCacheSync("namespace", w.nsInformer.HasSynced); !ok {
success = false
}
_, _ = w.nsInformer.AddEventHandler(cache.ResourceEventHandlerFuncs{
UpdateFunc: func(oldObj, newObj interface{}) {
old := oldObj.(*v1.Namespace)
cur := newObj.(*v1.Namespace)
// Periodic resync may resend the Namespace without changes
// in-between.
if old.ResourceVersion == cur.ResourceVersion {
return
}
for name, selector := range map[string]*metav1.LabelSelector{
"PodMonitorNamespaceSelector": w.podMonitorNamespaceSelector,
"ServiceMonitorNamespaceSelector": w.serviceMonitorNamespaceSelector,
"ProbeNamespaceSelector": w.probeNamespaceSelector,
"ScrapeConfigNamespaceSelector": w.scrapeConfigNamespaceSelector,
} {
sync, err := k8sutil.LabelSelectionHasChanged(old.Labels, cur.Labels, selector)
if err != nil {
w.logger.Error("Failed to check label selection between namespaces while handling namespace updates", "selector", name, "error", err)
return
}
if sync {
select {
case notifyEvents <- struct{}{}:
default:
}
return
}
}
},
})
} else {
w.logger.Info("Unable to watch namespaces since namespace informer is nil")
}
for name, resource := range w.informers {
resource.Start(w.stopChannel)
if ok := w.WaitForNamedCacheSync(name, resource.HasSynced); !ok {
w.logger.Info("skipping informer", "informer", name)
continue
}
// only send an event notification if there isn't one already
resource.AddEventHandler(cache.ResourceEventHandlerFuncs{
// these functions only write to the notification channel if it's empty to avoid blocking
// if scrape config updates are being rate-limited
AddFunc: func(obj interface{}) {
select {
case notifyEvents <- struct{}{}:
default:
}
},
UpdateFunc: func(oldObj, newObj interface{}) {
select {
case notifyEvents <- struct{}{}:
default:
}
},
DeleteFunc: func(obj interface{}) {
select {
case notifyEvents <- struct{}{}:
default:
}
},
})
}
if !success {
return fmt.Errorf("failed to sync one of the caches")
}
// limit the rate of outgoing events
w.rateLimitedEventSender(upstreamEvents, notifyEvents)
<-w.stopChannel
return nil
}
// rateLimitedEventSender sends events to the upstreamEvents channel whenever it gets a notification on the notifyEvents channel,
// but not more frequently than once per w.eventPeriod.
func (w *PrometheusCRWatcher) rateLimitedEventSender(upstreamEvents chan Event, notifyEvents chan struct{}) {
ticker := time.NewTicker(w.eventInterval)
defer ticker.Stop()
event := Event{
Source: EventSourcePrometheusCR,
Watcher: Watcher(w),
}
for {
select {
case <-w.stopChannel:
return
case <-ticker.C: // throttle events to avoid excessive updates
select {
case <-notifyEvents:
select {
case upstreamEvents <- event:
default: // put the notification back in the queue if we can't send it upstream
select {
case notifyEvents <- struct{}{}:
default:
}
}
default:
}
}
}
}
func (w *PrometheusCRWatcher) Close() error {
close(w.stopChannel)
return nil
}
func (w *PrometheusCRWatcher) LoadConfig(ctx context.Context) (*promconfig.Config, error) {
promCfg := &promconfig.Config{}
if w.resourceSelector != nil {
serviceMonitorInstances, err := w.resourceSelector.SelectServiceMonitors(ctx, w.informers[monitoringv1.ServiceMonitorName].ListAllByNamespace)
if err != nil {
return nil, err
}
podMonitorInstances, err := w.resourceSelector.SelectPodMonitors(ctx, w.informers[monitoringv1.PodMonitorName].ListAllByNamespace)
if err != nil {
return nil, err
}
probeInstances, err := w.resourceSelector.SelectProbes(ctx, w.informers[monitoringv1.ProbeName].ListAllByNamespace)
if err != nil {
return nil, err
}
scrapeConfigInstances, err := w.resourceSelector.SelectScrapeConfigs(ctx, w.informers[promv1alpha1.ScrapeConfigName].ListAllByNamespace)
if err != nil {
return nil, err
}
generatedConfig, err := w.configGenerator.GenerateServerConfiguration(
"30s",
"",
nil,
nil,
monitoringv1.TSDBSpec{},
nil,
nil,
serviceMonitorInstances,
podMonitorInstances,
probeInstances,
scrapeConfigInstances,
w.store,
nil,
nil,
nil,
[]string{})
if err != nil {
return nil, err
}
unmarshalErr := yaml.Unmarshal(generatedConfig, promCfg)
if unmarshalErr != nil {
return nil, unmarshalErr
}
// set kubeconfig path to service discovery configs, else kubernetes_sd will always attempt in-cluster
// authentication even if running with a detected kubeconfig
for _, scrapeConfig := range promCfg.ScrapeConfigs {
for _, serviceDiscoveryConfig := range scrapeConfig.ServiceDiscoveryConfigs {
if serviceDiscoveryConfig.Name() == "kubernetes" {
sdConfig := interface{}(serviceDiscoveryConfig).(*kubeDiscovery.SDConfig)
sdConfig.KubeConfig = w.kubeConfigPath
}
}
}
return promCfg, nil
} else {
w.logger.Info("Unable to load config since resource selector is nil, returning empty prometheus config")
return promCfg, nil
}
}
// WaitForNamedCacheSync adds a timeout to the informer's wait for the cache to be ready.
// If the PrometheusCRWatcher is unable to load an informer within 15 seconds, the method is
// cancelled and returns false. A successful informer load will return true. This method also
// will be cancelled if the target allocator's stopChannel is called before it returns.
//
// This method is inspired by the upstream prometheus-operator implementation, with a shorter timeout
// and support for the PrometheusCRWatcher's stopChannel.
// https://github.com/prometheus-operator/prometheus-operator/blob/293c16c854ce69d1da9fdc8f0705de2d67bfdbfa/pkg/operator/operator.go#L433
func (w *PrometheusCRWatcher) WaitForNamedCacheSync(controllerName string, inf cache.InformerSynced) bool {
ctx, cancel := context.WithTimeout(context.Background(), time.Second*15)
t := time.NewTicker(time.Second * 5)
defer t.Stop()
go func() {
for {
select {
case <-t.C:
w.logger.Debug("cache sync not yet completed")
case <-ctx.Done():
return
case <-w.stopChannel:
w.logger.Warn("stop received, shutting down cache syncing")
cancel()
return
}
}
}()
ok := cache.WaitForNamedCacheSync(controllerName, ctx.Done(), inf)
if !ok {
w.logger.Error("failed to sync cache")
} else {
w.logger.Debug("successfully synced cache")
}
return ok
}