...

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

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

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