-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.go
314 lines (282 loc) · 8.39 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
package main
import (
"context"
"encoding/json"
"flag"
"fmt"
"net/http"
"os"
"path/filepath"
"time"
"gopkg.in/alecthomas/kingpin.v2"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
_ "net/http/pprof"
log "github.com/sirupsen/logrus"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/client-go/util/homedir"
statsapi "k8s.io/kubelet/pkg/apis/stats/v1alpha1"
)
var (
Version = "unknown"
podUsageBytes = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Name: "kubelet_stats_ephemeral_storage_pod_usage",
Help: "Used to expose Ephemeral Storage metrics for pod",
},
[]string{
// name of pod for Ephemeral Storage
"pod_name",
"pod_namespace",
// Name of Node where pod is placed.
"node_name",
},
)
podCapacityBytes = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Name: "kubelet_stats_ephemeral_storage_pod_capacity",
Help: "Used to expose Ephemeral Storage capacity for a pod",
},
[]string{
// name of pod for Ephemeral Storage
"pod_name",
"pod_namespace",
// Name of Node where pod is placed.
"node_name",
},
)
podAvailableBytes = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Name: "kubelet_stats_ephemeral_storage_pod_available",
Help: "Used to expose Ephemeral Storage available for a pod",
},
[]string{
// name of pod for Ephemeral Storage
"pod_name",
"pod_namespace",
// Name of Node where pod is placed.
"node_name",
},
)
containerUsageBytes = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Name: "kubelet_stats_rootfs_pod_container_usage",
Help: "Used to expose rootfs metrics for containers",
},
[]string{
// name of pod for Ephemeral Storage
"pod_name",
"pod_namespace",
"container_name",
// Name of Node where pod is placed.
"node_name",
},
)
)
type Config struct {
logFormat string
logLevel string
kubeconfig string
metricsPort int32
inCluster bool
interval time.Duration
}
func setupClient(cfg Config) *kubernetes.Clientset {
var (
err error
config *rest.Config
)
if cfg.inCluster {
config, err = rest.InClusterConfig()
if err != nil {
log.Fatalf("Failed to get rest config for in cluster client %s", err.Error())
}
} else {
if home := homedir.HomeDir(); home != "" && cfg.kubeconfig == "" {
cfg.kubeconfig = filepath.Join(home, ".kube", "config")
}
flag.Parse()
// use the current context in kubeconfig
config, err = clientcmd.BuildConfigFromFlags("", cfg.kubeconfig)
if err != nil {
log.Fatalf("Failed to get rest config from kubeconfig %s", err.Error())
}
}
// create the clientset
cs, err := kubernetes.NewForConfig(config)
if err != nil {
log.Fatalf("Failed to get client set from kubeconfig %s", err.Error())
}
return cs
}
func metricsLoop(interval time.Duration, cs *kubernetes.Clientset) {
log.Debug("metricsLoop has been invoked")
for {
ctx := context.Background()
err := singleRun(ctx, cs)
if err != nil {
log.Errorf("ErrorBadRequest : %s", err.Error())
}
time.Sleep(interval)
}
}
func singleRun(ctx context.Context, cs *kubernetes.Clientset) error {
nodes, err := cs.CoreV1().Nodes().List(ctx, metav1.ListOptions{})
if err != nil {
return err
}
podUsageBytes.Reset()
podCapacityBytes.Reset()
containerUsageBytes.Reset()
podAvailableBytes.Reset()
var pods []ephemeralStoragePodData
for i := range nodes.Items {
currentNode := nodes.Items[i].Name
node, err := scrapeSingleNode(ctx, cs, currentNode)
if err != nil {
log.Errorf("ErrorBadRequest : %s", err.Error())
continue
}
pods = append(pods, node...)
}
for i := range pods {
pod := &pods[i]
if pod.ephemeral {
setGauge(podUsageBytes, prometheus.Labels{
"pod_name": pod.name,
"pod_namespace": pod.namespace,
"node_name": pod.nodeName,
}, pod.usedBytes)
setGauge(podCapacityBytes, prometheus.Labels{
"pod_name": pod.name,
"pod_namespace": pod.namespace,
"node_name": pod.nodeName,
}, pod.capacityBytes)
setGauge(podAvailableBytes, prometheus.Labels{
"pod_name": pod.name,
"pod_namespace": pod.namespace,
"node_name": pod.nodeName,
}, pod.availableBytes)
}
for k := range pod.containers {
container := &pod.containers[k]
setGauge(containerUsageBytes, prometheus.Labels{
"pod_name": pod.name,
"pod_namespace": pod.namespace,
"node_name": pod.nodeName,
"container_name": container.name,
}, container.usedBytes)
}
}
return nil
}
func setGauge(gauge *prometheus.GaugeVec, labels prometheus.Labels, data uint64) {
gauge.With(labels).Set(float64(data))
}
type ephemeralStorageContainerData struct {
name string
usedBytes uint64
}
type ephemeralStoragePodData struct {
name string
nodeName string
namespace string
ephemeral bool
usedBytes uint64
capacityBytes uint64
availableBytes uint64
containers []ephemeralStorageContainerData
}
func scrapeSingleNode(
ctx context.Context,
cs *kubernetes.Clientset,
currentNode string,
) ([]ephemeralStoragePodData, error) {
content, err := cs.RESTClient().Get().AbsPath(fmt.Sprintf("/api/v1/nodes/%s/proxy/stats/summary", currentNode)).DoRaw(ctx)
if err != nil {
return []ephemeralStoragePodData{}, err
}
log.Debugf("Fetched proxy stats from node : %s", currentNode)
var summary statsapi.Summary
if err = json.Unmarshal(content, &summary); err != nil {
return []ephemeralStoragePodData{}, err
}
var pods []ephemeralStoragePodData
nodeName := summary.Node.NodeName
for i := range summary.Pods {
pod := &summary.Pods[i]
pods = append(pods, extractSinglePodData(pod, nodeName))
}
return pods, nil
}
func extractSinglePodData(pod *statsapi.PodStats, nodeName string) ephemeralStoragePodData {
var podData = ephemeralStoragePodData{}
podData.name = pod.PodRef.Name
podData.namespace = pod.PodRef.Namespace
podData.nodeName = nodeName
if pod.EphemeralStorage != nil {
podData.ephemeral = true
podData.usedBytes = *pod.EphemeralStorage.UsedBytes
podData.capacityBytes = *pod.EphemeralStorage.CapacityBytes
podData.availableBytes = *pod.EphemeralStorage.AvailableBytes
}
for k := range pod.Containers {
container := &pod.Containers[k]
if container.Rootfs != nil {
podData.containers = append(podData.containers, ephemeralStorageContainerData{
name: container.Name,
usedBytes: *container.Rootfs.UsedBytes,
})
}
}
return podData
}
// allLogLevelsAsStrings returns all logrus levels as a list of strings
func allLogLevelsAsStrings() []string {
var levels []string
for _, level := range log.AllLevels {
levels = append(levels, level.String())
}
return levels
}
func (cfg *Config) ParseFlags(args []string) error {
app := kingpin.New("kubelet-stats-metrics", "extract metrics from kubelet summary")
app.Version(Version)
app.DefaultEnvars()
app.Flag("log-format", "The format in which log messages are printed (default: text, options: text, json)").
Default("text").EnumVar(&cfg.logFormat, "text", "json")
app.Flag("log-level", "Set the level of logging. (default: info, options: panic, debug, info, warning, error, fatal").
Default("debug").EnumVar(&cfg.logLevel, allLogLevelsAsStrings()...)
app.Flag("metrics-port", "Define metrics port (default: 9100)").Default("9100").Int32Var(&cfg.metricsPort)
app.Flag("in-cluster", "use in cluster selection (default: true)").Default("true").BoolVar(&cfg.inCluster)
app.Flag("kubeconfig", "Allows to define a path to a kubeconfig (default: \"\")").
Default("").StringVar(&cfg.kubeconfig)
app.Flag("interval", "Defines scrape interval (default: 30s)").
Default("30s").DurationVar(&cfg.interval)
_, err := app.Parse(args)
if err != nil {
return err
}
log.Infof("config: %s", cfg)
if cfg.logFormat == "json" {
log.SetFormatter(&log.JSONFormatter{})
}
ll, err := log.ParseLevel(cfg.logLevel)
if err != nil {
log.Fatalf("failed to parse log level: %v", err)
}
log.SetLevel(ll)
return nil
}
func main() {
cfg := Config{}
if err := cfg.ParseFlags(os.Args[1:]); err != nil {
log.Fatalf("flag parsing error: %v", err)
}
prometheus.MustRegister(podUsageBytes, podCapacityBytes, podAvailableBytes, containerUsageBytes)
cs := setupClient(cfg)
go metricsLoop(cfg.interval, cs)
http.Handle("/metrics", promhttp.Handler())
if err := http.ListenAndServe(fmt.Sprintf(":%d", cfg.metricsPort), nil); err != nil {
log.Fatalf("Listener Failed : %s\n", err.Error())
}
}