Skip to content

Commit

Permalink
Merge pull request #19 from tomplus/feat/tail
Browse files Browse the repository at this point in the history
tail.py
  • Loading branch information
tomplus authored Jun 14, 2018
2 parents 9eb6a21 + 0f0430f commit 0cdaf21
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 2 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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:
Expand Down Expand Up @@ -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.
78 changes: 78 additions & 0 deletions examples/tail.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
#
# Script to print logs from container in PODs.
#
# Usage:
# python tail.py <namespace> <prefix for pods' name> [-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()

0 comments on commit 0cdaf21

Please sign in to comment.