1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package server
17
18 import (
19 "context"
20 "strings"
21
22 "github.com/pingcap/errors"
23 v1 "k8s.io/api/core/v1"
24
25 "github.com/chaos-mesh/chaos-mesh/pkg/bpm"
26 )
27
28
29 func (r *Resolver) GetIpset(ctx context.Context, obj *v1.Pod) (string, error) {
30 cmd := "ipset list"
31 return r.ExecBypass(ctx, obj, cmd, bpm.PidNS, bpm.NetNS)
32 }
33
34
35 func (r *Resolver) GetTcQdisc(ctx context.Context, obj *v1.Pod) ([]string, error) {
36 cmd := "tc qdisc list"
37 rules, err := r.ExecBypass(ctx, obj, cmd, bpm.PidNS, bpm.NetNS)
38 if err != nil {
39 return nil, errors.Wrapf(err, "exec `%s`", cmd)
40 }
41 return strings.Split(rules, "\n"), nil
42 }
43
44
45 func (r *Resolver) GetIptables(ctx context.Context, obj *v1.Pod) ([]string, error) {
46 cmd := "iptables --list"
47 rules, err := r.ExecBypass(ctx, obj, cmd, bpm.PidNS, bpm.NetNS)
48 if err != nil {
49 return nil, errors.Wrapf(err, "exec `%s`", cmd)
50 }
51 return strings.Split(rules, "\n"), nil
52 }
53
54
55 func (r *Resolver) cleanTcs(ctx context.Context, obj *v1.Pod, devices []string) ([]string, error) {
56 var cleaned []string
57 for _, device := range devices {
58 cmd := "tc qdisc del dev " + device + " root"
59 _, err := r.ExecBypass(ctx, obj, cmd, bpm.PidNS, bpm.NetNS)
60 if err != nil {
61 return cleaned, errors.Wrapf(err, "exec `%s`", cmd)
62 }
63 cleaned = append(cleaned, device)
64 }
65 return cleaned, nil
66 }
67
68
69 func (r *Resolver) cleanIptables(ctx context.Context, obj *v1.Pod, chains []string) ([]string, error) {
70 var cleaned []string
71 for _, chain := range chains {
72 cmd := "iptables -F " + chain
73 _, err := r.ExecBypass(ctx, obj, cmd, bpm.PidNS, bpm.NetNS)
74 if err != nil {
75 return cleaned, errors.Wrapf(err, "exec `%s`", cmd)
76 }
77 cleaned = append(cleaned, chain)
78 }
79 return cleaned, nil
80 }
81