...

Source file src/github.com/chaos-mesh/chaos-mesh/controllers/chaosimpl/gcpchaos/nodereset/impl.go

Documentation: github.com/chaos-mesh/chaos-mesh/controllers/chaosimpl/gcpchaos/nodereset

     1  // Copyright 2021 Chaos Mesh Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    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