|
| 1 | +package experiment |
| 2 | + |
| 3 | +import ( |
| 4 | + "os" |
| 5 | + |
| 6 | + "github.com/litmuschaos/chaos-operator/api/litmuschaos/v1alpha1" |
| 7 | + litmusLIB "github.com/litmuschaos/litmus-go/chaoslib/litmus/k6-loadgen/lib" |
| 8 | + clients "github.com/litmuschaos/litmus-go/pkg/clients" |
| 9 | + "github.com/litmuschaos/litmus-go/pkg/events" |
| 10 | + experimentEnv "github.com/litmuschaos/litmus-go/pkg/load/k6-loadgen/environment" |
| 11 | + experimentTypes "github.com/litmuschaos/litmus-go/pkg/load/k6-loadgen/types" |
| 12 | + "github.com/litmuschaos/litmus-go/pkg/log" |
| 13 | + "github.com/litmuschaos/litmus-go/pkg/probe" |
| 14 | + "github.com/litmuschaos/litmus-go/pkg/result" |
| 15 | + "github.com/litmuschaos/litmus-go/pkg/status" |
| 16 | + "github.com/litmuschaos/litmus-go/pkg/types" |
| 17 | + "github.com/litmuschaos/litmus-go/pkg/utils/common" |
| 18 | + "github.com/sirupsen/logrus" |
| 19 | +) |
| 20 | + |
| 21 | +// Experiment contains steps to inject chaos |
| 22 | +func Experiment(clients clients.ClientSets) { |
| 23 | + |
| 24 | + experimentsDetails := experimentTypes.ExperimentDetails{} |
| 25 | + resultDetails := types.ResultDetails{} |
| 26 | + eventsDetails := types.EventDetails{} |
| 27 | + chaosDetails := types.ChaosDetails{} |
| 28 | + |
| 29 | + //Fetching all the ENV passed from the runner pod |
| 30 | + log.Infof("[PreReq]: Getting the ENV for the %v experiment", os.Getenv("EXPERIMENT_NAME")) |
| 31 | + experimentEnv.GetENV(&experimentsDetails) |
| 32 | + |
| 33 | + // Initialize the chaos attributes |
| 34 | + types.InitialiseChaosVariables(&chaosDetails) |
| 35 | + |
| 36 | + // Initialize Chaos Result Parameters |
| 37 | + types.SetResultAttributes(&resultDetails, chaosDetails) |
| 38 | + |
| 39 | + if experimentsDetails.EngineName != "" { |
| 40 | + // Get values from chaosengine. Bail out upon error, as we haven't entered exp business logic yet |
| 41 | + if err := types.GetValuesFromChaosEngine(&chaosDetails, clients, &resultDetails); err != nil { |
| 42 | + log.Errorf("Unable to initialize the probes, err: %v", err) |
| 43 | + return |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + //Updating the chaos result in the beginning of experiment |
| 48 | + log.Infof("[PreReq]: Updating the chaos result of %v experiment (SOT)", experimentsDetails.ExperimentName) |
| 49 | + if err := result.ChaosResult(&chaosDetails, clients, &resultDetails, "SOT"); err != nil { |
| 50 | + log.Errorf("Unable to Create the Chaos Result, err: %v", err) |
| 51 | + result.RecordAfterFailure(&chaosDetails, &resultDetails, err, clients, &eventsDetails) |
| 52 | + return |
| 53 | + } |
| 54 | + |
| 55 | + // Set the chaos result uid |
| 56 | + result.SetResultUID(&resultDetails, clients, &chaosDetails) |
| 57 | + |
| 58 | + // generating the event in chaosresult to marked the verdict as awaited |
| 59 | + msg := "experiment: " + experimentsDetails.ExperimentName + ", Result: Awaited" |
| 60 | + types.SetResultEventAttributes(&eventsDetails, types.AwaitedVerdict, msg, "Normal", &resultDetails) |
| 61 | + events.GenerateEvents(&eventsDetails, clients, &chaosDetails, "ChaosResult") |
| 62 | + |
| 63 | + //DISPLAY THE APP INFORMATION |
| 64 | + log.InfoWithValues("[Info]: The application information is as follows", logrus.Fields{ |
| 65 | + "Namespace": experimentsDetails.AppNS, |
| 66 | + "Label": experimentsDetails.AppLabel, |
| 67 | + "Chaos Duration": experimentsDetails.ChaosDuration, |
| 68 | + }) |
| 69 | + |
| 70 | + // Calling AbortWatcher go routine, it will continuously watch for the abort signal and generate the required events and result |
| 71 | + go common.AbortWatcher(experimentsDetails.ExperimentName, clients, &resultDetails, &chaosDetails, &eventsDetails) |
| 72 | + |
| 73 | + //PRE-CHAOS APPLICATION STATUS CHECK |
| 74 | + if chaosDetails.DefaultHealthCheck { |
| 75 | + log.Info("[Status]: Verify that the AUT (Application Under Test) is running (pre-chaos)") |
| 76 | + if err := status.AUTStatusCheck(clients, &chaosDetails); err != nil { |
| 77 | + log.Errorf("Application status check failed, err: %v", err) |
| 78 | + types.SetEngineEventAttributes(&eventsDetails, types.PreChaosCheck, "AUT: Not Running", "Warning", &chaosDetails) |
| 79 | + events.GenerateEvents(&eventsDetails, clients, &chaosDetails, "ChaosEngine") |
| 80 | + result.RecordAfterFailure(&chaosDetails, &resultDetails, err, clients, &eventsDetails) |
| 81 | + return |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + if experimentsDetails.EngineName != "" { |
| 86 | + // marking AUT as running, as we already checked the status of application under test |
| 87 | + msg := "AUT: Running" |
| 88 | + |
| 89 | + // run the probes in the pre-chaos check |
| 90 | + if len(resultDetails.ProbeDetails) != 0 { |
| 91 | + |
| 92 | + if err := probe.RunProbes(&chaosDetails, clients, &resultDetails, "PreChaos", &eventsDetails); err != nil { |
| 93 | + log.Errorf("Probe Failed, err: %v", err) |
| 94 | + msg := "AUT: Running, Probes: Unsuccessful" |
| 95 | + types.SetEngineEventAttributes(&eventsDetails, types.PreChaosCheck, msg, "Warning", &chaosDetails) |
| 96 | + events.GenerateEvents(&eventsDetails, clients, &chaosDetails, "ChaosEngine") |
| 97 | + result.RecordAfterFailure(&chaosDetails, &resultDetails, err, clients, &eventsDetails) |
| 98 | + return |
| 99 | + } |
| 100 | + msg = "AUT: Running, Probes: Successful" |
| 101 | + } |
| 102 | + // generating the events for the pre-chaos check |
| 103 | + types.SetEngineEventAttributes(&eventsDetails, types.PreChaosCheck, msg, "Normal", &chaosDetails) |
| 104 | + events.GenerateEvents(&eventsDetails, clients, &chaosDetails, "ChaosEngine") |
| 105 | + } |
| 106 | + chaosDetails.Phase = types.ChaosInjectPhase |
| 107 | + if err := litmusLIB.PrepareChaos(&experimentsDetails, clients, &resultDetails, &eventsDetails, &chaosDetails); err != nil { |
| 108 | + log.Errorf("Chaos injection failed, err: %v", err) |
| 109 | + result.RecordAfterFailure(&chaosDetails, &resultDetails, err, clients, &eventsDetails) |
| 110 | + return |
| 111 | + } |
| 112 | + log.Infof("[Confirmation]: %v chaos has been injected successfully", experimentsDetails.ExperimentName) |
| 113 | + resultDetails.Verdict = v1alpha1.ResultVerdictPassed |
| 114 | + chaosDetails.Phase = types.PostChaosPhase |
| 115 | + |
| 116 | + //POST-CHAOS APPLICATION STATUS CHECK |
| 117 | + if chaosDetails.DefaultHealthCheck { |
| 118 | + log.Info("[Status]: Verify that the AUT (Application Under Test) is running (post-chaos)") |
| 119 | + if err := status.AUTStatusCheck(clients, &chaosDetails); err != nil { |
| 120 | + log.Errorf("Application status check failed, err: %v", err) |
| 121 | + types.SetEngineEventAttributes(&eventsDetails, types.PostChaosCheck, "AUT: Not Running", "Warning", &chaosDetails) |
| 122 | + events.GenerateEvents(&eventsDetails, clients, &chaosDetails, "ChaosEngine") |
| 123 | + result.RecordAfterFailure(&chaosDetails, &resultDetails, err, clients, &eventsDetails) |
| 124 | + return |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + if experimentsDetails.EngineName != "" { |
| 129 | + // marking AUT as running, as we already checked the status of application under test |
| 130 | + msg := "AUT: Running" |
| 131 | + |
| 132 | + // run the probes in the post-chaos check |
| 133 | + if len(resultDetails.ProbeDetails) != 0 { |
| 134 | + if err := probe.RunProbes(&chaosDetails, clients, &resultDetails, "PostChaos", &eventsDetails); err != nil { |
| 135 | + log.Errorf("Probes Failed, err: %v", err) |
| 136 | + msg := "AUT: Running, Probes: Unsuccessful" |
| 137 | + types.SetEngineEventAttributes(&eventsDetails, types.PostChaosCheck, msg, "Warning", &chaosDetails) |
| 138 | + events.GenerateEvents(&eventsDetails, clients, &chaosDetails, "ChaosEngine") |
| 139 | + result.RecordAfterFailure(&chaosDetails, &resultDetails, err, clients, &eventsDetails) |
| 140 | + return |
| 141 | + } |
| 142 | + msg = "AUT: Running, Probes: Successful" |
| 143 | + } |
| 144 | + |
| 145 | + // generating post chaos event |
| 146 | + types.SetEngineEventAttributes(&eventsDetails, types.PostChaosCheck, msg, "Normal", &chaosDetails) |
| 147 | + events.GenerateEvents(&eventsDetails, clients, &chaosDetails, "ChaosEngine") |
| 148 | + } |
| 149 | + |
| 150 | + //Updating the chaosResult in the end of experiment |
| 151 | + log.Infof("[The End]: Updating the chaos result of %v experiment (EOT)", experimentsDetails.ExperimentName) |
| 152 | + if err := result.ChaosResult(&chaosDetails, clients, &resultDetails, "EOT"); err != nil { |
| 153 | + log.Errorf("Unable to Update the Chaos Result, err: %v", err) |
| 154 | + result.RecordAfterFailure(&chaosDetails, &resultDetails, err, clients, &eventsDetails) |
| 155 | + return |
| 156 | + } |
| 157 | + |
| 158 | + // generating the event in chaosresult to mark the verdict as pass/fail |
| 159 | + msg = "experiment: " + experimentsDetails.ExperimentName + ", Result: " + string(resultDetails.Verdict) |
| 160 | + reason, eventType := types.GetChaosResultVerdictEvent(resultDetails.Verdict) |
| 161 | + types.SetResultEventAttributes(&eventsDetails, reason, msg, eventType, &resultDetails) |
| 162 | + events.GenerateEvents(&eventsDetails, clients, &chaosDetails, "ChaosResult") |
| 163 | + |
| 164 | + if experimentsDetails.EngineName != "" { |
| 165 | + msg := experimentsDetails.ExperimentName + " experiment has been " + string(resultDetails.Verdict) + "ed" |
| 166 | + types.SetEngineEventAttributes(&eventsDetails, types.Summary, msg, "Normal", &chaosDetails) |
| 167 | + events.GenerateEvents(&eventsDetails, clients, &chaosDetails, "ChaosEngine") |
| 168 | + } |
| 169 | +} |
0 commit comments