-
Notifications
You must be signed in to change notification settings - Fork 89
/
gather_monitoring_pvs.go
90 lines (76 loc) · 2.35 KB
/
gather_monitoring_pvs.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
package clusterconfig
import (
"context"
"fmt"
"strings"
"github.com/openshift/insights-operator/pkg/record"
metaV1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
coreV1 "k8s.io/client-go/kubernetes/typed/core/v1"
)
// GatherMonitoringPVs Collects Persistent Volumes from openshift-monitoring namespace
// which matches with ConfigMap configuration yaml
//
// ### API Reference
// - https://github.com/kubernetes/client-go/blob/master/kubernetes/typed/core/v1/configmap.go
// - https://github.com/kubernetes/client-go/blob/master/kubernetes/typed/core/v1/persistentvolume.go
//
// ### Sample data
// - docs/insights-archive-sample/config/persistentvolumes/monitoring-persistent-volume.json
//
// ### Location in archive
// - `config/persistentvolumes/{persistent_volume_name}.json`
//
// ### Config ID
// `clusterconfig/monitoring_persistent_volumes`
//
// ### Released version
// - 4.14
//
// ### Backported versions
// - 4.13.0
// - 4.12.17
// - 4.11.41
//
// ### Changes
// None
func (g *Gatherer) GatherMonitoringPVs(ctx context.Context) ([]record.Record, []error) {
kubeClient, err := kubernetes.NewForConfig(g.gatherProtoKubeConfig)
if err != nil {
return nil, []error{err}
}
mg := MonitoringPVGatherer{client: kubeClient.CoreV1()}
return mg.gather(ctx)
}
type MonitoringPVGatherer struct {
client coreV1.CoreV1Interface
}
// gather returns the persistent volumes found as records for its gathering
// and a collection of errors
func (mg MonitoringPVGatherer) gather(ctx context.Context) ([]record.Record, []error) {
const Namespace = "openshift-monitoring"
const PrometheusDefault = "prometheus-k8s"
pvcList, err := mg.client.PersistentVolumeClaims(Namespace).List(ctx, metaV1.ListOptions{})
if err != nil {
return []record.Record{}, []error{err}
}
var records []record.Record
var errors []error
pvInterface := mg.client.PersistentVolumes()
for i := range pvcList.Items {
pvcName := pvcList.Items[i].Name
if strings.Contains(pvcName, PrometheusDefault) {
pvName := pvcList.Items[i].Spec.VolumeName
pv, err := pvInterface.Get(ctx, pvName, metaV1.GetOptions{})
if err != nil {
errors = append(errors, err)
continue
}
records = append(records, record.Record{
Name: fmt.Sprintf("config/persistentvolumes/%s", pv.Name),
Item: record.ResourceMarshaller{Resource: pv},
})
}
}
return records, errors
}