-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathclean.py
55 lines (45 loc) · 2.15 KB
/
clean.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import requests
import json
import os
from datetime import timedelta, datetime
# --- Variables ---------------------------------------------------------------
# Get the token for authenticate via the API
if os.path.exists("/var/run/secrets/kubernetes.io/serviceaccount"):
token = open("/var/run/secrets/kubernetes.io/serviceaccount/token", "r").read().replace('\n', '')
else:
token = os.environ["TOKEN"]
# API URL. Ex. https://kubernetes.default.svc/api/
apiURL = os.environ["API_URL"]
# Namespace where the pods are running
namespace = os.environ["NAMESPACE"]
# Expiration time in days, the pods older than "maxDays" are going to be deleted
maxDays = float(os.environ["MAX_DAYS"])
# Only pods with the following status are going to be deleted
# You can send a list of string separate by comma, Ex. "Pending, Running, Succeeded, Failed, Unknown"
podStatus = os.environ["POD_STATUS"].replace(' ','').split(",")
# --- Functions ---------------------------------------------------------------
def callAPI(method, url):
headers = {"Authorization": "Bearer "+token}
requests.packages.urllib3.disable_warnings()
request = requests.request(method, url, headers=headers, verify=False)
return request.json()
def getPods(namespace):
url = apiURL+"v1/namespaces/"+namespace+"/pods"
response = callAPI('GET', url)
return response["items"]
def deletePod(podName, namespace):
url = apiURL+"v1/namespaces/"+namespace+"/pods/"+podName
response = callAPI('DELETE', url)
return response
# --- Main --------------------------------------------------------------------
# Get all pods running in a namespace and delete older than "maxDays"
pods = getPods(namespace)
if not pods:
print("No pods for delete.")
for pod in pods:
if pod["status"]["phase"] in podStatus:
podStartTime = datetime.strptime(pod["status"]["startTime"], "%Y-%m-%dT%H:%M:%SZ")
todayDate = datetime.today()
if ((podStartTime + timedelta(days=maxDays)) < todayDate):
print("Deleting pod ("+pod["metadata"]["name"]+"). Status ("+pod["status"]["phase"]+"). Start time ("+str(podStartTime)+")")
deletePod(pod["metadata"]["name"], namespace)