-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauto_down_scaler.py
More file actions
33 lines (27 loc) · 1.34 KB
/
auto_down_scaler.py
File metadata and controls
33 lines (27 loc) · 1.34 KB
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
#!/usr/bin/env python3
from datetime import datetime
from kubernetes import client, config
def context_detection():
contexts, active_context = config.list_kube_config_contexts()
if not contexts:
print("Cannot find any context in kube-config file.")
return
contexts = [context['name'] for context in contexts]
return contexts
prod = client.CoreV1Api(api_client=config.new_client_from_config(context=context_detection()[0]))
prod_scale = client.AppsV1Api(api_client=config.new_client_from_config(context=context_detection()[0]))
resp = prod.list_namespaced_pod(namespace='vpn')
body = {"spec": {"replicas": 0}}
for describe_pods in resp.items:
if describe_pods.metadata.labels['app'] == 'pritunl-vpn':
start_time = describe_pods.status.start_time
start_time = str(start_time)[:-6]
start_time = datetime.strptime(start_time, '%Y-%m-%d %H:%M:%S')
start_time = datetime(start_time.year, start_time.month, start_time.day, start_time.hour, start_time.minute,
start_time.second)
delta = datetime.now() - start_time
if round(delta.total_seconds() / 60) >= 15:
prod_scale.patch_namespaced_deployment_scale(name='pritunl-vpn', namespace='vpn', body=body)
print('pritunl-vpn downscaled')
else:
print('pritunl-vpn online')