import asyncio

import powershift.endpoints as endpoints
import powershift.resources as resources

client = endpoints.AsyncClient()

async def run_query():
    projects = await client.oapi.v1.projects.get()

    #print(projects)
    #print(resources.dumps(projects, indent=4, sort_keys=True))
    #print()

    project = projects.items[0]
    namespace = project.metadata.name

    print('namespace=%r' % namespace)

    async with client.api.v1.namespaces(namespace=namespace).pods.get(watch='') as items:
        async for item in items:
            action = item['type']
            pod = item['object']
            print('    %s pod=%r' % (action, pod.metadata.name))

loop = asyncio.get_event_loop()

loop.run_until_complete(run_query())

import sys
import asyncio

import powershift.endpoints as endpoints

async def run_query():
    namespace = sys.argv[1]

    print('namespace=%r' % namespace)

    client = endpoints.AsyncClient()

    pods = await client.api.v1.namespaces(namespace=namespace).pods.get()

    for pod in pods.items:
        print('    OBJECT %s pod=%r' % (pod.metadata.resource_version, pod.metadata.name))

    resource_version = pods.metadata.resource_version

    while True:
        try:
            async with client.api.v1.namespaces(namespace=namespace).pods.get(watch='', resource_version=resource_version, timeout_seconds=10) as items:
                async for item in items:
                    action = item['type']
                    pod = item['object']

                    print('    %s %s pod=%r' % (action, pod.metadata.resource_version, pod.metadata.name))

                    resource_version = pod.metadata.resource_version
        except Exception:
            pass

loop = asyncio.get_event_loop()
loop.run_until_complete(run_query())

Make watch example for pods more durable.
Make watch example for pods more durable.

+ import sys
  import asyncio
  
  import powershift.endpoints as endpoints
- import powershift.resources as resources
- 
- client = endpoints.AsyncClient()
  
  async def run_query():
+     namespace = sys.argv[1]
-     projects = await client.oapi.v1.projects.get()
- 
-     #print(projects)
-     #print(resources.dumps(projects, indent=4, sort_keys=True))
-     #print()
- 
-     project = projects.items[0]
-     namespace = project.metadata.name
  
      print('namespace=%r' % namespace)
  
+     client = endpoints.AsyncClient()
+ 
-     async with client.api.v1.namespaces(namespace=namespace).pods.get(watch='') as items:
+     pods = await client.api.v1.namespaces(namespace=namespace).pods.get()
+ 
+     for pod in pods.items:
+         print('    OBJECT %s pod=%r' % (pod.metadata.resource_version, pod.metadata.name))
+ 
+     resource_version = pods.metadata.resource_version
+ 
+     while True:
+         try:
+             async with client.api.v1.namespaces(namespace=namespace).pods.get(watch='', resource_version=resource_version, timeout_seconds=10) as items:
-         async for item in items:
+                 async for item in items:
-             action = item['type']
+                     action = item['type']
-             pod = item['object']
+                     pod = item['object']
+ 
-             print('    %s pod=%r' % (action, pod.metadata.name))
+                     print('    %s %s pod=%r' % (action, pod.metadata.resource_version, pod.metadata.name))
+ 
+                     resource_version = pod.metadata.resource_version
+         except Exception:
+             pass
  
  loop = asyncio.get_event_loop()
- 
  loop.run_until_complete(run_query())
  
Make watch example for pods more durable.
## Code Before:
import asyncio

import powershift.endpoints as endpoints
import powershift.resources as resources

client = endpoints.AsyncClient()

async def run_query():
    projects = await client.oapi.v1.projects.get()

    #print(projects)
    #print(resources.dumps(projects, indent=4, sort_keys=True))
    #print()

    project = projects.items[0]
    namespace = project.metadata.name

    print('namespace=%r' % namespace)

    async with client.api.v1.namespaces(namespace=namespace).pods.get(watch='') as items:
        async for item in items:
            action = item['type']
            pod = item['object']
            print('    %s pod=%r' % (action, pod.metadata.name))

loop = asyncio.get_event_loop()

loop.run_until_complete(run_query())

## Instruction:
Make watch example for pods more durable.
## Code After:
import sys
import asyncio

import powershift.endpoints as endpoints

async def run_query():
    namespace = sys.argv[1]

    print('namespace=%r' % namespace)

    client = endpoints.AsyncClient()

    pods = await client.api.v1.namespaces(namespace=namespace).pods.get()

    for pod in pods.items:
        print('    OBJECT %s pod=%r' % (pod.metadata.resource_version, pod.metadata.name))

    resource_version = pods.metadata.resource_version

    while True:
        try:
            async with client.api.v1.namespaces(namespace=namespace).pods.get(watch='', resource_version=resource_version, timeout_seconds=10) as items:
                async for item in items:
                    action = item['type']
                    pod = item['object']

                    print('    %s %s pod=%r' % (action, pod.metadata.resource_version, pod.metadata.name))

                    resource_version = pod.metadata.resource_version
        except Exception:
            pass

loop = asyncio.get_event_loop()
loop.run_until_complete(run_query())
