|
5 | 5 |
|
6 | 6 |
|
7 | 7 | 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")) |
13 | 21 | for i, container in enumerate(current["spec"]["template"]["spec"]["containers"]):
|
14 | 22 | if container["name"] == "cluster-autoscaler":
|
15 | 23 | command = container["command"]
|
@@ -46,8 +54,12 @@ def main():
|
46 | 54 | args = parser.parse_args()
|
47 | 55 |
|
48 | 56 | 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) |
51 | 63 |
|
52 | 64 | if __name__ == "__main__":
|
53 | 65 | main()
|
0 commit comments