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