Skip to content

Commit

Permalink
Fix(targetContainer): Incorrect target container passed in the helper…
Browse files Browse the repository at this point in the history
… pod for pod level experiments (#496) (#497)

* Fix target container issue

Signed-off-by: uditgaurav <udit@chaosnative.com>
  • Loading branch information
uditgaurav committed Mar 28, 2022
1 parent 67b6db5 commit 5fb669f
Show file tree
Hide file tree
Showing 20 changed files with 237 additions and 144 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/push.yml
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,4 @@ jobs:
push: true
file: build/Dockerfile
platforms: linux/amd64,linux/arm64
tags: litmuschaos/go-runner:ci
tags: litmuschaos/go-runner:ci
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -97,4 +97,4 @@ trivy-check:
@echo "---> Running Trivy Check"
@echo "------------------------"
@./trivy --exit-code 0 --severity HIGH --no-progress $(DOCKER_REGISTRY)/$(DOCKER_REPO)/$(DOCKER_IMAGE):$(DOCKER_TAG)
@./trivy --exit-code 0 --severity CRITICAL --no-progress $(DOCKER_REGISTRY)/$(DOCKER_REPO)/$(DOCKER_IMAGE):$(DOCKER_TAG)
@./trivy --exit-code 0 --severity CRITICAL --no-progress $(DOCKER_REGISTRY)/$(DOCKER_REPO)/$(DOCKER_IMAGE):$(DOCKER_TAG)
2 changes: 1 addition & 1 deletion build/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Multi-stage docker build
# Build stage
FROM golang:1.16 AS builder
FROM golang:1.17 AS builder

ARG TARGETOS=linux
ARG TARGETARCH
Expand Down
29 changes: 19 additions & 10 deletions chaoslib/litmus/container-kill/lib/container-kill.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,20 +77,13 @@ func PrepareContainerKill(experimentsDetails *experimentTypes.ExperimentDetails,
}
}

//Get the target container name of the application pod
if experimentsDetails.TargetContainer == "" {
experimentsDetails.TargetContainer, err = common.GetTargetContainer(experimentsDetails.AppNS, targetPodList.Items[0].Name, clients)
if err != nil {
return errors.Errorf("unable to get the target container name, err: %v", err)
}
}

if experimentsDetails.EngineName != "" {
if err := common.SetHelperData(chaosDetails, clients); err != nil {
return err
}
}

experimentsDetails.IsTargetContainerProvided = (experimentsDetails.TargetContainer != "")
switch strings.ToLower(experimentsDetails.Sequence) {
case "serial":
if err = injectChaosInSerialMode(experimentsDetails, targetPodList, clients, chaosDetails, resultDetails, eventsDetails); err != nil {
Expand All @@ -116,7 +109,7 @@ func PrepareContainerKill(experimentsDetails *experimentTypes.ExperimentDetails,
func injectChaosInSerialMode(experimentsDetails *experimentTypes.ExperimentDetails, targetPodList apiv1.PodList, clients clients.ClientSets, chaosDetails *types.ChaosDetails, resultDetails *types.ResultDetails, eventsDetails *types.EventDetails) error {

labelSuffix := common.GetRunID()

var err error
// run the probes during chaos
if len(resultDetails.ProbeDetails) != 0 {
if err := probe.RunProbes(chaosDetails, clients, resultDetails, "DuringChaos", eventsDetails); err != nil {
Expand All @@ -127,6 +120,14 @@ func injectChaosInSerialMode(experimentsDetails *experimentTypes.ExperimentDetai
// creating the helper pod to perform container kill chaos
for _, pod := range targetPodList.Items {

//Get the target container name of the application pod
if !experimentsDetails.IsTargetContainerProvided {
experimentsDetails.TargetContainer, err = common.GetTargetContainer(experimentsDetails.AppNS, pod.Name, clients)
if err != nil {
return errors.Errorf("unable to get the target container name, err: %v", err)
}
}

log.InfoWithValues("[Info]: Details of application under chaos injection", logrus.Fields{
"PodName": pod.Name,
"NodeName": pod.Spec.NodeName,
Expand Down Expand Up @@ -168,7 +169,7 @@ func injectChaosInSerialMode(experimentsDetails *experimentTypes.ExperimentDetai
func injectChaosInParallelMode(experimentsDetails *experimentTypes.ExperimentDetails, targetPodList apiv1.PodList, clients clients.ClientSets, chaosDetails *types.ChaosDetails, resultDetails *types.ResultDetails, eventsDetails *types.EventDetails) error {

labelSuffix := common.GetRunID()

var err error
// run the probes during chaos
if len(resultDetails.ProbeDetails) != 0 {
if err := probe.RunProbes(chaosDetails, clients, resultDetails, "DuringChaos", eventsDetails); err != nil {
Expand All @@ -179,6 +180,14 @@ func injectChaosInParallelMode(experimentsDetails *experimentTypes.ExperimentDet
// creating the helper pod to perform container kill chaos
for _, pod := range targetPodList.Items {

//Get the target container name of the application pod
if !experimentsDetails.IsTargetContainerProvided {
experimentsDetails.TargetContainer, err = common.GetTargetContainer(experimentsDetails.AppNS, pod.Name, clients)
if err != nil {
return errors.Errorf("unable to get the target container name, err: %v", err)
}
}

log.InfoWithValues("[Info]: Details of application under chaos injection", logrus.Fields{
"PodName": pod.Name,
"NodeName": pod.Spec.NodeName,
Expand Down
31 changes: 21 additions & 10 deletions chaoslib/litmus/disk-fill/lib/disk-fill.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,14 +74,6 @@ func PrepareDiskFill(experimentsDetails *experimentTypes.ExperimentDetails, clie
common.WaitForDuration(experimentsDetails.RampTime)
}

//Get the target container name of the application pod
if experimentsDetails.TargetContainer == "" {
experimentsDetails.TargetContainer, err = common.GetTargetContainer(experimentsDetails.AppNS, targetPodList.Items[0].Name, clients)
if err != nil {
return errors.Errorf("unable to get the target container name, err: %v", err)
}
}

// Getting the serviceAccountName, need permission inside helper pod to create the events
if experimentsDetails.ChaosServiceAccount == "" {
experimentsDetails.ChaosServiceAccount, err = common.GetServiceAccount(experimentsDetails.ChaosNamespace, experimentsDetails.ChaosPodName, clients)
Expand All @@ -96,6 +88,7 @@ func PrepareDiskFill(experimentsDetails *experimentTypes.ExperimentDetails, clie
}
}

experimentsDetails.IsTargetContainerProvided = (experimentsDetails.TargetContainer != "")
switch strings.ToLower(experimentsDetails.Sequence) {
case "serial":
if err = injectChaosInSerialMode(experimentsDetails, targetPodList, clients, chaosDetails, execCommandDetails, resultDetails, eventsDetails); err != nil {
Expand All @@ -121,7 +114,7 @@ func PrepareDiskFill(experimentsDetails *experimentTypes.ExperimentDetails, clie
func injectChaosInSerialMode(experimentsDetails *experimentTypes.ExperimentDetails, targetPodList apiv1.PodList, clients clients.ClientSets, chaosDetails *types.ChaosDetails, execCommandDetails exec.PodDetails, resultDetails *types.ResultDetails, eventsDetails *types.EventDetails) error {

labelSuffix := common.GetRunID()

var err error
// run the probes during chaos
if len(resultDetails.ProbeDetails) != 0 {
if err := probe.RunProbes(chaosDetails, clients, resultDetails, "DuringChaos", eventsDetails); err != nil {
Expand All @@ -131,6 +124,15 @@ func injectChaosInSerialMode(experimentsDetails *experimentTypes.ExperimentDetai

// creating the helper pod to perform disk-fill chaos
for _, pod := range targetPodList.Items {

//Get the target container name of the application pod
if !experimentsDetails.IsTargetContainerProvided {
experimentsDetails.TargetContainer, err = common.GetTargetContainer(experimentsDetails.AppNS, pod.Name, clients)
if err != nil {
return errors.Errorf("unable to get the target container name, err: %v", err)
}
}

runID := common.GetRunID()
if err := createHelperPod(experimentsDetails, clients, chaosDetails, pod.Name, pod.Spec.NodeName, runID, labelSuffix); err != nil {
return errors.Errorf("unable to create the helper pod, err: %v", err)
Expand Down Expand Up @@ -169,7 +171,7 @@ func injectChaosInSerialMode(experimentsDetails *experimentTypes.ExperimentDetai
func injectChaosInParallelMode(experimentsDetails *experimentTypes.ExperimentDetails, targetPodList apiv1.PodList, clients clients.ClientSets, chaosDetails *types.ChaosDetails, execCommandDetails exec.PodDetails, resultDetails *types.ResultDetails, eventsDetails *types.EventDetails) error {

labelSuffix := common.GetRunID()

var err error
// run the probes during chaos
if len(resultDetails.ProbeDetails) != 0 {
if err := probe.RunProbes(chaosDetails, clients, resultDetails, "DuringChaos", eventsDetails); err != nil {
Expand All @@ -179,6 +181,15 @@ func injectChaosInParallelMode(experimentsDetails *experimentTypes.ExperimentDet

// creating the helper pod to perform disk-fill chaos
for _, pod := range targetPodList.Items {

//Get the target container name of the application pod
if !experimentsDetails.IsTargetContainerProvided {
experimentsDetails.TargetContainer, err = common.GetTargetContainer(experimentsDetails.AppNS, pod.Name, clients)
if err != nil {
return errors.Errorf("unable to get the target container name, err: %v", err)
}
}

runID := common.GetRunID()
if err := createHelperPod(experimentsDetails, clients, chaosDetails, pod.Name, pod.Spec.NodeName, runID, labelSuffix); err != nil {
return errors.Errorf("unable to create the helper pod, err: %v", err)
Expand Down
30 changes: 20 additions & 10 deletions chaoslib/litmus/network-chaos/lib/network-chaos.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,14 +102,6 @@ func PrepareAndInjectChaos(experimentsDetails *experimentTypes.ExperimentDetails
}
}

//Get the target container name of the application pod
if experimentsDetails.TargetContainer == "" {
experimentsDetails.TargetContainer, err = common.GetTargetContainer(experimentsDetails.AppNS, targetPodList.Items[0].Name, clients)
if err != nil {
return errors.Errorf("unable to get the target container name, err: %v", err)
}
}

experimentsDetails.DestinationIPs, err = GetTargetIps(experimentsDetails.DestinationIPs, experimentsDetails.DestinationHosts)
if err != nil {
return err
Expand All @@ -121,6 +113,7 @@ func PrepareAndInjectChaos(experimentsDetails *experimentTypes.ExperimentDetails
}
}

experimentsDetails.IsTargetContainerProvided = (experimentsDetails.TargetContainer != "")
switch strings.ToLower(experimentsDetails.Sequence) {
case "serial":
if err = injectChaosInSerialMode(experimentsDetails, targetPodList, clients, chaosDetails, args, resultDetails, eventsDetails); err != nil {
Expand All @@ -140,8 +133,8 @@ func PrepareAndInjectChaos(experimentsDetails *experimentTypes.ExperimentDetails
// injectChaosInSerialMode inject the network chaos in all target application serially (one by one)
func injectChaosInSerialMode(experimentsDetails *experimentTypes.ExperimentDetails, targetPodList apiv1.PodList, clients clients.ClientSets, chaosDetails *types.ChaosDetails, args string, resultDetails *types.ResultDetails, eventsDetails *types.EventDetails) error {

var err error
labelSuffix := common.GetRunID()

// run the probes during chaos
if len(resultDetails.ProbeDetails) != 0 {
if err := probe.RunProbes(chaosDetails, clients, resultDetails, "DuringChaos", eventsDetails); err != nil {
Expand All @@ -152,6 +145,14 @@ func injectChaosInSerialMode(experimentsDetails *experimentTypes.ExperimentDetai
// creating the helper pod to perform network chaos
for _, pod := range targetPodList.Items {

//Get the target container name of the application pod
if !experimentsDetails.IsTargetContainerProvided {
experimentsDetails.TargetContainer, err = common.GetTargetContainer(experimentsDetails.AppNS, pod.Name, clients)
if err != nil {
return errors.Errorf("unable to get the target container name, err: %v", err)
}
}

log.InfoWithValues("[Info]: Details of application under chaos injection", logrus.Fields{
"PodName": pod.Name,
"NodeName": pod.Spec.NodeName,
Expand Down Expand Up @@ -194,7 +195,7 @@ func injectChaosInSerialMode(experimentsDetails *experimentTypes.ExperimentDetai
func injectChaosInParallelMode(experimentsDetails *experimentTypes.ExperimentDetails, targetPodList apiv1.PodList, clients clients.ClientSets, chaosDetails *types.ChaosDetails, args string, resultDetails *types.ResultDetails, eventsDetails *types.EventDetails) error {

labelSuffix := common.GetRunID()

var err error
// run the probes during chaos
if len(resultDetails.ProbeDetails) != 0 {
if err := probe.RunProbes(chaosDetails, clients, resultDetails, "DuringChaos", eventsDetails); err != nil {
Expand All @@ -205,6 +206,15 @@ func injectChaosInParallelMode(experimentsDetails *experimentTypes.ExperimentDet
// creating the helper pod to perform network chaos
for _, pod := range targetPodList.Items {

//Get the target container name of the application pod
//It checks the empty target container for the first iteration only
if !experimentsDetails.IsTargetContainerProvided {
experimentsDetails.TargetContainer, err = common.GetTargetContainer(experimentsDetails.AppNS, pod.Name, clients)
if err != nil {
return errors.Errorf("unable to get the target container name, err: %v", err)
}
}

log.InfoWithValues("[Info]: Details of application under chaos injection", logrus.Fields{
"PodName": pod.Name,
"NodeName": pod.Spec.NodeName,
Expand Down
27 changes: 18 additions & 9 deletions chaoslib/litmus/pod-cpu-hog-exec/lib/pod-cpu-hog-exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,7 @@ func experimentCPU(experimentsDetails *experimentTypes.ExperimentDetails, client
}
log.Infof("Target pods list for chaos, %v", podNames)

//Get the target container name of the application pod
if experimentsDetails.TargetContainer == "" {
experimentsDetails.TargetContainer, err = common.GetTargetContainer(experimentsDetails.AppNS, targetPodList.Items[0].Name, clients)
if err != nil {
return errors.Errorf("unable to get the target container name, err: %v", err)
}
}

experimentsDetails.IsTargetContainerProvided = (experimentsDetails.TargetContainer != "")
switch strings.ToLower(experimentsDetails.Sequence) {
case "serial":
if err = injectChaosInSerialMode(experimentsDetails, targetPodList, clients, resultDetails, eventsDetails, chaosDetails); err != nil {
Expand All @@ -81,6 +74,7 @@ func experimentCPU(experimentsDetails *experimentTypes.ExperimentDetails, client
// injectChaosInSerialMode stressed the cpu of all target application serially (one by one)
func injectChaosInSerialMode(experimentsDetails *experimentTypes.ExperimentDetails, targetPodList corev1.PodList, clients clients.ClientSets, resultDetails *types.ResultDetails, eventsDetails *types.EventDetails, chaosDetails *types.ChaosDetails) error {

var err error
// run the probes during chaos
if len(resultDetails.ProbeDetails) != 0 {
if err := probe.RunProbes(chaosDetails, clients, resultDetails, "DuringChaos", eventsDetails); err != nil {
Expand Down Expand Up @@ -113,6 +107,14 @@ func injectChaosInSerialMode(experimentsDetails *experimentTypes.ExperimentDetai
events.GenerateEvents(eventsDetails, clients, chaosDetails, "ChaosEngine")
}

//Get the target container name of the application pod
if !experimentsDetails.IsTargetContainerProvided {
experimentsDetails.TargetContainer, err = common.GetTargetContainer(experimentsDetails.AppNS, pod.Name, clients)
if err != nil {
return errors.Errorf("unable to get the target container name, err: %v", err)
}
}

log.InfoWithValues("[Chaos]: The Target application details", logrus.Fields{
"Target Container": experimentsDetails.TargetContainer,
"Target Pod": pod.Name,
Expand Down Expand Up @@ -172,7 +174,7 @@ func injectChaosInSerialMode(experimentsDetails *experimentTypes.ExperimentDetai
func injectChaosInParallelMode(experimentsDetails *experimentTypes.ExperimentDetails, targetPodList corev1.PodList, clients clients.ClientSets, resultDetails *types.ResultDetails, eventsDetails *types.EventDetails, chaosDetails *types.ChaosDetails) error {
// creating err channel to receive the error from the go routine
stressErr := make(chan error)

var err error
// run the probes during chaos
if len(resultDetails.ProbeDetails) != 0 {
if err := probe.RunProbes(chaosDetails, clients, resultDetails, "DuringChaos", eventsDetails); err != nil {
Expand Down Expand Up @@ -201,6 +203,13 @@ func injectChaosInParallelMode(experimentsDetails *experimentTypes.ExperimentDet
types.SetEngineEventAttributes(eventsDetails, types.ChaosInject, msg, "Normal", chaosDetails)
events.GenerateEvents(eventsDetails, clients, chaosDetails, "ChaosEngine")
}
//Get the target container name of the application pod
if !experimentsDetails.IsTargetContainerProvided {
experimentsDetails.TargetContainer, err = common.GetTargetContainer(experimentsDetails.AppNS, pod.Name, clients)
if err != nil {
return errors.Errorf("unable to get the target container name, err: %v", err)
}
}

log.InfoWithValues("[Chaos]: The Target application details", logrus.Fields{
"Target Container": experimentsDetails.TargetContainer,
Expand Down
29 changes: 19 additions & 10 deletions chaoslib/litmus/pod-dns-chaos/lib/pod-dns-chaos.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,20 +50,13 @@ func PrepareAndInjectChaos(experimentsDetails *experimentTypes.ExperimentDetails
}
}

//Get the target container name of the application pod
if experimentsDetails.TargetContainer == "" {
experimentsDetails.TargetContainer, err = common.GetTargetContainer(experimentsDetails.AppNS, targetPodList.Items[0].Name, clients)
if err != nil {
return errors.Errorf("unable to get the target container name, err: %v", err)
}
}

if experimentsDetails.EngineName != "" {
if err := common.SetHelperData(chaosDetails, clients); err != nil {
return err
}
}

experimentsDetails.IsTargetContainerProvided = (experimentsDetails.TargetContainer != "")
switch strings.ToLower(experimentsDetails.Sequence) {
case "serial":
if err = injectChaosInSerialMode(experimentsDetails, targetPodList, clients, chaosDetails, resultDetails, eventsDetails); err != nil {
Expand All @@ -84,7 +77,7 @@ func PrepareAndInjectChaos(experimentsDetails *experimentTypes.ExperimentDetails
func injectChaosInSerialMode(experimentsDetails *experimentTypes.ExperimentDetails, targetPodList apiv1.PodList, clients clients.ClientSets, chaosDetails *types.ChaosDetails, resultDetails *types.ResultDetails, eventsDetails *types.EventDetails) error {

labelSuffix := common.GetRunID()

var err error
// run the probes during chaos
if len(resultDetails.ProbeDetails) != 0 {
if err := probe.RunProbes(chaosDetails, clients, resultDetails, "DuringChaos", eventsDetails); err != nil {
Expand All @@ -95,6 +88,14 @@ func injectChaosInSerialMode(experimentsDetails *experimentTypes.ExperimentDetai
// creating the helper pod to perform DNS Chaos
for _, pod := range targetPodList.Items {

//Get the target container name of the application pod
if !experimentsDetails.IsTargetContainerProvided {
experimentsDetails.TargetContainer, err = common.GetTargetContainer(experimentsDetails.AppNS, pod.Name, clients)
if err != nil {
return errors.Errorf("unable to get the target container name, err: %v", err)
}
}

log.InfoWithValues("[Info]: Details of application under chaos injection", logrus.Fields{
"PodName": pod.Name,
"NodeName": pod.Spec.NodeName,
Expand Down Expand Up @@ -137,7 +138,7 @@ func injectChaosInSerialMode(experimentsDetails *experimentTypes.ExperimentDetai
func injectChaosInParallelMode(experimentsDetails *experimentTypes.ExperimentDetails, targetPodList apiv1.PodList, clients clients.ClientSets, chaosDetails *types.ChaosDetails, resultDetails *types.ResultDetails, eventsDetails *types.EventDetails) error {

labelSuffix := common.GetRunID()

var err error
// run the probes during chaos
if len(resultDetails.ProbeDetails) != 0 {
if err := probe.RunProbes(chaosDetails, clients, resultDetails, "DuringChaos", eventsDetails); err != nil {
Expand All @@ -148,6 +149,14 @@ func injectChaosInParallelMode(experimentsDetails *experimentTypes.ExperimentDet
// creating the helper pod to perform DNS Chaos
for _, pod := range targetPodList.Items {

//Get the target container name of the application pod
if !experimentsDetails.IsTargetContainerProvided {
experimentsDetails.TargetContainer, err = common.GetTargetContainer(experimentsDetails.AppNS, pod.Name, clients)
if err != nil {
return errors.Errorf("unable to get the target container name, err: %v", err)
}
}

log.InfoWithValues("[Info]: Details of application under chaos injection", logrus.Fields{
"PodName": pod.Name,
"NodeName": pod.Spec.NodeName,
Expand Down

0 comments on commit 5fb669f

Please sign in to comment.