Skip to content
This repository has been archived by the owner on Sep 14, 2020. It is now read-only.

Commit

Permalink
Switch examples from kubernetes to pykube-ng
Browse files Browse the repository at this point in the history
The examples are used as the e2e test. With the warnings converted
to errors, they fails quite often (randomly) due to SSL sockets
not closed properly (ResourceWarnings), noticed within the executor's
threads (which are used only for the sync-handlers).

Switching to `pykube-ng` and closing the request's session explicitly
(instead of on garbage collection) seems to fix it. So as not having
the session0owning object module-scoped.
  • Loading branch information
nolar committed Feb 20, 2020
1 parent 55c9c76 commit 4664013
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 16 deletions.
10 changes: 6 additions & 4 deletions examples/02-children/example.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import kopf
import kubernetes.client
import pykube
import yaml


Expand Down Expand Up @@ -28,8 +28,10 @@ def create_fn(spec, **kwargs):
kopf.adopt(doc)

# Actually create an object by requesting the Kubernetes API.
api = kubernetes.client.CoreV1Api()
pod = api.create_namespaced_pod(namespace=doc['metadata']['namespace'], body=doc)
api = pykube.HTTPClient(pykube.KubeConfig.from_env())
pod = pykube.Pod(api, doc)
pod.create()
api.session.close()

# Update the parent's status.
return {'children': [pod.metadata.uid]}
return {'children': [pod.metadata['uid']]}
8 changes: 2 additions & 6 deletions examples/10-builtins/example.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,6 @@

tasks = {} # dict{namespace: dict{name: asyncio.Task}}

try:
cfg = pykube.KubeConfig.from_service_account()
except FileNotFoundError:
cfg = pykube.KubeConfig.from_file()
api = pykube.HTTPClient(cfg)


@kopf.on.resume('', 'v1', 'pods')
@kopf.on.create('', 'v1', 'pods')
Expand All @@ -36,8 +30,10 @@ async def pod_killer(namespace, name, logger, timeout=30):
await asyncio.sleep(timeout)
logger.info(f"=== Pod killing happens NOW!")

api = pykube.HTTPClient(pykube.KubeConfig.from_env())
pod = pykube.Pod.objects(api, namespace=namespace).get_by_name(name)
pod.delete()
api.session.close()

except asyncio.CancelledError:
logger.info(f"=== Pod killing is cancelled!")
Expand Down
8 changes: 2 additions & 6 deletions examples/99-all-at-once/example.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,6 @@
E2E_FAILURE_COUNTS = {}
E2E_TRACEBACKS = True

try:
cfg = pykube.KubeConfig.from_service_account()
except FileNotFoundError:
cfg = pykube.KubeConfig.from_file()
api = pykube.HTTPClient(cfg)


@kopf.on.startup()
async def startup_fn_simple(logger, **kwargs):
Expand Down Expand Up @@ -112,8 +106,10 @@ def create_pod(**kwargs):
kopf.label(pod_data, {'application': 'kopf-example-10'})

# Actually create an object by requesting the Kubernetes API.
api = pykube.HTTPClient(pykube.KubeConfig.from_env())
pod = pykube.Pod(api, pod_data)
pod.create()
api.session.close()


@kopf.on.event('', 'v1', 'pods', labels={'application': 'kopf-example-10'})
Expand Down

0 comments on commit 4664013

Please sign in to comment.