...

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 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  // SetContainer sets the container field of podiochaos
    69  type SetContainer struct {
    70  	Container string
    71  }
    72  
    73  // Apply runs this action
    74  func (s *SetContainer) Apply(chaos *v1alpha1.PodIOChaos) error {
    75  	chaos.Spec.Container = &s.Container
    76  
    77  	return nil
    78  }
    79  
    80  // SetVolumePath sets the volumePath field of podiochaos
    81  type SetVolumePath struct {
    82  	Path string
    83  }
    84  
    85  // Apply runs this action
    86  func (s *SetVolumePath) Apply(chaos *v1alpha1.PodIOChaos) error {
    87  	chaos.Spec.VolumeMountPath = s.Path
    88  
    89  	return nil
    90  }
    91  
    92  // Clear will clear all related items in podnetworkchaos
    93  func (t *PodIOTransaction) Clear(source string) {
    94  	t.Steps = append(t.Steps, &Clear{
    95  		Source: source,
    96  	})
    97  }
    98  
    99  // Append adds an item to corresponding list in podnetworkchaos
   100  func (t *PodIOTransaction) Append(item interface{}) error {
   101  	switch item.(type) {
   102  	case v1alpha1.IOChaosAction:
   103  		t.Steps = append(t.Steps, &Append{
   104  			Item: item,
   105  		})
   106  		return nil
   107  	default:
   108  		return fmt.Errorf("unknown type of item")
   109  	}
   110  }
   111  
   112  // SetVolumePath sets the volumePath field of podiochaos
   113  func (t *PodIOTransaction) SetVolumePath(path string) error {
   114  	t.Steps = append(t.Steps, &SetVolumePath{
   115  		Path: path,
   116  	})
   117  
   118  	return nil
   119  }
   120  
   121  func (t *PodIOTransaction) SetContainer(container string) error {
   122  	t.Steps = append(t.Steps, &SetContainer{
   123  		Container: container,
   124  	})
   125  
   126  	return nil
   127  }
   128  
   129  // Apply runs every step on the chaos
   130  func (t *PodIOTransaction) Apply(chaos *v1alpha1.PodIOChaos) error {
   131  	for _, s := range t.Steps {
   132  		err := s.Apply(chaos)
   133  		if err != nil {
   134  			return err
   135  		}
   136  	}
   137  
   138  	return nil
   139  }
   140