-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
307 lines (244 loc) · 7.13 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
package main
import (
"fmt"
"log"
"os"
"sync"
"github.com/olekukonko/tablewriter"
"github.com/pkg/errors"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
_ "k8s.io/client-go/plugin/pkg/client/auth/gcp"
"k8s.io/client-go/tools/clientcmd"
metricsclientset "k8s.io/metrics/pkg/client/clientset_generated/clientset"
)
const (
// TODO: Make configrable
maxLength = 30
)
type NodeMetrics struct {
Name string
Usage corev1.ResourceList
}
type NodeResources struct {
Name string
Allocatable corev1.ResourceList
Capacity corev1.ResourceList
}
type PodMetrics struct {
Name string
ContainerMetrics []ContainerMetrics
}
type PodResources struct {
Name string
Namespace string
NodeName string
ContainerResources []ContainerResources
}
type ContainerMetrics struct {
Name string
Usage corev1.ResourceList
}
type ContainerResources struct {
Name string
ResourceRequests corev1.ResourceList
ResourceLimits corev1.ResourceList
}
func formatResourceList(rl corev1.ResourceList) string {
return fmt.Sprintf("cpu=%s mem=%dMi", rl.Cpu().String(), rl.Memory().ScaledValue(resource.Mega))
}
type KubernetesResources struct {
namespace string
kubeClient *kubernetes.Clientset
metricsClient *metricsclientset.Clientset
nodeMetrics map[string]NodeMetrics
nodeResources map[string]NodeResources
podMetrics map[string]PodMetrics
podResources map[string]PodResources
}
func (kr *KubernetesResources) getNodeMetrics() error {
metrics, err := kr.metricsClient.Metrics().NodeMetricses().List(metav1.ListOptions{})
if err != nil {
return errors.Wrapf(err, "unable to get node metrics")
}
kr.nodeMetrics = make(map[string]NodeMetrics, len(metrics.Items))
for _, n := range metrics.Items {
kr.nodeMetrics[n.Name] = NodeMetrics{
Name: n.Name,
Usage: n.Usage,
}
}
return nil
}
func (kr *KubernetesResources) getNodeResources() error {
nodes, err := kr.kubeClient.CoreV1().Nodes().List(metav1.ListOptions{})
if err != nil {
return errors.Wrapf(err, "unable to get node resources")
}
kr.nodeResources = make(map[string]NodeResources, len(nodes.Items))
for _, node := range nodes.Items {
kr.nodeResources[node.Name] = NodeResources{
Name: node.Name,
Allocatable: node.Status.Allocatable,
Capacity: node.Status.Capacity,
}
}
return nil
}
func (kr *KubernetesResources) getPodMetrics() error {
metrics, err := kr.metricsClient.Metrics().PodMetricses(kr.namespace).List(metav1.ListOptions{})
if err != nil {
return errors.Wrapf(err, "unable to get pod metrics")
}
kr.podMetrics = make(map[string]PodMetrics, len(metrics.Items))
for _, pod := range metrics.Items {
pm := PodMetrics{
Name: pod.Name,
ContainerMetrics: make([]ContainerMetrics, len(pod.Containers)),
}
for i, c := range pod.Containers {
pm.ContainerMetrics[i] = ContainerMetrics{
Name: c.Name,
Usage: c.Usage,
}
}
kr.podMetrics[pod.Name] = pm
}
return nil
}
func (kr *KubernetesResources) getPodResources() error {
pods, err := kr.kubeClient.CoreV1().Pods("").List(metav1.ListOptions{})
if err != nil {
return errors.Wrapf(err, "unable to get pod resources")
}
kr.podResources = make(map[string]PodResources, len(pods.Items))
for _, pod := range pods.Items {
pr := PodResources{
Name: pod.Name,
NodeName: pod.Spec.NodeName,
Namespace: pod.Namespace,
ContainerResources: make([]ContainerResources, len(pod.Spec.Containers)),
}
for i, c := range pod.Spec.Containers {
pr.ContainerResources[i] = ContainerResources{
Name: c.Name,
ResourceRequests: c.Resources.Requests,
ResourceLimits: c.Resources.Limits,
}
}
kr.podResources[pod.Name] = pr
}
return nil
}
func (kr *KubernetesResources) Gather() {
wg := sync.WaitGroup{}
wg.Add(4)
go func() {
kr.getNodeMetrics()
wg.Done()
}()
go func() {
kr.getNodeResources()
wg.Done()
}()
go func() {
kr.getPodMetrics()
wg.Done()
}()
go func() {
kr.getPodResources()
wg.Done()
}()
wg.Wait()
frl := formatResourceList
ml := func(s string) string {
if len(s) > maxLength {
return s[:maxLength/2] + "..." + s[len(s)-maxLength/2:]
}
return s
}
table := tablewriter.NewWriter(os.Stdout)
table.SetHeader([]string{"Namespace", "Pod", "Container", "Usage", "Requests", "Limits"})
data := [][]string{}
nodeRequests := map[string][]corev1.ResourceList{}
nodeLimits := map[string][]corev1.ResourceList{}
for n := range kr.podMetrics {
m := kr.podMetrics[n]
r := kr.podResources[n]
for i := range m.ContainerMetrics {
cm := m.ContainerMetrics[i]
cr := r.ContainerResources[i]
data = append(data, []string{ml(r.Namespace), ml(n), ml(cm.Name), frl(cm.Usage), frl(cr.ResourceRequests), frl(cr.ResourceLimits)})
nodeRequests[r.NodeName] = append(nodeRequests[r.NodeName], cr.ResourceRequests)
nodeLimits[r.NodeName] = append(nodeLimits[r.NodeName], cr.ResourceLimits)
}
}
table.AppendBulk(data)
table.Render()
tableNode := tablewriter.NewWriter(os.Stdout)
tableNode.SetHeader([]string{"Node", "Usage", "Allocatable", "Resource Requests", "Resource Limits"})
data = [][]string{}
for n := range kr.nodeMetrics {
m := kr.nodeMetrics[n]
r := kr.nodeResources[n]
cpuReqTotal := resource.Quantity{}
memReqTotal := resource.Quantity{}
for _, rl := range nodeRequests[n] {
cpuReqTotal.Add(*rl.Cpu())
memReqTotal.Add(*rl.Memory())
}
cpuLimTotal := resource.Quantity{}
memLimTotal := resource.Quantity{}
for _, rl := range nodeLimits[n] {
cpuLimTotal.Add(*rl.Cpu())
memLimTotal.Add(*rl.Memory())
}
// TODO(vishen): move these to function that formatResourceList also uses
requestsString := fmt.Sprintf("cpu=%s mem=%dMi", cpuReqTotal.String(), memReqTotal.ScaledValue(resource.Mega))
limitsString := fmt.Sprintf("cpu=%s mem=%dMi", cpuLimTotal.String(), memLimTotal.ScaledValue(resource.Mega))
data = append(data, []string{ml(n), frl(m.Usage), frl(r.Allocatable), requestsString, limitsString})
}
tableNode.AppendBulk(data)
tableNode.Render()
}
func main() {
kubeConfig := ""
kubeContext := ""
namespace := ""
// Determine kubeconfig path
if kubeConfig == "" {
if os.Getenv("KUBECONFIG") != "" {
kubeConfig = os.Getenv("KUBECONFIG")
} else {
kubeConfig = clientcmd.RecommendedHomeFile
}
}
// Create the kubernetes client configuration
clientConfig, err := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(
&clientcmd.ClientConfigLoadingRules{
ExplicitPath: kubeConfig,
},
&clientcmd.ConfigOverrides{
CurrentContext: kubeContext,
},
).ClientConfig()
if err != nil {
log.Fatalf("unable to create k8s client config: %s\n", err)
}
client, err := kubernetes.NewForConfig(clientConfig)
if err != nil {
log.Fatalf("unable to create k8s client: %s\n", err)
}
metricsClient, err := metricsclientset.NewForConfig(clientConfig)
if err != nil {
log.Fatalf("unable to create metrics client: %s\n", err)
}
kr := KubernetesResources{
namespace: namespace,
kubeClient: client,
metricsClient: metricsClient,
}
kr.Gather()
}