-
Notifications
You must be signed in to change notification settings - Fork 1
/
k8s_conntrack_cleanup.py
executable file
·84 lines (67 loc) · 2.67 KB
/
k8s_conntrack_cleanup.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#!/usr/bin/env python
import subprocess
import os
from kubernetes import client, config, watch
from threading import Thread
def get_pod_ips(namespace):
config.load_kube_config()
v1 = client.CoreV1Api()
try:
pods = v1.list_namespaced_pod(namespace, watch=False)
return [x.status.pod_ip for x in pods.items]
except BaseException as e:
print("Error getting the pods for namespace {0}: {1}"
.format(namespace, e))
def run(namespace):
"""Monitor kubernetes pod events and cleanup conntrack table
when a pod is removed"""
config.load_kube_config()
v1 = client.CoreV1Api()
w = watch.Watch()
# cache the pod IPs as the delete event might not include the pod ip
pod_ips = {}
for event in w.stream(v1.list_namespaced_pod, namespace=namespace):
pod_ip = event['object'].status.pod_ip
pod_name = event['object'].metadata.name
print("Event:\t{}\t{}\t{}".format(event['type'], pod_name, pod_ip))
if pod_ip:
pod_ips[pod_name] = pod_ip
if event['type'] == 'DELETED':
ip = pod_ip or pod_ips.get(pod_name)
pod_ips.pop(pod_name, None)
if ip:
print('Action:\ttriggering conntrack cleanup for {}'
.format(pod_ip))
cleanup_conntrack(pod_ip)
else:
print("Error:\tpod ip not present, cannot cleanup!")
def cleanup_conntrack(pod_ip):
cmd_params = ['--reply-src', '--reply-dst', '--dst', '--src']
for param in cmd_params:
cmd = "/usr/sbin/conntrack -D {} {}".format(param, pod_ip)
t = Thread(target=run_cmd, args=(cmd, ))
t.start()
def run_cmd(cmd):
with open(os.devnull, 'w') as DEVNULL:
try:
subprocess.check_output(cmd, stderr=DEVNULL, shell=True)
except subprocess.CalledProcessError as e:
# conntrack returns 1 if there was no entry in the table
# matching the request, which is not an error
if e.returncode != 1:
print("Err:\t{}".format(e.output.strip()))
else:
print("Info:\tcmd '{}' returned code 1 (which is probably ok)"
.format(cmd))
else:
print("Info:\tcmd '{}' was executed successfully".format(cmd))
if __name__ == "__main__":
import argparse
parser = argparse.ArgumentParser(
description='Cleanup conntrack table when a K8s pod is deleted')
parser.add_argument('-n', '--namespace',
dest='namespace',
required=True,
help="a K8s namespace")
args = parser.parse_args()
run(args.namespace)