forked from solo-io/gloo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
kube_dump.go
46 lines (41 loc) · 1.48 KB
/
kube_dump.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
package helpers
import (
"bytes"
"fmt"
"io"
"os/exec"
"github.com/pkg/errors"
)
var dumpCommands = func(namespace string) []string {
return []string{
fmt.Sprintf("echo PODS FROM %s: && kubectl get pod -n %s --no-headers -o custom-columns=:metadata.name", namespace, namespace),
fmt.Sprintf("for i in $(kubectl get pod -n %s --no-headers -o custom-columns=:metadata.name); do echo STATUS FOR %s.$i: $(kubectl get pod -n %s $i -o go-template=\"{{range .status.containerStatuses}}{{.state}}{{end}}\"); done", namespace, namespace, namespace),
fmt.Sprintf("for i in $(kubectl get pod -n %s --no-headers -o custom-columns=:metadata.name); do echo LOGS FROM %s.$i: $(kubectl logs -n %s $i --all-containers); done", namespace, namespace, namespace),
}
}
// dump all data from the kube cluster
func KubeDump(namespaces ...string) (string, error) {
b := &bytes.Buffer{}
b.WriteString("** Begin Kubernetes Dump ** \n")
for _, ns := range namespaces {
for _, command := range dumpCommands(ns) {
cmd := exec.Command("bash", "-c", command)
cmd.Stdout = b
cmd.Stderr = b
if err := cmd.Run(); err != nil {
return "", errors.Errorf("command %v failed: %v", command, b.String())
}
}
}
b.WriteString("** End Kubernetes Dump ** \n")
return b.String(), nil
}
func KubeDumpOnFail(out io.Writer, namespaces ...string) func() {
return func() {
dump, err := KubeDump(namespaces...)
if err != nil {
fmt.Fprintf(out, "getting kube dump failed: %v", err)
}
fmt.Fprintf(out, dump)
}
}