Skip to content

Commit 6d6f6cb

Browse files
committed
Enhac(exp): Adds support for non-default vpc for the az down experiment
Signed-off-by: uditgaurav <[email protected]>
1 parent 3074364 commit 6d6f6cb

File tree

6 files changed

+70
-57
lines changed

6 files changed

+70
-57
lines changed

.github/workflows/push.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,4 @@ jobs:
4141
push: true
4242
file: Dockerfile
4343
platforms: linux/amd64,linux/arm64
44-
tags: litmuschaos/py-runner:ci
44+
tags: litmuschaos/py-runner:ci

build/go-multiarch-build.sh

-24
This file was deleted.

chaosLib/litmus/aws_az_chaos/lib/aws_az_chaos.py

+21-12
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,16 @@ def injectChaosInSerialMode(experimentsDetails , chaosDetails , eventsDetails ,
5555
# Detaching the target zones from loa balancer
5656
for azone in targetZones:
5757

58-
logging.info("[Info]: Detaching the following zone(s), Zone Name : %s", azone)
59-
err = statusAws.detachAZfromLB(experimentsDetails, azone)
58+
logging.info("[Info]: Detaching the following zone, Zone Name %s", azone)
59+
targetSubnet, err = statusAws.getTargetSubnet(experimentsDetails, azone)
60+
subnetList = list(targetSubnet.split(" "))
6061
if err != None:
6162
return err
63+
logging.info("[Info]: Detaching the following subnet, %s", subnetList)
64+
err = statusAws.detachSubnet(experimentsDetails, subnetList)
65+
if err != None:
66+
return err
67+
6268
if chaosDetails.Randomness:
6369
err = common.RandomInterval(experimentsDetails.ChaosInterval)
6470
if err != None:
@@ -85,19 +91,17 @@ def injectChaosInSerialMode(experimentsDetails , chaosDetails , eventsDetails ,
8591
duration = (datetime.now() - ChaosStartTimeStamp).seconds
8692

8793
logging.info("[Completion]: %s chaos is done",(experimentsDetails.ExperimentName))
88-
8994
return None
9095

9196
# injectChaosInParallelMode disable the target available zone from loadbalancer in parallel mode (all at once)
9297
def injectChaosInParallelMode(experimentsDetails , chaosDetails , eventsDetails , resultDetails, clients, statusAws):
9398

94-
9599
#ChaosStartTimeStamp contains the start timestamp, when the chaos injection begin
96100
ChaosStartTimeStamp = datetime.now()
97101
duration = (datetime.now() - ChaosStartTimeStamp).seconds
98-
102+
subnet = []
103+
99104
while duration < experimentsDetails.ChaosDuration:
100-
101105
# Get the target available zone details for the chaos execution
102106
targetZones = experimentsDetails.LoadBalancerZones.split(",")
103107
logging.info("[Info]: Target available zone list, %s", targetZones)
@@ -107,12 +111,17 @@ def injectChaosInParallelMode(experimentsDetails , chaosDetails , eventsDetails
107111
types.SetEngineEventAttributes(eventsDetails, types.ChaosInject, msg, "Normal", chaosDetails)
108112
events.GenerateEvents(eventsDetails, chaosDetails, "ChaosEngine",clients)
109113

110-
# Detaching the target zones from loa balancer
114+
# Detaching the target zones from load balancer
111115
for azone in targetZones:
112-
logging.info("[Info]: Detaching the following zone(s), Zone Name %s", azone)
113-
err = statusAws.detachAZfromLB(experimentsDetails, azone)
116+
logging.info("[Info]: Detaching the following zone, Zone Name %s", azone)
117+
targetSubnet, err = statusAws.getTargetSubnet(experimentsDetails, azone)
118+
subnet.append(targetSubnet)
114119
if err != None:
115-
return err
120+
return err
121+
logging.info("[Info]: Detaching the following subnet(s), %s", subnet)
122+
err = statusAws.detachSubnet(experimentsDetails,subnet)
123+
if err != None:
124+
return err
116125

117126
if chaosDetails.Randomness:
118127
err = common.RandomInterval(experimentsDetails.ChaosInterval)
@@ -128,10 +137,10 @@ def injectChaosInParallelMode(experimentsDetails , chaosDetails , eventsDetails
128137
# Attaching the target available zone after the chaos injection
129138
logging.info("[Status]: Attach the available zone back to load balancer")
130139
for azone in targetZones:
131-
err = statusAws.attachAZtoLB(experimentsDetails, azone)
140+
err = statusAws.attachSubnet(experimentsDetails, subnet)
132141
if err != None:
133142
return err
134-
143+
135144
#Verify the status of available zone after the chaos injection
136145
logging.info("[Status]: Checking AWS load balancer's AZ status")
137146
err = statusAws.CheckAWSStatus(experimentsDetails)

pkg/aws_az/environment/environment.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ def GetENV(experimentDetails):
88
experimentDetails.ExperimentName = os.getenv("EXPERIMENT_NAME", "aws-az-chaos")
99
experimentDetails.ChaosNamespace = os.getenv("CHAOS_NAMESPACE", "")
1010
experimentDetails.EngineName = os.getenv("CHAOSENGINE", "")
11-
experimentDetails.ChaosDuration = maths.atoi(os.getenv("TOTAL_CHAOS_DURATION", ""))
12-
experimentDetails.ChaosInterval = os.getenv("CHAOS_INTERVAL", "")
11+
experimentDetails.ChaosDuration = maths.atoi(os.getenv("TOTAL_CHAOS_DURATION", "60"))
12+
experimentDetails.ChaosInterval = os.getenv("CHAOS_INTERVAL", "30")
1313
experimentDetails.RampTime = maths.atoi(os.getenv("RAMP_TIME", ""))
1414
experimentDetails.ChaosLib = os.getenv("LIB", "litmus")
1515
experimentDetails.ChaosUID = os.getenv("CHAOS_UID", "")
@@ -22,7 +22,7 @@ def GetENV(experimentDetails):
2222
experimentDetails.LoadBalancerName = os.getenv("LOAD_BALANCER_NAME", "")
2323
experimentDetails.LoadBalancerZones = os.getenv("LOAD_BALANCER_ZONES", "")
2424
experimentDetails.LoadBalancerNameARN = os.getenv("LOAD_BALANCERNAME_ARN", "na")
25-
25+
2626
#InitialiseChaosVariables initialise all the global variables
2727
def InitialiseChaosVariables(chaosDetails, experimentDetails):
2828
appDetails = types.AppDetails()

pkg/aws_az/types/types.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
class ExperimentDetails(object):
33
def __init__(self, ExperimentName=None, EngineName=None, ChaosDuration=None, ChaosInterval=None, RampTime=None, Force=None, ChaosLib=None,
44
AWSZones=None, AWSRegion=None, AppNS=None, AppLabel=None, AppKind=None, InstanceID=None, ChaosNamespace=None, ChaosPodName=None, Timeout=None,
5-
Delay=None, LoadBalancerName= None, LIBImagePullPolicy=None, LoadBalancerNameARN=None, LoadBalancerZones=None, UID=None):
5+
Delay=None, LoadBalancerName= None, LIBImagePullPolicy=None, LoadBalancerNameARN=None, LoadBalancerZones=None, UID=None, VPCType=None):
66
self.ExperimentName = ExperimentName
77
self.EngineName = EngineName
88
self.ChaosDuration = ChaosDuration
@@ -23,4 +23,3 @@ def __init__(self, ExperimentName=None, EngineName=None, ChaosDuration=None, Cha
2323
self.LoadBalancerName = LoadBalancerName
2424
self.LoadBalancerZones = LoadBalancerZones
2525
self.LoadBalancerNameARN = LoadBalancerNameARN
26-

pkg/aws_status/status.py

+44-15
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from distutils.log import error
2+
from re import subn
13
import boto3, logging
24
import pkg.utils.client.client as client
35

@@ -20,29 +22,56 @@ def CheckAWSStatus(self, experimentsDetails):
2022
return ValueError(exp)
2123
logging.info("[Info]: LoadBalancer and Availablity of zone has been checked")
2224

23-
# detachAZfromLB detaching availablity zone from load balancer
24-
def detachAZfromLB(self, experimentsDetails, zone):
25-
self.clients = client.AWSClient().clientElb
25+
def getSubnetFromVPC(self, experimentsDetails):
26+
client = boto3.client('elb')
2627
try:
27-
self.clients.disable_availability_zones_for_load_balancer(
28-
LoadBalancerName=experimentsDetails.LoadBalancerName,
29-
AvailabilityZones=[
30-
zone,
28+
response = client.describe_load_balancers(
29+
LoadBalancerNames=[
30+
experimentsDetails.LoadBalancerName,
3131
]
3232
)
33+
return (response['LoadBalancerDescriptions'][0]['Subnets'])
34+
except (self.clients.exceptions.AccessPointNotFoundException, self.clients.exceptions.InvalidConfigurationRequestException) as exp:
35+
return ValueError(exp)
3336

37+
def getTargetSubnet(self, experimentsDetails, zone):
38+
client = boto3.client('ec2')
39+
try:
40+
lst=self.getSubnetFromVPC(experimentsDetails)
41+
i=0
42+
for i in range(len(lst)):
43+
response = client.describe_subnets(
44+
SubnetIds=[
45+
lst[i],
46+
],
47+
)
48+
if(response['Subnets'][0]['AvailabilityZone']) == zone:
49+
return lst[i], None
50+
except (self.clients.exceptions.AccessPointNotFoundException, self.clients.exceptions.InvalidConfigurationRequestException) as exp:
51+
return lst[i], ValueError(exp)
52+
53+
54+
def detachSubnet(self, experimentsDetails, subnet):
55+
client = boto3.client('elb')
56+
try:
57+
response = client.detach_load_balancer_from_subnets(
58+
LoadBalancerName=experimentsDetails.LoadBalancerName,
59+
Subnets=subnet
60+
)
61+
if (response['ResponseMetadata']['HTTPStatusCode']) != "200":
62+
ValueError("[Error]: Fail to detach the target subnet %s", subnet)
3463
except (self.clients.exceptions.AccessPointNotFoundException, self.clients.exceptions.InvalidConfigurationRequestException) as exp:
3564
return ValueError(exp)
36-
37-
# attachAZtoLB attaching availablity zone from load balancer
38-
def attachAZtoLB(self, experimentsDetails, zone):
39-
self.clients = client.AWSClient().clientElb
65+
66+
def attachSubnet(self, experimentsDetails, subnet):
67+
client = boto3.client('elb')
4068
try:
41-
self.clients.enable_availability_zones_for_load_balancer(
69+
response = client.attach_load_balancer_to_subnets(
4270
LoadBalancerName=experimentsDetails.LoadBalancerName,
43-
AvailabilityZones=[
44-
zone,
45-
]
71+
Subnets=subnet
4672
)
73+
if (response['ResponseMetadata']['HTTPStatusCode']) != "200":
74+
ValueError("[Error]: Fail to attach the target subnet %s", subnet)
4775
except (self.clients.exceptions.AccessPointNotFoundException, self.clients.exceptions.InvalidConfigurationRequestException) as exp:
4876
return ValueError(exp)
77+

0 commit comments

Comments
 (0)