...

Source file src/github.com/chaos-mesh/chaos-mesh/controllers/chaosimpl/azurechaos/vmstop/impl.go

Documentation: github.com/chaos-mesh/chaos-mesh/controllers/chaosimpl/azurechaos/vmstop

     1  // Copyright 2022 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  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  //
    15  
    16  package vmstop
    17  
    18  import (
    19  	"context"
    20  	"encoding/json"
    21  	"errors"
    22  
    23  	"github.com/go-logr/logr"
    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/azurechaos/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  	azurechaos, ok := chaos.(*v1alpha1.AzureChaos)
    41  	if !ok {
    42  		err := errors.New("chaos is not azurechaos")
    43  		impl.Log.Error(err, "chaos is not azureChaos", "chaos", chaos)
    44  		return v1alpha1.NotInjected, err
    45  	}
    46  
    47  	vmClient, err := utils.GetVMClient(ctx, impl.Client, azurechaos)
    48  	if err != nil {
    49  		impl.Log.Error(err, "fail to get the vm client")
    50  		return v1alpha1.NotInjected, err
    51  	}
    52  
    53  	var selected v1alpha1.AzureSelector
    54  	err = json.Unmarshal([]byte(records[index].Id), &selected)
    55  	if err != nil {
    56  		impl.Log.Error(err, "selector unmarshal error")
    57  		return v1alpha1.NotInjected, err
    58  	}
    59  
    60  	_, err = vmClient.PowerOff(ctx, selected.ResourceGroupName, selected.VMName, nil)
    61  	if err != nil {
    62  		impl.Log.Error(err, "fail to power off the vm")
    63  		return v1alpha1.NotInjected, err
    64  	}
    65  
    66  	return v1alpha1.Injected, nil
    67  }
    68  func (impl *Impl) Recover(ctx context.Context, index int, records []*v1alpha1.Record, chaos v1alpha1.InnerObject) (v1alpha1.Phase, error) {
    69  	azurechaos, ok := chaos.(*v1alpha1.AzureChaos)
    70  	if !ok {
    71  		err := errors.New("chaos is not azurechaos")
    72  		impl.Log.Error(err, "chaos is not AzureChaos", "chaos", chaos)
    73  		return v1alpha1.Injected, err
    74  	}
    75  	vmClient, err := utils.GetVMClient(ctx, impl.Client, azurechaos)
    76  	if err != nil {
    77  		impl.Log.Error(err, "fail to get the vm client")
    78  		return v1alpha1.Injected, err
    79  	}
    80  
    81  	var selected v1alpha1.AzureSelector
    82  	err = json.Unmarshal([]byte(records[index].Id), &selected)
    83  	if err != nil {
    84  		impl.Log.Error(err, "fail to unmarshal the selector")
    85  		return v1alpha1.NotInjected, err
    86  	}
    87  
    88  	_, err = vmClient.Start(ctx, selected.ResourceGroupName, selected.VMName)
    89  	if err != nil {
    90  		impl.Log.Error(err, "fail to start the vm")
    91  		return v1alpha1.Injected, err
    92  	}
    93  	return v1alpha1.NotInjected, nil
    94  }
    95  
    96  func NewImpl(c client.Client, log logr.Logger) *Impl {
    97  	return &Impl{
    98  		Client: c,
    99  		Log:    log.WithName("vmstop"),
   100  	}
   101  }
   102