Documentation ¶
Overview ¶
Package action introduces a multiplexer for actions.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type ErrorUnknownAction ¶
func NewErrorUnknownAction ¶
func NewErrorUnknownAction(GVK string, action string) ErrorUnknownAction
func (ErrorUnknownAction) Error ¶
func (it ErrorUnknownAction) Error() string
type Multiplexer ¶
type Multiplexer struct {
// contains filtered or unexported fields
}
Multiplexer could combine ChaosImpl implementations into one, and route them by Action in the ChaosSpec. Field impl should be a struct which contains several fields with struct tag "action", each field should be an implementation of ChaosImpl. For example:
type tempStruct struct { Impl1 impltypes.ChaosImpl `action:"action1"` Impl2 impltypes.ChaosImpl `action:"action2"` }
is valid to be the field in Multiplexer.
Because we use reflect fo iterate fields in tempStruct, so fields in tempStruct should be public/exported.
When some Chaos like:
type SomeChaos struct { *** Spec SomeChaosSpec `json:"spec"` *** } type SomeChaosSpec struct { *** // available actions: action1, action2 Action string `json:"action"` *** }
is created, the corresponding ChaosImpl(s) for each action will be invoked by struct tag.
Example ¶
package main import ( "context" "fmt" "github.com/chaos-mesh/chaos-mesh/api/v1alpha1" ) type chaosImplForAction1 struct { } func (it *chaosImplForAction1) Apply(ctx context.Context, index int, records []*v1alpha1.Record, obj v1alpha1.InnerObject) (v1alpha1.Phase, error) { fmt.Println("action1-apply") return v1alpha1.Injected, nil } func (it *chaosImplForAction1) Recover(ctx context.Context, index int, records []*v1alpha1.Record, obj v1alpha1.InnerObject) (v1alpha1.Phase, error) { fmt.Println("action1-recover") return v1alpha1.NotInjected, nil } type chaosImplForAction2 struct { } func (it *chaosImplForAction2) Apply(ctx context.Context, index int, records []*v1alpha1.Record, obj v1alpha1.InnerObject) (v1alpha1.Phase, error) { fmt.Println("action2-apply") return v1alpha1.Injected, nil } func (it *chaosImplForAction2) Recover(ctx context.Context, index int, records []*v1alpha1.Record, obj v1alpha1.InnerObject) (v1alpha1.Phase, error) { fmt.Println("action2-recover") return v1alpha1.NotInjected, nil } func main() { type adHoc struct { AnyName1 *chaosImplForAction1 `action:"struct-tag"` WhateverTheName *chaosImplForAction2 `action:"is-important"` } multiplexer := NewMultiplexer(&adHoc{ AnyName1: &chaosImplForAction1{}, WhateverTheName: &chaosImplForAction2{}, }) // Just use PodChaos as example, you could use any struct that contains Spec.Action for it. chaosA := v1alpha1.PodChaos{ Spec: v1alpha1.PodChaosSpec{ Action: "struct-tag", }, } chaosB := v1alpha1.PodChaos{ Spec: v1alpha1.PodChaosSpec{ Action: "is-important", }, } if _, err := multiplexer.Apply(context.Background(), 0, nil, &chaosA); err != nil { panic(err) } if _, err := multiplexer.Recover(context.Background(), 0, nil, &chaosA); err != nil { panic(err) } if _, err := multiplexer.Apply(context.Background(), 0, nil, &chaosB); err != nil { panic(err) } if _, err := multiplexer.Recover(context.Background(), 0, nil, &chaosB); err != nil { panic(err) } }
Output: action1-apply action1-recover action2-apply action2-recover
func NewMultiplexer ¶
func NewMultiplexer(impl interface{}) Multiplexer
NewMultiplexer is a constructor of Multiplexer. For the detail of the parameter "impl", see the comment of type Multiplexer.