...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package containerkill
17
18 import (
19 "context"
20
21 "github.com/go-logr/logr"
22 "sigs.k8s.io/controller-runtime/pkg/client"
23
24 "github.com/chaos-mesh/chaos-mesh/api/v1alpha1"
25 impltypes "github.com/chaos-mesh/chaos-mesh/controllers/chaosimpl/types"
26 "github.com/chaos-mesh/chaos-mesh/controllers/chaosimpl/utils"
27 "github.com/chaos-mesh/chaos-mesh/pkg/chaosdaemon/pb"
28 )
29
30 var _ impltypes.ChaosImpl = (*Impl)(nil)
31
32 type Impl struct {
33 client.Client
34
35 Log logr.Logger
36
37 decoder *utils.ContainerRecordDecoder
38 }
39
40 func (impl *Impl) Apply(ctx context.Context, index int, records []*v1alpha1.Record, obj v1alpha1.InnerObject) (v1alpha1.Phase, error) {
41 decodedContainer, err := impl.decoder.DecodeContainerRecord(ctx, records[index], obj)
42 pbClient := decodedContainer.PbClient
43 containerId := decodedContainer.ContainerId
44 if pbClient != nil {
45 defer pbClient.Close()
46 }
47 if err != nil {
48 return v1alpha1.NotInjected, err
49 }
50
51 if _, err = pbClient.ContainerKill(ctx, &pb.ContainerRequest{
52 Action: &pb.ContainerAction{
53 Action: pb.ContainerAction_KILL,
54 },
55 ContainerId: containerId,
56 }); err != nil {
57 impl.Log.Error(err, "kill container error", "containerID", containerId)
58 return v1alpha1.NotInjected, err
59 }
60
61 return v1alpha1.Injected, nil
62 }
63
64 func (impl *Impl) Recover(ctx context.Context, index int, records []*v1alpha1.Record, obj v1alpha1.InnerObject) (v1alpha1.Phase, error) {
65 return v1alpha1.NotInjected, nil
66 }
67
68 func NewImpl(c client.Client, log logr.Logger, decoder *utils.ContainerRecordDecoder) *Impl {
69 return &Impl{
70 Client: c,
71 Log: log.WithName("containerkill"),
72 decoder: decoder,
73 }
74 }
75