1
1
#!/opt/venvs/paasta-tools/bin/python
2
2
import argparse
3
+ import json
3
4
import time
4
5
from typing import Any
5
6
from typing import Dict
11
12
from typing import Optional
12
13
from typing import Tuple
13
14
14
- import a_sync
15
- import simplejson as json
16
15
from kubernetes .client import V1Pod
17
16
from kubernetes .client import V1ResourceRequirements
18
17
19
18
from paasta_tools import kubernetes_tools
20
- from paasta_tools import mesos_tools
21
19
from paasta_tools .kubernetes_tools import KubeClient
22
- from paasta_tools .mesos .exceptions import SlaveDoesNotExist
23
- from paasta_tools .mesos .task import Task
24
20
from paasta_tools .utils import load_system_paasta_config
25
21
26
22
@@ -40,81 +36,10 @@ class TaskAllocationInfo(NamedTuple):
40
36
host_ip : str
41
37
git_sha : str
42
38
config_sha : str
43
- mesos_container_id : str # Because Mesos task info does not have docker id
39
+ mesos_container_id : str # XXX(luisp): can we delete this now or do we need to cleanup splunk usages first?
44
40
namespace : Optional [str ]
45
41
46
42
47
- def get_container_info_from_mesos_task (
48
- task : Task ,
49
- ) -> Tuple [Optional [str ], Optional [float ]]:
50
- for status in task ["statuses" ]:
51
- if status ["state" ] != "TASK_RUNNING" :
52
- continue
53
- container_id = (
54
- status .get ("container_status" , {}).get ("container_id" , {}).get ("value" )
55
- )
56
- time_start = status .get ("timestamp" )
57
- return container_id , time_start
58
- return None , None
59
-
60
-
61
- def get_paasta_service_instance_from_mesos_task (
62
- task : Task ,
63
- ) -> Tuple [Optional [str ], Optional [str ]]:
64
- try :
65
- docker_params = task ["container" ].get ("docker" , {}).get ("parameters" , [])
66
- except KeyError :
67
- return None , None
68
- service , instance = None , None
69
- for param in docker_params :
70
- if param ["key" ] == "label" :
71
- label = param ["value" ]
72
- if label .startswith ("paasta_service=" ):
73
- service = label .split ("=" )[1 ]
74
- if label .startswith ("paasta_instance=" ):
75
- instance = label .split ("=" )[1 ]
76
- return service , instance
77
-
78
-
79
- async def get_pool_from_mesos_task (task : Task ) -> Optional [str ]:
80
- try :
81
- attributes = (await task .slave ())["attributes" ]
82
- return attributes .get ("pool" , "default" )
83
- except SlaveDoesNotExist :
84
- return None
85
-
86
-
87
- @a_sync .to_blocking
88
- async def get_mesos_task_allocation_info () -> Iterable [TaskAllocationInfo ]:
89
- tasks = await mesos_tools .get_cached_list_of_running_tasks_from_frameworks ()
90
- info_list = []
91
- for task in tasks :
92
- mesos_container_id , start_time = get_container_info_from_mesos_task (task )
93
- paasta_service , paasta_instance = get_paasta_service_instance_from_mesos_task (
94
- task
95
- )
96
- paasta_pool = await get_pool_from_mesos_task (task )
97
- info_list .append (
98
- TaskAllocationInfo (
99
- paasta_service = paasta_service ,
100
- paasta_instance = paasta_instance ,
101
- container_type = MAIN_CONTAINER_TYPE ,
102
- paasta_pool = paasta_pool ,
103
- resources = task ["resources" ],
104
- start_time = start_time ,
105
- docker_id = None ,
106
- pod_name = None ,
107
- pod_ip = None ,
108
- host_ip = None ,
109
- git_sha = None ,
110
- config_sha = None ,
111
- mesos_container_id = mesos_container_id ,
112
- namespace = None ,
113
- )
114
- )
115
- return info_list
116
-
117
-
118
43
def get_all_running_kubernetes_pods (
119
44
kube_client : KubeClient , namespace : str
120
45
) -> Iterable [V1Pod ]:
@@ -256,39 +181,24 @@ def get_kubernetes_task_allocation_info(
256
181
return info_list
257
182
258
183
259
- def get_task_allocation_info (
260
- scheduler : str ,
261
- namespace : str ,
262
- kube_client : Optional [KubeClient ],
263
- ) -> Iterable [TaskAllocationInfo ]:
264
- if scheduler == "mesos" :
265
- return get_mesos_task_allocation_info ()
266
- elif scheduler == "kubernetes" :
267
- return get_kubernetes_task_allocation_info (namespace , kube_client )
268
- else :
269
- return []
270
-
271
-
272
184
def parse_args () -> argparse .Namespace :
273
185
parser = argparse .ArgumentParser (description = "" )
274
186
parser .add_argument (
275
187
"--scheduler" ,
276
188
help = "Scheduler to get task info from" ,
277
189
dest = "scheduler" ,
278
- default = "mesos " ,
279
- choices = ["mesos" , " kubernetes" ],
190
+ default = "kubernetes " ,
191
+ choices = ["kubernetes" ],
280
192
)
281
193
parser .add_argument (
282
194
"--namespace-prefix" ,
283
- help = "prefix of the namespace to fetch the logs for"
284
- "Used only when scheduler is kubernetes" ,
195
+ help = "prefix of the namespace to fetch the logs for" ,
285
196
dest = "namespace_prefix" ,
286
197
default = "paasta" ,
287
198
)
288
199
parser .add_argument (
289
200
"--additional-namespaces" ,
290
- help = "full names of namespaces to fetch allocation info for that don't match --namespace-prefix"
291
- "Used only when scheduler is kubernetes" ,
201
+ help = "full names of namespaces to fetch allocation info for that don't match --namespace-prefix" ,
292
202
dest = "additional_namespaces" ,
293
203
nargs = "+" ,
294
204
# we default this to tron since this is really the only non-paasta-prefix namespaced that is part of paasta
@@ -310,28 +220,20 @@ def get_matching_namespaces(
310
220
311
221
def main (args : argparse .Namespace ) -> None :
312
222
cluster = load_system_paasta_config ().get_cluster ()
313
- if args .scheduler == "mesos" :
314
- display_task_allocation_info (
315
- cluster , args .scheduler , args .namespace_prefix , kube_client = None
316
- )
317
- else :
318
- kube_client = KubeClient ()
319
- all_namespaces = kubernetes_tools .get_all_namespaces (kube_client )
320
- for matching_namespace in get_matching_namespaces (
321
- all_namespaces , args .namespace_prefix , args .additional_namespaces
322
- ):
323
- display_task_allocation_info (
324
- cluster , args .scheduler , matching_namespace , kube_client
325
- )
223
+ kube_client = KubeClient ()
224
+ all_namespaces = kubernetes_tools .get_all_namespaces (kube_client )
225
+ for matching_namespace in get_matching_namespaces (
226
+ all_namespaces , args .namespace_prefix , args .additional_namespaces
227
+ ):
228
+ display_task_allocation_info (cluster , matching_namespace , kube_client )
326
229
327
230
328
231
def display_task_allocation_info (
329
232
cluster : str ,
330
- scheduler : str ,
331
233
namespace : str ,
332
- kube_client : Optional [ KubeClient ] ,
234
+ kube_client : KubeClient ,
333
235
) -> None :
334
- info_list = get_task_allocation_info ( scheduler , namespace , kube_client )
236
+ info_list = get_kubernetes_task_allocation_info ( namespace , kube_client )
335
237
timestamp = time .time ()
336
238
for info in info_list :
337
239
info_dict = info ._asdict ()
0 commit comments