Files
deployment-portal-be/scripts/update-maxReplica-count.py

71 lines
2.5 KiB
Python

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'].startswith(('qa','dev'))):
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")