Skip to content

Fixed deprecation issue and Bug fix for signal to inject multiple target pods together and fix for Target Pods ENV not Set #23

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 4, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions contribute/developer-guide/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ Follow the steps provided below to setup okteto & test the experiment execution.
This should take you to the bash prompt on the dev container into which the content of the litmus-python repo is loaded.

- Note :
- Avoid use `_` in names ex: sample_category(not sample-category)
- Replace `_` in chart manifest with `-` ex: sample_category to sample-category. Don't replace in directory name.
- Add packages routes for all the files which are generated from sdk in `setup.py` before creating image.
example :
```
Expand All @@ -238,7 +238,8 @@ Follow the steps provided below to setup okteto & test the experiment execution.
'experiments/sample_category/sample_exec_chaos',
'experiments/sample_category/sample_exec_chaos/experiment',
```
- Add `&` operator in the end of chaos-inject command `CHAOS_INJECT_COMMAND` example: `md5sum /dev/zero &`. As we are running chaos command as a background process in a separate thread.
- Add `&` operator at the end of chaos commands `CHAOS_INJECT_COMMAND` example: `md5sum /dev/zero &`.
As we are running chaos commands as a background process in a separate thread.
- Import main file it in bin/experiment/experiment.py and add case. example: line number 3 in experiment.py
- Then go to root(litmus-python) and run `python3 setup.py install`

Expand Down
17 changes: 8 additions & 9 deletions contribute/developer-guide/templates/chaoslib.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -53,35 +53,34 @@ def runChaos(experimentsDetails , targetPodList, clients , resultDetails, events
sign = Signals()
sign.timeSignal = False
sign.sigSignal = False
signal.alarm(experimentsDetails.ChaosDuration)


def signal_handler(sig, frame):
if sig == 14:
sign.timeSignal = True
else:
sign.sigSignal = True

for pod in targetPodList.items :
for pod in targetPodList.items:

if experimentsDetails.EngineName != "" :
msg = "Injecting " + experimentsDetails.ExperimentName + " chaos on application pod"
types.SetEngineEventAttributes(eventsDetails, types.ChaosInject, msg, "Normal", chaosDetails)
events.GenerateEvents(eventsDetails, chaosDetails, "ChaosEngine", clients)

logging.info("[Chaos]: The Target application details container : %s, Pod : %s", experimentsDetails.TargetContainer, pod.metadata.name)

signal.alarm(experimentsDetails.ChaosDuration)
# sendor thread is used to transmit signal notifications.
threading.Thread(target=injectChaos, args=(experimentsDetails, pod.metadata.name, clients)).start()
signal.signal(signal.SIGTERM, signal_handler)
signal.signal(signal.SIGINT, signal_handler)
signal.signal(signal.SIGALRM, signal_handler)

logging.info("[Chaos]:Waiting for: %s", experimentsDetails.ChaosDuration)
logging.info("[Chaos]: Waiting for: %s", experimentsDetails.ChaosDuration)

while True:
if sign.timeSignal:
logging.info("[Chaos]: Time is up for experiment: %s", experimentsDetails.ExperimentName)
c1 = None
sign.timeSignal = False
break
elif sign.sigSignal:
logging.info("[Chaos]: Revert Started")
Expand All @@ -91,11 +90,11 @@ def runChaos(experimentsDetails , targetPodList, clients , resultDetails, events
break
logging.info("[Chaos]: Revert Completed")
sys.exit(0)

err = killChaos(experimentsDetails, pod.metadata.name, clients)
if err != None:
return err

return None

#PrepareChaos contains the prepration steps before chaos injection
Expand Down
2 changes: 1 addition & 1 deletion pkg/maths/maths.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ def atoi(string):

#Adjustment contains rule of three for calculating an integer given another integer representing a percentage
def Adjustment(a, b):
return (a * b) / 100
return (a * b) / 100
2 changes: 1 addition & 1 deletion pkg/utils/annotation/annotation.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ def getRolloutName(targetPod, chaosDetails, clients):
for own in rsOwnerRef :
if own.kind == "ReplicaSet":
try:
rs = clients.clientsAppsV1.read_namespaced_replica_set(own.name, chaosDetails.AppDetail.Namespace)
rs = clients.clientApps.read_namespaced_replica_set(own.name, chaosDetails.AppDetail.Namespace)
except Exception as exp:
return "", exp

Expand Down
1 change: 0 additions & 1 deletion pkg/utils/client/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ def __init__(self, conf=None):
self.clientCoreV1 = client.CoreV1Api(conf)
self.clientDyn = dynamic.DynamicClient(api_client.ApiClient(configuration=conf))
self.clientApps = client.AppsV1Api(conf)
self.clientsAppsV1 = client.AppsV1beta1Api(conf)

# Config maintain configuration for in and out cluster
class Configuration(object):
Expand Down
100 changes: 44 additions & 56 deletions pkg/utils/common/pods.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def VerifyExistanceOfPods(self, namespace, pods, clients):
if err != None :
return False, err
if not isPodsAvailable:
return isPodsAvailable, None
return isPodsAvailable, ValueError("{} pod is not available in {} namespace".format(pod, namespace))

return True, None

Expand All @@ -37,25 +37,18 @@ def VerifyExistanceOfPods(self, namespace, pods, clients):
def GetPodList(self, targetPods, podAffPerc , chaosDetails, clients):

realpods = client.V1PodList
try:
podList = clients.clientCoreV1.list_namespaced_pod(chaosDetails.AppDetail.Namespace, label_selector=chaosDetails.AppDetail.Label)
except Exception as exp:
return client.V1PodList, exp
if len(podList.items) == 0:
return False, ValueError("Failed to find the pod with matching labels in {} namespace".format(chaosDetails.AppDetail.Namespace))

isPodsAvailable, err = self.VerifyExistanceOfPods(chaosDetails.AppDetail.Namespace, targetPods, clients)
if err != None:
return client.V1PodList, err

# getting the pod, if the target pods is defined
# else select a random target pod from the specified labels
if isPodsAvailable:
realpods, err = self.GetTargetPodsWhenTargetPodsENVSet(targetPods, chaosDetails, clients)
if err != None or len(realpods.items) == 0:
return client.V1PodList, err
else:
nonChaosPods = self.FilterNonChaosPods(podList, chaosDetails, clients)
nonChaosPods = self.FilterNonChaosPods(chaosDetails, clients)
realpods, err = self.GetTargetPodsWhenTargetPodsENVNotSet(podAffPerc, nonChaosPods, chaosDetails, clients)
if err != None or len(realpods.items) == 0:
return client.V1PodList, err
Expand All @@ -72,54 +65,56 @@ def CheckForAvailibiltyOfPod(self, namespace, name, clients):
try:
clients.clientCoreV1.read_namespaced_pod(name, namespace)
except Exception as err:
if not k8serror.K8serror().IsNotFound(err):
return False, err
elif k8serror.K8serror().IsNotFound(err):
if k8serror.K8serror().IsNotFound(err):
return False, None
else:
return False, err

return True, None

#FilterNonChaosPods remove the chaos pods(operator, runner) for the podList
# it filter when the applabels are not defined and it will select random pods from appns
def FilterNonChaosPods(self, podList, chaosDetails, clients):
if chaosDetails.AppDetail.Label == "":
nonChaosPods = []
# ignore chaos pods
for pod in podList.items:
if pod.metadata.labels.get("chaosUID") == "" and pod.metadata.labels.get("name") != "chaos-operator":
nonChaosPods.append(pod)
return client.V1PodList(items=nonChaosPods)
return podList

# GetTargetPodsWhenTargetPodsENVSet derive the specific target pods, if TARGET_PODS env is set
def GetTargetPodsWhenTargetPodsENVSet(self, targetPods, chaosDetails, clients):
def FilterNonChaosPods(self, chaosDetails, clients):
try:
podList = clients.clientCoreV1.list_namespaced_pod(chaosDetails.AppDetail.Namespace, label_selector=chaosDetails.AppDetail.Label)
except Exception as exp:
return client.V1PodList, exp

if len(podList.items) == 0 :
return client.V1PodList, ValueError("Failed to find the pods with matching labels in {} namespace".format(chaosDetails.AppDetail.Namespace))
if len(podList.items) == 0:
return False, ValueError("Failed to find the pod with matching labels in {} namespace".format(chaosDetails.AppDetail.Namespace))

nonChaosPods = []
# ignore chaos pods
for pod in podList.items:
if pod.metadata.labels.get("chaosUID") == None and pod.metadata.labels.get("name") != "chaos-operator":
nonChaosPods.append(pod)
return client.V1PodList(items=nonChaosPods)

# GetTargetPodsWhenTargetPodsENVSet derive the specific target pods, if TARGET_PODS env is set
def GetTargetPodsWhenTargetPodsENVSet(self, targetPods, chaosDetails, clients):

targetPodsList = targetPods.split(",")
realPodList = []
for pod in podList.items :
for podTarget in targetPodsList :

for targetPod in targetPodsList :
try:
pod = clients.clientCoreV1.read_namespaced_pod(targetPod, chaosDetails.AppDetail.Namespace)
except Exception as exp:
return client.V1PodList, exp

if chaosDetails.AppDetail.AnnotationCheck:
parentName, err = annotation.GetParentName(clients, pod, chaosDetails)
if err != None:
return client.V1PodList, err
if podTarget == pod.metadata.name :
if chaosDetails.AppDetail.AnnotationCheck:
isPodAnnotated, err = annotation.IsParentAnnotated(clients, parentName, chaosDetails)
if err != None :
return client.V1PodList, err

if not isPodAnnotated:
return client.V1PodList, ValueError("{} target pods are not annotated".format(targetPods))

isPodAnnotated, err = annotation.IsParentAnnotated(clients, parentName, chaosDetails)
if err != None :
return client.V1PodList, err

if not isPodAnnotated:
return client.V1PodList, ValueError("{} target pods are not annotated".format(targetPods))

realPodList.append(pod)

realPodList.append(pod)
logging.info("[Info]: chaos candidate of kind: %s, name: %s, namespace: %s", chaosDetails.AppDetail.Kind, parentName, chaosDetails.AppDetail.Namespace)

return client.V1PodList(items=realPodList), None

# GetTargetPodsWhenTargetPodsENVNotSet derives the random target pod list, if TARGET_PODS env is not set
Expand All @@ -128,32 +123,25 @@ def GetTargetPodsWhenTargetPodsENVNotSet(self, podAffPerc , nonChaosPods, chaosD
filteredPods = []
realPods = []
for pod in nonChaosPods.items:

parentName, err = annotation.GetParentName(clients, pod, chaosDetails)

if err != None:
return client.V1PodList, err

if chaosDetails.AppDetail.AnnotationCheck:
parentName, err = annotation.GetParentName(clients, pod, chaosDetails)
if err != None:
return client.V1PodList, err
isParentAnnotated, err = annotation.IsParentAnnotated(clients, parentName, chaosDetails)

if err != None:
return client.V1PodList, err
return client.V1PodList, err

if isParentAnnotated:
filteredPods.append(pod)
logging.info("[Info]: chaos candidate of kind: %s, name: %s, namespace: %s", chaosDetails.AppDetail.Kind, parentName, chaosDetails.AppDetail.Namespace)
else:
for pod in nonChaosPods.items:
filteredPods.append(pod)
logging.info("[Info]: chaos candidate of kind: %s, name: %s, namespace: %s", chaosDetails.AppDetail.Kind, parentName, chaosDetails.AppDetail.Namespace)

filteredPods.append(pod)

if len(filteredPods) == 0:
return client.V1PodList(items=filteredPods), ValueError("No target pod found")

newPodListLength = max(1, maths.Adjustment(podAffPerc, len(filteredPods)))
newPodListLength = max(1, maths.Adjustment(min(podAffPerc,100), len(filteredPods)))

# it will generate the random podlist
# it will generate the random podlist
# it starts from the random index and choose requirement no of pods next to that index in a circular way.
index = random.randint(0,len(filteredPods)-1)
for i in range(int(newPodListLength)):
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ dateparser==1.0.0
decorator==5.0.9
idna==2.10
Jinja2==2.11.3
kubernetes
kubernetes==17.17.0
protobuf==3.17.1
py==1.10.0
pyasn1==0.4.8
Expand Down
1 change: 0 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

import setuptools


def get_version_from_package() -> str:
"""
Read the package version from the source without importing it.
Expand Down