Skip to content

Commit 141ed96

Browse files
committed
add get_pods_with_label
1 parent af44fda commit 141ed96

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

src/warnet/k8s.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -545,3 +545,19 @@ def write_kubeconfig(kube_config: dict, kubeconfig_path: str) -> None:
545545
except Exception as e:
546546
os.remove(temp_file.name)
547547
raise K8sError(f"Error writing kubeconfig: {kubeconfig_path}") from e
548+
549+
550+
def get_pods_with_label(label_selector: str, namespace: Optional[str] = None) -> list[V1Pod]:
551+
"""Get a list of pods by label.
552+
Label example: "mission=lightning"
553+
"""
554+
namespace = get_default_namespace_or(namespace)
555+
v1 = get_static_client()
556+
557+
try:
558+
pods = v1.list_namespaced_pod(namespace=namespace, label_selector=label_selector)
559+
v1_pods = [pod for pod in pods.items]
560+
return v1_pods
561+
except client.exceptions.ApiException as e:
562+
print(f"Error fetching pods: {e}")
563+
return []

0 commit comments

Comments
 (0)