...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package podkill
17
18 import (
19 "context"
20
21 v1 "k8s.io/api/core/v1"
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/utils/controller"
27 )
28
29 var _ impltypes.ChaosImpl = (*Impl)(nil)
30
31 type Impl struct {
32 client.Client
33 }
34
35 func (impl *Impl) Apply(ctx context.Context, index int, records []*v1alpha1.Record, obj v1alpha1.InnerObject) (v1alpha1.Phase, error) {
36 podchaos := obj.(*v1alpha1.PodChaos)
37
38 var pod v1.Pod
39 namespacedName, err := controller.ParseNamespacedName(records[index].Id)
40 if err != nil {
41 return v1alpha1.NotInjected, err
42 }
43 err = impl.Get(ctx, namespacedName, &pod)
44 if err != nil {
45
46 return v1alpha1.NotInjected, err
47 }
48
49 err = impl.Delete(ctx, &pod, &client.DeleteOptions{
50 GracePeriodSeconds: &podchaos.Spec.GracePeriod,
51 })
52 if err != nil {
53
54 return v1alpha1.NotInjected, err
55 }
56
57 return v1alpha1.Injected, nil
58 }
59
60 func (impl *Impl) Recover(ctx context.Context, index int, records []*v1alpha1.Record, obj v1alpha1.InnerObject) (v1alpha1.Phase, error) {
61 return v1alpha1.NotInjected, nil
62 }
63
64 func NewImpl(c client.Client) *Impl {
65 return &Impl{
66 Client: c,
67 }
68 }
69