...

Source file src/github.com/chaos-mesh/chaos-mesh/controllers/chaosimpl/networkchaos/podnetworkchaosmanager/transaction.go

Documentation: github.com/chaos-mesh/chaos-mesh/controllers/chaosimpl/networkchaos/podnetworkchaosmanager

     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 podnetworkchaosmanager
    15  
    16  import (
    17  	"fmt"
    18  
    19  	"github.com/chaos-mesh/chaos-mesh/api/v1alpha1"
    20  )
    21  
    22  // PodNetworkTransaction represents a modification on podnetwork
    23  type PodNetworkTransaction struct {
    24  	Steps []Step
    25  }
    26  
    27  // Step represents a step of PodNetworkTransaction
    28  type Step interface {
    29  	// Apply will apply an action on podnetworkchaos
    30  	Apply(chaos *v1alpha1.PodNetworkChaos) 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.PodNetworkChaos) error {
    40  	ipsets := []v1alpha1.RawIPSet{}
    41  	for _, ipset := range chaos.Spec.IPSets {
    42  		if ipset.Source != s.Source {
    43  			ipsets = append(ipsets, ipset)
    44  		}
    45  	}
    46  	chaos.Spec.IPSets = ipsets
    47  
    48  	chains := []v1alpha1.RawIptables{}
    49  	for _, chain := range chaos.Spec.Iptables {
    50  		if chain.Source != s.Source {
    51  			chains = append(chains, chain)
    52  		}
    53  	}
    54  	chaos.Spec.Iptables = chains
    55  
    56  	qdiscs := []v1alpha1.RawTrafficControl{}
    57  	for _, qdisc := range chaos.Spec.TrafficControls {
    58  		if qdisc.Source != s.Source {
    59  			qdiscs = append(qdiscs, qdisc)
    60  		}
    61  	}
    62  	chaos.Spec.TrafficControls = qdiscs
    63  
    64  	return nil
    65  }
    66  
    67  // Append adds an item to corresponding list in podnetworkchaos
    68  type Append struct {
    69  	Item interface{}
    70  }
    71  
    72  // Apply runs this action
    73  func (a *Append) Apply(chaos *v1alpha1.PodNetworkChaos) error {
    74  	switch item := a.Item.(type) {
    75  	case v1alpha1.RawIPSet:
    76  		chaos.Spec.IPSets = append(chaos.Spec.IPSets, item)
    77  	case v1alpha1.RawIptables:
    78  		chaos.Spec.Iptables = append(chaos.Spec.Iptables, item)
    79  	case v1alpha1.RawTrafficControl:
    80  		chaos.Spec.TrafficControls = append(chaos.Spec.TrafficControls, item)
    81  	default:
    82  		return fmt.Errorf("unknown type of item")
    83  	}
    84  
    85  	return nil
    86  }
    87  
    88  // Clear will clear all related items in podnetworkchaos
    89  func (t *PodNetworkTransaction) Clear(source string) {
    90  	t.Steps = append(t.Steps, &Clear{
    91  		Source: source,
    92  	})
    93  }
    94  
    95  // Append adds an item to corresponding list in podnetworkchaos
    96  func (t *PodNetworkTransaction) Append(item interface{}) error {
    97  	switch item.(type) {
    98  	case v1alpha1.RawIPSet, v1alpha1.RawIptables, v1alpha1.RawTrafficControl:
    99  		t.Steps = append(t.Steps, &Append{
   100  			Item: item,
   101  		})
   102  		return nil
   103  	default:
   104  		return fmt.Errorf("unknown type of item")
   105  	}
   106  }
   107  
   108  // Apply runs every step on the chaos
   109  func (t *PodNetworkTransaction) Apply(chaos *v1alpha1.PodNetworkChaos) error {
   110  	for _, s := range t.Steps {
   111  		err := s.Apply(chaos)
   112  		if err != nil {
   113  			return err
   114  		}
   115  	}
   116  
   117  	return nil
   118  }
   119