kubeReleaseScript/release.py

45 lines
1.2 KiB
Python

from kubernetes import client, config
import sys
import time
if len(sys.argv) != 3:
print("Call this script with 2 paramters -> release.py <NAMESPACE> <DEPLOYMENT>")
sys.exit(2)
namespace = sys.argv[1]
deployment = sys.argv[2]
print("Redeploy "+namespace+"/"+deployment)
config.load_kube_config()
v1 = client.CoreV1Api()
def getPods():
pods = []
v1 = client.CoreV1Api()
ret = v1.list_pod_for_all_namespaces(watch=False)
for i in ret.items:
if i.status.phase == "Running":
if i.metadata.namespace == namespace:
if i.metadata.name[0:len(deployment) + 1] == deployment+"-":
pods.append(i)
return pods
toRemovePods = getPods()
podsCount = len(toRemovePods);
print("Found "+str(podsCount)+" Pods")
for pod in toRemovePods:
v1.delete_namespaced_pod(pod.metadata.name, pod.metadata.namespace)
time.sleep(5)
print("Waiting until Pod is back")
currentPodCount = len(getPods())
print("Current there are "+str(currentPodCount)+" Pods")
while currentPodCount < podsCount:
sys.stdout.write(".")
time.sleep(1)
currentPodCount = len(getPods())
sys.stdout.write(" - Done\n")
print("Done")