diff --git a/README.md b/README.md index 0e0dc6520..283ea44bd 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,5 @@ # Kubernetes Python Client -![Status - under heavy development](https://img.shields.io/badge/status-%20under%20heavy%20development-red.svg) [![Build Status](https://travis-ci.org/tomplus/kubernetes_asyncio.svg?branch=master)](https://travis-ci.org/tomplus/kubernetes_asyncio) [![PyPI version](https://badge.fury.io/py/kubernetes_asyncio.svg)](https://badge.fury.io/py/kubernetes_asyncio) [![codecov](https://codecov.io/gh/tomplus/kubernetes_asyncio/branch/master/graph/badge.svg)](https://codecov.io/gh/tomplus/kubernetes_asyncio) @@ -10,6 +9,8 @@ Asynchronous (AsyncIO) client library for the [Kubernetes](http://kubernetes.io/ This library is created in the same way as official https://github.com/kubernetes-client/python but uses asynchronous version of swagger-codegen. +My motivation: https://github.com/kubernetes-client/python/pull/324 + ## Installation From [PyPi](https://pypi.python.org/pypi/kubernetes_asyncio/) directly: @@ -48,4 +49,5 @@ if __name__ == '__main__': loop.close() ``` -More examples you can find in `examples/` folder. +More complicated examples, like asynchronous multiple watch or tail logs from pods, +you can find in `examples/` folder. diff --git a/examples/tail.py b/examples/tail.py new file mode 100644 index 000000000..f4180141a --- /dev/null +++ b/examples/tail.py @@ -0,0 +1,78 @@ +# +# Script to print logs from container in PODs. +# +# Usage: +# python tail.py [-n LINES] [-f] +# +# Example: +# python tail.py kube-system kube-dns -f +# +# It will be streaming logs from all containers from PODs which name ~ "^kube-dns.*" +# + +import argparse +import asyncio + +from kubernetes_asyncio import client, config + + +def parse_args(): + parser = argparse.ArgumentParser(description='Tail for pods') + parser.add_argument('namespace', help="k8s namespace") + parser.add_argument('pod', help="pod name or prefix") + parser.add_argument('-f', '--follow', action='store_true', + help="output appended data as the file grows") + parser.add_argument('-n', '--lines', default=10, + help='show the last LINES lines') + return parser.parse_args() + + +async def print_pod_log(pod, namespace, container, lines, follow): + v1 = client.CoreV1Api() + if not follow: + resp = await v1.read_namespaced_pod_log(pod, + namespace, + container=container, + tail_lines=lines) + print(resp) + else: + resp = await v1.read_namespaced_pod_log(pod, + namespace, + container=container, + tail_lines=lines, + follow=True, _preload_content=False) + while True: + line = await resp.content.readline() + if not line: + break + print(line.decode('utf-8'), end="") + + +async def main(): + args = parse_args() + + config.load_kube_config() + + v1 = client.CoreV1Api() + ret = await v1.list_namespaced_pod(args.namespace) + cmd = [] + for pod in ret.items: + if pod.metadata.name.startswith(args.pod): + for container in pod.spec.containers: + cmd.append(print_pod_log(pod.metadata.name, + args.namespace, + container.name, + args.lines, + args.follow)) + + if cmd == []: + print('No matching PODs !') + return + + await asyncio.wait(cmd) + + +if __name__ == '__main__': + loop = asyncio.get_event_loop() + loop.run_until_complete(main()) + loop.close()