...

Source file src/github.com/chaos-mesh/chaos-mesh/controllers/chaosimpl/httpchaos/podhttpchaosmanager/transaction.go

Documentation: github.com/chaos-mesh/chaos-mesh/controllers/chaosimpl/httpchaos/podhttpchaosmanager

     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 podhttpchaosmanager
    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  // PodHttpTransaction represents a modification on podhttpchaos
    26  type PodHttpTransaction struct {
    27  	Steps []Step
    28  }
    29  
    30  // Step represents a step of PodHttpTransaction
    31  type Step interface {
    32  	// Apply will apply an action on podnetworkchaos
    33  	Apply(chaos *v1alpha1.PodHttpChaos) 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.PodHttpChaos) error {
    43  	rules := []v1alpha1.PodHttpChaosRule{}
    44  	for _, rule := range chaos.Spec.Rules {
    45  		if rule.Source != s.Source {
    46  			rules = append(rules, rule)
    47  		}
    48  	}
    49  	chaos.Spec.Rules = rules
    50  	return nil
    51  }
    52  
    53  // Append adds an item to corresponding list in podhttpchaos
    54  type Append struct {
    55  	Item interface{}
    56  }
    57  
    58  // Apply runs this action
    59  func (a *Append) Apply(chaos *v1alpha1.PodHttpChaos) error {
    60  	switch item := a.Item.(type) {
    61  	case v1alpha1.PodHttpChaosRule:
    62  		chaos.Spec.Rules = append(chaos.Spec.Rules, item)
    63  	case *v1alpha1.PodHttpChaosTLS:
    64  		chaos.Spec.TLS = item
    65  	default:
    66  		return errors.Wrapf(utils.ErrUnknownType, "type: %T", item)
    67  	}
    68  
    69  	return nil
    70  }
    71  
    72  // Clear will clear all related items in podhttpchaos
    73  func (t *PodHttpTransaction) Clear(source string) {
    74  	t.Steps = append(t.Steps, &Clear{
    75  		Source: source,
    76  	})
    77  }
    78  
    79  // Append adds an item to corresponding list in podnetworkchaos
    80  func (t *PodHttpTransaction) Append(item interface{}) error {
    81  	switch item.(type) {
    82  	case v1alpha1.PodHttpChaosRule:
    83  		t.Steps = append(t.Steps, &Append{
    84  			Item: item,
    85  		})
    86  	case *v1alpha1.PodHttpChaosTLS:
    87  		t.Steps = append(t.Steps, &Append{
    88  			Item: item,
    89  		})
    90  	default:
    91  		return errors.Wrapf(utils.ErrUnknownType, "type: %T", item)
    92  	}
    93  	return nil
    94  }
    95  
    96  // Apply runs every step on the chaos
    97  func (t *PodHttpTransaction) Apply(chaos *v1alpha1.PodHttpChaos) error {
    98  	for _, s := range t.Steps {
    99  		err := s.Apply(chaos)
   100  		if err != nil {
   101  			return err
   102  		}
   103  	}
   104  
   105  	return nil
   106  }
   107