From 72fc3e66d822b6b82e2da9b97c632c5e34a1e419 Mon Sep 17 00:00:00 2001 From: Saqib Perwaiz Date: Fri, 17 May 2024 15:35:46 +0530 Subject: [PATCH] INFRA-3277 | Saqib | portal maxReplica migration script --- scripts/update-maxReplica-count.py | 73 ++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 scripts/update-maxReplica-count.py diff --git a/scripts/update-maxReplica-count.py b/scripts/update-maxReplica-count.py new file mode 100644 index 00000000..998369a4 --- /dev/null +++ b/scripts/update-maxReplica-count.py @@ -0,0 +1,73 @@ +import requests +import time + +url = "" +s = requests.Session() +cookie = "" +x_xsrf_token = "" +s.headers.update({"cookie": cookie, "x-xsrf-token": x_xsrf_token}) +list_manifest_path = "/api/manifest/list" +r = s.get(url + list_manifest_path) +manifest_list = r.json() +env = {"qa", "dev"} +env_manifest_list = [ + manifest for manifest in manifest_list if manifest["environment"] in env +] + + +def is_current_replica_count_more_than_desired(max_replica): + return max_replica > 3 + + +for i in manifest_list: + perform_post = False + r = s.get(url + "/api/manifest/" + str(i["id"])) + manifest = r.json() + if "deployment" in manifest: + deployment = manifest["deployment"] + if ( + deployment["namespace"] == "dev-datastores" + or deployment["namespace"] == "qa-datastores" + ): + continue + + if "hpa" in deployment: + if is_current_replica_count_more_than_desired( + deployment["hpa"]["maxReplicas"] + ): + if deployment["hpa"]["minReplicas"] > 3: + print( + f'maxReplica: {deployment["hpa"]["maxReplicas"]} ' + f'and minReplica: {deployment["hpa"]["minReplicas"]} ' + f"are more than desired replica count: {3}" + ) + print(f"Setting min and max Replica to {3}") + deployment["hpa"]["minReplicas"] = 3 + deployment["hpa"]["maxReplicas"] = 3 + perform_post = True + elif deployment["hpa"]["maxReplicas"] > 3: + deployment["hpa"]["maxReplicas"] = 3 + + else: + print( + f'maxReplica: {deployment["hpa"]["maxReplicas"]} ' + f'and minReplica: {deployment["hpa"]["minReplicas"]} ' + f"are less than or euqal to desired replica count: {3}" + ) + + if perform_post: + response = s.post(url + "/api/manifest", json=manifest) + if response.status_code < 200 or response.status_code >= 300: + isSuccessful = False + print( + f'For manifest id: {i["id"]} name: {manifest["environment"]}/{manifest["name"]}, ' + f"the response is {response.json()}" + ) + else: + print( + f'Manifest id:{i["id"]} name: {manifest["environment"]}/{manifest["name"]} ' + f"processed successfully!" + ) + time.sleep(2) + else: + print(f"No action required")