diff --git a/pkg/probe/comparator/integer.go b/pkg/probe/comparator/integer.go index 81cc7fdc1..e1a96e0f4 100644 --- a/pkg/probe/comparator/integer.go +++ b/pkg/probe/comparator/integer.go @@ -17,9 +17,7 @@ func (model Model) CompareInt(errorCode cerrors.ErrorType) error { obj := Integer{} obj.setValues(reflect.ValueOf(model.a).String(), reflect.ValueOf(model.b).String()) - if model.rc == 1 { - log.Infof("[Probe]: {Actual value: %v}, {Expected value: %v}, {Operator: %v}", obj.a, obj.b, model.operator) - } + log.Infof("[Probe]: {Actual value: %v}, {Expected value: %v}, {Operator: %v}", obj.a, obj.b, model.operator) switch model.operator { case ">=": diff --git a/pkg/probe/httpprobe.go b/pkg/probe/httpprobe.go index cbac3542b..6f730f10f 100644 --- a/pkg/probe/httpprobe.go +++ b/pkg/probe/httpprobe.go @@ -222,24 +222,31 @@ func triggerContinuousHTTPProbe(probe v1alpha1.ProbeAttributes, clients clients. // it triggers the http probe for the entire duration of chaos and it fails, if any error encounter // it marked the error for the probes, if any + loop: for { - err = triggerHTTPProbe(probe, chaosresult) - // record the error inside the probeDetails, we are maintaining a dedicated variable for the err, inside probeDetails - if err != nil { - err = addProbePhase(err, string(chaosDetails.Phase)) - for index := range chaosresult.ProbeDetails { - if chaosresult.ProbeDetails[index].Name == probe.Name { - chaosresult.ProbeDetails[index].IsProbeFailedWithError = err - chaosresult.ProbeDetails[index].Status.Description = getDescription(err) - log.Errorf("The %v http probe has been Failed, err: %v", probe.Name, err) - isExperimentFailed = true - break loop + select { + case <-chaosDetails.ProbeContext.Ctx.Done(): + log.Info("Chaos Execution completed. Stopping Probes") + break + default: + err = triggerHTTPProbe(probe, chaosresult) + // record the error inside the probeDetails, we are maintaining a dedicated variable for the err, inside probeDetails + if err != nil { + err = addProbePhase(err, string(chaosDetails.Phase)) + for index := range chaosresult.ProbeDetails { + if chaosresult.ProbeDetails[index].Name == probe.Name { + chaosresult.ProbeDetails[index].IsProbeFailedWithError = err + chaosresult.ProbeDetails[index].Status.Description = getDescription(err) + log.Errorf("The %v http probe has been Failed, err: %v", probe.Name, err) + isExperimentFailed = true + break loop + } } } + // waiting for the probe polling interval + time.Sleep(probeTimeout.ProbePollingInterval) } - // waiting for the probe polling interval - time.Sleep(probeTimeout.ProbePollingInterval) } // if experiment fails and stopOnfailure is provided as true then it will patch the chaosengine for abort // if experiment fails but stopOnfailure is provided as false then it will continue the execution @@ -292,7 +299,9 @@ func preChaosHTTPProbe(probe v1alpha1.ProbeAttributes, resultDetails *types.Resu "Mode": probe.Mode, "Phase": "PreChaos", }) + go triggerContinuousHTTPProbe(probe, clients, resultDetails, chaosDetails) + } return nil } diff --git a/pkg/probe/probe.go b/pkg/probe/probe.go index d3e1ac41c..605a2f94b 100644 --- a/pkg/probe/probe.go +++ b/pkg/probe/probe.go @@ -55,6 +55,8 @@ func RunProbes(chaosDetails *types.ChaosDetails, clients clients.ClientSets, res // it first evaluate the onchaos and continuous modes then it evaluates the other modes // as onchaos and continuous probes are already completed var probeError []string + // call cancel function from chaosDetails context + chaosDetails.ProbeContext.CancelFunc() for _, probe := range probes { // evaluate continuous and onchaos probes switch strings.ToLower(probe.Mode) { diff --git a/pkg/types/types.go b/pkg/types/types.go index 79a0657b8..c3cee2a23 100644 --- a/pkg/types/types.go +++ b/pkg/types/types.go @@ -132,6 +132,7 @@ type ChaosDetails struct { ImagePullSecrets []corev1.LocalObjectReference Labels map[string]string Phase ExperimentPhase + ProbeContext ProbeContext SideCar []SideCar } @@ -150,6 +151,11 @@ type ParentResource struct { Namespace string } +type ProbeContext struct { + Ctx context.Context + CancelFunc context.CancelFunc +} + // AppDetails contains all the application related envs type AppDetails struct { Namespace string @@ -190,7 +196,7 @@ func parse(val string) []string { return strings.Split(val, ",") } -//InitialiseChaosVariables initialise all the global variables +// InitialiseChaosVariables initialise all the global variables func InitialiseChaosVariables(chaosDetails *ChaosDetails) { targets := Getenv("TARGETS", "") chaosDetails.AppDetail = GetTargets(strings.TrimSpace(targets)) @@ -213,7 +219,7 @@ func InitialiseChaosVariables(chaosDetails *ChaosDetails) { chaosDetails.Phase = PreChaosPhase } -//SetResultAttributes initialise all the chaos result ENV +// SetResultAttributes initialise all the chaos result ENV func SetResultAttributes(resultDetails *ResultDetails, chaosDetails ChaosDetails) { resultDetails.Verdict = "Awaited" resultDetails.Phase = "Running" @@ -230,7 +236,7 @@ func SetResultAttributes(resultDetails *ResultDetails, chaosDetails ChaosDetails } -//SetResultAfterCompletion set all the chaos result ENV in the EOT +// SetResultAfterCompletion set all the chaos result ENV in the EOT func SetResultAfterCompletion(resultDetails *ResultDetails, verdict v1alpha1.ResultVerdict, phase v1alpha1.ResultPhase, failStep string, errorCode cerrors.ErrorType) { resultDetails.Verdict = verdict resultDetails.Phase = phase @@ -242,7 +248,7 @@ func SetResultAfterCompletion(resultDetails *ResultDetails, verdict v1alpha1.Res } } -//SetEngineEventAttributes initialise attributes for event generation in chaos engine +// SetEngineEventAttributes initialise attributes for event generation in chaos engine func SetEngineEventAttributes(eventsDetails *EventDetails, Reason, Message, Type string, chaosDetails *ChaosDetails) { eventsDetails.Reason = Reason @@ -253,7 +259,7 @@ func SetEngineEventAttributes(eventsDetails *EventDetails, Reason, Message, Type } -//SetResultEventAttributes initialise attributes for event generation in chaos result +// SetResultEventAttributes initialise attributes for event generation in chaos result func SetResultEventAttributes(eventsDetails *EventDetails, Reason, Message, Type string, resultDetails *ResultDetails) { eventsDetails.Reason = Reason