|
| 1 | +package lib |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + clients "github.com/litmuschaos/litmus-go/pkg/clients" |
| 6 | + "github.com/litmuschaos/litmus-go/pkg/events" |
| 7 | + "github.com/litmuschaos/litmus-go/pkg/log" |
| 8 | + "github.com/litmuschaos/litmus-go/pkg/probe" |
| 9 | + experimentTypes "github.com/litmuschaos/litmus-go/pkg/{{ .Category }}/{{ .Name }}/types" |
| 10 | + "github.com/litmuschaos/litmus-go/pkg/status" |
| 11 | + "github.com/litmuschaos/litmus-go/pkg/types" |
| 12 | + "github.com/litmuschaos/litmus-go/pkg/utils/common" |
| 13 | + "github.com/pkg/errors" |
| 14 | + "github.com/sirupsen/logrus" |
| 15 | + corev1 "k8s.io/api/core/v1" |
| 16 | + v1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 17 | +) |
| 18 | + |
| 19 | +func experimentExecution(experimentsDetails *experimentTypes.ExperimentDetails, clients clients.ClientSets, resultDetails *types.ResultDetails, eventsDetails *types.EventDetails, chaosDetails *types.ChaosDetails) error { |
| 20 | + |
| 21 | + // Get the target pod details for the chaos execution |
| 22 | + // if the target pod is not defined it will derive the random target pod list using pod affected percentage |
| 23 | + targetPodList, err := common.GetPodList(experimentsDetails.TargetPods, experimentsDetails.PodsAffectedPerc, clients, chaosDetails) |
| 24 | + if err != nil { |
| 25 | + return err |
| 26 | + } |
| 27 | + |
| 28 | + podNames := []string{} |
| 29 | + for _, pod := range targetPodList.Items { |
| 30 | + podNames = append(podNames, pod.Name) |
| 31 | + } |
| 32 | + log.Infof("Target pods list for chaos, %v", podNames) |
| 33 | + |
| 34 | + //Get the target container name of the application pod |
| 35 | + if experimentsDetails.TargetContainer == "" { |
| 36 | + experimentsDetails.TargetContainer, err = common.GetTargetContainer(experimentsDetails.AppNS, targetPodList.Items[0].Name, clients) |
| 37 | + if err != nil { |
| 38 | + return errors.Errorf("unable to get the target container name, err: %v", err) |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + if experimentsDetails.EngineName != "" { |
| 43 | + if err := common.SetHelperData(chaosDetails, experimentsDetails.SetHelperData, clients); err != nil { |
| 44 | + return err |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + return runChaos(experimentsDetails, targetPodList, clients, resultDetails, eventsDetails, chaosDetails) |
| 49 | +} |
| 50 | + |
| 51 | +func runChaos(experimentsDetails *experimentTypes.ExperimentDetails, targetPodList corev1.PodList, clients clients.ClientSets, resultDetails *types.ResultDetails, eventsDetails *types.EventDetails, chaosDetails *types.ChaosDetails) error { |
| 52 | + if experimentsDetails.EngineName != "" { |
| 53 | + msg := "Injecting " + experimentsDetails.ExperimentName + " chaos on target pod" |
| 54 | + types.SetEngineEventAttributes(eventsDetails, types.ChaosInject, msg, "Normal", chaosDetails) |
| 55 | + events.GenerateEvents(eventsDetails, clients, chaosDetails, "ChaosEngine") |
| 56 | + } |
| 57 | + |
| 58 | + labelSuffix := common.GetRunID() |
| 59 | + |
| 60 | + // run the probes during chaos |
| 61 | + if len(resultDetails.ProbeDetails) != 0 { |
| 62 | + if err := probe.RunProbes(chaosDetails, clients, resultDetails, "DuringChaos", eventsDetails); err != nil { |
| 63 | + return err |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + // creating the helper pod to perform container kill chaos |
| 68 | + for _, pod := range targetPodList.Items { |
| 69 | + |
| 70 | + runID := common.GetRunID() |
| 71 | + |
| 72 | + log.InfoWithValues("[Info]: Details of application under chaos injection", logrus.Fields{ |
| 73 | + "Target Pod": pod.Name, |
| 74 | + "NodeName": pod.Spec.NodeName, |
| 75 | + "Target Container": experimentsDetails.TargetContainer, |
| 76 | + }) |
| 77 | + |
| 78 | + if err := createHelperPod(experimentsDetails, clients, chaosDetails, pod.Name, pod.Spec.NodeName, runID, labelSuffix); err != nil { |
| 79 | + return errors.Errorf("unable to create the helper pod, err: %v", err) |
| 80 | + } |
| 81 | + |
| 82 | + common.SetTargets(pod.Name, "targeted", "pod", chaosDetails) |
| 83 | + |
| 84 | + appLabel := "name=" + experimentsDetails.ExperimentName + "-helper-" + runID |
| 85 | + |
| 86 | + //checking the status of the helper pod, wait till the pod comes to running state else fail the experiment |
| 87 | + log.Info("[Status]: Checking the status of the helper pod") |
| 88 | + if err := status.CheckHelperStatus(experimentsDetails.ChaosNamespace, appLabel, experimentsDetails.Timeout, experimentsDetails.Delay, clients); err != nil { |
| 89 | + common.DeleteHelperPodBasedOnJobCleanupPolicy(experimentsDetails.ExperimentName+"-helper-"+runID, appLabel, chaosDetails, clients) |
| 90 | + return errors.Errorf("helper pod is not in running state, err: %v", err) |
| 91 | + } |
| 92 | + |
| 93 | + log.Infof("[Wait]: Waiting for the %vs chaos duration", experimentsDetails.ChaosDuration) |
| 94 | + common.WaitForDuration(experimentsDetails.ChaosDuration) |
| 95 | + |
| 96 | + //Deleting the helper pod |
| 97 | + log.Info("[Cleanup]: Deleting the helper pod") |
| 98 | + if err := common.DeletePod(experimentsDetails.ExperimentName+"-helper-"+runID, appLabel, experimentsDetails.ChaosNamespace, chaosDetails.Timeout, chaosDetails.Delay, clients); err != nil { |
| 99 | + return errors.Errorf("unable to delete the helper pod, err: %v", err) |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + return nil |
| 104 | +} |
| 105 | + |
| 106 | +//PrepareChaos contains the preparation steps before chaos injection |
| 107 | +func PrepareChaos(experimentsDetails *experimentTypes.ExperimentDetails, clients clients.ClientSets, resultDetails *types.ResultDetails, eventsDetails *types.EventDetails, chaosDetails *types.ChaosDetails) error { |
| 108 | + |
| 109 | + //Waiting for the ramp time before chaos injection |
| 110 | + if experimentsDetails.RampTime != 0 { |
| 111 | + log.Infof("[Ramp]: Waiting for the %vs ramp time before injecting chaos", experimentsDetails.RampTime) |
| 112 | + common.WaitForDuration(experimentsDetails.RampTime) |
| 113 | + } |
| 114 | + //Starting the CPU stress experiment |
| 115 | + if err := experimentExecution(experimentsDetails, clients, resultDetails, eventsDetails, chaosDetails);err != nil { |
| 116 | + return err |
| 117 | + } |
| 118 | + //Waiting for the ramp time after chaos injection |
| 119 | + if experimentsDetails.RampTime != 0 { |
| 120 | + log.Infof("[Ramp]: Waiting for the %vs ramp time after injecting chaos", experimentsDetails.RampTime) |
| 121 | + common.WaitForDuration(experimentsDetails.RampTime) |
| 122 | + } |
| 123 | + return nil |
| 124 | +} |
| 125 | + |
| 126 | +// createHelperPod derive the attributes for helper pod and create the helper pod |
| 127 | +func createHelperPod(experimentsDetails *experimentTypes.ExperimentDetails, clients clients.ClientSets, chaosDetails *types.ChaosDetails, appName, appNodeName, runID, labelSuffix string) error { |
| 128 | + |
| 129 | + helperPod := &corev1.Pod{ |
| 130 | + ObjectMeta: v1.ObjectMeta{ |
| 131 | + Name: experimentsDetails.ExperimentName + "-helper-" + runID, |
| 132 | + Namespace: experimentsDetails.ChaosNamespace, |
| 133 | + Labels: common.GetHelperLabels(chaosDetails.Labels, runID, labelSuffix, experimentsDetails.ExperimentName), |
| 134 | + Annotations: chaosDetails.Annotations, |
| 135 | + }, |
| 136 | + Spec: corev1.PodSpec{ |
| 137 | + RestartPolicy: corev1.RestartPolicyNever, |
| 138 | + ImagePullSecrets: chaosDetails.ImagePullSecrets, |
| 139 | + NodeName: appNodeName, |
| 140 | + Containers: []corev1.Container{ |
| 141 | + { |
| 142 | + Name: experimentsDetails.ExperimentName, |
| 143 | + Image: experimentsDetails.LIBImage, |
| 144 | + ImagePullPolicy: corev1.PullPolicy(experimentsDetails.LIBImagePullPolicy), |
| 145 | + Command: []string{ |
| 146 | + "/bin/bash", |
| 147 | + "-c", |
| 148 | + }, |
| 149 | + Args: []string{ |
| 150 | + "echo This is a sample pod", |
| 151 | + "sleep 10", |
| 152 | + }, |
| 153 | + Resources: chaosDetails.Resources, |
| 154 | + }, |
| 155 | + }, |
| 156 | + }, |
| 157 | + } |
| 158 | + |
| 159 | + _, err := clients.KubeClient.CoreV1().Pods(experimentsDetails.ChaosNamespace).Create(context.Background(), helperPod, v1.CreateOptions{}) |
| 160 | + return err |
| 161 | +} |
0 commit comments