...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package debug
17
18 import (
19 "context"
20 "strings"
21
22 "github.com/hasura/go-graphql-client"
23
24 "github.com/chaos-mesh/chaos-mesh/api/v1alpha1"
25 "github.com/chaos-mesh/chaos-mesh/pkg/chaosctl/common"
26 ctrlclient "github.com/chaos-mesh/chaos-mesh/pkg/ctrl/client"
27 )
28
29 type networkDebugger struct {
30 client *ctrlclient.CtrlClient
31 }
32
33 func NetworkDebug(client *ctrlclient.CtrlClient) Debugger {
34 return &networkDebugger{
35 client: client,
36 }
37 }
38
39 func (d *networkDebugger) Collect(ctx context.Context, namespace, chaosName string) ([]*common.ChaosResult, error) {
40 var results []*common.ChaosResult
41
42 var name *graphql.String
43 if chaosName != "" {
44 n := graphql.String(chaosName)
45 name = &n
46 }
47
48 var query struct {
49 Namespace []struct {
50 NetworkChaos []struct {
51 Name string
52 Podnetwork []struct {
53 Spec *v1alpha1.PodNetworkChaosSpec
54 Namespace string
55 Name string
56 Pod struct {
57 Ipset string
58 TcQdisc []string
59 Iptables []string
60 }
61 }
62 } `graphql:"networkchaos(name: $name)"`
63 } `graphql:"namespace(ns: $namespace)"`
64 }
65
66 variables := map[string]interface{}{
67 "namespace": graphql.String(namespace),
68 "name": name,
69 }
70
71 err := d.client.QueryClient.Query(ctx, &query, variables)
72 if err != nil {
73 return nil, err
74 }
75
76 if len(query.Namespace) == 0 {
77 return results, nil
78 }
79
80 for _, networkChaos := range query.Namespace[0].NetworkChaos {
81 result := &common.ChaosResult{
82 Name: networkChaos.Name,
83 }
84
85 for _, podNetworkChaos := range networkChaos.Podnetwork {
86 podResult := common.PodResult{
87 Name: podNetworkChaos.Name,
88 }
89
90 podResult.Items = append(podResult.Items, common.ItemResult{Name: "ipset list", Value: podNetworkChaos.Pod.Ipset})
91 podResult.Items = append(podResult.Items, common.ItemResult{Name: "tc qdisc list", Value: strings.Join(podNetworkChaos.Pod.TcQdisc, "\n")})
92 podResult.Items = append(podResult.Items, common.ItemResult{Name: "iptables list", Value: strings.Join(podNetworkChaos.Pod.Iptables, "\n")})
93 output, err := common.MarshalChaos(podNetworkChaos.Spec)
94 if err != nil {
95 return nil, err
96 }
97 podResult.Items = append(podResult.Items, common.ItemResult{Name: "podnetworkchaos", Value: output})
98 result.Pods = append(result.Pods, podResult)
99 }
100
101 results = append(results, result)
102 }
103 return results, nil
104 }
105
106 func (d *networkDebugger) List(ctx context.Context, namespace string) ([]string, error) {
107 var query struct {
108 Namespace []struct {
109 NetworkChaos []struct {
110 Name string
111 } `graphql:"networkchaos"`
112 } `graphql:"namespace(ns: $namespace)"`
113 }
114
115 variables := map[string]interface{}{
116 "namespace": graphql.String(namespace),
117 }
118
119 err := d.client.QueryClient.Query(ctx, &query, variables)
120 if err != nil {
121 return nil, err
122 }
123
124 if len(query.Namespace) == 0 {
125 return nil, nil
126 }
127
128 var names []string
129 for _, networkChaos := range query.Namespace[0].NetworkChaos {
130 names = append(names, string(networkChaos.Name))
131 }
132 return names, nil
133 }
134