-
Notifications
You must be signed in to change notification settings - Fork 7
/
main.go
439 lines (405 loc) · 18.7 KB
/
main.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
package main
import (
"context"
"encoding/json"
"flag"
"fmt"
"net/http"
"os"
"strconv"
"time"
"github.com/gorilla/mux"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/clientcmd"
stats "k8s.io/kubelet/pkg/apis/stats/v1alpha1"
// Support auth providers in kubeconfig files
_ "k8s.io/client-go/plugin/pkg/client/auth"
)
var (
flagKubeConfigPath = flag.String("kubeconfig", "", "Path of a kubeconfig file, if not provided the app will try $KUBECONFIG, $HOME/.kube/config or in cluster config")
flagListenAddress = flag.String("listen-address", ":9779", "Listen address")
metricsNamespace = "kube_summary"
)
type Collectors struct {
containerLogsInodesFree *prometheus.GaugeVec
containerLogsInodes *prometheus.GaugeVec
containerLogsInodesUsed *prometheus.GaugeVec
containerLogsAvailableBytes *prometheus.GaugeVec
containerLogsCapacityBytes *prometheus.GaugeVec
containerLogsUsedBytes *prometheus.GaugeVec
containerRootFsInodesFree *prometheus.GaugeVec
containerRootFsInodes *prometheus.GaugeVec
containerRootFsInodesUsed *prometheus.GaugeVec
containerRootFsAvailableBytes *prometheus.GaugeVec
containerRootFsCapacityBytes *prometheus.GaugeVec
containerRootFsUsedBytes *prometheus.GaugeVec
podEphemeralStorageAvailableBytes *prometheus.GaugeVec
podEphemeralStorageCapacityBytes *prometheus.GaugeVec
podEphemeralStorageUsedBytes *prometheus.GaugeVec
podEphemeralStorageInodesFree *prometheus.GaugeVec
podEphemeralStorageInodes *prometheus.GaugeVec
podEphemeralStorageInodesUsed *prometheus.GaugeVec
nodeRuntimeImageFSAvailableBytes *prometheus.GaugeVec
nodeRuntimeImageFSCapacityBytes *prometheus.GaugeVec
nodeRuntimeImageFSUsedBytes *prometheus.GaugeVec
nodeRuntimeImageFSInodesFree *prometheus.GaugeVec
nodeRuntimeImageFSInodes *prometheus.GaugeVec
nodeRuntimeImageFSInodesUsed *prometheus.GaugeVec
}
func newCollectors() *Collectors {
return &Collectors{
containerLogsInodesFree: prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: metricsNamespace,
Name: "container_logs_inodes_free",
Help: "Number of available Inodes for logs",
}, []string{"node", "pod", "namespace", "name"}),
containerLogsInodes: prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: metricsNamespace,
Name: "container_logs_inodes",
Help: "Number of Inodes for logs",
}, []string{"node", "pod", "namespace", "name"}),
containerLogsInodesUsed: prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: metricsNamespace,
Name: "container_logs_inodes_used",
Help: "Number of used Inodes for logs",
}, []string{"node", "pod", "namespace", "name"}),
containerLogsAvailableBytes: prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: metricsNamespace,
Name: "container_logs_available_bytes",
Help: "Number of bytes that aren't consumed by the container logs",
}, []string{"node", "pod", "namespace", "name"}),
containerLogsCapacityBytes: prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: metricsNamespace,
Name: "container_logs_capacity_bytes",
Help: "Number of bytes that can be consumed by the container logs",
}, []string{"node", "pod", "namespace", "name"}),
containerLogsUsedBytes: prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: metricsNamespace,
Name: "container_logs_used_bytes",
Help: "Number of bytes that are consumed by the container logs",
}, []string{"node", "pod", "namespace", "name"}),
containerRootFsInodesFree: prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: metricsNamespace,
Name: "container_rootfs_inodes_free",
Help: "Number of available Inodes",
}, []string{"node", "pod", "namespace", "name"}),
containerRootFsInodes: prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: metricsNamespace,
Name: "container_rootfs_inodes",
Help: "Number of Inodes",
}, []string{"node", "pod", "namespace", "name"}),
containerRootFsInodesUsed: prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: metricsNamespace,
Name: "container_rootfs_inodes_used",
Help: "Number of used Inodes",
}, []string{"node", "pod", "namespace", "name"}),
containerRootFsAvailableBytes: prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: metricsNamespace,
Name: "container_rootfs_available_bytes",
Help: "Number of bytes that aren't consumed by the container",
}, []string{"node", "pod", "namespace", "name"}),
containerRootFsCapacityBytes: prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: metricsNamespace,
Name: "container_rootfs_capacity_bytes",
Help: "Number of bytes that can be consumed by the container",
}, []string{"node", "pod", "namespace", "name"}),
containerRootFsUsedBytes: prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: metricsNamespace,
Name: "container_rootfs_used_bytes",
Help: "Number of bytes that are consumed by the container",
}, []string{"node", "pod", "namespace", "name"}),
podEphemeralStorageAvailableBytes: prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: metricsNamespace,
Name: "pod_ephemeral_storage_available_bytes",
Help: "Number of bytes of Ephemeral storage that aren't consumed by the pod",
}, []string{"node", "pod", "namespace"}),
podEphemeralStorageCapacityBytes: prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: metricsNamespace,
Name: "pod_ephemeral_storage_capacity_bytes",
Help: "Number of bytes of Ephemeral storage that can be consumed by the pod",
}, []string{"node", "pod", "namespace"}),
podEphemeralStorageUsedBytes: prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: metricsNamespace,
Name: "pod_ephemeral_storage_used_bytes",
Help: "Number of bytes of Ephemeral storage that are consumed by the pod",
}, []string{"node", "pod", "namespace"}),
podEphemeralStorageInodesFree: prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: metricsNamespace,
Name: "pod_ephemeral_storage_inodes_free",
Help: "Number of available Inodes for pod Ephemeral storage",
}, []string{"node", "pod", "namespace"}),
podEphemeralStorageInodes: prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: metricsNamespace,
Name: "pod_ephemeral_storage_inodes",
Help: "Number of Inodes for pod Ephemeral storage",
}, []string{"node", "pod", "namespace"}),
podEphemeralStorageInodesUsed: prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: metricsNamespace,
Name: "pod_ephemeral_storage_inodes_used",
Help: "Number of used Inodes for pod Ephemeral storage",
}, []string{"node", "pod", "namespace"}),
nodeRuntimeImageFSAvailableBytes: prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: metricsNamespace,
Name: "node_runtime_imagefs_available_bytes",
Help: "Number of bytes of node Runtime ImageFS that aren't consumed",
}, []string{"node"}),
nodeRuntimeImageFSCapacityBytes: prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: metricsNamespace,
Name: "node_runtime_imagefs_capacity_bytes",
Help: "Number of bytes of node Runtime ImageFS that can be consumed",
}, []string{"node"}),
nodeRuntimeImageFSUsedBytes: prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: metricsNamespace,
Name: "node_runtime_imagefs_used_bytes",
Help: "Number of bytes of node Runtime ImageFS that are consumed",
}, []string{"node"}),
nodeRuntimeImageFSInodesFree: prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: metricsNamespace,
Name: "node_runtime_imagefs_inodes_free",
Help: "Number of available Inodes for node Runtime ImageFS",
}, []string{"node"}),
nodeRuntimeImageFSInodes: prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: metricsNamespace,
Name: "node_runtime_imagefs_inodes",
Help: "Number of Inodes for node Runtime ImageFS",
}, []string{"node"}),
nodeRuntimeImageFSInodesUsed: prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: metricsNamespace,
Name: "node_runtime_imagefs_inodes_used",
Help: "Number of used Inodes for node Runtime ImageFS",
}, []string{"node"}),
}
}
func (c *Collectors) register(registry *prometheus.Registry) {
registry.MustRegister(
c.containerLogsInodesFree,
c.containerLogsInodes,
c.containerLogsInodesUsed,
c.containerLogsAvailableBytes,
c.containerLogsCapacityBytes,
c.containerLogsUsedBytes,
c.containerRootFsInodesFree,
c.containerRootFsInodes,
c.containerRootFsInodesUsed,
c.containerRootFsAvailableBytes,
c.containerRootFsCapacityBytes,
c.containerRootFsUsedBytes,
c.podEphemeralStorageAvailableBytes,
c.podEphemeralStorageCapacityBytes,
c.podEphemeralStorageUsedBytes,
c.podEphemeralStorageInodesFree,
c.podEphemeralStorageInodes,
c.podEphemeralStorageInodesUsed,
c.nodeRuntimeImageFSAvailableBytes,
c.nodeRuntimeImageFSCapacityBytes,
c.nodeRuntimeImageFSUsedBytes,
c.nodeRuntimeImageFSInodesFree,
c.nodeRuntimeImageFSInodes,
c.nodeRuntimeImageFSInodesUsed,
)
}
// collectSummaryMetrics collects metrics from a /stats/summary response
func collectSummaryMetrics(summary *stats.Summary, collectors *Collectors) {
nodeName := summary.Node.NodeName
for _, pod := range summary.Pods {
for _, container := range pod.Containers {
if logs := container.Logs; logs != nil {
if inodesFree := logs.InodesFree; inodesFree != nil {
collectors.containerLogsInodesFree.WithLabelValues(nodeName, pod.PodRef.Name, pod.PodRef.Namespace, container.Name).Set(float64(*inodesFree))
}
if inodes := logs.Inodes; inodes != nil {
collectors.containerLogsInodes.WithLabelValues(nodeName, pod.PodRef.Name, pod.PodRef.Namespace, container.Name).Set(float64(*inodes))
}
if inodesUsed := logs.InodesUsed; inodesUsed != nil {
collectors.containerLogsInodesUsed.WithLabelValues(nodeName, pod.PodRef.Name, pod.PodRef.Namespace, container.Name).Set(float64(*inodesUsed))
}
if availableBytes := logs.AvailableBytes; availableBytes != nil {
collectors.containerLogsAvailableBytes.WithLabelValues(nodeName, pod.PodRef.Name, pod.PodRef.Namespace, container.Name).Set(float64(*availableBytes))
}
if capacityBytes := logs.CapacityBytes; capacityBytes != nil {
collectors.containerLogsCapacityBytes.WithLabelValues(nodeName, pod.PodRef.Name, pod.PodRef.Namespace, container.Name).Set(float64(*capacityBytes))
}
if usedBytes := logs.UsedBytes; usedBytes != nil {
collectors.containerLogsUsedBytes.WithLabelValues(nodeName, pod.PodRef.Name, pod.PodRef.Namespace, container.Name).Set(float64(*usedBytes))
}
}
if rootfs := container.Rootfs; rootfs != nil {
if inodesFree := rootfs.InodesFree; inodesFree != nil {
collectors.containerRootFsInodesFree.WithLabelValues(nodeName, pod.PodRef.Name, pod.PodRef.Namespace, container.Name).Set(float64(*inodesFree))
}
if inodes := rootfs.Inodes; inodes != nil {
collectors.containerRootFsInodes.WithLabelValues(nodeName, pod.PodRef.Name, pod.PodRef.Namespace, container.Name).Set(float64(*inodes))
}
if inodesUsed := rootfs.InodesUsed; inodesUsed != nil {
collectors.containerRootFsInodesUsed.WithLabelValues(nodeName, pod.PodRef.Name, pod.PodRef.Namespace, container.Name).Set(float64(*inodesUsed))
}
if availableBytes := rootfs.AvailableBytes; availableBytes != nil {
collectors.containerRootFsAvailableBytes.WithLabelValues(nodeName, pod.PodRef.Name, pod.PodRef.Namespace, container.Name).Set(float64(*availableBytes))
}
if capacityBytes := rootfs.CapacityBytes; capacityBytes != nil {
collectors.containerRootFsCapacityBytes.WithLabelValues(nodeName, pod.PodRef.Name, pod.PodRef.Namespace, container.Name).Set(float64(*capacityBytes))
}
if usedBytes := rootfs.UsedBytes; usedBytes != nil {
collectors.containerRootFsUsedBytes.WithLabelValues(nodeName, pod.PodRef.Name, pod.PodRef.Namespace, container.Name).Set(float64(*usedBytes))
}
}
}
if ephemeralStorage := pod.EphemeralStorage; ephemeralStorage != nil {
if ephemeralStorage.AvailableBytes != nil {
collectors.podEphemeralStorageAvailableBytes.WithLabelValues(nodeName, pod.PodRef.Name, pod.PodRef.Namespace).Set(float64(*ephemeralStorage.AvailableBytes))
}
if ephemeralStorage.CapacityBytes != nil {
collectors.podEphemeralStorageCapacityBytes.WithLabelValues(nodeName, pod.PodRef.Name, pod.PodRef.Namespace).Set(float64(*ephemeralStorage.CapacityBytes))
}
if ephemeralStorage.UsedBytes != nil {
collectors.podEphemeralStorageUsedBytes.WithLabelValues(nodeName, pod.PodRef.Name, pod.PodRef.Namespace).Set(float64(*ephemeralStorage.UsedBytes))
}
if ephemeralStorage.InodesFree != nil {
collectors.podEphemeralStorageInodesFree.WithLabelValues(nodeName, pod.PodRef.Name, pod.PodRef.Namespace).Set(float64(*ephemeralStorage.InodesFree))
}
if ephemeralStorage.Inodes != nil {
collectors.podEphemeralStorageInodes.WithLabelValues(nodeName, pod.PodRef.Name, pod.PodRef.Namespace).Set(float64(*ephemeralStorage.Inodes))
}
if ephemeralStorage.InodesUsed != nil {
collectors.podEphemeralStorageInodesUsed.WithLabelValues(nodeName, pod.PodRef.Name, pod.PodRef.Namespace).Set(float64(*ephemeralStorage.InodesUsed))
}
}
}
if runtime := summary.Node.Runtime; runtime != nil {
if runtime.ImageFs.AvailableBytes != nil {
collectors.nodeRuntimeImageFSAvailableBytes.WithLabelValues(nodeName).Set(float64(*runtime.ImageFs.AvailableBytes))
}
if runtime.ImageFs.CapacityBytes != nil {
collectors.nodeRuntimeImageFSCapacityBytes.WithLabelValues(nodeName).Set(float64(*runtime.ImageFs.CapacityBytes))
}
if runtime.ImageFs.UsedBytes != nil {
collectors.nodeRuntimeImageFSUsedBytes.WithLabelValues(nodeName).Set(float64(*runtime.ImageFs.UsedBytes))
}
if runtime.ImageFs.InodesFree != nil {
collectors.nodeRuntimeImageFSInodesFree.WithLabelValues(nodeName).Set(float64(*runtime.ImageFs.InodesFree))
}
if runtime.ImageFs.Inodes != nil {
collectors.nodeRuntimeImageFSInodes.WithLabelValues(nodeName).Set(float64(*runtime.ImageFs.Inodes))
}
if runtime.ImageFs.InodesUsed != nil {
collectors.nodeRuntimeImageFSInodesUsed.WithLabelValues(nodeName).Set(float64(*runtime.ImageFs.InodesUsed))
}
}
}
// nodeHandler returns metrics for the /stats/summary API of the given node
func nodeHandler(w http.ResponseWriter, r *http.Request, kubeClient *kubernetes.Clientset) {
node := mux.Vars(r)["node"]
ctx, cancel := timeoutContext(r)
defer cancel()
summary, err := nodeSummary(ctx, kubeClient, node)
if err != nil {
http.Error(w, fmt.Sprintf("Error querying /stats/summary for %s: %v", node, err), http.StatusInternalServerError)
return
}
collectors := newCollectors()
registry := prometheus.NewRegistry()
collectors.register(registry)
collectSummaryMetrics(summary, collectors)
h := promhttp.HandlerFor(registry, promhttp.HandlerOpts{})
h.ServeHTTP(w, r)
}
// allNodesHandler returns metrics for all nodes in the cluster
func allNodesHandler(w http.ResponseWriter, r *http.Request, kubeClient *kubernetes.Clientset) {
ctx, cancel := timeoutContext(r)
defer cancel()
nodes, err := kubeClient.CoreV1().Nodes().List(ctx, metav1.ListOptions{})
if err != nil {
http.Error(w, fmt.Sprintf("Error listing nodes: %v", err), http.StatusInternalServerError)
return
}
collectors := newCollectors()
registry := prometheus.NewRegistry()
collectors.register(registry)
for _, node := range nodes.Items {
summary, err := nodeSummary(ctx, kubeClient, node.Name)
if err != nil {
http.Error(w, fmt.Sprintf("Error querying /stats/summary for %s: %v", node.Name, err), http.StatusInternalServerError)
return
}
collectSummaryMetrics(summary, collectors)
}
h := promhttp.HandlerFor(registry, promhttp.HandlerOpts{})
h.ServeHTTP(w, r)
}
// nodeSummary retrieves the summary for a single node
func nodeSummary(ctx context.Context, kubeClient *kubernetes.Clientset, nodeName string) (*stats.Summary, error) {
req := kubeClient.CoreV1().RESTClient().Get().Resource("nodes").Name(nodeName).SubResource("proxy").Suffix("stats/summary")
resp, err := req.DoRaw(ctx)
if err != nil {
return nil, fmt.Errorf("error querying /stats/summary for %s: %v", nodeName, err)
}
summary := &stats.Summary{}
if err := json.Unmarshal(resp, summary); err != nil {
return nil, fmt.Errorf("error unmarshaling /stats/summary response for %s: %v", nodeName, err)
}
return summary, nil
}
// timeoutContext returns a context with timeout based on the X-Prometheus-Scrape-Timeout-Seconds header
func timeoutContext(r *http.Request) (context.Context, context.CancelFunc) {
if v := r.Header.Get("X-Prometheus-Scrape-Timeout-Seconds"); v != "" {
timeoutSeconds, err := strconv.ParseFloat(v, 64)
if err == nil {
return context.WithTimeout(r.Context(), time.Duration(timeoutSeconds*float64(time.Second)))
}
}
return context.WithCancel(r.Context())
}
// newKubeClient returns a Kubernetes client (clientset) with configurable
// rate limits from a supplied kubeconfig path, the KUBECONFIG environment variable,
// the default config file location ($HOME/.kube/config), or from the in-cluster
// service account environment.
func newKubeClient(path string) (*kubernetes.Clientset, error) {
loadingRules := clientcmd.NewDefaultClientConfigLoadingRules()
if path != "" {
loadingRules.ExplicitPath = path
}
kubeConfig := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(
loadingRules,
&clientcmd.ConfigOverrides{},
)
config, err := kubeConfig.ClientConfig()
if err != nil {
return nil, err
}
// Set rate limits to reduce client-side throttling
config.QPS = 100
config.Burst = 200
return kubernetes.NewForConfig(config)
}
func main() {
flag.Parse()
kubeClient, err := newKubeClient(*flagKubeConfigPath)
if err != nil {
fmt.Printf("[Error] Cannot create kube client: %v", err)
os.Exit(1)
}
r := mux.NewRouter()
r.HandleFunc("/nodes", func(w http.ResponseWriter, r *http.Request) {
allNodesHandler(w, r, kubeClient)
})
r.HandleFunc("/node/{node}", func(w http.ResponseWriter, r *http.Request) {
nodeHandler(w, r, kubeClient)
})
r.Handle("/metrics", promhttp.Handler())
r.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
_, _ = w.Write([]byte(`<html>
<head><title>Kube Summary Exporter</title></head>
<body>
<h1>Kube Summary Exporter</h1>
<p><a href="/nodes">Retrieve metrics for all nodes</a></p>
<p><a href="/node/example-node">Retrieve metrics for 'example-node'</a></p>
<p><a href="/metrics">Metrics</a></p>
</body>
</html>`))
})
fmt.Printf("Listening on %s\n", *flagListenAddress)
fmt.Printf("error: %v\n", http.ListenAndServe(*flagListenAddress, r))
}