-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexample.py
83 lines (68 loc) · 2.55 KB
/
example.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
"""
Controller example.
Usage:
kind create cluster
python example.py
Output:
kube-system/kube-controller-manager-kind-control-plane: ip 172.17.0.2
kube-system/kube-proxy-zvl88: ip 172.17.0.2
kube-system/kube-apiserver-kind-control-plane: ip 172.17.0.2
kube-system/kube-scheduler-kind-control-plane: ip 172.17.0.2
kube-system/etcd-kind-control-plane: ip 172.17.0.2
kube-system/coredns-5644d7b6d9-l9vhx: ip 10.244.0.35
kube-system/coredns-5644d7b6d9-zj2rw: ip 10.244.0.36
kube-system/kindnet-85dqj: ip 172.17.0.2
kube-system/coredns-5644d7b6d9-l9vhx: ip 10.244.0.35
kube-system/coredns-5644d7b6d9-fh7l8: ip None
kube-system/coredns-5644d7b6d9-zj2rw: ip 10.244.0.36
kube-system/coredns-5644d7b6d9-fh7l8: ip None
kube-system/coredns-5644d7b6d9-dvf8h: ip None
kube-system/coredns-5644d7b6d9-dvf8h: ip None
kube-system/coredns-5644d7b6d9-fh7l8: ip None
kube-system/coredns-5644d7b6d9-dvf8h: ip None
kube-system/coredns-5644d7b6d9-l9vhx: ip 10.244.0.35
kube-system/coredns-5644d7b6d9-zj2rw: ip 10.244.0.36
kube-system/coredns-5644d7b6d9-fh7l8: ip 10.244.0.37
kube-system/coredns-5644d7b6d9-dvf8h: ip 10.244.0.38
kube-system/coredns-5644d7b6d9-dvf8h: ip 10.244.0.38
kube-system/coredns-5644d7b6d9-zj2rw: ip 10.244.0.36
coredns-5644d7b6d9-zj2rw/coredns-5644d7b6d9-zj2rw deleted
kube-system/coredns-5644d7b6d9-l9vhx: ip 10.244.0.35
coredns-5644d7b6d9-l9vhx/coredns-5644d7b6d9-l9vhx deleted
kube-system/coredns-5644d7b6d9-fh7l8: ip 10.244.0.37
"""
import asyncio
import signal
from kubernetes_asyncio import client, config
from aiok8s.controller import builder, manager, reconcile
class Reconciler:
def __init__(self, cache):
self._cache = cache
async def reconcile(self, request):
try:
pod = await self._cache.get(request.namespaced_name, client.V1Pod)
except KeyError:
print(
f"{request.namespaced_name.name}/{request.namespaced_name.name} deleted"
)
else:
print(
f"{pod.metadata.namespace}/{pod.metadata.name}: ip", pod.status.pod_ip
)
return reconcile.Result()
async def _run():
await config.load_kube_config()
mgr = manager.new()
await builder.build_controller(
mgr, Reconciler(mgr.get_cache()), api_type=client.V1Pod
)
task = asyncio.ensure_future(mgr.start())
loop = asyncio.get_running_loop()
for signal_ in (signal.SIGINT, signal.SIGTERM):
loop.add_signal_handler(signal_, task.cancel)
try:
await task
except asyncio.CancelledError:
print()
if __name__ == "__main__":
asyncio.run(_run())