10
10
import base64
11
11
import hashlib
12
12
import logging
13
+ import os
13
14
import subprocess
14
15
import sys
15
16
from typing import Any
@@ -58,7 +59,18 @@ def limit_size_with_hash(name: str, limit: int = 63, suffix: int = 4) -> str:
58
59
59
60
def parse_args ():
60
61
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
+ )
62
74
parser .add_argument (
63
75
"--do-work" ,
64
76
dest = "do_work" ,
@@ -77,6 +89,10 @@ def parse_args():
77
89
parser .add_argument ("-v" , "--verbose" , dest = "verbose" , action = "store_true" , default = False , help = "Verbose logging" )
78
90
args = parser .parse_args ()
79
91
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
+
80
96
# tron's base level is critical, not info, adjust accoringly
81
97
if args .verbose :
82
98
level = logging .DEBUG
@@ -97,7 +113,10 @@ def parse_args():
97
113
return args
98
114
99
115
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
101
120
kube_client = KubeClient (kubeconfig_path = kubeconfig_path , user_agent = "sync_tron_state_from_k8s" )
102
121
103
122
# Bit of a hack, no helper to fetch pods so reach into core api
@@ -214,7 +233,16 @@ def update_tron_from_pods(
214
233
jobs = get_tron_state_from_api (args .tron_url , args .num_runs )
215
234
log .debug (f"Found { len (jobs )} jobs." )
216
235
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
+
218
246
log .debug (f"Found { len (pods .keys ())} pods." )
219
247
220
248
update_tron_from_pods (jobs , pods , args .tronctl_wrapper , args .do_work )
0 commit comments