Skip to content

Commit 25bea8a

Browse files
[Cherry-Pick for 1.5.1] (#39)
* refactor(experiments): Refactor litmus go experiments (#29) Signed-off-by: Udit Gaurav <uditgaurav@gmail.com> * feat(experiments): Add pod memory hog experiment (#31) Signed-off-by: Udit Gaurav <uditgaurav@gmail.com> * refactor(go-experiments): separate the types.go file for each experiment (#34) Signed-off-by: shubhamchaudhary <shubham.chaudhary@mayadata.io> * update(contribution-guide): updating contribution guide according to new schema changes (#35) Signed-off-by: shubhamchaudhary <shubham.chaudhary@mayadata.io> * chore(experiment): Add pod network duplication experiment in generic experiments of LitmusChaos (#27) * chore(experiment): Add pod network duplication experiment in generic experiments of LitmusChaos Signed-off-by: Udit Gaurav <uditgaurav@gmail.com> * bug(fix): Add for summary event and getting target container name (#37) Signed-off-by: Udit Gaurav <uditgaurav@gmail.com> * bug(fix): Remove extra index from the list in pod duplication experiment (#38) Signed-off-by: Udit Gaurav <uditgaurav@gmail.com> Co-authored-by: Shubham Chaudhary <shubham.chaudhary@mayadata.io>
1 parent 8d6cc4b commit 25bea8a

File tree

35 files changed

+1750
-366
lines changed

35 files changed

+1750
-366
lines changed

build/generate_go_binary

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11
# Building go binaries for pod_delete experiment
22
go build -o build/_output/pod-delete ./experiments/generic/pod-delete
3-
# Building go binaries for cpu_hog experiment
4-
go build -o build/_output/cpu-hog ./experiments/generic/cpu-hog
3+
# Building go binaries for pod_cpu_hog experiment
4+
go build -o build/_output/pod-cpu-hog ./experiments/generic/pod-cpu-hog
5+
# Building go binaries for pod_memory_hog experiment
6+
go build -o build/_output/pod-memory-hog ./experiments/generic/pod-memory-hog
7+
# Buiding go binaries for pod_network_duplication experiment
8+
go build -o build/_output/pod-network-duplication ./experiments/generic/pod-network-duplication
9+

chaoslib/litmus/cpu_hog/cpu-hog.go renamed to chaoslib/litmus/pod_cpu_hog/pod-cpu-hog.go

Lines changed: 30 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package cpu_hog
1+
package pod_cpu_hog
22

33
import (
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
3131
func 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
202208
func 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
}

chaoslib/litmus/pod_delete/pod-delete.go

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,24 @@ import (
55
"strconv"
66
"time"
77

8+
"github.com/litmuschaos/litmus-go/pkg/environment"
89
"github.com/litmuschaos/litmus-go/pkg/log"
10+
"github.com/litmuschaos/litmus-go/pkg/math"
11+
experimentTypes "github.com/litmuschaos/litmus-go/pkg/pod-delete/types"
912
"github.com/litmuschaos/litmus-go/pkg/status"
13+
"github.com/litmuschaos/litmus-go/pkg/types"
1014
"github.com/openebs/maya/pkg/util/retry"
1115
"github.com/pkg/errors"
1216
apiv1 "k8s.io/api/core/v1"
1317
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
14-
15-
"github.com/litmuschaos/litmus-go/pkg/environment"
16-
"github.com/litmuschaos/litmus-go/pkg/math"
17-
"github.com/litmuschaos/litmus-go/pkg/types"
1818
)
1919

2020
var err error
2121

2222
//PreparePodDelete contains the prepration steps before chaos injection
23-
func PreparePodDelete(experimentsDetails *types.ExperimentDetails, clients environment.ClientSets, resultDetails *types.ResultDetails, eventsDetails *types.EventDetails) error {
23+
func PreparePodDelete(experimentsDetails *experimentTypes.ExperimentDetails, clients environment.ClientSets, resultDetails *types.ResultDetails, eventsDetails *types.EventDetails) error {
2424

25-
//getting the iteration count for the pod deletion
25+
//Getting the iteration count for the pod deletion
2626
GetIterations(experimentsDetails)
2727
//Waiting for the ramp time before chaos injection
2828
if experimentsDetails.RampTime != 0 {
@@ -39,7 +39,7 @@ func PreparePodDelete(experimentsDetails *types.ExperimentDetails, clients envir
3939
// Generate the run_id
4040
runID := GetRunID()
4141

42-
// Creating the helper pod
42+
// Creating a helper pod
4343
err = CreateHelperPod(experimentsDetails, clients, runID)
4444
if err != nil {
4545
errors.Errorf("Unable to create the helper pod, err: %v", err)
@@ -65,7 +65,7 @@ func PreparePodDelete(experimentsDetails *types.ExperimentDetails, clients envir
6565
//ChaosCurrentTimeStamp contains the current timestamp
6666
ChaosCurrentTimeStamp := time.Now().Unix()
6767
//ChaosDiffTimeStamp contains the difference of current timestamp and start timestamp
68-
//It will helpful to track the total chaos duration
68+
//It will helpful to track the total chaos duration
6969
chaosDiffTimeStamp := ChaosCurrentTimeStamp - ChaosStartTimeStamp
7070

7171
if int(chaosDiffTimeStamp) < experimentsDetails.ChaosDuration {
@@ -88,7 +88,7 @@ func PreparePodDelete(experimentsDetails *types.ExperimentDetails, clients envir
8888
}
8989

9090
//GetIterations derive the iterations value from given parameters
91-
func GetIterations(experimentsDetails *types.ExperimentDetails) {
91+
func GetIterations(experimentsDetails *experimentTypes.ExperimentDetails) {
9292
var Iterations int
9393
if experimentsDetails.ChaosInterval != 0 {
9494
Iterations = experimentsDetails.ChaosDuration / experimentsDetails.ChaosInterval
@@ -100,7 +100,7 @@ func GetIterations(experimentsDetails *types.ExperimentDetails) {
100100
}
101101

102102
//waitForRampTime waits for the given ramp time duration (in seconds)
103-
func waitForRampTime(experimentsDetails *types.ExperimentDetails) {
103+
func waitForRampTime(experimentsDetails *experimentTypes.ExperimentDetails) {
104104
time.Sleep(time.Duration(experimentsDetails.RampTime) * time.Second)
105105
}
106106

@@ -115,7 +115,7 @@ func GetRunID() string {
115115
}
116116

117117
// GetServiceAccount find the serviceAccountName for the helper pod
118-
func GetServiceAccount(experimentsDetails *types.ExperimentDetails, clients environment.ClientSets) error {
118+
func GetServiceAccount(experimentsDetails *experimentTypes.ExperimentDetails, clients environment.ClientSets) error {
119119
pod, err := clients.KubeClient.CoreV1().Pods(experimentsDetails.ChaosNamespace).Get(experimentsDetails.ChaosPodName, v1.GetOptions{})
120120
if err != nil {
121121
return err
@@ -125,7 +125,7 @@ func GetServiceAccount(experimentsDetails *types.ExperimentDetails, clients envi
125125
}
126126

127127
// CreateHelperPod derive the attributes for helper pod and create the helper pod
128-
func CreateHelperPod(experimentsDetails *types.ExperimentDetails, clients environment.ClientSets, runID string) error {
128+
func CreateHelperPod(experimentsDetails *experimentTypes.ExperimentDetails, clients environment.ClientSets, runID string) error {
129129

130130
helperPod := &apiv1.Pod{
131131
ObjectMeta: v1.ObjectMeta{
@@ -164,7 +164,7 @@ func CreateHelperPod(experimentsDetails *types.ExperimentDetails, clients enviro
164164
}
165165

166166
//DeleteHelperPod delete the helper pod
167-
func DeleteHelperPod(experimentsDetails *types.ExperimentDetails, clients environment.ClientSets, runID string) error {
167+
func DeleteHelperPod(experimentsDetails *experimentTypes.ExperimentDetails, clients environment.ClientSets, runID string) error {
168168

169169
err := clients.KubeClient.CoreV1().Pods(experimentsDetails.ChaosNamespace).Delete("pod-delete-"+runID, &v1.DeleteOptions{})
170170

@@ -187,12 +187,12 @@ func DeleteHelperPod(experimentsDetails *types.ExperimentDetails, clients enviro
187187
}
188188

189189
// GetPodEnv derive all the env required for the helper pod
190-
func GetPodEnv(experimentsDetails *types.ExperimentDetails) []apiv1.EnvVar{
190+
func GetPodEnv(experimentsDetails *experimentTypes.ExperimentDetails) []apiv1.EnvVar {
191191

192192
var envVar []apiv1.EnvVar
193-
ENVList := map[string]string{"FORCE": strconv.FormatBool(experimentsDetails.Force),"APP_NS": experimentsDetails.AppNS,"KILL_COUNT": strconv.Itoa(experimentsDetails.KillCount),
194-
"TOTAL_CHAOS_DURATION": strconv.Itoa(experimentsDetails.ChaosDuration),"CHAOS_NAMESPACE": experimentsDetails.ChaosNamespace,"APP_LABEL": experimentsDetails.AppLabel,
195-
"CHAOS_ENGINE": experimentsDetails.EngineName,"CHAOS_UID": string(experimentsDetails.ChaosUID),"CHAOS_INTERVAL": strconv.Itoa(experimentsDetails.ChaosInterval),"ITERATIONS": strconv.Itoa(experimentsDetails.Iterations)}
193+
ENVList := map[string]string{"FORCE": strconv.FormatBool(experimentsDetails.Force), "APP_NS": experimentsDetails.AppNS, "KILL_COUNT": strconv.Itoa(experimentsDetails.KillCount),
194+
"TOTAL_CHAOS_DURATION": strconv.Itoa(experimentsDetails.ChaosDuration), "CHAOS_NAMESPACE": experimentsDetails.ChaosNamespace, "APP_LABEL": experimentsDetails.AppLabel,
195+
"CHAOS_ENGINE": experimentsDetails.EngineName, "CHAOS_UID": string(experimentsDetails.ChaosUID), "CHAOS_INTERVAL": strconv.Itoa(experimentsDetails.ChaosInterval), "ITERATIONS": strconv.Itoa(experimentsDetails.Iterations)}
196196
for key, value := range ENVList {
197197
var perEnv apiv1.EnvVar
198198
perEnv.Name = key
@@ -204,7 +204,7 @@ func GetPodEnv(experimentsDetails *types.ExperimentDetails) []apiv1.EnvVar{
204204

205205
var downwardEnv apiv1.EnvVar
206206
downwardEnv.Name = "POD_NAME"
207-
downwardEnv.ValueFrom = &experimentPodName
207+
downwardEnv.ValueFrom = &experimentPodName
208208
envVar = append(envVar, downwardEnv)
209209

210210
return envVar
@@ -219,4 +219,4 @@ func GetValueFromDownwardAPI(apiVersion string, fieldPath string) apiv1.EnvVarSo
219219
},
220220
}
221221
return downwardENV
222-
}
222+
}

0 commit comments

Comments
 (0)