/
handler.go
694 lines (552 loc) · 20.6 KB
/
handler.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
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
package stub
import (
"bytes"
"fmt"
"github.com/banzaicloud/prometheus-jmx-exporter-operator/pkg/apis/banzaicloud/v1alpha1"
"github.com/operator-framework/operator-sdk/pkg/sdk/action"
"github.com/operator-framework/operator-sdk/pkg/sdk/handler"
"github.com/operator-framework/operator-sdk/pkg/sdk/query"
"github.com/operator-framework/operator-sdk/pkg/sdk/types"
"github.com/ghodss/yaml"
"github.com/sirupsen/logrus"
"io/ioutil"
"k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"os"
"path"
"strconv"
"strings"
)
func NewHandler() handler.Handler {
return &Handler{}
}
type Handler struct {
// Fill me
}
func (h *Handler) Handle(ctx types.Context, event types.Event) error {
switch o := event.Object.(type) {
case *v1alpha1.PrometheusJmxExporter:
prometheusJmxExporter := o
logrus.Infof("PrometheusJmxExporter event received for '%s/%s'",
prometheusJmxExporter.Namespace,
prometheusJmxExporter.Name)
if event.Deleted {
logrus.Infof(
"PrometheusJmxExporter deleted event received for '%s/%s'",
prometheusJmxExporter.Namespace,
prometheusJmxExporter.Name)
return nil
}
logrus.Infof(
"Retrieving prometheus jmx exporter config from configMap '%s/%s:%s'",
prometheusJmxExporter.Namespace,
prometheusJmxExporter.Spec.Config.ConfigMapName,
prometheusJmxExporter.Spec.Config.ConfigMapKey)
config, err := getConfig(
prometheusJmxExporter.Namespace,
prometheusJmxExporter.Spec.Config.ConfigMapName,
prometheusJmxExporter.Spec.Config.ConfigMapKey)
logrus.Debug(config)
if err != nil {
logrus.Errorf("Error during retrieving prometheus jmx exporter config")
return err
}
logrus.Infof("Retrieving pods with label: '%s'", prometheusJmxExporter.Spec.LabelSelector)
podList, err := queryPods(
prometheusJmxExporter.Namespace,
labels.SelectorFromSet(prometheusJmxExporter.Spec.LabelSelector).String())
if err != nil {
logrus.Errorf("Error during querying pods : %v", err)
return err
}
logrus.Infof("Pods found: namespace='%s', %s", prometheusJmxExporter.Namespace, formatSimplePods(podList.Items))
if len(podList.Items) == 0 {
return nil
}
if err := checkPrometheusJmxExporterConflict(podList, prometheusJmxExporter); err != nil {
return err
}
processPods(podList.Items, config, prometheusJmxExporter.Spec.Port)
// update status
newStatus := createPrometheusJmxExporterStatus(podList.Items)
if !prometheusJmxExporter.Status.Equals(newStatus) {
prometheusJmxExporter.Status = createPrometheusJmxExporterStatus(podList.Items)
logrus.Infof(
"PrometheusJmxExporter: '%s/%s' : Update status",
prometheusJmxExporter.Namespace,
prometheusJmxExporter.Name)
action.Update(prometheusJmxExporter)
}
case *v1.Pod:
pod := o
if !event.Deleted && pod.Status.Phase != v1.PodRunning {
// process only running pods
return nil
}
prometheusJmxExporters, err := queryPrometheusJmxExporters(pod.Namespace)
if err != nil {
logrus.Errorf("Error during querying prometheusjmxexporters in namespace '%s': %v", pod.Namespace, err)
return err
}
prometheusJmxExporter, err := findExporterForPod(prometheusJmxExporters, pod)
if err != nil {
return err
}
if prometheusJmxExporter == nil {
return nil
}
if event.Deleted {
logrus.Infof("Pod deleted event received for '%s/%s'", pod.Namespace, pod.Name)
// pod is being deleted thus remove the prometheus endpoint that
// is exposed by this pod if there is any
removePrometheusJmxExporterEndpoint(prometheusJmxExporter, pod)
action.Update(prometheusJmxExporter)
return nil
}
if isVerified(pod) {
logrus.Infof("Ignoring pod '%s/%s' as it has already been processed.", pod.Namespace, pod.Name)
} else {
logrus.Infof("Retrieving prometheus jmx exporter config from configMap '%s/%s:%s'",
prometheusJmxExporter.Namespace,
prometheusJmxExporter.Spec.Config.ConfigMapName,
prometheusJmxExporter.Spec.Config.ConfigMapKey)
config, err := getConfig(
prometheusJmxExporter.Namespace,
prometheusJmxExporter.Spec.Config.ConfigMapName,
prometheusJmxExporter.Spec.Config.ConfigMapKey)
logrus.Debug(config)
if err != nil {
logrus.Errorf("Error during retrieving prometheus jmx exporter config")
return err
}
if err := processPod(pod, config, prometheusJmxExporter.Spec.Port); err != nil {
return err
}
if updatePrometheusJmxExporterEndpoints(prometheusJmxExporter, pod) {
logrus.Infof(
"PrometheusJmxExporter: '%s/%s' : Update status",
prometheusJmxExporter.Namespace,
prometheusJmxExporter.Name)
action.Update(prometheusJmxExporter)
}
}
}
return nil
}
// queryPrometheusJmxExporters returns PrometheusJmxExporterList from given namespace
func queryPrometheusJmxExporters(namespace string) (*v1alpha1.PrometheusJmxExporterList, error) {
jmxExporterList := v1alpha1.PrometheusJmxExporterList{
TypeMeta: metav1.TypeMeta{
Kind: "PrometheusJmxExporter",
APIVersion: "banzaicloud.com/v1alpha1",
},
}
listOptions := query.WithListOptions(&metav1.ListOptions{
IncludeUninitialized: false,
})
if err := query.List(namespace, &jmxExporterList, listOptions); err != nil {
logrus.Errorf("Failed to query prometheusjmxexporters : %v", err)
return nil, err
}
return &jmxExporterList, nil
}
// getConfig returns the content of the config data stored under configMapKey
func getConfig(namespace, configMapName, configMapKey string) (*v1alpha1.PrometheusJmxExporterConfig, error) {
configMap := v1.ConfigMap{
TypeMeta: metav1.TypeMeta{
Kind: "ConfigMap",
APIVersion: "v1",
},
ObjectMeta: metav1.ObjectMeta{
Name: configMapName,
Namespace: namespace,
},
}
getOptions := query.WithGetOptions(&metav1.GetOptions{
IncludeUninitialized: false,
})
err := query.Get(&configMap, getOptions)
if err != nil {
logrus.Errorf("Failed to get configMap namespace='%s', name='%s': %v", namespace, configMapName, err)
return nil, err
}
config, ok := configMap.Data[configMapKey]
if !ok {
return nil, fmt.Errorf("configMap data with key '%s' not found in configMap '%s/%s'", configMapKey, namespace, configMapName)
}
logrus.Debugf("Validating config data '%s'", config)
var configObj = v1alpha1.PrometheusJmxExporterConfig{}
err = yaml.Unmarshal([]byte(config), &configObj)
if err != nil {
return nil, err
}
return &configObj, err
}
// queryPods returns list of pods according to the labelSelector
func queryPods(namespace, labelSelector string) (*v1.PodList, error) {
podList := v1.PodList{
TypeMeta: metav1.TypeMeta{
Kind: "Pod",
APIVersion: "v1",
},
}
listOptions := query.WithListOptions(&metav1.ListOptions{
LabelSelector: labelSelector,
IncludeUninitialized: false,
})
err := query.List(namespace, &podList, listOptions)
if err != nil {
logrus.Errorf("Failed to query pods : %v", err)
return nil, err
}
var filteredPods []v1.Pod
for i := 0; i < len(podList.Items); i++ {
if podList.Items[i].Status.Phase == v1.PodRunning {
filteredPods = append(filteredPods, podList.Items[i])
}
}
podList.Items = filteredPods
return &podList, nil
}
// processPods loads prometheus jmx exporter agent into each pod
func processPods(pods []v1.Pod, config *v1alpha1.PrometheusJmxExporterConfig, portNumber int) {
logrus.Info("Processing running pods...")
for i := 0; i < len(pods); i++ {
pod := &pods[i]
if isVerified(pod) {
logrus.Infof("Ignoring pod '%s/%s' as it has already been processed.", pod.Namespace, pod.Name)
} else {
err := processPod(pod, config, portNumber)
if err != nil {
logrus.Warnf("Processing pod failed: %v", err)
}
}
}
logrus.Info("Processing pods finished.")
}
// isVerified returns true of the pod was already processed and verified
func isVerified(pod *v1.Pod) bool {
v, ok := pod.Annotations[prometheusJmxExporterAnnotationKey]
return ok && (v == prometheusJmxExporterAnnotationVerified || v == prometheusJmxExporterAnnotationVerifiedFailed)
}
// processPod loads prometheus jmx exporter agent into the pod
func processPod(pod *v1.Pod, config *v1alpha1.PrometheusJmxExporterConfig, portNumber int) error {
logrus.Infof("Inspecting pod '%s'", pod.Name)
if len(pod.Spec.Containers) > 0 {
// TODO: prometheus doesn't support scraping multiple containers running on the same pod
// TODO: in case of multiple containers which container we should go with
container := pod.Spec.Containers[0]
pids, err := queryJavaProcesses(pod, &container)
if err != nil {
logrus.Infof("Mark pod '%s' as verify failed", pod.Name)
podVerifiedFailed(pod)
return err
}
if len(pids) > 1 {
// TODO: should we support multiple java processes per container?
return fmt.Errorf("multiple java processes found")
}
// copy jars
if err := copyJmxPrometheusExporterJars(pod, &container); err != nil {
return err
}
// copy config to pod container
if err := copyPrometheusJmxExporterConfToPod(config, pod, &container); err != nil {
return err
}
// open port for prometheus jmx exporter
// TODO: port number should be determined dynamically such way that doesn't conflicts with ports
// already in use by the processes running in the container
logrus.Infof("Exposing port number %d on '%s/%s/%s'", portNumber, pod.Namespace, pod.Name, container.Name)
if err := exposeContainerPort(int32(portNumber), pod, &container); err != nil {
return err
}
// load prometheus jmx exporter agent
if err := loadPrometheusJmxExporterAgent(pod, &container, portNumber, pids[0]); err != nil {
return err
}
// annotade pod for prometheus
annotateForPrometheus(pod, portNumber)
}
logrus.Infof("Mark pod '%s' as verified", pod.Name)
return podVerified(pod)
}
// podVerified updates the pod annotations to mark it as verified.
func podVerifiedFailed(pod *v1.Pod) error {
annotations := map[string]string{
prometheusJmxExporterAnnotationKey: prometheusJmxExporterAnnotationVerifiedFailed,
}
return annotatePod(pod, annotations)
}
// podVerified updates the pod annotations to mark it as verified.
func podVerified(pod *v1.Pod) error {
annotations := map[string]string{
prometheusJmxExporterAnnotationKey: prometheusJmxExporterAnnotationVerified,
}
return annotatePod(pod, annotations)
}
// annotatePod annotates pod with the given annotations
func annotatePod(pod *v1.Pod, annotations map[string]string) error {
if pod.Annotations == nil {
pod.Annotations = make(map[string]string)
}
for key, value := range annotations {
pod.Annotations[key] = value
}
err := action.Update(pod)
if err != nil {
logrus.Errorf("Updating pod '%s' failed: %v", pod.Name, err)
return err
}
return nil
}
// queryJavaProcesses inspects container for running java processes
func queryJavaProcesses(pod *v1.Pod, container *v1.Container) ([]string, error) {
logrus.Infof("Inspecting container '%s' for java processes", container.Name)
stdout, err := execCommand(pod.Namespace, pod.Name, nil, container,
"sh", "-c", "$JAVA_HOME/bin/jps")
if err != nil {
logrus.Warnf("Failed to retrieve java process list: %v", err)
return nil, err
} else {
var javaProcIds []string
procs := strings.Split(strings.TrimSpace(stdout), "\n")
for _, proc := range procs {
idAndClassName := strings.Split(proc, " ")
procId := idAndClassName[0]
className := idAndClassName[1]
if !strings.EqualFold(className, "jps") { // skip jps
javaProcIds = append(javaProcIds, procId)
}
}
logrus.Infof("Java processes: %s", javaProcIds)
return javaProcIds, nil
}
}
// copyPrometheusJmxExporterConfToPod creates config file from the configContent and copies it to the pod container
func copyPrometheusJmxExporterConfToPod(configContent *v1alpha1.PrometheusJmxExporterConfig, pod *v1.Pod, container *v1.Container) error {
tmpDir, err := ioutil.TempDir("", "prometheus-jmx-exporter-conf")
if err != nil {
return err
}
defer os.RemoveAll(tmpDir) // clean up
configData, err := yaml.Marshal(configContent)
if err != nil {
return err
}
f, err := os.Create(path.Join(tmpDir, prometheusJmxExportedConfigFilename))
if err != nil {
return err
}
if _, err = f.Write(configData); err != nil {
return err
}
if err := f.Close(); err != nil {
return err
}
err = copyToPod(pod.Namespace, pod.Name, container, tmpDir, path.Join(prometheusJmxExporterTargetDir, prometheusJmxExporterTargetConfDir))
if err != nil {
logrus.Errorf("Copying config for jmx-exporter to container '%s/%s/%s'failed: %v",
pod.Namespace, pod.Name, container.Name, err)
return err
}
return nil
}
// copyJmxPrometheusExporterJars copies the jars of prometheus jmx exporter to pod
func copyJmxPrometheusExporterJars(pod *v1.Pod, container *v1.Container) error {
err := copyToPod(pod.Namespace, pod.Name, container, prometheusJmxExporterSrcJarsDir, prometheusJmxExporterTargetDir)
if err != nil {
logrus.Errorf("Copying jmx-exporter-loader jars to container '%s/%s/%s'failed: %v",
pod.Namespace, pod.Name, container.Name, err)
return err
}
return nil
}
// exposeContainerPort exposes portNumber on container
func exposeContainerPort(portNumber int32, pod *v1.Pod, container *v1.Container) error {
for _, port := range container.Ports {
if port.ContainerPort == portNumber {
return fmt.Errorf("port number %d is already in use", portNumber)
}
}
container.Ports = append(container.Ports, v1.ContainerPort{
ContainerPort: portNumber,
Name: "prometheusJmxMetrics",
Protocol: v1.ProtocolTCP,
})
return action.Update(pod)
}
// loadPrometheusJmxExporterAgent loads prometheus jmx exporter agent into the process with pid
// running inside container
func loadPrometheusJmxExporterAgent(pod *v1.Pod, container *v1.Container, portNumber int, pid string) error {
logrus.Infof("Loading prometheus jmx exporter agent into process with pid %s running inside '%s/%s/%s'",
pid, pod.Namespace, pod.Name, container.Name)
var javaCmd bytes.Buffer
javaCmd.WriteString("$JAVA_HOME/bin/java -cp ")
javaCmd.WriteString(path.Join(prometheusJmxExporterTargetDir, prometheusJmxExporterLoaderJar))
javaCmd.WriteString(" -Dpid=")
javaCmd.WriteString(pid)
javaCmd.WriteString(" -Dprometheus.javaagent.path=")
javaCmd.WriteString(path.Join(prometheusJmxExporterTargetDir, prometheusJmxExporterAgentJar))
javaCmd.WriteString(" -Dprometheus.port=")
javaCmd.WriteString(strconv.Itoa(portNumber))
javaCmd.WriteString(" -Dprometheus.javaagent.configPath=")
javaCmd.WriteString(path.Join(prometheusJmxExporterTargetDir, prometheusJmxExporterTargetConfDir, prometheusJmxExportedConfigFilename))
javaCmd.WriteString(" ")
javaCmd.WriteString(prometheusJmxExporterLoaderClass)
command := []string{"sh", "-c", javaCmd.String()}
_, err := execCommand(pod.Namespace, pod.Name, nil, container, command...)
return err
}
// annotateForPrometheus places annotation on the pod that provide
func annotateForPrometheus(pod *v1.Pod, portNumber int) error {
annotations := map[string]string{
"prometheus.io/scrape": "true",
"prometheus.io/port": strconv.Itoa(portNumber),
}
return annotatePod(pod, annotations)
}
// createPrometheusJmxExporterStatus collects the endpoints through which prometheus can
// scrape metrics published by the prometheus jmx exporter
func createPrometheusJmxExporterStatus(pods []v1.Pod) v1alpha1.PrometheusJmxExporterStatus {
var status v1alpha1.PrometheusJmxExporterStatus
for i := 0; i < len(pods); i++ {
pod := pods[i]
if endpoint := createMetricEndpoint(&pod); endpoint != nil {
status.MetricsEndpoints = append(status.MetricsEndpoints, endpoint)
}
}
return status
}
// updatePrometheusJmxExporterEndpoints adds prometheus endpoint published by pod
// to the metrics endpoints of prometheusJmxExporter. If the pod doesn't exposes port for prometheus
// or port hasn't changed then metrics endpoints of prometheusJmxExporter is not changed.
// Returns true if metrics endpoints of prometheusJmxExporter is changed otherwise returns false.
func updatePrometheusJmxExporterEndpoints(prometheusJmxExporter *v1alpha1.PrometheusJmxExporter, pod *v1.Pod) bool {
if endpoint := createMetricEndpoint(pod); endpoint != nil {
for _, endpointUpd := range prometheusJmxExporter.Status.MetricsEndpoints {
if endpointUpd.Pod == pod.Name {
if endpointUpd.Port != endpoint.Port {
endpointUpd.Port = endpoint.Port
return true
}
return false
}
}
prometheusJmxExporter.Status.MetricsEndpoints = append(prometheusJmxExporter.Status.MetricsEndpoints, endpoint)
return true
}
return false
}
// removePrometheusJmxExporterEndpoint removes the endpoint entry from prometheusJmxExporter.Status.MetricsEndpoints
// that corresponds to pod
func removePrometheusJmxExporterEndpoint(prometheusJmxExporter *v1alpha1.PrometheusJmxExporter, pod *v1.Pod) {
matchIdx := -1
for i, endpointUpd := range prometheusJmxExporter.Status.MetricsEndpoints {
if endpointUpd.Pod == pod.Name {
matchIdx = i
break
}
}
if matchIdx == 0 {
prometheusJmxExporter.Status.MetricsEndpoints = prometheusJmxExporter.Status.MetricsEndpoints[1:]
} else if matchIdx > 0 {
prometheusJmxExporter.Status.MetricsEndpoints =
append(prometheusJmxExporter.Status.MetricsEndpoints[:matchIdx], prometheusJmxExporter.Status.MetricsEndpoints[matchIdx+1:]...)
}
}
func createMetricEndpoint(pod *v1.Pod) *v1alpha1.MetricsEndpoint {
if enabled, ok := pod.Annotations["prometheus.io/scrape"]; ok && enabled == "true" {
if portStr, ok := pod.Annotations["prometheus.io/port"]; ok {
port, _ := strconv.Atoi(portStr)
return &v1alpha1.MetricsEndpoint{
Pod: pod.Name,
Port: port,
}
}
}
return nil
}
// findExporterForPod searches through prometheusJmxExporterList and returns PrometheusJmxExporter
// of which pod label selector matches the labels of pod. In case of multiple matches found return with error.
func findExporterForPod(prometheusJmxExporterList *v1alpha1.PrometheusJmxExporterList, pod *v1.Pod) (*v1alpha1.PrometheusJmxExporter, error) {
var matchIdx []int
for i := 0; i < len(prometheusJmxExporterList.Items); i++ {
prometheusJmxExporter := prometheusJmxExporterList.Items[i]
selector := labels.SelectorFromSet(prometheusJmxExporter.Spec.LabelSelector)
if selector.Matches(labels.Set(pod.Labels)) {
matchIdx = append(matchIdx, i)
}
}
if len(matchIdx) > 1 {
var list []v1alpha1.PrometheusJmxExporter
for _, idx := range matchIdx {
list = append(list, prometheusJmxExporterList.Items[idx])
}
logrus.Errorf("Multiple prometheusjmxexporters for pod '%s/%s' found: %s",
pod.Namespace, pod.Name, formatSimplePrometheusJmxExporters(list))
return nil, fmt.Errorf("multiple prometheusjmxexporters for pod '%s/%s' found", pod.Namespace, pod.Namespace)
}
if len(matchIdx) == 0 {
return nil, nil
}
return &prometheusJmxExporterList.Items[matchIdx[0]], nil
}
// checkPrometheusJmxExporterConflict verifies whether beside prometheusJmxExporter there are any other
// PrometheusJmxExporters that matches the pods in podList
func checkPrometheusJmxExporterConflict(podList *v1.PodList, prometheusJmxExporter *v1alpha1.PrometheusJmxExporter) error {
prometheusJmxExporters, err := queryPrometheusJmxExporters(prometheusJmxExporter.Namespace)
if err != nil {
return err
}
for i := 0; i < len(podList.Items); i++ {
pod := podList.Items[i]
for j := 0; j < len(prometheusJmxExporters.Items); j++ {
otherPrometheusJmxExporter := prometheusJmxExporters.Items[j]
if otherPrometheusJmxExporter.Name != prometheusJmxExporter.Name {
selector := labels.SelectorFromSet(otherPrometheusJmxExporter.Spec.LabelSelector)
if selector.Matches(labels.Set(pod.Labels)) {
logrus.Errorf("prometheusjmxexporter '%s' for pod '%s/%s' already defined",
pod.Namespace,
pod.Namespace,
otherPrometheusJmxExporter.Name)
return fmt.Errorf(
"prometheusjmxexporter '%s' for pod '%s/%s' already defined",
pod.Namespace,
pod.Namespace,
otherPrometheusJmxExporter.Name)
}
}
}
}
return nil
}
// formatSimplePods returns the name of the pods delimited by comma surrounded by parenthesis.
func formatSimplePods(pods []v1.Pod) string {
var buffer bytes.Buffer
buffer.WriteString("(")
for i := 0; i < len(pods); i++ {
pod := pods[i]
if i != 0 {
buffer.WriteString(",")
}
buffer.WriteString(pod.Name)
}
buffer.WriteString(")")
return buffer.String()
}
// formatSimplePrometheusJmxExporters returns the name of the prometheusjmxexporters delimited by comma surrounded by parenthesis.
func formatSimplePrometheusJmxExporters(prometheusjmxexporters []v1alpha1.PrometheusJmxExporter) string {
var buffer bytes.Buffer
buffer.WriteString("(")
for i := 0; i < len(prometheusjmxexporters); i++ {
prometheusjmxexporter := prometheusjmxexporters[i]
if i != 0 {
buffer.WriteString(",")
}
buffer.WriteString(prometheusjmxexporter.Name)
}
buffer.WriteString(")")
return buffer.String()
}