Skip to content

Commit d51984e

Browse files
authored
Merge pull request #991 from Yelp/u/jfong/sync_tron_from_k8s_multicluster
Support multiple clusters in sync_tron_from_k8s
2 parents bff89ee + 172ad0c commit d51984e

File tree

1 file changed

+31
-3
lines changed

1 file changed

+31
-3
lines changed

tools/sync_tron_state_from_k8s.py

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import base64
1111
import hashlib
1212
import logging
13+
import os
1314
import subprocess
1415
import sys
1516
from typing import Any
@@ -58,7 +59,18 @@ def limit_size_with_hash(name: str, limit: int = 63, suffix: int = 4) -> str:
5859

5960
def parse_args():
6061
parser = argparse.ArgumentParser()
61-
parser.add_argument("--kubeconfig-path", dest="kubeconfig_path", help="KUBECONFIG path")
62+
parser.add_argument(
63+
"--kubeconfig-path",
64+
dest="kubeconfig_path",
65+
help="KUBECONFIG path; multiple can be specified to find pods in multiple clusters",
66+
nargs="+",
67+
)
68+
parser.add_argument(
69+
"--kubecontext",
70+
dest="kubecontext",
71+
help="kubecontext to use from specified kubeconfig. multiple can be specified to find pods in multiple clusters, ONLY if a single kubeconfig-path is provided",
72+
nargs="*",
73+
)
6274
parser.add_argument(
6375
"--do-work",
6476
dest="do_work",
@@ -77,6 +89,10 @@ def parse_args():
7789
parser.add_argument("-v", "--verbose", dest="verbose", action="store_true", default=False, help="Verbose logging")
7890
args = parser.parse_args()
7991

92+
# We can only have multiple kubeconfigs, or multiple contexts with a single config
93+
if len(args.kubeconfig_path) > 1 and args.kubecontext:
94+
parser.error("You can only specify a single --kubeconfig-path if specifying --kubecontext arguments.")
95+
8096
# tron's base level is critical, not info, adjust accoringly
8197
if args.verbose:
8298
level = logging.DEBUG
@@ -97,7 +113,10 @@ def parse_args():
97113
return args
98114

99115

100-
def fetch_pods(kubeconfig_path: str) -> Dict[str, V1Pod]:
116+
def fetch_pods(kubeconfig_path: str, kubecontext: Optional[str]) -> Dict[str, V1Pod]:
117+
if kubecontext:
118+
# KubeClient only uses the environment variable
119+
os.environ["KUBECONTEXT"] = kubecontext
101120
kube_client = KubeClient(kubeconfig_path=kubeconfig_path, user_agent="sync_tron_state_from_k8s")
102121

103122
# Bit of a hack, no helper to fetch pods so reach into core api
@@ -214,7 +233,16 @@ def update_tron_from_pods(
214233
jobs = get_tron_state_from_api(args.tron_url, args.num_runs)
215234
log.debug(f"Found {len(jobs)} jobs.")
216235

217-
pods = fetch_pods(args.kubeconfig_path)
236+
pods = {}
237+
kube_client_args = (
238+
[(args.kubeconfig_path[0], kubecontext) for kubecontext in args.kubecontext]
239+
if args.kubecontext
240+
else [(kubeconfig_path, None) for kubeconfig_path in args.kubeconfig_path]
241+
)
242+
243+
for kubeconfig_path, kubecontext in kube_client_args:
244+
pods.update(fetch_pods(kubeconfig_path, kubecontext))
245+
218246
log.debug(f"Found {len(pods.keys())} pods.")
219247

220248
update_tron_from_pods(jobs, pods, args.tronctl_wrapper, args.do_work)

0 commit comments

Comments
 (0)