1- package cpu_hog
1+ package pod_cpu_hog
22
33import (
44 "fmt"
@@ -13,19 +13,19 @@ import (
1313 "github.com/litmuschaos/litmus-go/pkg/events"
1414 "github.com/litmuschaos/litmus-go/pkg/log"
1515 "github.com/litmuschaos/litmus-go/pkg/math"
16+ experimentTypes "github.com/litmuschaos/litmus-go/pkg/pod-cpu-hog/types"
1617 "github.com/litmuschaos/litmus-go/pkg/result"
1718 "github.com/litmuschaos/litmus-go/pkg/types"
1819 "github.com/pkg/errors"
1920 "github.com/sirupsen/logrus"
2021 core_v1 "k8s.io/api/core/v1"
2122 v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
22- "k8s.io/klog"
23-
2423 "k8s.io/apimachinery/pkg/runtime"
2524 "k8s.io/client-go/tools/remotecommand"
25+ "k8s.io/klog"
2626)
2727
28- // Using the REST API to exec into the target container of the target pod
28+ // StressCPU Uses the REST API to exec into the target container of the target pod
2929// The function will be constantly increasing the CPU utilisation until it reaches the maximum available or allowed number.
3030// Using the TOTAL_CHAOS_DURATION we will need to specify for how long this experiment will last
3131func StressCPU (containerName , podName , namespace string , clients environment.ClientSets ) error {
@@ -68,8 +68,8 @@ func StressCPU(containerName, podName, namespace string, clients environment.Cli
6868 })
6969
7070 if err != nil {
71- error_code := strings .Contains (err .Error (), "143" )
72- if error_code != true {
71+ errorCode := strings .Contains (err .Error (), "143" )
72+ if errorCode != true {
7373 log .Infof ("[Chaos]:CPU stress error: %v" , err .Error ())
7474 return err
7575 }
@@ -78,8 +78,8 @@ func StressCPU(containerName, podName, namespace string, clients environment.Cli
7878 return nil
7979}
8080
81- //This function orchestrates the experiment by calling the StressCPU function for every core, of every container, of every pod that is targetted
82- func ExperimentCPU (experimentsDetails * types .ExperimentDetails , clients environment.ClientSets , resultDetails * types.ResultDetails ) error {
81+ //ExperimentCPU function orchestrates the experiment by calling the StressCPU function for every core, of every container, of every pod that is targetted
82+ func ExperimentCPU (experimentsDetails * experimentTypes .ExperimentDetails , clients environment.ClientSets , resultDetails * types.ResultDetails , eventsDetails * types. EventDetails , chaosDetails * types. ChaosDetails ) error {
8383
8484 var endTime <- chan time.Time
8585 timeDelay := time .Duration (experimentsDetails .ChaosDuration ) * time .Second
@@ -102,6 +102,13 @@ func ExperimentCPU(experimentsDetails *types.ExperimentDetails, clients environm
102102 log .Infof ("[Chaos]:Stressing: %v cores" , strconv .Itoa (experimentsDetails .CPUcores ))
103103
104104 for i := 0 ; i < experimentsDetails .CPUcores ; i ++ {
105+
106+ if experimentsDetails .EngineName != "" {
107+ msg := "Injecting " + experimentsDetails .ExperimentName + " chaos on " + pod .Name + " pod"
108+ environment .SetEngineEventAttributes (eventsDetails , types .ChaosInject , msg , chaosDetails )
109+ events .GenerateEvents (eventsDetails , clients , chaosDetails , "ChaosEngine" )
110+ }
111+
105112 go StressCPU (container .Name , pod .Name , experimentsDetails .AppNS , clients )
106113
107114 log .Infof ("[Chaos]:Waiting for: %vs" , strconv .Itoa (experimentsDetails .ChaosDuration ))
@@ -123,7 +130,7 @@ func ExperimentCPU(experimentsDetails *types.ExperimentDetails, clients environm
123130 }
124131 resultDetails .FailStep = "CPU hog Chaos injection stopped!"
125132 resultDetails .Verdict = "Stopped"
126- result .ChaosResult (experimentsDetails , clients , resultDetails , "EOT" )
133+ result .ChaosResult (chaosDetails , clients , resultDetails , "EOT" )
127134 os .Exit (1 )
128135 case <- endTime :
129136 log .Infof ("[Chaos]: Time is up for experiment: %v" , experimentsDetails .ExperimentName )
@@ -133,8 +140,8 @@ func ExperimentCPU(experimentsDetails *types.ExperimentDetails, clients environm
133140 }
134141 err = KillStressCPU (container .Name , pod .Name , experimentsDetails .AppNS , clients )
135142 if err != nil {
136- error_code := strings .Contains (err .Error (), "143" )
137- if error_code != true {
143+ errorCode := strings .Contains (err .Error (), "143" )
144+ if errorCode != true {
138145 log .Infof ("[Chaos]:CPU stress error: %v" , err .Error ())
139146 return err
140147 }
@@ -146,16 +153,16 @@ func ExperimentCPU(experimentsDetails *types.ExperimentDetails, clients environm
146153 return nil
147154}
148155
149- //PreparePodDelete contains the steps for prepration before chaos
150- func PrepareCPUstress (experimentsDetails * types .ExperimentDetails , clients environment.ClientSets , resultDetails * types.ResultDetails , recorder * events. Recorder ) error {
156+ //PrepareCPUstress contains the steps for prepration before chaos
157+ func PrepareCPUstress (experimentsDetails * experimentTypes .ExperimentDetails , clients environment.ClientSets , resultDetails * types.ResultDetails , eventsDetails * types. EventDetails , chaosDetails * types. ChaosDetails ) error {
151158
152159 //Waiting for the ramp time before chaos injection
153160 if experimentsDetails .RampTime != 0 {
154161 log .Infof ("[Ramp]: Waiting for the %vs ramp time before injecting chaos" , strconv .Itoa (experimentsDetails .RampTime ))
155162 waitForRampTime (experimentsDetails )
156163 }
157164 //Starting the CPU stress experiment
158- err := ExperimentCPU (experimentsDetails , clients , resultDetails )
165+ err := ExperimentCPU (experimentsDetails , clients , resultDetails , eventsDetails , chaosDetails )
159166 if err != nil {
160167 return err
161168 }
@@ -168,13 +175,12 @@ func PrepareCPUstress(experimentsDetails *types.ExperimentDetails, clients envir
168175}
169176
170177//waitForRampTime waits for the given ramp time duration (in seconds)
171- func waitForRampTime (experimentsDetails * types .ExperimentDetails ) {
178+ func waitForRampTime (experimentsDetails * experimentTypes .ExperimentDetails ) {
172179 time .Sleep (time .Duration (experimentsDetails .RampTime ) * time .Second )
173180}
174181
175- //PreparePodList derive the list of target pod for deletion
176- //It will also adjust the number of the target pods depending on the specified percentage in PODS_AFFECTED_PERC variable
177- func PreparePodList (experimentsDetails * types.ExperimentDetails , clients environment.ClientSets , resultDetails * types.ResultDetails ) (* core_v1.PodList , error ) {
182+ //PreparePodList will also adjust the number of the target pods depending on the specified percentage in PODS_AFFECTED_PERC variable
183+ func PreparePodList (experimentsDetails * experimentTypes.ExperimentDetails , clients environment.ClientSets , resultDetails * types.ResultDetails ) (* core_v1.PodList , error ) {
178184
179185 log .Infof ("[Chaos]:Pods percentage to affect is %v" , strconv .Itoa (experimentsDetails .PodsAffectedPerc ))
180186
@@ -188,17 +194,17 @@ func PreparePodList(experimentsDetails *types.ExperimentDetails, clients environ
188194 //If the default value has changed, means that we are aiming for a subset of the pods.
189195 if experimentsDetails .PodsAffectedPerc != 100 {
190196
191- new_podlist_length := math .Adjustment (experimentsDetails .PodsAffectedPerc , len (pods .Items ))
197+ newPodListLength := math .Maximum ( 1 , math . Adjustment (experimentsDetails .PodsAffectedPerc , len (pods .Items ) ))
192198
193- pods .Items = pods .Items [:new_podlist_length ]
199+ pods .Items = pods .Items [:newPodListLength ]
194200
195- log .Infof ("[Chaos]:Number of pods targetted: %v" , strconv .Itoa (new_podlist_length ))
201+ log .Infof ("[Chaos]:Number of pods targetted: %v" , strconv .Itoa (newPodListLength ))
196202
197203 }
198204 return pods , nil
199205}
200206
201- // Function to kill the experiment. Triggered by either timeout of chaos duration or termination of the experiment
207+ // KillStressCPU function to kill the experiment. Triggered by either timeout of chaos duration or termination of the experiment
202208func KillStressCPU (containerName , podName , namespace string , clients environment.ClientSets ) error {
203209
204210 command := []string {"/bin/sh" , "-c" , "kill $(find /proc -name exe -lname '*/md5sum' 2>&1 | grep -v 'Permission denied' | awk -F/ '{print $(NF-1)}' | head -n 1)" }
@@ -240,8 +246,8 @@ func KillStressCPU(containerName, podName, namespace string, clients environment
240246
241247 //The kill command returns a 143 when it kills a process. This is expected
242248 if err != nil {
243- error_code := strings .Contains (err .Error (), "143" )
244- if error_code != true {
249+ errorCode := strings .Contains (err .Error (), "143" )
250+ if errorCode != true {
245251 log .Infof ("[Chaos]:CPU stress error: %v" , err .Error ())
246252 return err
247253 }
0 commit comments