...

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