-
Notifications
You must be signed in to change notification settings - Fork 75
/
Copy pathtail.py
99 lines (85 loc) · 2.76 KB
/
tail.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#
# 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
from kubernetes_asyncio.client.api_client import ApiClient, Configuration
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(v1_api, pod, namespace, container, lines, follow):
if not follow:
resp = await v1_api.read_namespaced_pod_log(
pod, namespace, container=container, tail_lines=lines
)
print(resp)
else:
resp = await v1_api.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()
client_configuration = Configuration()
loader = await config.load_kube_config(client_configuration=client_configuration)
api = ApiClient(configuration=client_configuration)
v1_api = client.CoreV1Api(api)
ret = await v1_api.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(
v1_api,
pod.metadata.name,
args.namespace,
container.name,
args.lines,
args.follow,
)
)
if cmd == []:
print("No matching PODs !")
await api.close()
return
if args.follow:
# autorefresh gcp token
cmd.append(
config.refresh_token(
loader=loader, client_configuration=client_configuration
)
)
await asyncio.wait(cmd)
await api.close()
if __name__ == "__main__":
loop = asyncio.get_event_loop()
loop.run_until_complete(main())
loop.close()