go.ligato.io/vpp-agent/v3@v3.5.0/tests/robot/libraries/kubernetes/kube_parser.py (about) 1 """ 2 Library to parse output (stdout) of kubectl adn kubeadm command 3 4 TODO: Do not use this, call the following (example): 5 kubectl get pod -l "app=test-server" -o jsonpath='{.items[0].status.podIP}' 6 """ 7 8 9 def _general_parser(stdout): 10 """Parse any kubectl output with column like output""" 11 lines = stdout.splitlines() 12 result = {} 13 kws = lines[0].split() 14 for line in lines[1:]: 15 parsed_line = line.split() 16 item = {} 17 for i in range(len(kws)): 18 item[kws[i]] = parsed_line[i] 19 name = item.pop('NAME') 20 result[name] = item 21 return result 22 23 24 def parse_kubectl_get_pods(stdout): 25 """Parse kubectl get pods output""" 26 lines = stdout.splitlines() 27 result = {} 28 if "No resources found." in stdout: 29 return result 30 kws = lines[0].split() 31 for line in lines[1:]: 32 parsed_line = line.split() 33 item = {} 34 for i in range(len(kws)): 35 item[kws[i]] = parsed_line[i] 36 print item, kws 37 name = item.pop('NAME') 38 result[name] = item 39 return result 40 41 42 def parse_kubectl_get_pods_and_get_pod_name(stdout, pod_prefix): 43 """Get list of pod names with given prefix""" 44 pods = parse_kubectl_get_pods(stdout) 45 print pods 46 pod = [pod_name for pod_name, pod_value in pods.iteritems() 47 if pod_prefix in pod_name] 48 return pod 49 50 51 def parse_kubectl_get_nodes(stdout): 52 nodes_details = _general_parser(stdout) 53 return nodes_details 54 55 56 def parse_kubectl_describe_pod(stdout): 57 """Parse kubectl describe pod output""" 58 lines = stdout.splitlines() 59 result = {} 60 info = ["IP", "Name", "Status"] 61 for line in lines: 62 for item in info: 63 if line.startswith("{}:".format(item)): 64 result[item] = line.split(":")[-1].strip() 65 name = result.pop("Name") 66 return {name: result} 67 68 69 _CID = "Container ID:" 70 71 72 def parse_for_first_container_id(stdout): 73 lines = stdout.splitlines() 74 for line in lines: 75 stripline = line.strip() 76 if stripline.startswith(_CID): 77 return stripline[len(_CID):].strip().rpartition("//")[2] 78 79 80 def get_join_from_kubeadm_init(stdout): 81 """Parse kubeadm init output 82 83 Returns the join command, 84 """ 85 lines = stdout.splitlines() 86 join_cmd = [] 87 for line in lines: 88 if "kubeadm join --token" in line: 89 join_cmd.append(line) 90 if len(join_cmd) != 1: 91 raise Exception("Not expected result: {}".format(join_cmd) ) 92 return join_cmd[0]