-
Notifications
You must be signed in to change notification settings - Fork 444
/
kube_dump.go
349 lines (300 loc) · 10.6 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
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
package helpers
import (
"bytes"
"context"
"fmt"
"io"
"os"
"os/exec"
"path/filepath"
"strings"
"time"
"github.com/hashicorp/go-multierror"
"github.com/solo-io/go-utils/threadsafe"
"github.com/solo-io/gloo/pkg/utils/kubeutils/kubectl"
"github.com/onsi/ginkgo/v2"
"github.com/solo-io/gloo/pkg/cliutil/install"
gateway_defaults "github.com/solo-io/gloo/projects/gateway/pkg/defaults"
"github.com/solo-io/gloo/projects/gloo/cli/pkg/cmd/gateway"
"github.com/solo-io/gloo/projects/gloo/pkg/defaults"
"github.com/solo-io/skv2/codegen/util"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
var (
kubeOutDir = filepath.Join(util.GetModuleRoot(), "_output", "kube2e-artifacts")
envoyOutDir = filepath.Join(kubeOutDir, "envoy-dump")
)
// StandardGlooDumpOnFail creates adump of the kubernetes state and certain envoy data from the admin interface when a test fails
// Look at `KubeDumpOnFail` && `EnvoyDumpOnFail` for more details
func StandardGlooDumpOnFail(out io.Writer, proxies ...metav1.ObjectMeta) func() {
return func() {
var namespaces []string
for _, proxy := range proxies {
if proxy.GetNamespace() != "" {
namespaces = append(namespaces, proxy.Namespace)
}
}
KubeDumpOnFail(out, namespaces...)()
EnvoyDumpOnFail(out, proxies...)()
}
}
// KubeDumpOnFail creates a small dump of the kubernetes state when a test fails.
// This is useful for debugging test failures.
// The dump is written to _output/kube2e-artifacts.
// The dump includes:
// - docker state
// - process state
// - kubernetes state
// - logs from all pods in the given namespaces
// - yaml representations of all solo.io CRs in the given namespaces
func KubeDumpOnFail(out io.Writer, namespaces ...string) func() {
return func() {
setupOutDir(kubeOutDir)
recordDockerState(fileAtPath(filepath.Join(kubeOutDir, "docker-state.log")))
recordProcessState(fileAtPath(filepath.Join(kubeOutDir, "process-state.log")))
recordKubeState(fileAtPath(filepath.Join(kubeOutDir, "kube-state.log")))
recordKubeDump(namespaces...)
}
}
func recordDockerState(f *os.File) {
defer f.Close()
dockerCmd := exec.Command("docker", "ps")
dockerState := &bytes.Buffer{}
dockerCmd.Stdout = dockerState
dockerCmd.Stderr = dockerState
err := dockerCmd.Run()
if err != nil {
f.WriteString("*** Unable to get docker state ***. Reason: " + err.Error() + " \n")
return
}
f.WriteString("*** Docker state ***\n")
f.WriteString(dockerState.String() + "\n")
f.WriteString("*** End Docker state ***\n")
}
func recordProcessState(f *os.File) {
defer f.Close()
psCmd := exec.Command("ps", "-auxf")
psState := &bytes.Buffer{}
psCmd.Stdout = psState
psCmd.Stderr = psState
err := psCmd.Run()
if err != nil {
f.WriteString("unable to get process state. Reason: " + err.Error() + " \n")
return
}
f.WriteString("*** Process state ***\n")
f.WriteString(psState.String() + "\n")
f.WriteString("*** End Process state ***\n")
}
func recordKubeState(f *os.File) {
defer f.Close()
kubeCli := &install.CmdKubectl{}
kubeState, err := kubeCli.KubectlOut(nil, "get", "all", "-A")
if err != nil {
f.WriteString("*** Unable to get kube state ***\n")
return
}
// Describe everything to identify the reason for issues such as Pods, LoadBalancers stuck in pending state
// (insufficient resources, unable to acquire an IP), etc.
// Ie: More context around the output of the previous command `kubectl get all -A`
kubeDescribe, err := kubeCli.KubectlOut(nil, "describe", "all", "-A")
if err != nil {
f.WriteString("*** Unable to get kube describe ***. Reason: " + err.Error() + " \n")
return
}
kubeEndpointsState, err := kubeCli.KubectlOut(nil, "get", "endpoints", "-A")
if err != nil {
f.WriteString("*** Unable to get endpoint state ***. Reason: " + err.Error() + " \n")
return
}
f.WriteString("*** Kube state ***\n")
f.WriteString(string(kubeState) + "\n")
f.WriteString(string(kubeDescribe) + "\n")
f.WriteString(string(kubeEndpointsState) + "\n")
f.WriteString("*** End Kube state ***\n")
}
func recordKubeDump(namespaces ...string) {
// for each namespace, create a namespace directory that contains...
for _, ns := range namespaces {
// ...a pod logs subdirectoy
if err := recordPods(filepath.Join(kubeOutDir, ns, "_pods"), ns); err != nil {
fmt.Printf("error recording pod logs: %f, \n", err)
}
// ...and a subdirectory for each solo.io CRD with non-zero resources
if err := recordCRs(filepath.Join(kubeOutDir, ns), ns); err != nil {
fmt.Printf("error recording pod logs: %f, \n", err)
}
}
}
// recordPods records logs from each pod to _output/kube2e-artifacts/$namespace/pods/$pod.log
func recordPods(podDir, namespace string) error {
pods, _, err := kubeList(namespace, "pod")
if err != nil {
return err
}
outErr := &multierror.Error{}
for _, pod := range pods {
if err := os.MkdirAll(podDir, os.ModePerm); err != nil {
return err
}
logs, errOutput, err := kubeLogs(namespace, pod)
// store any error running the log command to return later
// the error represents the cause of the failure, and should be bubbled up
// we will still try to get logs for other pods even if this one returns an error
if err != nil {
outErr = multierror.Append(outErr, err)
}
// write any log output to the standard file
if logs != "" {
f := fileAtPath(filepath.Join(podDir, pod+".log"))
f.WriteString(logs)
f.Close()
}
// write any error output to the error file
// this will consist of the combined stdout and stderr of the command
if errOutput != "" {
f := fileAtPath(filepath.Join(podDir, pod+"-error.log"))
f.WriteString(errOutput)
f.Close()
}
}
return outErr.ErrorOrNil()
}
// recordCRs records all unique CRs floating about to _output/kube2e-artifacts/$namespace/$crd/$cr.yaml
func recordCRs(namespaceDir string, namespace string) error {
crds, _, err := kubeList(namespace, "crd")
if err != nil {
return err
}
// record all unique CRs floating about
for _, crd := range crds {
// consider all installed CRDs that are solo-managed
if !strings.Contains(crd, "solo.io") {
continue
}
// if there are any existing CRs corresponding to this CRD
crs, _, err := kubeList(namespace, crd)
if err != nil {
return err
}
if len(crs) == 0 {
continue
}
crdDir := filepath.Join(namespaceDir, crd)
if err := os.MkdirAll(crdDir, os.ModePerm); err != nil {
return err
}
// we record each one in its own .yaml representation
for _, cr := range crs {
f := fileAtPath(filepath.Join(crdDir, cr+".yaml"))
errF := fileAtPath(filepath.Join(crdDir, cr+"-error.log"))
crDetails, errOutput, err := kubeGet(namespace, crd, cr)
if crDetails != "" {
f.WriteString(crDetails)
f.Close()
}
if errOutput != "" {
errF.WriteString(errOutput)
errF.Close()
}
return err
}
}
return nil
}
// kubeLogs runs $(kubectl -n $namespace logs $pod --all-containers) and returns the string result
func kubeLogs(namespace string, pod string) (string, string, error) {
args := []string{"-n", namespace, "logs", pod, "--all-containers"}
return kubeExecute(args)
}
// kubeGet runs $(kubectl -n $namespace get $kubeType $name -oyaml) and returns the string result
func kubeGet(namespace string, kubeType string, name string) (string, string, error) {
args := []string{"-n", namespace, "get", kubeType, name, "-oyaml"}
return kubeExecute(args)
}
func kubeExecute(args []string) (string, string, error) {
cli := kubectl.NewCli().WithReceiver(ginkgo.GinkgoWriter)
var outLocation threadsafe.Buffer
runError := cli.Command(context.Background(), args...).WithStdout(&outLocation).Run()
if runError != nil {
return outLocation.String(), runError.OutputString(), runError.Cause()
}
return outLocation.String(), "", nil
}
// kubeList runs $(kubectl -n $namespace $target) and returns a slice of kubernetes object names
func kubeList(namespace string, target string) ([]string, string, error) {
args := []string{"-n", namespace, "get", target}
lines, errContent, err := kubeExecute(args)
if err != nil {
return nil, errContent, err
}
var toReturn []string
for _, line := range strings.Split(strings.TrimSuffix(lines, "\n"), "\n") {
if strings.HasPrefix(line, "NAME") || strings.HasPrefix(line, "No resources found") {
continue // skip header line and cases where there are no resources
}
if split := strings.Split(line, " "); len(split) > 1 {
toReturn = append(toReturn, split[0])
}
}
return toReturn, "", nil
}
// EnvoyDumpOnFail creates a small dump of the envoy admin interface when a test fails.
// This is useful for debugging test failures.
// The dump is written to _output/envoy-dump.
// The dump includes:
// - config dump
// - stats
// - clusters
// - listeners
func EnvoyDumpOnFail(_ io.Writer, proxies ...metav1.ObjectMeta) func() {
return func() {
setupOutDir(envoyOutDir)
for _, proxy := range proxies {
proxyName := proxy.GetName()
if proxyName == "" {
proxyName = gateway_defaults.GatewayProxyName
}
proxyNamespace := proxy.GetNamespace()
if proxyNamespace == "" {
proxyNamespace = defaults.GlooSystem
}
recordEnvoyAdminData(fileAtPath(filepath.Join(envoyOutDir, "config.log")), "/config_dump", proxyName, proxyNamespace)
recordEnvoyAdminData(fileAtPath(filepath.Join(envoyOutDir, "stats.log")), "/stats", proxyName, proxyNamespace)
recordEnvoyAdminData(fileAtPath(filepath.Join(envoyOutDir, "clusters.log")), "/clusters", proxyName, proxyNamespace)
recordEnvoyAdminData(fileAtPath(filepath.Join(envoyOutDir, "listeners.log")), "/listeners", proxyName, proxyNamespace)
}
}
}
func recordEnvoyAdminData(f *os.File, path, proxyName, namespace string) {
defer f.Close()
fmt.Printf("Getting and storing envoy output for %s path on %s.%s proxy\n", path, proxyName, namespace)
cfg, err := gateway.GetEnvoyAdminData(context.Background(), proxyName, namespace, path, 30*time.Second)
if err != nil {
f.WriteString("*** Unable to get envoy " + path + " dump ***. Reason: " + err.Error() + " \n")
return
}
f.WriteString("*** Envoy " + path + " dump ***\n")
f.WriteString(cfg + "\n")
f.WriteString("*** End Envoy " + path + " dump ***\n")
}
// setupOutDir forcibly deletes/creates the output directory
func setupOutDir(outdir string) {
err := os.RemoveAll(outdir)
if err != nil {
fmt.Printf("error removing log directory: %f\n", err)
}
err = os.MkdirAll(outdir, os.ModePerm)
if err != nil {
fmt.Printf("error creating log directory: %f\n", err)
}
fmt.Println("kube dump artifacts will be stored at: " + outdir)
}
// fileAtPath creates a file at the given path, and returns the file object
func fileAtPath(path string) *os.File {
f, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0600)
if err != nil {
fmt.Printf("unable to openfile: %f\n", err)
}
return f
}