Skip to content

Commit ee11ede

Browse files
committed
add exception handling in toggle-scaledown.py
1 parent a2f51a0 commit ee11ede

File tree

1 file changed

+19
-7
lines changed

1 file changed

+19
-7
lines changed

test/e2e/toggle-scaledown.py

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,19 @@
55

66

77
def toggle_scaledown(enabled):
8-
current = json.loads(
9-
subprocess.check_output(
10-
["kubectl", "get", "daemonset", "-o", "json", "-n", "kube-system", "kube-cluster-autoscaler"]
11-
).decode("utf-8")
12-
)
8+
# Try to retrieve daemonset/kube-cluster-autoscalerfrom the e2e cluster
9+
cmd_output = ""
10+
try:
11+
cmd_output = subprocess.check_output(
12+
["kubectl", "get", "daemonset", "-o", "json", "-n", "kube-system", "kube-cluster-autoscaler"],
13+
stderr=subprocess.STDOUT)
14+
except subprocess.CalledProcessError as e:
15+
# This happens when a cluster has been cleaned up, so we should handle it gracefully
16+
if "no such host" in e.output.decode("utf-8"):
17+
print("Failed to reach the API server, is the cluster running?")
18+
raise e
19+
20+
current = json.loads(cmd_output.decode("utf-8"))
1321
for i, container in enumerate(current["spec"]["template"]["spec"]["containers"]):
1422
if container["name"] == "cluster-autoscaler":
1523
command = container["command"]
@@ -46,8 +54,12 @@ def main():
4654
args = parser.parse_args()
4755

4856
enabled = args.action == "enable"
49-
toggle_scaledown(enabled)
50-
57+
58+
try:
59+
toggle_scaledown(enabled)
60+
except subprocess.CalledProcessError as e:
61+
print("Failed to toggle scale-down.")
62+
exit(1)
5163

5264
if __name__ == "__main__":
5365
main()

0 commit comments

Comments
 (0)