forked from litmuschaos/litmus-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
litmus-chaos-orchestrator.go
197 lines (163 loc) · 4.02 KB
/
litmus-chaos-orchestrator.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
package lib
import (
"fmt"
"os"
"time"
"github.com/litmuschaos/litmus-go/pkg/log"
"github.com/litmuschaos/litmus-go/pkg/probe"
"github.com/litmuschaos/litmus-go/pkg/types"
"github.com/sirupsen/logrus"
corev1 "k8s.io/api/core/v1"
)
// litmusChaosOrchestrator is an abstraction for maintaining orchestration
// state and lifting errors.
type litmusChaosOrchestrator struct {
errChannel chan error
endTime <-chan time.Time
signalChannel chan os.Signal
err error
injector LitmusChaosInjector
exp ExperimentOrchestrationDetails
chaosParams interface{}
}
func litmusChaosOrchestratorInstance(injector LitmusChaosInjector,
exp ExperimentOrchestrationDetails) litmusChaosOrchestrator {
return litmusChaosOrchestrator{
errChannel: make(chan error),
signalChannel: make(chan os.Signal, 1),
injector: injector,
exp: exp,
}
}
func (c *litmusChaosOrchestrator) runProbes() {
if c.err != nil || len(c.exp.ResultDetails.ProbeDetails) == 0 {
return
}
c.err = probe.RunProbes(
c.exp.ChaosDetails,
c.exp.Clients,
c.exp.ResultDetails,
"During Chaos",
c.exp.EventDetails,
)
}
func (c *litmusChaosOrchestrator) startEndTimer() {
if c.err != nil {
return
}
chaosDuration := c.exp.ExperimentDetails.ChaosDuration
duration := time.Duration(chaosDuration) * time.Second
c.endTime = time.After(duration)
}
func (c *litmusChaosOrchestrator) generateChaosEventsOnPod(pod corev1.Pod) {
if c.err != nil || c.exp.ExperimentDetails.EngineName == "" {
return
}
msg := fmt.Sprintf("Injecting %s chaos on %s pod.",
c.exp.ChaosDetails.ExperimentName,
pod.Name)
types.SetEngineEventAttributes(
c.exp.EventDetails,
types.ChaosInject,
msg,
"Normal",
c.exp.ChaosDetails,
)
}
func (c *litmusChaosOrchestrator) obtainChaosParams() {
if c.err != nil {
return
}
c.chaosParams = c.injector.ChaosParamsFn(
c.exp.ExperimentDetails)
}
func (c *litmusChaosOrchestrator) logChaosDetails(pod corev1.Pod) {
if c.err != nil {
return
}
log.InfoWithValues("[Chaos] The target application details",
logrus.Fields{
"Target Container": c.exp.ExperimentDetails.TargetContainer,
"Target Pod": pod.Name,
"Chaos Details": fmt.Sprint(c.chaosParams),
},
)
}
func (c *litmusChaosOrchestrator) orchestrateChaos(pod corev1.Pod) {
if c.err != nil {
return
}
executor := LitmusExecutor{
ContainerName: c.exp.ExperimentDetails.TargetContainer,
PodName: pod.Name,
Namespace: c.exp.ExperimentDetails.AppNS,
Clients: c.exp.Clients,
}
go c.injector.ChaosInjectorFn(
executor, c.chaosParams, c.errChannel)
}
func (c *litmusChaosOrchestrator) injectChaosOnPod(pod corev1.Pod) {
if c.err != nil {
return
}
c.obtainChaosParams()
c.generateChaosEventsOnPod(pod)
c.logChaosDetails(pod)
c.orchestrateChaos(pod)
}
func (c *litmusChaosOrchestrator) killChaos(pod corev1.Pod) {
if c.err != nil {
return
}
executor := LitmusExecutor{
ContainerName: c.exp.ExperimentDetails.TargetContainer,
PodName: pod.Name,
Namespace: c.exp.ExperimentDetails.AppNS,
Clients: c.exp.Clients,
}
c.err = c.injector.ResetChaosFn(executor, c.chaosParams)
}
func (c *litmusChaosOrchestrator) revertChaos(pods []corev1.Pod) {
if c.err != nil {
return
}
for _, pod := range pods {
c.killChaos(pod)
}
}
func (c *litmusChaosOrchestrator) abortChaos(pods []corev1.Pod) {
if c.err != nil {
return
}
log.Info("[Chaos] Revert started")
c.revertChaos(pods)
if c.err != nil {
log.Errorf(
"Error killing chaos after abortion: %v", c.err)
}
log.Info("[Chaos] Revert completed")
os.Exit(1)
}
func (c *litmusChaosOrchestrator) observeAndReact(pods []corev1.Pod) {
if c.err != nil {
return
}
observeLoop:
for {
c.startEndTimer()
select {
case err := <-c.errChannel:
if err != nil {
c.err = err
break observeLoop
}
case <-c.signalChannel:
c.abortChaos(pods)
case <-c.endTime:
log.Infof("[Chaos]: Tims is up for experiment",
c.exp.ExperimentDetails.ExperimentName)
break observeLoop
}
}
c.revertChaos(pods)
}