-
Notifications
You must be signed in to change notification settings - Fork 0
/
kube_crawler.py
57 lines (42 loc) · 1.7 KB
/
kube_crawler.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
56
57
import time
import requests
import os
# Read the token from kubernetes runtime mount
with open('/var/run/secrets/kubernetes.io/serviceaccount/token', 'r') as f:
token = f.read()
KUBERNETES_SERVICE_HOST = os.getenv('KUBERNETES_SERVICE_HOST')
KUBERNETES_PORT_443_TCP_PORT = os.getenv('KUBERNETES_PORT_443_TCP_PORT')
NODE_NAME = os.getenv('NODE_NAME')
DEBUG = os.getenv("DEBUG", False)
pods_list = {}
def get_metadata(ip):
if ip in pods_list:
return pods_list[ip]
else:
None
def poll_kube_api():
print("Started polling Kubernetes API")
while True:
# Query Kubernetes API with a fieldSelector to scope to the pod
r = requests.get(f"https://{KUBERNETES_SERVICE_HOST}:{KUBERNETES_PORT_443_TCP_PORT}/api/v1/pods?fieldSelector=spec.nodeName={NODE_NAME}", headers={'Authorization': f'Bearer {token}'}, verify="/var/run/secrets/kubernetes.io/serviceaccount/ca.crt")
try:
data = r.json()
pods_list_ = {}
# for each pod update the hashmap with the key as the pod IP
# and namespace and name as the value, so we can easily query
# the pod data by IP
for item in data['items']:
pods_list_[item['status']['podIP']] = {
'namespace': item['metadata']['namespace'],
'name': item['metadata']['name']
}
global pods_list
pods_list = pods_list_
print(f"{len(pods_list)} pods are monitored by the agent")
if DEBUG:
print(pods_list)
except:
print("Error while reading data from kube api")
print(r.text)
return
time.sleep(10)