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