...
1
2
3
4
5
6
7
8
9
10
11
12
13
14 package tc
15
16 import (
17 "context"
18 "fmt"
19
20 v1 "k8s.io/api/core/v1"
21 "sigs.k8s.io/controller-runtime/pkg/client"
22
23 "github.com/chaos-mesh/chaos-mesh/controllers/utils"
24 "github.com/chaos-mesh/chaos-mesh/pkg/chaosdaemon/pb"
25 )
26
27
28 func SetTcs(ctx context.Context, c client.Client, pod *v1.Pod, tcs []*pb.Tc) error {
29 pbClient, err := utils.NewChaosDaemonClient(ctx, c, pod)
30 if err != nil {
31 return err
32 }
33 defer pbClient.Close()
34
35 if len(pod.Status.ContainerStatuses) == 0 {
36 return fmt.Errorf("%s %s can't get the state of container", pod.Namespace, pod.Name)
37 }
38
39 containerID := pod.Status.ContainerStatuses[0].ContainerID
40
41 _, err = pbClient.SetTcs(ctx, &pb.TcsRequest{
42 Tcs: tcs,
43 ContainerId: containerID,
44
45 Device: "eth0",
46 EnterNS: true,
47 })
48 return err
49 }
50