...

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