1
2
3
4
5
6
7
8
9
10
11
12
13
14 package iochaos
15
16 import (
17 "context"
18 "fmt"
19
20 "github.com/pkg/errors"
21 v1 "k8s.io/api/core/v1"
22 "k8s.io/apimachinery/pkg/runtime"
23
24 "github.com/chaos-mesh/chaos-mesh/api/v1alpha1"
25 cm "github.com/chaos-mesh/chaos-mesh/pkg/chaosctl/common"
26 )
27
28
29 func Debug(ctx context.Context, chaos runtime.Object, c *cm.ClientSet, result *cm.ChaosResult) error {
30 ioChaos, ok := chaos.(*v1alpha1.IOChaos)
31 if !ok {
32 return fmt.Errorf("chaos is not iochaos")
33 }
34 chaosStatus := ioChaos.Status.ChaosStatus
35 chaosSelector := ioChaos.Spec.Selector
36
37 pods, daemons, err := cm.GetPods(ctx, ioChaos.GetName(), chaosStatus, chaosSelector, c.CtrlCli)
38 if err != nil {
39 return err
40 }
41
42 for i := range pods {
43 podName := pods[i].Name
44 podResult := cm.PodResult{Name: podName}
45 _ = debugEachPod(ctx, pods[i], daemons[i], ioChaos, c, &podResult)
46 result.Pods = append(result.Pods, podResult)
47
48 }
49 return nil
50 }
51
52 func debugEachPod(ctx context.Context, pod v1.Pod, daemon v1.Pod, chaos *v1alpha1.IOChaos, c *cm.ClientSet, result *cm.PodResult) error {
53
54 cmd := "cat /proc/mounts"
55 out, err := cm.ExecBypass(ctx, pod, daemon, cmd, c.KubeCli)
56 if err != nil {
57 return errors.Wrapf(err, "run command '%s' failed", cmd)
58 }
59 result.Items = append(result.Items, cm.ItemResult{Name: "mount information", Value: string(out)})
60
61 pids, commands, err := cm.GetPidFromPS(ctx, pod, daemon, c.KubeCli)
62 if err != nil {
63 return errors.Wrapf(err, "get pid for pod %s/%s from ps failed", pod.GetNamespace(), pod.GetName())
64 }
65
66 for i := range pids {
67 cmd = fmt.Sprintf("ls -l /proc/%s/fd", pids[i])
68 out, err = cm.ExecBypass(ctx, pod, daemon, cmd, c.KubeCli)
69
70 var itemValue string
71 if err != nil {
72 itemValue = err.Error()
73 } else {
74 itemValue = string(out)
75 }
76 result.Items = append(result.Items, cm.ItemResult{Name: fmt.Sprintf("file descriptors of PID: %s, COMMAND: %s", pids[i], commands[i]), Value: itemValue})
77 }
78
79 return nil
80 }
81