-
Notifications
You must be signed in to change notification settings - Fork 432
/
kube_dump.go
103 lines (90 loc) · 2.83 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
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
package helpers
import (
"bytes"
"fmt"
"io"
"os/exec"
"github.com/solo-io/gloo/pkg/cliutil/install"
errors "github.com/rotisserie/eris"
)
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() {
PrintKubeState()
PrintDockerState()
PrintProcessState()
dump, err := KubeDump(namespaces...)
if err != nil {
fmt.Fprintf(out, "getting kube dump failed: %v", err)
}
fmt.Fprintf(out, dump)
}
}
func PrintKubeState() {
kubeCli := &install.CmdKubectl{}
kubeState, err := kubeCli.KubectlOut(nil, "get", "all", "-A")
if err != nil {
fmt.Println("*** Unable to get kube state ***")
return
}
kubeEndpointsState, err := kubeCli.KubectlOut(nil, "get", "endpoints", "-A")
if err != nil {
fmt.Println("*** Unable to get kube state ***")
return
}
fmt.Println("*** Kube state ***")
fmt.Println(string(kubeState))
fmt.Println(string(kubeEndpointsState))
fmt.Println("*** End Kube state ***")
}
func PrintDockerState() {
dockerCmd := exec.Command("docker", "ps")
dockerState := &bytes.Buffer{}
dockerCmd.Stdout = dockerState
dockerCmd.Stderr = dockerState
err := dockerCmd.Run()
if err != nil {
fmt.Println("*** Unable to get docker state ***")
return
}
fmt.Println("*** Docker state ***")
fmt.Println(dockerState.String())
fmt.Println("*** End Docker state ***")
}
func PrintProcessState() {
psCmd := exec.Command("ps", "-auxf")
psState := &bytes.Buffer{}
psCmd.Stdout = psState
psCmd.Stderr = psState
err := psCmd.Run()
if err != nil {
fmt.Println("*** Unable to get process state ***")
return
}
fmt.Println("*** Process state ***")
fmt.Println(psState.String())
fmt.Println("*** End Process state ***")
}