1616import sys
1717
1818from datetime import datetime , timezone
19-
2019from pathlib import Path
20+ from threading import Thread
21+
2122from kubernetes import client , config
2223from kubernetes .stream import stream
24+ from kubernetes import watch
2325
2426from stack import constants
2527from stack .deploy .deployer import Deployer , DeployerConfigGenerator
@@ -477,23 +479,48 @@ def logs(self, services, tail, follow, stream):
477479 if len (pods ) == 0 :
478480 log_data = "******* Pods not running ********\n "
479481
480- all_logs = []
481- for k8s_pod_name in pods :
482- matched = True
483- if services :
484- matched = False
485- for svc in services :
486- if f"{ self .cluster_info .app_name } -deploy-{ svc } " in k8s_pod_name :
487- matched = True
488- break
489- if matched :
482+ if services :
483+ matched_pods = []
484+ for svc in services :
485+ for pod in pods :
486+ if f"{ self .cluster_info .app_name } -deploy-{ svc } " in pod :
487+ matched_pods .append (pod )
488+ pods = matched_pods
489+
490+ if follow :
491+
492+ def log_follower (pod_name , container ):
493+ w = watch .Watch ()
494+ for line in w .stream (
495+ self .core_api .read_namespaced_pod_log ,
496+ name = pod_name ,
497+ container = container ,
498+ tail_lines = tail ,
499+ namespace = self .k8s_namespace ,
500+ ):
501+ print (f"{ container } : { line } " )
502+
503+ threads = []
504+ for k8s_pod_name in pods :
505+ containers = containers_in_pod (self .core_api , k8s_pod_name )
506+ for container in containers :
507+ t = Thread (target = log_follower , args = (k8s_pod_name , container ), daemon = True )
508+ t .start ()
509+ threads .append (t )
510+ for t in threads :
511+ t .join ()
512+
513+ return log_stream_from_string ("" )
514+ else :
515+ all_logs = []
516+ for k8s_pod_name in pods :
490517 containers = containers_in_pod (self .core_api , k8s_pod_name )
491518 # If the pod is not yet started, the logs request below will throw an exception
492519 try :
493520 log_data = ""
494521 for container in containers :
495522 container_log = self .core_api .read_namespaced_pod_log (
496- k8s_pod_name , namespace = self .k8s_namespace , container = container
523+ k8s_pod_name , namespace = self .k8s_namespace , container = container , tail_lines = tail
497524 )
498525 container_log_lines = container_log .splitlines ()
499526 for line in container_log_lines :
@@ -503,7 +530,7 @@ def logs(self, services, tail, follow, stream):
503530 print (f"Error from read_namespaced_pod_log: { e } " )
504531 log_data = "******* No logs available ********\n "
505532 all_logs .append (log_data )
506- return log_stream_from_string ("\n " .join (all_logs ))
533+ return log_stream_from_string ("\n " .join (all_logs ))
507534
508535 def update (self ):
509536 self .connect_api ()
0 commit comments