...

Source file src/github.com/chaos-mesh/chaos-mesh/controllers/iochaos/podiochaosmanager/transaction.go

Documentation: github.com/chaos-mesh/chaos-mesh/controllers/iochaos/podiochaosmanager

     1  // Copyright 2020 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 podiochaosmanager
    15  
    16  import (
    17  	"fmt"
    18  
    19  	"github.com/chaos-mesh/chaos-mesh/api/v1alpha1"
    20  )
    21  
    22  // PodIoTransaction represents a modification on podnetwork
    23  type PodIoTransaction struct {
    24  	Steps []Step
    25  }
    26  
    27  // Step represents a step of PodIoTransaction
    28  type Step interface {
    29  	// Apply will apply an action on podnetworkchaos
    30  	Apply(chaos *v1alpha1.PodIoChaos) error
    31  }
    32  
    33  // Clear removes all resources with the same source
    34  type Clear struct {
    35  	Source string
    36  }
    37  
    38  // Apply runs this action
    39  func (s *Clear) Apply(chaos *v1alpha1.PodIoChaos) error {
    40  	actions := []v1alpha1.IoChaosAction{}
    41  	for _, action := range chaos.Spec.Actions {
    42  		if action.Source != s.Source {
    43  			actions = append(actions, action)
    44  		}
    45  	}
    46  	chaos.Spec.Actions = actions
    47  
    48  	return nil
    49  }
    50  
    51  // Append adds an item to corresponding list in podnetworkchaos
    52  type Append struct {
    53  	Item interface{}
    54  }
    55  
    56  // Apply runs this action
    57  func (a *Append) Apply(chaos *v1alpha1.PodIoChaos) error {
    58  	switch item := a.Item.(type) {
    59  	case v1alpha1.IoChaosAction:
    60  		chaos.Spec.Actions = append(chaos.Spec.Actions, item)
    61  	default:
    62  		return fmt.Errorf("unknown type of item")
    63  	}
    64  
    65  	return nil
    66  }
    67  
    68  // SetVolumePath sets the volumePath field of podiochaos
    69  type SetVolumePath struct {
    70  	Path string
    71  }
    72  
    73  // Apply runs this action
    74  func (s *SetVolumePath) Apply(chaos *v1alpha1.PodIoChaos) error {
    75  	chaos.Spec.VolumeMountPath = s.Path
    76  
    77  	return nil
    78  }
    79  
    80  // Clear will clear all related items in podnetworkchaos
    81  func (t *PodIoTransaction) Clear(source string) {
    82  	t.Steps = append(t.Steps, &Clear{
    83  		Source: source,
    84  	})
    85  }
    86  
    87  // Append adds an item to corresponding list in podnetworkchaos
    88  func (t *PodIoTransaction) Append(item interface{}) error {
    89  	switch item.(type) {
    90  	case v1alpha1.IoChaosAction:
    91  		t.Steps = append(t.Steps, &Append{
    92  			Item: item,
    93  		})
    94  		return nil
    95  	default:
    96  		return fmt.Errorf("unknown type of item")
    97  	}
    98  }
    99  
   100  // SetVolumePath sets the volumePath field of podiochaos
   101  func (t *PodIoTransaction) SetVolumePath(path string) error {
   102  	t.Steps = append(t.Steps, &SetVolumePath{
   103  		Path: path,
   104  	})
   105  
   106  	return nil
   107  }
   108  
   109  // Apply runs every step on the chaos
   110  func (t *PodIoTransaction) Apply(chaos *v1alpha1.PodIoChaos) error {
   111  	for _, s := range t.Steps {
   112  		err := s.Apply(chaos)
   113  		if err != nil {
   114  			return err
   115  		}
   116  	}
   117  
   118  	return nil
   119  }
   120