From 496968d91fcfbc0dc56617438deccf9cb247d794 Mon Sep 17 00:00:00 2001 From: Tomasz Prus Date: Thu, 14 Jun 2018 22:17:43 +0200 Subject: [PATCH 1/4] feat: add new example --- examples/tail.py | 77 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 examples/tail.py diff --git a/examples/tail.py b/examples/tail.py new file mode 100644 index 000000000..6936241f0 --- /dev/null +++ b/examples/tail.py @@ -0,0 +1,77 @@ +# +# 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() From 52efa6afd98281844abd52b93f36372519e5662d Mon Sep 17 00:00:00 2001 From: Tomasz Prus Date: Thu, 14 Jun 2018 22:17:48 +0200 Subject: [PATCH 2/4] feat: updat doc --- README.md | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) 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. From 51c06de7c9b97aba2c23cf5c8aff60b130cfca35 Mon Sep 17 00:00:00 2001 From: Tomasz Prus Date: Thu, 14 Jun 2018 22:22:17 +0200 Subject: [PATCH 3/4] chore: pep8 --- examples/tail.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/examples/tail.py b/examples/tail.py index 6936241f0..4fff2efd6 100644 --- a/examples/tail.py +++ b/examples/tail.py @@ -5,7 +5,7 @@ # python tail.py [-n LINES] [-f] # # Example: -# python tail.py kube-system kube-dns -f +# python tail.py kube-system kube-dns -f # # It will be streaming logs from all containers from PODs which name ~ "^kube-dns.*" # @@ -21,8 +21,10 @@ 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') + 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() From 0f0430f3c09aee616c4f0fbcf01c62d9acc205db Mon Sep 17 00:00:00 2001 From: Tomasz Prus Date: Thu, 14 Jun 2018 22:30:24 +0200 Subject: [PATCH 4/4] chore: isort --- examples/tail.py | 1 - 1 file changed, 1 deletion(-) diff --git a/examples/tail.py b/examples/tail.py index 4fff2efd6..f4180141a 100644 --- a/examples/tail.py +++ b/examples/tail.py @@ -11,7 +11,6 @@ # import argparse - import asyncio from kubernetes_asyncio import client, config