def cj_out(t, ns, res, output, show_type, show_labels): output_res=[[]] # header if ns == '_all': output_res[0].append('NAMESPACE') if show_labels: output_res[0].extend(['NAME','SCHEDULE','SUSPEND','ACTIVE', 'LAST SCHEDULE','AGE','LABELS']) else: output_res[0].extend(['NAME','SCHEDULE','SUSPEND','ACTIVE', 'LAST SCHEDULE','AGE']) # resources for r in res: cj = r['res'] row = [] # namespace (for --all-namespaces) if ns == '_all': row.append(cj['metadata']['namespace']) # name if show_type: row.append(t + '/' + cj['metadata']['name']) else: row.append(cj['metadata']['name']) # schedule try: row.append(cj['spec']['schedule']) except: row.append('??') # suspend try: row.append(cj['spec']['suspend']) except: row.append('??') # active try: row.append(len(cj['status']['active'])) except: row.append('0') # last schedule try: ct = str(cj['status']['lastScheduleTime']) ts = r['gen_ts'] row.append(age(ct,ts)) except: row.append('Unknown') # age try: ct = str(cj['metadata']['creationTimestamp']) ts = r['gen_ts'] row.append(age(ct,ts)) except: row.append('Unknown') # show-labels if show_labels: row.append(extract_labels(cj)) output_res.append(row) print(tabulate(output_res,tablefmt="plain"))
def job_out(t, ns, res, output, show_type, show_labels): output_res = [[]] # header if ns == "_all": output_res[0].append("NAMESPACE") if show_labels: output_res[0].extend( ["NAME", "COMPLETIONS", "DURATION", "AGE", "LABELS"]) else: output_res[0].extend(["NAME", "COMPLETIONS", "DURATION", "AGE"]) # resources for r in res: job = r["res"] row = [] # namespace (for --all-namespaces) if ns == "_all": row.append(job["metadata"]["namespace"]) # name if show_type: row.append(t + "/" + job["metadata"]["name"]) else: row.append(job["metadata"]["name"]) # completions try: # test numerator try: comp_num = str(job["status"]["succeeded"]) except: comp_num = "0" # append denominator comp_den = str(job["spec"]["completions"]) row.append(comp_num + "/" + comp_den) except: row.append("??") # duration try: st = str(job["status"]["startTime"]) ct = str(job["status"]["completionTime"]) row.append(age(st, ct, ts2_type="iso")) except: row.append("Unknown") # age try: ct = str(job["status"]["startTime"]) ts = r["gen_ts"] row.append(age(ct, ts)) except: row.append("Unknown") # show-labels if show_labels: row.append(extract_labels(job)) output_res.append(row) print(tabulate(output_res, tablefmt="plain"))
def job_out(t, ns, res, output, show_type, show_labels): output_res = [[]] # header if ns == '_all': output_res[0].append('NAMESPACE') if show_labels: output_res[0].extend( ['NAME', 'COMPLETIONS', 'DURATION', 'AGE', 'LABELS']) else: output_res[0].extend(['NAME', 'COMPLETIONS', 'DURATION', 'AGE']) # resources for r in res: job = r['res'] row = [] # namespace (for --all-namespaces) if ns == '_all': row.append(job['metadata']['namespace']) # name if show_type: row.append(t + '/' + job['metadata']['name']) else: row.append(job['metadata']['name']) # completions try: # test numerator try: comp_num = str(job['status']['succeeded']) except: comp_num = '0' # append denominator comp_den = str(job['spec']['completions']) row.append(comp_num + '/' + comp_den) except: row.append('??') # duration try: st = str(job['status']['startTime']) ct = str(job['status']['completionTime']) row.append(age(st, ct, ts2_type='iso')) except: row.append('Unknown') # age try: ct = str(job['status']['startTime']) ts = r['gen_ts'] row.append(age(ct, ts)) except: row.append('Unknown') # show-labels if show_labels: row.append(extract_labels(job)) output_res.append(row) print(tabulate(output_res, tablefmt="plain"))
def mwhc_out(t, ns, res, output, show_type, show_labels): output_res = [[]] # header output_res[0].extend(['NAME', 'WEBHOOKS', 'AGE']) # resources for r in res: mwhc = r['res'] row = [] # name if show_type: row.append(t + '/' + mwhc['metadata']['name']) else: row.append(mwhc['metadata']['name']) # webhooks if 'webhooks' in mwhc: webhooks = len(mwhc['webhooks']) else: webhooks = 0 row.append(webhooks) # age try: ct = str(mwhc['metadata']['creationTimestamp']) ts = r['gen_ts'] row.append(age(ct, ts)) except: row.append('Unknown') output_res.append(row) print(tabulate(output_res, tablefmt="plain"))
def hpa_out(t, ns, res, output, show_type): output_res=[[]] # header if ns == '_all': output_res[0].append('NAMESPACE') output_res[0].extend(['NAME','REFERENCE','TARGETS','MINPODS', 'MAXPODS','REPLICAS','AGE']) # resources for r in res: hpa = r['res'] row = [] # namespace (for --all-namespaces) if ns == '_all': row.append(hpa['metadata']['namespace']) # name if show_type: row.append(t + '/' + hpa['metadata']['name']) else: row.append(hpa['metadata']['name']) # reference try: row.append(hpa['spec']['scaleTargetRef']['kind'] + '/' + hpa['spec']['scaleTargetRef']['name']) except: row.append('??') # targets try: # test numerator try: target_num = str(hpa['status']['currentCPUUtilizationPercentage']) + '%' except: target_num = '<unknown>' # append denominator target_den = str(hpa['spec']['targetCPUUtilizationPercentage']) + '%' row.append(target_num + '/' + target_den) except: row.append('??') # minpods try: row.append(hpa['spec']['minReplicas']) except: row.append('??') # maxpods try: row.append(hpa['spec']['maxReplicas']) except: row.append('??') # replicas try: row.append(hpa['status']['currentReplicas']) except: row.append('??') # age try: ct = str(hpa['metadata']['creationTimestamp']) ts = r['gen_ts'] row.append(age(ct,ts)) except: row.append('Unknown') output_res.append(row) print(tabulate(output_res,tablefmt="plain"))
def mwhc_out(t, ns, res, output, show_type, show_labels): output_res = [[]] # header output_res[0].extend(["NAME", "WEBHOOKS", "AGE"]) # resources for r in res: mwhc = r["res"] row = [] # name if show_type: row.append(t + "/" + mwhc["metadata"]["name"]) else: row.append(mwhc["metadata"]["name"]) # webhooks if "webhooks" in mwhc: webhooks = len(mwhc["webhooks"]) else: webhooks = 0 row.append(webhooks) # age try: ct = str(mwhc["metadata"]["creationTimestamp"]) ts = r["gen_ts"] row.append(age(ct, ts)) except: row.append("Unknown") output_res.append(row) print(tabulate(output_res, tablefmt="plain"))
def opgrp_out(*args): """ Operator OperatorGroups parser. """ ns = args[1] show_labels = args[5] output_res = _build_output_res(*args, fields=["NAME", "AGE"]) for r in args[2]: # resource rs = r["res"] row = [] if ns == "_all": row.append(rs["metadata"]["namespace"]) row.append(rs["metadata"]["name"]) row.append(age(rs["metadata"]["creationTimestamp"], r["gen_ts"])) if show_labels: row.append(extract_labels(rs)) output_res.append(row) print(tabulate(output_res, tablefmt="plain"))
def simple_out(t, ns, res, output, show_type): output_res = [[]] # header if ns == '_all': output_res[0].append('NAMESPACE') output_res[0].extend(['NAME', 'AGE']) # resources for r in res: p = r['res'] row = [] # namespace (for --all-namespaces) if ns == '_all': row.append(p['metadata']['namespace']) # name if show_type: row.append(t + '/' + p['metadata']['name']) else: row.append(p['metadata']['name']) # age try: ct = str(p['metadata']['creationTimestamp']) ts = r['gen_ts'] row.append(age(ct, ts)) except: row.append('Unknown') output_res.append(row) print(tabulate(output_res, tablefmt="plain"))
def simple_out(t, ns, res, output, show_type, show_labels): output_res = [[]] # header if ns == "_all": output_res[0].append("NAMESPACE") output_res[0].extend(["NAME", "AGE"]) # resources for r in res: p = r["res"] row = [] # namespace (for --all-namespaces) if ns == "_all": row.append(p["metadata"]["namespace"]) # name if show_type: row.append(t + "/" + p["metadata"]["name"]) else: row.append(p["metadata"]["name"]) # age try: ct = str(p["metadata"]["creationTimestamp"]) ts = r["gen_ts"] row.append(age(ct, ts)) except: row.append("Unknown") output_res.append(row) print(tabulate(output_res, tablefmt="plain"))
def opcatsrc_out(*args): """ Operator CatalogSources parser. """ ns = args[1] show_labels = args[5] output_res = _build_output_res( *args, fields=["NAME", "DISPLAY", "TYPE", "PUBLISHER", "AGE"]) for r in args[2]: # resource rs = r["res"] row = [] if ns == "_all": row.append(rs["metadata"]["namespace"]) row.append(rs["metadata"]["name"]) row.append(rs["spec"]["displayName"]) row.append(rs["spec"]["sourceType"]) row.append(rs["spec"]["publisher"]) row.append(age(rs["metadata"]["creationTimestamp"], r["gen_ts"])) if show_labels: row.append(extract_labels(rs)) output_res.append(row) print(tabulate(output_res, tablefmt="plain"))
def build_out(t, ns, res, output, show_type, show_labels): output_res = [[]] # header if ns == '_all': output_res[0].append('NAMESPACE') if show_labels: output_res[0].extend([ 'NAME', 'TYPE', 'FROM', 'STATUS', 'STARTED', 'DURATION', 'LABELS' ]) else: output_res[0].extend( ['NAME', 'TYPE', 'FROM', 'STATUS', 'STARTED', 'DURATION']) # resources for r in res: build = r['res'] row = [] # namespace (for --all-namespaces) if ns == '_all': row.append(build['metadata']['namespace']) # name if show_type: row.append(t + '/' + build['metadata']['name']) else: row.append(build['metadata']['name']) # type try: row.append(build['spec']['strategy']['type']) except: row.append('??') # from try: row.append(build['spec']['source']['type']) except: row.append('??') # status try: row.append(build['status']['phase']) except: row.append('??') # started try: ct = str(build['status']['startTimestamp']) ts = r['gen_ts'] row.append(age(ct, ts)) except: row.append('Unknown') # duration try: row.append( str(int((build['status']['duration']) / 1000000000)) + 's') except: row.append('Unknown') # show-labels if show_labels: row.append(extract_labels(build)) output_res.append(row) print(tabulate(output_res, tablefmt="plain"))
def pv_out(t, ns, res, output, show_type): output_res=[] # header # we will append the header array at last after sorting header = ['NAME','CAPACITY','ACCESS MODES','RECLAIM POLICY','STATUS','CLAIM','STORAGECLASS','REASON','AGE'] # resources for r in res: p = r['res'] row = [] # name if show_type: row.append(t + '/' + p['metadata']['name']) else: row.append(p['metadata']['name']) # capacity row.append(p['spec']['capacity']['storage']) # access access = ",".join(p['spec']['accessModes']) if access == 'ReadWriteOnce': row.append('RWO') elif access == 'ReadOnlyMany': row.append('ROX') elif access == 'ReadWriteMany': row.append('RWX') # reclaim row.append(p['spec']['persistentVolumeReclaimPolicy']) # status row.append(p['status']['phase']) # claim try: cr_name = p['spec']['claimRef']['name'] cr_ns = p['spec']['claimRef']['namespace'] claim = cr_ns + '/' + cr_name except: claim = '' row.append(claim) # storage class if 'storageClassName' in p['spec']: row.append(p['spec']['storageClassName']) else: row.append('') # reason (?) TODO need sample of this yaml row.append('') # age try: ct = p['metadata']['creationTimestamp'] ts = r['gen_ts'] row.append(age(ct,ts)) except: row.append('Unknown') output_res.append(row) # sort by 1st column sorted_output = sorted(output_res) sorted_output.insert(0,header) print(tabulate(sorted_output,tablefmt="plain"))
def rc_out(t, ns, res, output, show_type): output_res = [[]] # header if ns == '_all': output_res[0].append('NAMESPACE') output_res[0].extend(['NAME', 'DESIRED', 'CURRENT', 'READY', 'AGE']) if output == 'wide': output_res[0].extend(['CONTAINERS', 'IMAGES']) # resources for r in res: rc = r['res'] row = [] # namespace (for --all-namespaces) if ns == '_all': row.append(rc['metadata']['namespace']) # name if show_type: row.append(t + '/' + rc['metadata']['name']) else: row.append(rc['metadata']['name']) # desired try: row.append(rc['status']['replicas']) except: row.append('0') # current try: row.append(rc['status']['fullyLabeledReplicas']) except: row.append('0') # ready try: row.append(rc['status']['readyReplicas']) except: row.append('0') # age try: ct = str(rc['metadata']['creationTimestamp']) ts = r['gen_ts'] row.append(age(ct, ts)) except: row.append('Unknown') # -o wide if output == 'wide': # container: conts = [ c['name'] for c in rc['spec']['template']['spec']['containers'] ] row.append(','.join(conts)) # images: images = [ c['image'] for c in rc['spec']['template']['spec']['containers'] ] row.append(','.join(images)) output_res.append(row) print(tabulate(output_res, tablefmt="plain"))
def co_out(t, ns, res, output, show_type, show_labels): # Generate output table if -o not set or 'wide' # We will create an array of array and then print if with tabulate # header output_res = [[ "NAME", "VERSION", "AVAILABLE", "PROGRESSING", "DEGRADED", "SINCE" ]] # resources for r in res: co = r["res"] row = [] # name if show_type: row.append(t + "/" + co["metadata"]["name"]) else: row.append(co["metadata"]["name"]) # version try: vers = co["status"]["versions"] ver = next(v["version"] for v in vers if v["name"] == "operator") row.append(ver) except: row.append("") # available,progressing,degraded,since try: transitions = [] cond = co["status"]["conditions"] for c in cond: if c["type"] == "Degraded": dg = c["status"] transitions.append(str(c["lastTransitionTime"])) elif c["type"] == "Progressing": pr = c["status"] transitions.append(str(c["lastTransitionTime"])) elif c["type"] == "Available": av = c["status"] transitions.append(str(c["lastTransitionTime"])) row.append(av) row.append(pr) row.append(dg) # since latest_trans = None for tr in transitions: if latest_trans is None: latest_trans = tr else: if parse(tr) > parse(latest_trans): latest_trans = tr since = age(latest_trans, r["gen_ts"]) row.append(since) except: row.append("") row.append("") row.append("") row.append("") output_res.append(row) print(tabulate(output_res, tablefmt="plain"))
def build_out(t, ns, res, output, show_type, show_labels): output_res = [[]] # header if ns == "_all": output_res[0].append("NAMESPACE") if show_labels: output_res[0].extend([ "NAME", "TYPE", "FROM", "STATUS", "STARTED", "DURATION", "LABELS" ]) else: output_res[0].extend( ["NAME", "TYPE", "FROM", "STATUS", "STARTED", "DURATION"]) # resources for r in res: build = r["res"] row = [] # namespace (for --all-namespaces) if ns == "_all": row.append(build["metadata"]["namespace"]) # name if show_type: row.append(t + "/" + build["metadata"]["name"]) else: row.append(build["metadata"]["name"]) # type try: row.append(build["spec"]["strategy"]["type"]) except: row.append("??") # from try: row.append(build["spec"]["source"]["type"]) except: row.append("??") # status try: row.append(build["status"]["phase"]) except: row.append("??") # started try: ct = str(build["status"]["startTimestamp"]) ts = r["gen_ts"] row.append(age(ct, ts)) except: row.append("Unknown") # duration try: row.append( str(int((build["status"]["duration"]) / 1000000000)) + "s") except: row.append("Unknown") # show-labels if show_labels: row.append(extract_labels(build)) output_res.append(row) print(tabulate(output_res, tablefmt="plain"))
def sc_out(t, ns, res, output, show_type, show_labels): output_res = [] # header if show_labels: header = [ "NAME", "PROVISIONER", "RECLAIMPOLICY", "VOLUMEBINDINGMODE", "ALLOWVOLUMEEXPANSION", "AGE", "LABELS", ] else: header = [ "NAME", "PROVISIONER", "RECLAIMPOLICY", "VOLUMEBINDINGMODE", "ALLOWVOLUMEEXPANSION", "AGE", ] # resources for r in res: sc = r["res"] row = [] # name if show_type: row.append(t + "/" + sc["metadata"]["name"]) else: row.append(sc["metadata"]["name"]) # provisioner row.append(sc["provisioner"]) # reclaimpolicy row.append(sc["reclaimPolicy"]) # volumebindingmode row.append(sc["volumeBindingMode"]) # allowvolumeexpansion try: row.append(sc["allowVolumeExpansion"]) except: row.append("false") # age try: ct = str(sc["metadata"]["creationTimestamp"]) ts = r["gen_ts"] row.append(age(ct, ts)) except: row.append("Unknown") # show-labels if show_labels: row.append(extract_labels(sc)) output_res.append(row) # sort by 1st column sorted_output = sorted(output_res) sorted_output.insert(0, header) print(tabulate(sorted_output, tablefmt="plain"))
def co_out(t, ns, res, output, show_type): # Generate output table if -o not set or 'wide' # We will create an array of array and then print if with tabulate # header output_res = [[ 'NAME', 'VERSION', 'AVAILABLE', 'PROGRESSING', 'DEGRADED', 'SINCE' ]] # resources for r in res: co = r['res'] row = [] # name if show_type: row.append(t + '/' + co['metadata']['name']) else: row.append(co['metadata']['name']) # version try: vers = co['status']['versions'] ver = next(v['version'] for v in vers if v['name'] == 'operator') row.append(ver) except: row.append('') # available,progressing,degraded,since try: transitions = [] cond = co['status']['conditions'] for c in cond: if c['type'] == 'Degraded': dg = c['status'] transitions.append(str(c['lastTransitionTime'])) elif c['type'] == 'Progressing': pr = c['status'] transitions.append(str(c['lastTransitionTime'])) elif c['type'] == 'Available': av = c['status'] transitions.append(str(c['lastTransitionTime'])) row.append(av) row.append(pr) row.append(dg) # since latest_trans = None for tr in transitions: if latest_trans is None: latest_trans = tr else: if parse(tr) > parse(latest_trans): latest_trans = tr since = age(latest_trans, r['gen_ts']) row.append(since) except: row.append('') row.append('') row.append('') row.append('') output_res.append(row) print(tabulate(output_res, tablefmt="plain"))
def cv_out(t, ns, res, output, show_type, show_labels): output_res = [[]] # header output_res[0].extend( ["NAME", "VERSION", "AVAILABLE", "PROGRESSING", "SINCE", "STATUS"]) # resources for r in res: cv = r["res"] row = [] # name if show_type: row.append(t + "/" + cv["metadata"]["name"]) else: row.append(cv["metadata"]["name"]) # version cur_ver = "" history = cv["status"]["history"] for h in history: if h["state"] == "Completed": cur_ver = h["version"] break row.append(cur_ver) # available,progressing,status avail = "" prog = "" status = "" transitions = [] conds = cv["status"]["conditions"] for c in conds: if c["type"] == "Available": avail = c["status"] transitions.append(str(c["lastTransitionTime"])) elif c["type"] == "Progressing": prog = c["status"] status = c["message"] transitions.append(str(c["lastTransitionTime"])) elif c["type"] == "Failing": transitions.append(str(c["lastTransitionTime"])) row.append(avail) row.append(prog) # since latest_trans = None for t in transitions: if latest_trans is None: latest_trans = t else: if parse(t) > parse(latest_trans): latest_trans = t since = age(latest_trans, r["gen_ts"]) row.append(since) # status row.append(status) output_res.append(row) print(tabulate(output_res, tablefmt="plain"))
def cv_out(t, ns, res, output, show_type, show_labels): output_res = [[]] # header output_res[0].extend( ['NAME', 'VERSION', 'AVAILABLE', 'PROGRESSING', 'SINCE', 'STATUS']) # resources for r in res: cv = r['res'] row = [] # name if show_type: row.append(t + '/' + cv['metadata']['name']) else: row.append(cv['metadata']['name']) # version cur_ver = '' history = cv['status']['history'] for h in history: if h['state'] == 'Completed': cur_ver = h['version'] break row.append(cur_ver) # available,progressing,status avail = '' prog = '' status = '' transitions = [] conds = cv['status']['conditions'] for c in conds: if c['type'] == 'Available': avail = c['status'] transitions.append(str(c['lastTransitionTime'])) elif c['type'] == 'Progressing': prog = c['status'] status = c['message'] transitions.append(str(c['lastTransitionTime'])) elif c['type'] == 'Failing': transitions.append(str(c['lastTransitionTime'])) row.append(avail) row.append(prog) # since latest_trans = None for t in transitions: if latest_trans is None: latest_trans = t else: if parse(t) > parse(latest_trans): latest_trans = t since = age(latest_trans, r['gen_ts']) row.append(since) # status row.append(status) output_res.append(row) print(tabulate(output_res, tablefmt="plain"))
def eps_out(t, ns, res, output, show_type): output_res = [[]] # header if ns == '_all': output_res[0].append('NAMESPACE') output_res[0].extend(['NAME', 'ADDRESSTYPE', 'PORTS', 'ENDPOINTS', 'AGE']) # resources for r in res: eps = r['res'] row = [] # namespace (for --all-namespaces) if ns == '_all': row.append(eps['metadata']['namespace']) # name if show_type: row.append(t + '/' + eps['metadata']['name']) else: row.append(eps['metadata']['name']) # addresstype row.append(eps['addressType']) # ports ports = [] if 'ports' in eps and eps['ports'] is not None: for s in eps['ports']: ports.append(str(str(s['port']))) if len(ports) == 0: row.append('<none>') elif len(ports) < 4: row.append(','.join(ports)) else: row.append(','.join(ports[:3]) + ' + ' + str(len(ports) - 3) + ' more...') # endpoints endpoints = [] if 'endpoints' in eps and eps['endpoints'] is not None: for s in eps['endpoints']: endpoints.append(str(s['addresses'][0])) if len(endpoints) == 0: row.append('<none>') elif len(endpoints) < 4: row.append(','.join(endpoints)) else: row.append(','.join(endpoints[:3]) + ' + ' + str(len(endpoints) - 3) + ' more...') # age try: ct = str(eps['metadata']['creationTimestamp']) ts = r['gen_ts'] row.append(age(ct, ts)) except: row.append('Unknown') output_res.append(row) print(tabulate(output_res, tablefmt="plain"))
def eps_out(t, ns, res, output, show_type, show_labels): output_res = [[]] # header if ns == "_all": output_res[0].append("NAMESPACE") output_res[0].extend(["NAME", "ADDRESSTYPE", "PORTS", "ENDPOINTS", "AGE"]) # resources for r in res: eps = r["res"] row = [] # namespace (for --all-namespaces) if ns == "_all": row.append(eps["metadata"]["namespace"]) # name if show_type: row.append(t + "/" + eps["metadata"]["name"]) else: row.append(eps["metadata"]["name"]) # addresstype row.append(eps["addressType"]) # ports ports = [] if "ports" in eps and eps["ports"] is not None: for s in eps["ports"]: ports.append(str(str(s["port"]))) if len(ports) == 0: row.append("<none>") elif len(ports) < 4: row.append(",".join(ports)) else: row.append(",".join(ports[:3]) + " + " + str(len(ports) - 3) + " more...") # endpoints endpoints = [] if "endpoints" in eps and eps["endpoints"] is not None: for s in eps["endpoints"]: endpoints.append(str(s["addresses"][0])) if len(endpoints) == 0: row.append("<none>") elif len(endpoints) < 4: row.append(",".join(endpoints)) else: row.append( ",".join(endpoints[:3]) + " + " + str(len(endpoints) - 3) + " more..." ) # age try: ct = str(eps["metadata"]["creationTimestamp"]) ts = r["gen_ts"] row.append(age(ct, ts)) except: row.append("Unknown") output_res.append(row) print(tabulate(output_res, tablefmt="plain"))
def pvc_out(t, ns, res, output, show_type): output_res = [[]] # header if ns == '_all': output_res[0].append('NAMESPACE') output_res[0].extend([ 'NAME', 'STATUS', 'VOLUME', 'CAPACITY', 'ACCESS MODES', 'STORAGECLASS', 'AGE' ]) if output == 'wide': output_res[0].extend(['VOLUMEMODE']) # resources for r in res: p = r['res'] row = [] # namespace (for --all-namespaces) if ns == '_all': row.append(p['metadata']['namespace']) # name if show_type: row.append(t + '/' + p['metadata']['name']) else: row.append(p['metadata']['name']) # status row.append(p['status']['phase']) # volume row.append(p['spec']['volumeName']) # capacity row.append(p['status']['capacity']['storage']) # access access = ",".join(p['spec']['accessModes']) if access == 'ReadWriteOnce': row.append('RWO') elif access == 'ReadOnlyMany': row.append('ROX') elif access == 'ReadWriteMany': row.append('RWX') # storage class if 'storageClassName' in p['spec']: row.append(p['spec']['storageClassName']) else: row.append('') # age try: ct = p['metadata']['creationTimestamp'] ts = r['gen_ts'] row.append(age(ct, ts)) except: row.append('Unknown') # -o wide if output == 'wide': row.append(p['spec']['volumeMode']) output_res.append(row) print(tabulate(output_res, tablefmt="plain"))
def machine_out(t, ns, res, output, show_type, show_labels): output_res = [] # header header = [] if ns == "_all": header.append("NAMESPACE") if show_labels: header.extend( ["NAME", "PHASE", "TYPE", "REGION", "ZONE", "AGE", "LABELS"]) else: header.extend(["NAME", "PHASE", "TYPE", "REGION", "ZONE", "AGE"]) # resources for r in res: m = r["res"] row = [] # namespace (for --all-namespaces) if ns == "_all": row.append(m["metadata"]["namespace"]) # name if show_type: row.append(t + "/" + m["metadata"]["name"]) else: row.append(m["metadata"]["name"]) # phase if "status" in m and "phase" in m["status"]: row.append(m["status"]["phase"]) else: row.append("") # type, region, zone for l in [ "machine.openshift.io/instance-type", "machine.openshift.io/region", "machine.openshift.io/zone", ]: if "labels" in m["metadata"] and l in m["metadata"]["labels"]: row.append(m["metadata"]["labels"][l]) else: row.append("") try: ct = str(m["metadata"]["creationTimestamp"]) ts = r["gen_ts"] row.append(age(ct, ts)) except: row.append("Unknown") # show-labels if show_labels: row.append(extract_labels(m)) output_res.append(row) # sort by NAME column whose index will be # 1 if we are showing namespaces otherwise 0 ni = 1 if ns == "_all" else 0 sorted_output = sorted(output_res, key=lambda x: x[ni]) sorted_output.insert(0, header) print(tabulate(sorted_output, tablefmt="plain"))
def rs_out(t, ns, res, output, show_type): output_res=[[]] # header if ns == '_all': output_res[0].append('NAMESPACE') output_res[0].extend(['NAME','DESIRED','CURRENT','READY','AGE']) if output == 'wide': output_res[0].extend(['CONTAINERS', 'IMAGES','SELECTOR']) # resources for r in res: rs = r['res'] row = [] # namespace (for --all-namespaces) if ns == '_all': row.append(rs['metadata']['namespace']) # name if show_type: row.append(t + '/' + rs['metadata']['name']) else: row.append(rs['metadata']['name']) # desired, current, ready for x in ['replicas', 'availableReplicas', 'readyReplicas']: if x in rs['status']: row.append(rs['status'][x]) else: row.append('0') # age try: ct = str(rs['metadata']['creationTimestamp']) ts = r['gen_ts'] row.append(age(ct,ts)) except: row.append('Unknown') # -o wide if output == 'wide': # containers conts = [ c['name'] for c in rs['spec']['template']['spec']['containers'] ] row.append(','.join(conts)) # images images = [ c['image'] for c in rs['spec']['template']['spec']['containers'] ] row.append(','.join(images)) # selector try: ml = ','.join( [ k+'='+v for k,v in rs['spec']['selector']['matchLabels'].items() ] ) except: ml = '?' row.append(ml) output_res.append(row) print(tabulate(output_res,tablefmt="plain"))
def is_out(t, ns, res, output, show_type, show_labels): output_res = [[]] # header if ns == '_all': output_res[0].append('NAMESPACE') if show_labels: output_res[0].extend( ['NAME', 'IMAGE REPOSITORY', 'TAGS', 'UPDATED', 'LABELS']) else: output_res[0].extend(['NAME', 'IMAGE REPOSITORY', 'TAGS', 'UPDATED']) # resources for r in res: is_ = r['res'] row = [] # namespace (for --all-namespaces) if ns == '_all': row.append(is_['metadata']['namespace']) # name if show_type: row.append(t + '/' + is_['metadata']['name']) else: row.append(is_['metadata']['name']) # image repository try: row.append(is_['status']['publicDockerImageRepository']) except: row.append('') # tags try: tags = [] for tag in is_['status']['tags']: tags.append(tag['tag']) row.append(','.join(tags)) except: row.append('') # updated ## TODO: should update this to parse list of tags and determine latest updated image, rather than use metadata.creationTimestamp try: ## TODO: replace this check when reimplementing latest updated image if len(is_['status']['tags']) > 0: ct = str(is_['metadata']['creationTimestamp']) ts = r['gen_ts'] row.append(age(ct, ts)) else: row.append('') except: row.append('') # show-labels if show_labels: row.append(extract_labels(is_)) output_res.append(row) print(tabulate(output_res, tablefmt="plain"))
def csr_out(t, ns, res, output, show_type, show_labels): output_res = [] # header header = ["NAME", "AGE", "SIGNERNAME", "REQUESTOR", "CONDITION"] # resources for r in res: csr = r["res"] row = [] # name if show_type: row.append(t + "/" + csr["metadata"]["name"]) else: row.append(csr["metadata"]["name"]) # age try: ct = str(csr["metadata"]["creationTimestamp"]) ts = r["gen_ts"] row.append(age(ct, ts)) except: row.append("Unknown") # signername signername = csr["spec"]["signerName"] row.append(signername) # requestor requestor = csr["spec"]["username"] row.append(requestor) # conditions condition = "" # empty status block means this request is still pending if csr.get("status", {}) == {}: condition = "Pending" else: conds = csr["status"]["conditions"] for c in conds: if c["type"] == "Approved": condition = "Approved,Issued" elif c["type"] == "Denied": condition = "Denied" elif c["type"] == "Failed": condition = "Failed" else: condition = "Unknown" row.append(condition) output_res.append(row) # print(tabulate(output_res,tablefmt="plain")) # sort by 1st column (name) sorted_output = sorted(output_res, key=lambda x: x[0]) sorted_output.insert(0, header) print(tabulate(sorted_output, tablefmt="plain"))
def machineset_out(t, ns, res, output, show_type): output_res = [[]] # header if ns == '_all': output_res[0].append('NAMESPACE') output_res[0].extend( ['NAME', 'DESIRED', 'CURRENT', 'READY', 'AVAILABLE', 'AGE']) # resources for r in res: ms = r['res'] row = [] # namespace (for --all-namespaces) if ns == '_all': row.append(ms['metadata']['namespace']) # name if show_type: row.append(t + '/' + ms['metadata']['name']) else: row.append(ms['metadata']['name']) # desired if 'spec' in ms and 'replicas' in ms['spec']: row.append(ms['spec']['replicas']) else: row.append(0) if 'status' in ms: # current if 'replicas' in ms['status']: row.append(ms['status']['replicas']) else: row.append(0) # ready if 'readyReplicas' in ms['status']: row.append(ms['status']['readyReplicas']) else: row.append('') # available if 'availableReplicas' in ms['status']: row.append(ms['status']['availableReplicas']) else: row.append('') else: row.extend(['?', '?', '?']) # age try: ct = str(ms['metadata']['creationTimestamp']) ts = r['gen_ts'] row.append(age(ct, ts)) except: row.append('Unknown') output_res.append(row) print(tabulate(output_res, tablefmt="plain"))
def mcp_out(t, ns, res, output, show_type): output_res = [[]] # header output_res[0].extend([ 'NAME', 'CONFIG', 'UPDATED', 'UPDATING', 'DEGRADED', 'MACHINECOUNT', 'READYMACHINECOUNT', 'UPDATEDMACHINECOUNT', 'DEGRADEDMACHINECOUNT', 'AGE' ]) # resources for r in res: mcp = r['res'] row = [] # name if show_type: row.append(t + '/' + mcp['metadata']['name']) else: row.append(mcp['metadata']['name']) # config try: row.append(mcp['spec']['configuration']['name']) except: row.append('') # updated, updating, degraded updated = '' updating = '' degraded = '' conds = mcp['status']['conditions'] for c in conds: if c['type'] == 'Updated': updated = c['status'] if c['type'] == 'Updating': updating = c['status'] if c['type'] == 'Degraded': degraded = c['status'] row.append(updated) row.append(updating) row.append(degraded) # machine count row.append(mcp['status']['machineCount']) row.append(mcp['status']['readyMachineCount']) row.append(mcp['status']['updatedMachineCount']) row.append(mcp['status']['degradedMachineCount']) # age try: ct = str(mcp['metadata']['creationTimestamp']) ts = r['gen_ts'] row.append(age(ct, ts)) except: row.append('Unknown') output_res.append(row) print(tabulate(output_res, tablefmt="plain"))
def ep_out(t, ns, res, output, show_type, show_labels): output_res = [[]] # header if ns == '_all': output_res[0].append('NAMESPACE') if show_labels: output_res[0].extend(['NAME', 'ENDPOINTS', 'AGE', 'LABELS']) else: output_res[0].extend(['NAME', 'ENDPOINTS', 'AGE']) # resources for r in res: ep = r['res'] row = [] # namespace (for --all-namespaces) if ns == '_all': row.append(ep['metadata']['namespace']) # name if show_type: row.append(t + '/' + ep['metadata']['name']) else: row.append(ep['metadata']['name']) # endpoints endpoints = [] if 'subsets' in ep: for s in ep['subsets']: if 'addresses' in s and 'ports' in s: for a in s['addresses']: for p in s['ports']: endpoints.append( str(a['ip']) + ':' + str(p['port'])) if len(endpoints) == 0: row.append('<none>') elif len(endpoints) < 4: row.append(','.join(endpoints)) else: row.append(','.join(endpoints[:3]) + ' + ' + str(len(endpoints) - 3) + ' more...') # age try: ct = str(ep['metadata']['creationTimestamp']) ts = r['gen_ts'] row.append(age(ct, ts)) except: row.append('Unknown') # show-labels if show_labels: row.append(extract_labels(ep)) output_res.append(row) print(tabulate(output_res, tablefmt="plain"))
def cephclusters_out(t, ns, res, output, show_type, show_labels): output_res = [[]] # header if ns == "_all": output_res[0].append("NAMESPACE") if show_labels: output_res[0].extend([ "NAME", "DATADIRHOSTPATH", "MONCOUNT", "AGE", "PHASE", "MESSAGE", "HEALTH", "LABELS" ]) else: output_res[0].extend([ "NAME", "DATADIRHOSTPATH", "MONCOUNT", "AGE", "PHASE", "MESSAGE", "HEALTH" ]) # resources for r in res: cs = r["res"] row = [] # namespace (for --all-namespaces) if ns == "_all": row.append(cs["metadata"]["namespace"]) # name if show_type: row.append(t + "/" + cs["metadata"]["name"]) else: row.append(cs["metadata"]["name"]) # dataDirHostPath row.append(cs["spec"]["dataDirHostPath"]) # mon.count row.append(cs["spec"]["mon"]["count"]) # age try: ct = str(cs["metadata"]["creationTimestamp"]) ts = r["gen_ts"] row.append(age(ct, ts)) except: row.append("Unknown") # phase row.append(cs["status"]["phase"]) # message row.append(cs["status"]["message"]) # health row.append(cs["status"]["ceph"]["health"]) if show_labels: row.append(extract_labels(cs)) output_res.append(row) print(tabulate(output_res, tablefmt="plain"))