1
1
package helper
2
2
3
3
import (
4
+ "bytes"
4
5
"fmt"
5
6
"os"
6
7
"os/exec"
@@ -63,7 +64,7 @@ func Helper(clients clients.ClientSets) {
63
64
64
65
}
65
66
66
- //prepareK8sHttpChaos contains the prepration steps before chaos injection
67
+ // prepareK8sHttpChaos contains the prepration steps before chaos injection
67
68
func prepareK8sHttpChaos (experimentsDetails * experimentTypes.ExperimentDetails , clients clients.ClientSets , eventsDetails * types.EventDetails , chaosDetails * types.ChaosDetails , resultDetails * types.ResultDetails ) error {
68
69
69
70
containerID , err := common .GetRuntimeBasedContainerID (experimentsDetails .ContainerRuntime , experimentsDetails .SocketPath , experimentsDetails .TargetPods , experimentsDetails .AppNS , experimentsDetails .TargetContainer , clients )
@@ -163,16 +164,15 @@ func startProxy(experimentDetails *experimentTypes.ExperimentDetails, pid int) e
163
164
createProxyCommand := fmt .Sprintf ("(sudo nsenter -t %d -n toxiproxy-cli create -l 0.0.0.0:%d -u 0.0.0.0:%d proxy)" , pid , experimentDetails .ProxyPort , experimentDetails .TargetServicePort )
164
165
createToxicCommand := fmt .Sprintf ("(sudo nsenter -t %d -n toxiproxy-cli toxic add %s --toxicity %f proxy)" , pid , toxics , float32 (experimentDetails .Toxicity )/ 100.0 )
165
166
166
- // sleep 10 is added for proxy-server to be ready for creating proxy and adding toxics
167
- chaosCommand := fmt .Sprintf ("%s && sleep 10 && %s && %s" , startProxyServerCommand , createProxyCommand , createToxicCommand )
167
+ // sleep 2 is added for proxy-server to be ready for creating proxy and adding toxics
168
+ chaosCommand := fmt .Sprintf ("%s && sleep 2 && %s && %s" , startProxyServerCommand , createProxyCommand , createToxicCommand )
168
169
169
- cmd := exec .Command ("/bin/bash" , "-c" , chaosCommand )
170
- log .Infof ("[Chaos]: Starting proxy server: %s" , cmd .String ())
170
+ log .Infof ("[Chaos]: Starting proxy server" )
171
171
172
- _ , err := cmd .CombinedOutput ()
173
- if err != nil {
172
+ if err := runCommand (chaosCommand ); err != nil {
174
173
return err
175
174
}
175
+
176
176
log .Info ("[Info]: Proxy started successfully" )
177
177
return nil
178
178
}
@@ -182,11 +182,9 @@ func startProxy(experimentDetails *experimentTypes.ExperimentDetails, pid int) e
182
182
// and execute the proxy related command inside it.
183
183
func killProxy (experimentDetails * experimentTypes.ExperimentDetails , pid int ) error {
184
184
stopProxyServerCommand := fmt .Sprintf ("sudo nsenter -t %d -n sudo kill -9 $(ps aux | grep [t]oxiproxy | awk 'FNR==1{print $1}')" , pid )
185
- cmd := exec .Command ("/bin/bash" , "-c" , stopProxyServerCommand )
186
- log .Infof ("[Chaos]: Stopping proxy server: %s" , cmd .String ())
185
+ log .Infof ("[Chaos]: Stopping proxy server" )
187
186
188
- _ , err := cmd .CombinedOutput ()
189
- if err != nil {
187
+ if err := runCommand (stopProxyServerCommand ); err != nil {
190
188
return err
191
189
}
192
190
@@ -199,13 +197,12 @@ func killProxy(experimentDetails *experimentTypes.ExperimentDetails, pid int) er
199
197
// and execute the iptables related command inside it.
200
198
func addIPRuleSet (experimentDetails * experimentTypes.ExperimentDetails , pid int ) error {
201
199
addIPRuleSetCommand := fmt .Sprintf ("(sudo nsenter -t %d -n iptables -t nat -A PREROUTING -i %v -p tcp --dport %d -j REDIRECT --to-port %d)" , pid , experimentDetails .NetworkInterface , experimentDetails .TargetServicePort , experimentDetails .ProxyPort )
202
- cmd := exec .Command ("/bin/bash" , "-c" , addIPRuleSetCommand )
203
- log .Infof ("[Chaos]: Adding IPtables ruleset: %s" , cmd .String ())
200
+ log .Infof ("[Chaos]: Adding IPtables ruleset" )
204
201
205
- _ , err := cmd .CombinedOutput ()
206
- if err != nil {
202
+ if err := runCommand (addIPRuleSetCommand ); err != nil {
207
203
return err
208
204
}
205
+
209
206
log .Info ("[Info]: IP rule set added successfully" )
210
207
return nil
211
208
}
@@ -215,18 +212,17 @@ func addIPRuleSet(experimentDetails *experimentTypes.ExperimentDetails, pid int)
215
212
// and execute the iptables related command inside it.
216
213
func removeIPRuleSet (experimentDetails * experimentTypes.ExperimentDetails , pid int ) error {
217
214
removeIPRuleSetCommand := fmt .Sprintf ("sudo nsenter -t %d -n iptables -t nat -D PREROUTING -i %v -p tcp --dport %d -j REDIRECT --to-port %d" , pid , experimentDetails .NetworkInterface , experimentDetails .TargetServicePort , experimentDetails .ProxyPort )
218
- cmd := exec .Command ("/bin/bash" , "-c" , removeIPRuleSetCommand )
219
- log .Infof ("[Chaos]: Removing IPtables ruleset: %s" , cmd .String ())
215
+ log .Infof ("[Chaos]: Removing IPtables ruleset" )
220
216
221
- _ , err := cmd .CombinedOutput ()
222
- if err != nil {
217
+ if err := runCommand (removeIPRuleSetCommand ); err != nil {
223
218
return err
224
219
}
220
+
225
221
log .Info ("[Info]: IP rule set removed successfully" )
226
222
return nil
227
223
}
228
224
229
- //getENV fetches all the env variables from the runner pod
225
+ // getENV fetches all the env variables from the runner pod
230
226
func getENV (experimentDetails * experimentTypes.ExperimentDetails ) {
231
227
experimentDetails .ExperimentName = types .Getenv ("EXPERIMENT_NAME" , "" )
232
228
experimentDetails .InstanceID = types .Getenv ("INSTANCE_ID" , "" )
@@ -247,6 +243,25 @@ func getENV(experimentDetails *experimentTypes.ExperimentDetails) {
247
243
experimentDetails .Toxicity , _ = strconv .Atoi (types .Getenv ("TOXICITY" , "100" ))
248
244
}
249
245
246
+ func runCommand (chaosCommand string ) error {
247
+ var stdout , stderr bytes.Buffer
248
+
249
+ cmd := exec .Command ("/bin/bash" , "-c" , chaosCommand )
250
+ cmd .Stdout = & stdout
251
+ cmd .Stderr = & stderr
252
+ err = cmd .Run ()
253
+ errStr := stderr .String ()
254
+ if err != nil {
255
+ // if we get standard error then, return the same
256
+ if errStr != "" {
257
+ return errors .New (errStr )
258
+ }
259
+ // if not standard error found, return error
260
+ return err
261
+ }
262
+ return nil
263
+ }
264
+
250
265
// abortWatcher continuously watch for the abort signals
251
266
func abortWatcher (targetPID int , resultName , chaosNS string , experimentDetails * experimentTypes.ExperimentDetails ) {
252
267
0 commit comments