Skip to content

Commit 3d4273c

Browse files
lleizerovichREDMOND\lleizerovich
and
REDMOND\lleizerovich
authored
added exception handling in cases of no helm on host, or attacker container existing (#17)
Co-authored-by: REDMOND\lleizerovich <lleizerovich@microsoft.com>
1 parent 550958e commit 3d4273c

File tree

1 file changed

+8
-3
lines changed

1 file changed

+8
-3
lines changed

simulation.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,18 @@ def run_scenario(scenario):
3838
# Install or update the helm chart
3939
print("Helm - creating simulation objects in namespace mdc-simulation...")
4040
try:
41+
existing_pod=subprocess.run(["kubectl", "get", "pod", ATTACKER, "-n", NAMESPACE], capture_output=True, text=True)
42+
if existing_pod.stdout:
43+
subprocess.run(["kubectl", "delete", "pod", ATTACKER, "-n", NAMESPACE], check=True, capture_output=True)
4144
subprocess.run(["helm", "upgrade", "--install", HELM_RELEASE, HELM_CHART,
4245
"--set", f"env.name={NAMESPACE}", "--set", f"scenario={scenario}"],
4346
check=True, capture_output=True)
44-
except subprocess.CalledProcessError as e:
45-
print("Failed to create Helm chart. Exiting")
46-
raise subprocess.CalledProcessError(returncode=e.returncode, cmd=e.cmd)
4747
except FileNotFoundError:
4848
print("Can't find Helm. Exiting")
4949
raise FileNotFoundError
50+
except subprocess.CalledProcessError as e:
51+
print("Failed to create Helm chart. Exiting")
52+
raise subprocess.CalledProcessError(returncode=e.returncode, cmd=e.cmd)
5053

5154
print("Creating resources...")
5255
attacker_status = subprocess.run(["kubectl", "get", "pod", ATTACKER, "-n", NAMESPACE, "-o",
@@ -99,6 +102,8 @@ def start_simulation():
99102

100103
try:
101104
run_scenario(SCENARIOS[choise-1])
105+
except FileNotFoundError:
106+
return
102107
except Exception:
103108
release_status = subprocess.run(["helm", "status", HELM_RELEASE], capture_output=True)
104109
if release_status.returncode == 0:

0 commit comments

Comments
 (0)