88import os
99import tempfile
1010from abc import ABC , abstractmethod
11- from pathlib import Path
1211from typing import Dict , List , Optional , Tuple , Union
1312
1413from azext_aks_sreclaw ._consts import (
1514 AGENT_NAMESPACE ,
1615 AKS_SRECLAW_LABEL_SELECTOR ,
17- AKS_SRECLAW_VERSION ,
1816)
1917from azext_aks_sreclaw .sreclaw .k8s .helm_manager import HelmManager
2018from azext_aks_sreclaw .sreclaw .llm_config_manager import LLMConfigManager
@@ -480,7 +478,6 @@ def _wait_for_pods_ready(self, timeout: int = 300, interval: int = 5) -> bool:
480478
481479 while time .time () - start_time < timeout :
482480 try :
483- # Check for pods with label selector
484481 pod_list = self .core_v1 .list_namespaced_pod (
485482 namespace = self .namespace ,
486483 label_selector = AKS_SRECLAW_LABEL_SELECTOR
@@ -491,30 +488,7 @@ def _wait_for_pods_ready(self, timeout: int = 300, interval: int = 5) -> bool:
491488 time .sleep (interval )
492489 continue
493490
494- # Check if all pods are ready
495- all_ready = True
496- for pod in pod_list .items :
497- pod_name = pod .metadata .name
498- pod_phase = pod .status .phase
499-
500- if pod_phase != "Running" :
501- logger .debug ("Pod %s is in phase %s, waiting..." , pod_name , pod_phase )
502- all_ready = False
503- break
504-
505- # Check pod readiness condition
506- pod_ready = False
507- if pod .status .conditions :
508- for condition in pod .status .conditions :
509- if condition .type == "Ready" and condition .status == "True" :
510- pod_ready = True
511- break
512-
513- if not pod_ready :
514- logger .debug ("Pod %s is not ready yet, waiting..." , pod_name )
515- all_ready = False
516- break
517-
491+ all_ready = self ._check_all_pods_ready (pod_list .items )
518492 if all_ready :
519493 logger .info ("All SREClaw pods are ready" )
520494 return True
@@ -531,6 +505,30 @@ def _wait_for_pods_ready(self, timeout: int = 300, interval: int = 5) -> bool:
531505 logger .warning ("Timeout waiting for SREClaw pods to be ready" )
532506 return False
533507
508+ def _check_all_pods_ready (self , pods ) -> bool :
509+ """Check if all pods are ready."""
510+ for pod in pods :
511+ pod_name = pod .metadata .name
512+ pod_phase = pod .status .phase
513+
514+ if pod_phase != "Running" :
515+ logger .debug ("Pod %s is in phase %s, waiting..." , pod_name , pod_phase )
516+ return False
517+
518+ if not self ._is_pod_ready (pod ):
519+ logger .debug ("Pod %s is not ready yet, waiting..." , pod_name )
520+ return False
521+
522+ return True
523+
524+ def _is_pod_ready (self , pod ) -> bool :
525+ """Check if a pod is ready."""
526+ if pod .status .conditions :
527+ for condition in pod .status .conditions :
528+ if condition .type == "Ready" and condition .status == "True" :
529+ return True
530+ return False
531+
534532 def deploy_sreclaw (self , chart_version : Optional [str ] = None , no_wait : bool = False ) -> Tuple [bool , str ]:
535533 """
536534 Deploy SREClaw using helm chart.
@@ -1042,7 +1040,7 @@ def get_gateway_token(self) -> str:
10421040 )
10431041 raise AzCLIError (f"Failed to retrieve gateway token: { e } " )
10441042
1045- def port_forward_to_service (self , local_port : int = 18789 ) -> str :
1043+ def port_forward_to_service (self , local_port : int = 18789 ) -> str : # pylint: disable=unused-argument
10461044 """Port-forward to aks-sreclaw service.
10471045
10481046 Args:
@@ -1054,12 +1052,6 @@ def port_forward_to_service(self, local_port: int = 18789) -> str:
10541052 Raises:
10551053 AzCLIError: If service or pod is not found, or port-forwarding fails
10561054 """
1057- import select
1058- import socket
1059- import threading
1060-
1061- from kubernetes .stream import portforward
1062-
10631055 # Get gateway token first before starting port-forward
10641056 gateway_token = self .get_gateway_token ()
10651057
@@ -1092,7 +1084,7 @@ def port_forward_to_service(self, local_port: int = 18789) -> str:
10921084 pod_name = pod .metadata .name
10931085 target_port = 18789
10941086
1095- logger .info (f "Found running pod: { pod_name } " )
1087+ logger .info ("Found running pod: %s" , pod_name )
10961088
10971089 # Return token to caller before starting blocking port-forward
10981090 return gateway_token , pod_name , target_port
@@ -1114,7 +1106,7 @@ def start_port_forward(self, pod_name: str, target_port: int, local_port: int =
11141106
11151107 from kubernetes .stream import portforward
11161108
1117- logger .info (f "Port-forwarding localhost:{ local_port } -> { pod_name } : { target_port } " )
1109+ logger .info ("Port-forwarding localhost:%d -> %s:%d" , local_port , pod_name , target_port )
11181110
11191111 # Start a local TCP server and forward each connection through the k8s portforward API
11201112 server = socket .socket (socket .AF_INET , socket .SOCK_STREAM )
@@ -1146,7 +1138,7 @@ def _forward(local_conn, pf_socket):
11461138 if not data :
11471139 break
11481140 local_conn .sendall (data )
1149- except Exception :
1141+ except Exception : # pylint: disable=broad-exception-caught
11501142 pass
11511143 finally :
11521144 local_conn .close ()
0 commit comments