()
| 36 | |
| 37 | |
| 38 | def main(): |
| 39 | # Configs can be set in Configuration class directly or using helper |
| 40 | # utility. If no argument provided, the config will be loaded from |
| 41 | # default location. |
| 42 | config.load_kube_config() |
| 43 | client = DynamicClient(api_client.ApiClient()) |
| 44 | api = client.resources.get(api_version="v1", kind="Pod") |
| 45 | |
| 46 | # Setting resource_version=None means the server will send synthetic |
| 47 | # ADDED events for all resources that exist when the watch starts. |
| 48 | resource_version = None |
| 49 | while True: |
| 50 | try: |
| 51 | for event in api.watch( |
| 52 | namespace=NAMESPACE, |
| 53 | resource_version=resource_version, |
| 54 | allow_watch_bookmarks=True, |
| 55 | ): |
| 56 | # Remember the last resourceVersion we saw, so we can resume |
| 57 | # watching from there if the connection is lost. |
| 58 | resource_version = event['object'].metadata.resourceVersion |
| 59 | |
| 60 | print("Event: %s %s %s" % ( |
| 61 | resource_version, |
| 62 | event['type'], |
| 63 | event['object'].metadata.name, |
| 64 | )) |
| 65 | |
| 66 | except ApiException as err: |
| 67 | if err.status == 410: |
| 68 | print("ERROR: The requested resource version is no longer available.") |
| 69 | resource_version = None |
| 70 | else: |
| 71 | raise |
| 72 | |
| 73 | except urllib3.exceptions.ProtocolError: |
| 74 | print("Lost connection to the k8s API server. Reconnecting...") |
| 75 | |
| 76 | |
| 77 | if __name__ == "__main__": |
no test coverage detected