forked from kubernetes/test-infra
-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_job_pods.py
executable file
·66 lines (58 loc) · 2.14 KB
/
get_job_pods.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#!/usr/bin/env python
# Copyright 2017 The Kubernetes Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# This script prints out lines with: "job-name: ['pod-id', 'pod-id-2']""
#
# USAGE: have KUBECONFIG pointed at your prow builds cluster then:
#
# get_job_pods.py [--show-all]
#
# EG:
# `experiment/get_job_pods.py --show-all | grep pull-kubernetes-bazel-test`
# will get you something like:
# pull-kubernetes-bazel-test: ['c9e634a5-cbe6-11e7-9149-0a580a6c0216']
from __future__ import print_function
from argparse import ArgumentParser
from collections import defaultdict
import json
import subprocess
def get_pods_json(show_all):
cmd = ["kubectl", "get", "po", "-n=test-pods", "-o=json"]
if show_all:
cmd.append("--show-all")
res = subprocess.check_output(cmd)
return json.loads(res)["items"]
def get_pods_by_job(show_all):
pods = get_pods_json(show_all)
pods_by_job = defaultdict(list)
for pod in pods:
# check if prow job
if "prow.k8s.io/job" not in pod["metadata"]["annotations"]:
continue
# get pod and job name, add to dict
pod_name = pod["metadata"]["name"].encode('utf-8')
job_name = pod["metadata"]["annotations"]["prow.k8s.io/job"].encode('utf-8')
pods_by_job[job_name].append(pod_name)
return pods_by_job
def main():
parser = ArgumentParser()
parser.add_argument("--show-all", action='store_true')
args = parser.parse_args()
# get and print pods for each job
pods_by_job = get_pods_by_job(args.show_all)
jobs = sorted(pods_by_job.keys())
for job in jobs:
print(job+":", pods_by_job[job])
if __name__ == '__main__':
main()