...

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