|
| 1 | + |
| 2 | +import pkg.types.types as types |
| 3 | +import pkg.events.events as events |
| 4 | +from kubernetes import client, config |
| 5 | +import time |
| 6 | +import logging |
| 7 | +logging.basicConfig(format='time=%(asctime)s level=%(levelname)s msg=%(message)s', level=logging.INFO) |
| 8 | +import pkg.utils.common.common as common |
| 9 | +from pkg.utils.common.pods import Pods |
| 10 | +from datetime import datetime |
| 11 | +from pkg.status.application import Application |
| 12 | +import pkg.maths.maths as maths |
| 13 | +import os |
| 14 | + |
| 15 | +#PreparePodDelete contains the prepration steps before chaos injection |
| 16 | +def PreparePodDelete(experimentsDetails , resultDetails, eventsDetails, chaosDetails, clients): |
| 17 | + |
| 18 | + #Waiting for the ramp time before chaos injection |
| 19 | + if experimentsDetails.RampTime != 0 : |
| 20 | + logging.info("[Ramp]: Waiting for the %s ramp time before injecting chaos",(experimentsDetails.RampTime)) |
| 21 | + common.WaitForDuration(experimentsDetails.RampTime) |
| 22 | + |
| 23 | + if experimentsDetails.Sequence.lower() == "serial": |
| 24 | + err = injectChaosInSerialMode(experimentsDetails, chaosDetails, eventsDetails, resultDetails, clients) |
| 25 | + if err != None: |
| 26 | + return err |
| 27 | + elif experimentsDetails.Sequence.lower() == "parallel": |
| 28 | + err = injectChaosInParallelMode(experimentsDetails, chaosDetails, eventsDetails, resultDetails, clients) |
| 29 | + if err != None: |
| 30 | + return err |
| 31 | + else: |
| 32 | + logging.warning("[Warning]: %s sequence is not supported",(experimentsDetails.Sequence)) |
| 33 | + |
| 34 | + #Waiting for the ramp time after chaos injection |
| 35 | + if experimentsDetails.RampTime != 0 : |
| 36 | + logging.info("[Ramp]: Waiting for the %s ramp time after injecting chaos",(experimentsDetails.RampTime)) |
| 37 | + common.WaitForDuration(experimentsDetails.RampTime) |
| 38 | + return None |
| 39 | + |
| 40 | +# injectChaosInSerialMode delete the target application pods serial mode(one by one) |
| 41 | +def injectChaosInSerialMode(experimentsDetails , chaosDetails , eventsDetails , resultDetails, clients): |
| 42 | + |
| 43 | + status = Application() |
| 44 | + |
| 45 | + pods = Pods() |
| 46 | + GracePeriod = 0 |
| 47 | + |
| 48 | + #ChaosStartTimeStamp contains the start timestamp, when the chaos injection begin |
| 49 | + ChaosStartTimeStamp = datetime.now() |
| 50 | + duration = (datetime.now() - ChaosStartTimeStamp).seconds |
| 51 | + |
| 52 | + while duration < experimentsDetails.ChaosDuration: |
| 53 | + # Get the target pod details for the chaos execution |
| 54 | + # if the target pod is not defined it will derive the random target pod list using pod affected percentage |
| 55 | + if experimentsDetails.TargetPods == "" and chaosDetails.AppDetail.Label == "" : |
| 56 | + return logging.warning("[Warning]: Please provide one of the appLabel or TARGET_PODS") |
| 57 | + |
| 58 | + targetPodList, err = pods.GetPodList(experimentsDetails.TargetPods, experimentsDetails.PodsAffectedPerc, chaosDetails, clients) |
| 59 | + if err != None: |
| 60 | + return err |
| 61 | + |
| 62 | + podNames = [] |
| 63 | + for pod in targetPodList.items: |
| 64 | + podNames.append(pod.metadata.name) |
| 65 | + |
| 66 | + logging.info("[Info]: Target pods list, %s",(podNames)) |
| 67 | + |
| 68 | + if experimentsDetails.EngineName != "" : |
| 69 | + msg = "Injecting " + experimentsDetails.ExperimentName + " chaos on application pod" |
| 70 | + types.SetEngineEventAttributes(eventsDetails, types.ChaosInject, msg, "Normal", chaosDetails) |
| 71 | + events.GenerateEvents(eventsDetails, chaosDetails, "ChaosEngine", clients) |
| 72 | + |
| 73 | + #Deleting the application pod |
| 74 | + for pod in targetPodList.items : |
| 75 | + |
| 76 | + logging.info("[Info]: Killing the following pods, PodName : %s", pod.metadata.name) |
| 77 | + try: |
| 78 | + if experimentsDetails.Force == True: |
| 79 | + err = clients.clientk8s.delete_namespaced_pod(pod.metadata.name, experimentsDetails.AppNS, grace_period_seconds=GracePeriod) |
| 80 | + else: |
| 81 | + err = clients.clientk8s.delete_namespaced_pod(pod.metadata.name, experimentsDetails.AppNS) |
| 82 | + except Exception as e: |
| 83 | + return e |
| 84 | + |
| 85 | + if chaosDetails.Randomness == True : |
| 86 | + err = common.RandomInterval(experimentsDetails.ChaosInterval) |
| 87 | + if err != None: |
| 88 | + return err |
| 89 | + else: |
| 90 | + #Waiting for the chaos interval after chaos injection |
| 91 | + if experimentsDetails.ChaosInterval != "": |
| 92 | + logging.info("[Wait]: Wait for the chaos interval %s",(experimentsDetails.ChaosInterval)) |
| 93 | + waitTime = maths.atoi(experimentsDetails.ChaosInterval) |
| 94 | + common.WaitForDuration(waitTime) |
| 95 | + |
| 96 | + #Verify the status of pod after the chaos injection |
| 97 | + logging.info("[Status]: Verification for the recreation of application pod") |
| 98 | + err = status.CheckApplicationStatus(experimentsDetails.AppNS, experimentsDetails.AppLabel, experimentsDetails.Timeout, experimentsDetails.Delay,clients) |
| 99 | + if err != None: |
| 100 | + return err |
| 101 | + |
| 102 | + duration = (datetime.now() - ChaosStartTimeStamp).seconds |
| 103 | + |
| 104 | + logging.info("[Completion]: %s chaos is done",(experimentsDetails.ExperimentName)) |
| 105 | + |
| 106 | + return None |
| 107 | + |
| 108 | +# injectChaosInParallelMode delete the target application pods in parallel mode (all at once) |
| 109 | +def injectChaosInParallelMode(experimentsDetails , chaosDetails , eventsDetails , resultDetails, clients): |
| 110 | + |
| 111 | + status = Application() |
| 112 | + pods = Pods() |
| 113 | + |
| 114 | + GracePeriod = 0 |
| 115 | + |
| 116 | + #ChaosStartTimeStamp contains the start timestamp, when the chaos injection begin |
| 117 | + ChaosStartTimeStamp = datetime.now() |
| 118 | + duration = (datetime.now() - ChaosStartTimeStamp).seconds |
| 119 | + |
| 120 | + while duration < experimentsDetails.ChaosDuration: |
| 121 | + # Get the target pod details for the chaos execution |
| 122 | + # if the target pod is not defined it will derive the random target pod list using pod affected percentage |
| 123 | + if experimentsDetails.TargetPods == "" and chaosDetails.AppDetail.Label == "" : |
| 124 | + return logging.info("[Warning]: Please provide one of the appLabel or TARGET_PODS") |
| 125 | + |
| 126 | + targetPodList, err = pods.GetPodList(experimentsDetails.TargetPods, experimentsDetails.PodsAffectedPerc, chaosDetails, clients) |
| 127 | + if err != None: |
| 128 | + return err |
| 129 | + podNames = [] |
| 130 | + for pod in targetPodList.items: |
| 131 | + podNames.append(str(pod.metadata.name)) |
| 132 | + |
| 133 | + logging.info("[Info]: Target pods list for chaos, %s",(podNames)) |
| 134 | + |
| 135 | + if experimentsDetails.EngineName != "" : |
| 136 | + msg = "Injecting " + experimentsDetails.ExperimentName + " chaos on application pod" |
| 137 | + types.SetEngineEventAttributes(eventsDetails, types.ChaosInject, msg, "Normal", chaosDetails) |
| 138 | + events.GenerateEvents(eventsDetails, chaosDetails, "ChaosEngine",clients) |
| 139 | + |
| 140 | + #Deleting the application pod |
| 141 | + for pod in targetPodList.items: |
| 142 | + logging.info("[Info]: Killing the following pods, PodName : %s", pod.metadata.name) |
| 143 | + try: |
| 144 | + if experimentsDetails.Force == True: |
| 145 | + clients.clientk8s.delete_namespaced_pod(pod.metadata.name, experimentsDetails.AppNS, grace_period_seconds=GracePeriod) |
| 146 | + else: |
| 147 | + clients.clientk8s.delete_namespaced_pod(pod.metadata.name, experimentsDetails.AppNS) |
| 148 | + except Exception as err: |
| 149 | + return err |
| 150 | + |
| 151 | + if chaosDetails.Randomness == True: |
| 152 | + err = common.RandomInterval(experimentsDetails.ChaosInterval) |
| 153 | + if err != None: |
| 154 | + return err |
| 155 | + else: |
| 156 | + #Waiting for the chaos interval after chaos injection |
| 157 | + if experimentsDetails.ChaosInterval != "" : |
| 158 | + logging.info("[Wait]: Wait for the chaos interval %s", experimentsDetails.ChaosInterval) |
| 159 | + waitTime = maths.atoi(experimentsDetails.ChaosInterval) |
| 160 | + common.WaitForDuration(waitTime) |
| 161 | + |
| 162 | + #Verify the status of pod after the chaos injection |
| 163 | + logging.info("[Status]: Verification for the recreation of application pod") |
| 164 | + err = status.CheckApplicationStatus(experimentsDetails.AppNS, experimentsDetails.AppLabel, experimentsDetails.Timeout, experimentsDetails.Delay, clients) |
| 165 | + if err != None: |
| 166 | + return err |
| 167 | + duration = (datetime.now() - ChaosStartTimeStamp).seconds |
| 168 | + |
| 169 | + logging.info("[Completion]: %s chaos is done",(experimentsDetails.ExperimentName)) |
| 170 | + |
| 171 | + return None |
0 commit comments