python
python copied to clipboard
Watch() returns wrong "DELETED" event
What happened (please include outputs or screenshots):
A python script watching the existence of a set of configmaps gets notified that a CM got deleted when this is not the case. Upon investigation, we found that this event is triggered when a CM does not match the label selector anymore (i.e. the selected label is removed).
The fact that the CM is removed from the list of watched CMs does not imply it has been deleted, meaning this classifies as a bug,
What you expected to happen:
- The CM that was formerly watched, and whose labels changed, should just not be watched anymore.
- the python client should mimick the behaviour of kubectl (no event is sent)
$ kubectl wait configmap --for=delete -l foo=bar &
[1] 240648
$ kubectl label configmap foo foo-
rolebinding.rbac.authorization.k8s.io/foo unlabeled
$ fg
kubectl wait configmap --for=delete -l foo=bar
error: timed out waiting for the condition on rolebindings/foo
How to reproduce it (as minimally and precisely as possible):
- run the script (change namespace and labelselector accordingly)
- send it to background or open another shell
- remove the watched label from a watched resource
- notice the message:
ConfigMap 'foo' was deleted
#!/usr/bin/env python3
from kubernetes import client, config, watch
import os
config.load_kube_config()
v1 = client.CoreV1Api()
label_selector = "foo=bar"
namespace = "foo-test"
while True:
try:
stream = watch.Watch().stream(v1.list_namespaced_config_map, namespace=namespace, label_selector=label_selector)
for event in stream:
config_map = event['object']
event_type = event['type']
if event_type == 'DELETED':
print(f"ConfigMap '{config_map.metadata.name}' was deleted")
except Exception as e:
print(f"Exception occurred: {str(e)}")
continue
Anything else we need to know?:
Environment:
- Python version (
python --version
): 3.11.5 - Python client version (
pip list | grep kubernetes
): 24.2.0