-
Notifications
You must be signed in to change notification settings - Fork 135
/
cluster_metrics.go
143 lines (119 loc) · 3.85 KB
/
cluster_metrics.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
package clustermetrics
import (
"context"
"time"
"github.com/stackrox/rox/generated/internalapi/central"
"github.com/stackrox/rox/generated/storage"
"github.com/stackrox/rox/pkg/centralsensor"
"github.com/stackrox/rox/pkg/concurrency"
"github.com/stackrox/rox/pkg/logging"
"github.com/stackrox/rox/sensor/common"
"github.com/stackrox/rox/sensor/common/message"
metricsPkg "github.com/stackrox/rox/sensor/common/metrics"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
)
var log = logging.LoggerForModule()
// Interval for querying cluster metrics from Kubernetes and sending to Central.
var defaultInterval = 5 * time.Minute
// Timeout for querying cluster metrics from Kubernetes.
var defaultTimeout = 10 * time.Second
// ClusterMetrics collects metrics from secured clusters and sends them to Central.
type ClusterMetrics interface {
common.SensorComponent
}
// New returns a new cluster metrics Sensor component.
func New(k8sClient kubernetes.Interface) ClusterMetrics {
return NewWithInterval(k8sClient, defaultInterval)
}
// NewWithInterval returns a new cluster metrics Sensor component.
func NewWithInterval(k8sClient kubernetes.Interface, pollInterval time.Duration) ClusterMetrics {
ticker := time.NewTicker(pollInterval)
ticker.Stop()
return &clusterMetricsImpl{
output: make(chan *message.ExpiringMessage),
stopper: concurrency.NewStopper(),
pollingInterval: pollInterval,
pollingTimeout: defaultTimeout,
k8sClient: k8sClient,
pollTicker: ticker,
}
}
type clusterMetricsImpl struct {
output chan *message.ExpiringMessage
stopper concurrency.Stopper
pollingInterval time.Duration
pollingTimeout time.Duration
k8sClient kubernetes.Interface
pollTicker *time.Ticker
}
func (cm *clusterMetricsImpl) Start() error {
go cm.Poll(cm.pollTicker.C)
return nil
}
func (cm *clusterMetricsImpl) Stop(_ error) {
cm.pollTicker.Stop()
cm.stopper.Client().Stop()
_ = cm.stopper.Client().Stopped().Wait()
}
func (cm *clusterMetricsImpl) Notify(e common.SensorComponentEvent) {
switch e {
case common.SensorComponentEventCentralReachable:
cm.pollTicker.Reset(cm.pollingInterval)
case common.SensorComponentEventOfflineMode:
cm.pollTicker.Stop()
}
}
func (cm *clusterMetricsImpl) Capabilities() []centralsensor.SensorCapability {
return []centralsensor.SensorCapability{}
}
func (cm *clusterMetricsImpl) ProcessMessage(_ *central.MsgToSensor) error {
return nil
}
func (cm *clusterMetricsImpl) ResponsesC() <-chan *message.ExpiringMessage {
return cm.output
}
func (cm *clusterMetricsImpl) ProcessIndicator(_ *storage.ProcessIndicator) {}
func (cm *clusterMetricsImpl) Poll(tickerC <-chan time.Time) {
defer cm.stopper.Flow().ReportStopped()
cm.runPipeline()
go func() {
for {
select {
case <-cm.stopper.Flow().StopRequested():
return
case <-tickerC:
cm.runPipeline()
}
}
}()
}
func (cm *clusterMetricsImpl) runPipeline() {
if metrics, err := cm.collectMetrics(); err == nil {
cm.output <- message.New(¢ral.MsgFromSensor{
Msg: ¢ral.MsgFromSensor_ClusterMetrics{
ClusterMetrics: metrics,
},
})
metricsPkg.SetTelemetryMetrics(metrics)
} else {
log.Errorf("Collection of cluster metrics failed: %v", err.Error())
}
}
func (cm *clusterMetricsImpl) collectMetrics() (*central.ClusterMetrics, error) {
ctx, cancel := context.WithTimeout(context.Background(), cm.pollingTimeout)
defer cancel()
nodes, err := cm.k8sClient.CoreV1().Nodes().List(ctx, metav1.ListOptions{})
if err != nil {
return nil, err
}
var nodeCount int64 = int64(len(nodes.Items))
var capacity int64
for _, node := range nodes.Items {
if cpu := node.Status.Capacity.Cpu(); cpu != nil {
// Rounds up to the nearest integer away from zero.
capacity += cpu.Value()
}
}
return ¢ral.ClusterMetrics{NodeCount: nodeCount, CpuCapacity: capacity}, nil
}