[skip ci] Add export logs method to utils (#13067)

Signed-off-by: ThreadDao <yufen.zong@zilliz.com>
pull/13080/head
ThreadDao 2021-12-09 15:51:44 +08:00 committed by GitHub
parent 29f4a209c0
commit 3b24b891ef
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 30 additions and 0 deletions

View File

@ -1,3 +1,4 @@
import os.path
import time
from kubernetes import client, config
from kubernetes.client.rest import ApiException
@ -78,6 +79,35 @@ def get_pod_list(namespace, label_selector):
raise Exception(str(e))
def export_pods_log(namespace, label_selector):
"""
export pod logs with label selector to '/tmp/milvus'
:param namespace: the namespace where the release
:type namespace: str
:param label_selector: labels to restrict which pods logs to export
:type label_selector: str
:example:
>>> export_logs("chaos-testing", "app.kubernetes.io/instance=mic-milvus")
"""
pod_log_path = '/tmp/milvus'
if not os.path.isdir(pod_log_path):
os.makedirs(pod_log_path)
# get pods and export logs
items = get_pod_list("chaos-testing", label_selector=label)
try:
for item in items:
pod_name = item.metadata.name
os.system(f'kubectl logs {pod_name} > {pod_log_path}/{pod_name}.log 2>&1')
except Exception as e:
log.error(f"Exception when export pod {pod_name} logs: %s\n" % e)
raise Exception(str(e))
if __name__ == '__main__':
label = "app.kubernetes.io/instance=test-proxy-pod-failure, component=proxy"
res = get_pod_list("chaos-testing", label_selector=label)
export_pods_log(namespace='chaos-testing', label_selector=label)