forked from litmuschaos/litmus-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
litmus-chaos-injector.go
73 lines (53 loc) · 2.72 KB
/
litmus-chaos-injector.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
// Package pod-io-error-retval/lib is a library for orchestrating
// io-error chaos experiments based on the debugfs fail function feature.
package lib
import (
"os"
"os/signal"
"syscall"
"github.com/litmuschaos/litmus-go/pkg/log"
experimentTypes "github.com/litmuschaos/litmus-go/pkg/generic/pod-memory-hog/types"
corev1 "k8s.io/api/core/v1"
)
// ChaosInjectorFn represents first order function for injecting chaos with the given executor.
// The chaos ia parameterized and is initialized with the provided chaos parameters. Any errors
// resulting from the injection of the chaos is emitted to the provided erro channel.
type ChaosInjectorFunction func(executor Executor, chaosParams interface{}, errChannel chan error)
// ResetChaosFunction represents first order function for resetting chaos with the given executor.
type ResetChaosFunction func(Executor Executor, chaosParams interface{}) error
// LitmusChaosParamFunction Parses chaos parameters from experiment details.
type LitmusChaosParamFunction func(exp *experimentTypes.ExperimentDetails) interface{}
// LitmusChaosInjector
type LitmusChaosInjector struct {
ChaosParamsFn LitmusChaosParamFunction
ChaosInjectorFn ChaosInjectorFunction
ResetChaosFn ResetChaosFunction
}
// InjectChaosInSerialMode injects chaos with the given experiment details in serial mode.
func (injector LitmusChaosInjector) InjectChaosInSerialMode(exp ExperimentOrchestrationDetails) error {
orchestrator := litmusChaosOrchestratorInstance(injector, exp)
orchestrator.runProbes()
for _, pod := range exp.TargetPodList.Items {
orchestrator.injectChaosOnPod(pod)
log.Infof("[Chaos]:Waiting for: %vs", exp.ExperimentDetails.ChaosDuration)
// Trap os.Interrupt and syscall.SIGTERM an emit a value on the signal channel. Note
// that syscall.SIGKILL cannot be trapped.
signal.Notify(orchestrator.signalChannel, os.Interrupt, syscall.SIGTERM) // , syscall.SIGKILL)
orchestrator.observeAndReact([]corev1.Pod{pod})
}
return orchestrator.err
}
// InjectChaosInParallelMode injects chaos with the given experiment details in parallel mode.
func (injector LitmusChaosInjector) InjectChaosInParallelMode(exp ExperimentOrchestrationDetails) error {
orchestrator := litmusChaosOrchestratorInstance(injector, exp)
orchestrator.runProbes()
for _, pod := range exp.TargetPodList.Items {
orchestrator.injectChaosOnPod(pod)
}
log.Infof("[Chaos]:Waiting for: %vs", exp.ExperimentDetails.ChaosDuration)
// Trap os.Interrupt and syscall.SIGTERM an emit a value on the signal channel. Note
// that syscall.SIGKILL cannot be trapped.
signal.Notify(orchestrator.signalChannel, os.Interrupt, syscall.SIGTERM) // , syscall.SIGKILL)
orchestrator.observeAndReact(exp.TargetPodList.Items)
return orchestrator.err
}