-
Notifications
You must be signed in to change notification settings - Fork 65
List+watch cache for Kubernetes objects #719
Copy link
Copy link
Open
Labels
enhancementNew feature or requestNew feature or requestneeds infoNeeds more informationNeeds more information
Description
I've been working on the implementation of zero-cost always green list of the Kubernetes objects for my application. I believe this is similar to k8s.io/client-go/informers module from GO client.
The Idea is to list arbitrary resources and then only watch for changes. Here is an example how this could be used:
import asyncio
from typing import cast
from watch import ResourceWatch
from kr8s.asyncio import api
from kr8s.asyncio.objects import Pod
def is_ready_for_connect(pod: Pod):
if pod.metadata.get("deletionTimestamp"):
print("-deletionTimestamp", pod.metadata.deletionTimestamp)
return False
for condition in (pod.status.get("conditions") or []):
if condition.type == "Ready" and condition.status == "True":
break
else:
print("-conditions", pod.status.get("conditions"))
return False
if not pod.status.get("podIP"):
print("-pod_ip", pod.status.get("podIP"))
return False
return True
async def wait_changes(w: ResourceWatch):
print("Start waiting")
async for _ in w.wait_changes():
snapshot = w.snapshot()
for pod in snapshot.values():
pod = cast(Pod, pod)
print(" ", pod.metadata.name, "ready =", is_ready_for_connect(pod))
print("Stop waiting")
async def main():
selector = "app.kubernetes.io/name=robots,app.kubernetes.io/component=redis"
selector = {}
w = ResourceWatch(
Pod, namespace="processing", label_selector=selector,
)
wait = asyncio.create_task(wait_changes(w))
run = asyncio.create_task(w.run_forever())
done, pending = await asyncio.wait([wait, run], return_when=asyncio.FIRST_COMPLETED)
for task in pending:
task.cancel()
for task in done:
task.result()
watch_task = asyncio.run(main())The most important part is calling w.snapshot(), which instantly returns full list of current resources.
Instead of implementing this as a separate library, I'd like to propose to include this functionality right into kr8s. So I want to know your opinion, is it worth this?
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
enhancementNew feature or requestNew feature or requestneeds infoNeeds more informationNeeds more information