...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package nodereset
17
18 import (
19 "context"
20 "encoding/json"
21
22 "github.com/go-logr/logr"
23 "github.com/pkg/errors"
24 "sigs.k8s.io/controller-runtime/pkg/client"
25
26 "github.com/chaos-mesh/chaos-mesh/api/v1alpha1"
27 "github.com/chaos-mesh/chaos-mesh/controllers/chaosimpl/gcpchaos/utils"
28 impltypes "github.com/chaos-mesh/chaos-mesh/controllers/chaosimpl/types"
29 )
30
31 var _ impltypes.ChaosImpl = (*Impl)(nil)
32
33 type Impl struct {
34 client.Client
35
36 Log logr.Logger
37 }
38
39 func (impl *Impl) Apply(ctx context.Context, index int, records []*v1alpha1.Record, chaos v1alpha1.InnerObject) (v1alpha1.Phase, error) {
40 gcpchaos, ok := chaos.(*v1alpha1.GCPChaos)
41 if !ok {
42 err := errors.New("chaos is not gcpchaos")
43 impl.Log.Error(err, "chaos is not GCPChaos", "chaos", chaos)
44 return v1alpha1.NotInjected, err
45 }
46 computeService, err := utils.GetComputeService(ctx, impl.Client, gcpchaos)
47 if err != nil {
48 impl.Log.Error(err, "fail to get the compute service")
49 return v1alpha1.NotInjected, err
50 }
51
52 var selected v1alpha1.GCPSelector
53 err = json.Unmarshal([]byte(records[index].Id), &selected)
54 if err != nil {
55 impl.Log.Error(err, "fail to unmarshal the selector")
56 return v1alpha1.NotInjected, err
57 }
58
59 _, err = computeService.Instances.Reset(selected.Project, selected.Zone, selected.Instance).Do()
60 if err != nil {
61 impl.Log.Error(err, "fail to reset the instance")
62 return v1alpha1.NotInjected, err
63 }
64
65 return v1alpha1.Injected, nil
66 }
67
68 func (impl *Impl) Recover(ctx context.Context, index int, records []*v1alpha1.Record, chaos v1alpha1.InnerObject) (v1alpha1.Phase, error) {
69 return v1alpha1.NotInjected, nil
70 }
71
72 func NewImpl(c client.Client, log logr.Logger) *Impl {
73 return &Impl{
74 Client: c,
75 Log: log.WithName("nodereset"),
76 }
77 }
78