-
Notifications
You must be signed in to change notification settings - Fork 0
/
inspector.go
498 lines (444 loc) · 14.4 KB
/
inspector.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
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
package inspector
import (
"context"
"encoding/json"
"errors"
"flag"
"fmt"
"io"
"os"
"strings"
appsv1 "k8s.io/api/apps/v1"
coordv1 "k8s.io/api/coordination/v1"
corev1 "k8s.io/api/core/v1"
netv1 "k8s.io/api/networking/v1"
apiextv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
crd "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset"
"k8s.io/metrics/pkg/apis/metrics/v1beta1"
metrics "k8s.io/metrics/pkg/client/clientset/versioned"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/clientcmd"
)
// Inspector is an inspector client.
type Inspector struct {
Verbose bool
K8sClient kubernetes.Interface
CRDClient *crd.Clientset
MetricsClient *metrics.Clientset
}
// BuildInspectorFromKubeConfig builds an inspector client ready to interact with the K8s cluster.
func BuildInspectorFromKubeConfig() (*Inspector, error) {
home, err := os.UserHomeDir()
if err != nil {
return nil, err
}
config, err := clientcmd.BuildConfigFromFlags("", home+"/.kube/config")
if err != nil {
return nil, err
}
kubeClient, err := kubernetes.NewForConfig(config)
if err != nil {
return nil, err
}
crdClient, err := crd.NewForConfig(config)
if err != nil {
return nil, err
}
metricsClient, err := metrics.NewForConfig(config)
if err != nil {
return nil, err
}
i := Inspector{
Verbose: false,
K8sClient: kubeClient,
CRDClient: crdClient,
MetricsClient: metricsClient,
}
return &i, nil
}
// ClusterVersion returns K8s version.
func (i *Inspector) ClusterVersion() (string, error) {
sv, err := i.K8sClient.Discovery().ServerVersion()
if err != nil {
return "", err
}
return sv.String(), nil
}
// ClusterID returns kube-system namespace UID representing K8s clusterID.
func (i *Inspector) ClusterID(ctx context.Context) (string, error) {
cluster, err := i.K8sClient.CoreV1().Namespaces().Get(ctx, "kube-system", metav1.GetOptions{})
if err != nil {
return "", err
}
return string(cluster.UID), nil
}
// Platform returns K8s platform name.
func (i *Inspector) Platform(ctx context.Context) (string, error) {
nodes, err := i.K8sClient.CoreV1().Nodes().List(ctx, metav1.ListOptions{})
if err != nil {
return "", err
}
if len(nodes.Items) == 0 {
return "", errors.New("cannot verify platform name")
}
return platformName(nodes.Items[0].Spec.ProviderID), nil
}
// platformName takes a string representing a K8s PlatformID
// retrieved from a cluster node and returns a string
// representing the platform name.
func platformName(providerID string) string {
provider := strings.TrimSpace(providerID)
if provider == "" {
return "unknown"
}
provider = strings.ToLower(providerID)
p := strings.Split(provider, ":")
if len(p) == 0 {
return "unknown"
}
if p[0] == "" {
return "unknown"
}
return p[0]
}
// Nodes returns the total number of [nodes] in a cluster.
//
// [nodes]: https://kubernetes.io/docs/concepts/architecture/nodes/
func (i *Inspector) Nodes(ctx context.Context) (int, error) {
nodes, err := i.K8sClient.CoreV1().Nodes().List(ctx, metav1.ListOptions{})
if err != nil {
return 0, err
}
return len(nodes.Items), nil
}
// Pods returns list of [pods] in a given [namespace].
//
// [pods]: https://kubernetes.io/docs/concepts/workloads/pods/
// [namespace]: https://kubernetes.io/docs/concepts/overview/working-with-objects/namespaces/
func (i *Inspector) Pods(ctx context.Context, namespace string) (*corev1.PodList, error) {
pods, err := i.K8sClient.CoreV1().Pods(namespace).List(ctx, metav1.ListOptions{})
if err != nil {
return nil, err
}
return pods, nil
}
// PodLog represents a pod and collected logs
// from containers in the pod.
type PodLog struct {
Name string `json:"name"`
Log string `json:"log"`
}
// Podlogs returns logs from [pods] in a given [namespace].
//
// [pods]: https://kubernetes.io/docs/concepts/workloads/pods/
// [namespace]: https://kubernetes.io/docs/concepts/overview/working-with-objects/namespaces/
func (i *Inspector) Podlogs(ctx context.Context, namespace string) ([]PodLog, error) {
pods, err := i.K8sClient.CoreV1().Pods(namespace).List(ctx, metav1.ListOptions{})
if err != nil {
return nil, err
}
logs := []PodLog{}
for _, pod := range pods.Items {
for _, container := range pod.Spec.Containers {
logReq := i.K8sClient.CoreV1().Pods(namespace).GetLogs(pod.Name, &corev1.PodLogOptions{Container: container.Name})
res, err := logReq.Stream(ctx)
if err != nil {
return nil, err
}
log, err := io.ReadAll(res)
if err != nil {
return []PodLog{}, err
}
pl := PodLog{
Name: fmt.Sprintf("%s_%s", pod.Name, container.Name),
Log: string(log),
}
logs = append(logs, pl)
}
}
return logs, nil
}
// Events returns [events] for a given namespace.
//
// [events]: https://kubernetes.io/docs/reference/kubectl/generated/kubectl_events/
func (i *Inspector) Events(ctx context.Context, namespace string) (*corev1.EventList, error) {
events, err := i.K8sClient.CoreV1().Events(namespace).List(ctx, metav1.ListOptions{})
if err != nil {
return nil, err
}
return events, nil
}
// ConfigMaps returns a list of [config maps] for a given namespace.
//
// [config maps]: https://kubernetes.io/docs/concepts/configuration/configmap/
func (i *Inspector) ConfigMaps(ctx context.Context, namespace string) (*corev1.ConfigMapList, error) {
configMaps, err := i.K8sClient.CoreV1().ConfigMaps(namespace).List(ctx, metav1.ListOptions{})
if err != nil {
return nil, err
}
// json.MarshalIndent(cm, "", " ")
return configMaps, nil
}
// Services returns a list of [services] for a given namespace.
//
// [services]: https://kubernetes.io/docs/concepts/services-networking/service/
func (i *Inspector) Services(ctx context.Context, namespace string) (*corev1.ServiceList, error) {
services, err := i.K8sClient.CoreV1().Services(namespace).List(ctx, metav1.ListOptions{})
if err != nil {
return nil, err
}
return services, nil
}
// Deployments returns a list of [deployments] in a given namespace.
//
// [deployments]: https://kubernetes.io/docs/concepts/workloads/controllers/deployment/
func (i *Inspector) Deployments(ctx context.Context, namespace string) (*appsv1.DeploymentList, error) {
deployments, err := i.K8sClient.AppsV1().Deployments(namespace).List(ctx, metav1.ListOptions{})
if err != nil {
return nil, err
}
return deployments, nil
}
// StatefulSets returns a list of [stateful sets] in a given namespace.
//
// [stateful set]: https://kubernetes.io/docs/concepts/workloads/controllers/statefulset/
func (i *Inspector) StatefulSets(ctx context.Context, namespace string) (*appsv1.StatefulSetList, error) {
statefulSets, err := i.K8sClient.AppsV1().StatefulSets(namespace).List(ctx, metav1.ListOptions{})
if err != nil {
return nil, err
}
return statefulSets, nil
}
// ReplicaSets returns a list of [replica sets] in a given namespace.
//
// [replica sets]: https://kubernetes.io/docs/concepts/workloads/controllers/replicaset/
func (i *Inspector) ReplicaSets(ctx context.Context, namespace string) (*appsv1.ReplicaSetList, error) {
replicaSets, err := i.K8sClient.AppsV1().ReplicaSets(namespace).List(ctx, metav1.ListOptions{})
if err != nil {
return nil, err
}
return replicaSets, nil
}
// Leasess returns a list of [leases] in a given namespace.
//
// [leases]: https://kubernetes.io/docs/concepts/architecture/leases/
func (i *Inspector) Leases(ctx context.Context, namespace string) (*coordv1.LeaseList, error) {
leases, err := i.K8sClient.CoordinationV1().Leases(namespace).List(ctx, metav1.ListOptions{})
if err != nil {
return nil, err
}
return leases, nil
}
// IngressClasses returns a list of [ingress classes] in a cluster.
//
// [ingress classes]: https://kubernetes.io/docs/concepts/services-networking/ingress/#ingress-class
func (i *Inspector) IngressClasses(ctx context.Context) (*netv1.IngressClassList, error) {
ingressClasses, err := i.K8sClient.NetworkingV1().IngressClasses().List(ctx, metav1.ListOptions{})
if err != nil {
return nil, err
}
return ingressClasses, nil
}
// Ingresses returns a list of [ingresses] in a given namespace.
//
// [ingresses]: https://kubernetes.io/docs/concepts/services-networking/ingress/
func (i *Inspector) Ingresses(ctx context.Context, namespace string) (*netv1.IngressList, error) {
ingresses, err := i.K8sClient.NetworkingV1().Ingresses(namespace).List(ctx, metav1.ListOptions{})
if err != nil {
return nil, err
}
return ingresses, nil
}
// CustomResourceDefinitions returns a list of [CRDs] in a cluster.
//
// [CRDs]: https://kubernetes.io/docs/concepts/extend-kubernetes/api-extension/custom-resources/
func (i *Inspector) CustomResourceDefinitions(ctx context.Context) (*apiextv1.CustomResourceDefinitionList, error) {
crds, err := i.CRDClient.ApiextensionsV1().CustomResourceDefinitions().List(ctx, metav1.ListOptions{})
if err != nil {
return nil, err
}
return crds, nil
}
// ClusterNodes returns a list of [nodes] in a [cluster].
//
// [nodes]: https://kubernetes.io/docs/concepts/architecture/nodes/
// [cluster]: https://kubernetes.io/docs/concepts/cluster-administration/
func (i *Inspector) ClusterNodes(ctx context.Context) (*corev1.NodeList, error) {
nodes, err := i.K8sClient.CoreV1().Nodes().List(ctx, metav1.ListOptions{})
if err != nil {
return nil, err
}
return nodes, nil
}
// NodeMetrics returns a list of [node metrics] in a cluster.
//
// [node metrics]: https://kubernetes.io/docs/concepts/cluster-administration/system-metrics/
func (i *Inspector) NodeMetrics(ctx context.Context) (*v1beta1.NodeMetricsList, error) {
metrics, err := i.MetricsClient.MetricsV1beta1().NodeMetricses().List(ctx, metav1.ListOptions{})
if err != nil {
return nil, err
}
return metrics, nil
}
// PodMetrics returns a list of [pods metrics] in a given namespace.
//
// [pods metrics]: https://kubernetes.io/docs/concepts/cluster-administration/kube-state-metrics/
func (i *Inspector) PodMetrics(ctx context.Context, namespace string) (*v1beta1.PodMetricsList, error) {
metrics, err := i.MetricsClient.MetricsV1beta1().PodMetricses(namespace).List(ctx, metav1.ListOptions{})
if err != nil {
return nil, err
}
return metrics, nil
}
// RunDiagnostics collects cluster data points for a given namespace.
func (i *Inspector) Report(ctx context.Context, namespace string) (Report, error) {
version, err := i.ClusterVersion()
if err != nil {
return Report{}, err
}
id, err := i.ClusterID(ctx)
if err != nil {
return Report{}, err
}
n, err := i.Nodes(ctx)
if err != nil {
return Report{}, err
}
p, err := i.Platform(ctx)
if err != nil {
return Report{}, err
}
pods, err := i.Pods(ctx, namespace)
if err != nil {
return Report{}, err
}
podLogs, err := i.Podlogs(ctx, namespace)
if err != nil {
return Report{}, err
}
events, err := i.Events(ctx, namespace)
if err != nil {
return Report{}, err
}
configMaps, err := i.ConfigMaps(ctx, namespace)
if err != nil {
return Report{}, err
}
services, err := i.Services(ctx, namespace)
if err != nil {
return Report{}, err
}
deployments, err := i.Deployments(ctx, namespace)
if err != nil {
return Report{}, err
}
statefulSets, err := i.StatefulSets(ctx, namespace)
if err != nil {
return Report{}, err
}
replicaSets, err := i.ReplicaSets(ctx, namespace)
if err != nil {
return Report{}, err
}
leases, err := i.Leases(ctx, namespace)
if err != nil {
return Report{}, err
}
ingressClasses, err := i.IngressClasses(ctx)
if err != nil {
return Report{}, err
}
ingresses, err := i.Ingresses(ctx, namespace)
if err != nil {
return Report{}, err
}
crds, err := i.CustomResourceDefinitions(ctx)
if err != nil {
return Report{}, err
}
clusterNodes, err := i.ClusterNodes(ctx)
if err != nil {
return Report{}, err
}
return Report{
K8sVersion: version,
ClusterID: id,
Nodes: n,
Platform: p,
Pods: pods,
Podlogs: podLogs,
Events: events,
ConfigMaps: configMaps,
Services: services,
Deployments: deployments,
StatefulSets: statefulSets,
ReplicaSets: replicaSets,
Leases: leases,
IngressClasses: ingressClasses,
Ingresses: ingresses,
CRDs: crds,
ClusterNodes: clusterNodes,
}, nil
}
// ReportJSON returns collected metrics in a JSON format.
func ReportJSON(rep Report) (string, error) {
b, err := json.MarshalIndent(rep, "", " ")
if err != nil {
return "", err
}
return string(b), nil
}
// Report holds collected data points.
type Report struct {
K8sVersion string `json:"k8s_version"`
ClusterID string `json:"cluster_id"`
Nodes int `json:"nodes"`
Platform string `json:"platform"`
Pods *corev1.PodList `json:"pods"`
Podlogs []PodLog `json:"pod_logs"`
Events *corev1.EventList `json:"events"`
ConfigMaps *corev1.ConfigMapList `json:"config_maps"`
Services *corev1.ServiceList `json:"services"`
Deployments *appsv1.DeploymentList `json:"deployments"`
StatefulSets *appsv1.StatefulSetList `json:"stateful_sets"`
ReplicaSets *appsv1.ReplicaSetList `json:"replica_sets"`
Leases *coordv1.LeaseList `json:"leases"`
IngressClasses *netv1.IngressClassList `json:"ingress_classes"`
Ingresses *netv1.IngressList `json:"ingresses"`
CRDs *apiextv1.CustomResourceDefinitionList `json:"crds"`
ClusterNodes *corev1.NodeList `json:"cluster_nodes"`
}
var usage = `Usage:
inspector [-h] [-v] [-n] namespace
Collect K8s and Ingress Controller diagnostics in the given namespace.
In verbose mode (-v), prints out progess, steps and all data points to stdout.`
// Main runs the inspector program.
func Main() int {
namespace := flag.String("n", "default", "K8s namespace")
verbose := flag.Bool("v", false, "verbose output")
help := flag.Bool("h", false, "show help")
flag.Parse()
if *help {
fmt.Println(usage)
return 0
}
i, err := BuildInspectorFromKubeConfig()
if err != nil {
fmt.Fprintf(os.Stderr, "%s\n", usage)
return 1
}
i.Verbose = *verbose
report, err := i.Report(context.Background(), *namespace)
if err != nil {
fmt.Fprintf(os.Stderr, "%s\n", usage)
return 1
}
rep, err := ReportJSON(report)
if err != nil {
fmt.Fprintf(os.Stderr, "%s\n", usage)
return 1
}
fmt.Println(rep)
return 0
}