github.com/munnerz/test-infra@v0.0.0-20190108210205-ce3d181dc989/gubernator/kubelet_parser.py (about) 1 #!/usr/bin/env python 2 # Copyright 2016 The Kubernetes Authors. 3 # 4 # Licensed under the Apache License, Version 2.0 (the "License"); 5 # you may not use this file except in compliance with the License. 6 # You may obtain a copy of the License at 7 # 8 # http://www.apache.org/licenses/LICENSE-2.0 9 # 10 # Unless required by applicable law or agreed to in writing, software 11 # distributed under the License is distributed on an "AS IS" BASIS, 12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 # See the License for the specific language governing permissions and 14 # limitations under the License. 15 16 import re 17 import json 18 19 import jinja2 20 21 import regex 22 23 def parse(lines, highlight_words, filters, objref_dict): 24 """ 25 Given filters returns indices of wanted lines from log 26 27 Args: 28 lines: array of log lines 29 highlight_words: array of words that need to be bolded 30 filters: dictionary of which filters to apply 31 objref_dict: a dictionary where the keys are possible filters 32 and the values are the words to be highlighted 33 Returns: 34 matched_lines: ordered array of indices of lines to display 35 highlight_words: updated highlight_words 36 """ 37 matched_lines = [] 38 39 if not filters["pod"] and objref_dict: 40 highlight_words = [] 41 42 # If the filter is on, look for it in the objref_dict 43 for k in filters: 44 if k != "pod" and filters[k] and k in objref_dict: 45 highlight_words.append(objref_dict[k]) 46 47 words_re = regex.combine_wordsRE(highlight_words) 48 49 for n, line in enumerate(lines): 50 if words_re.search(line): 51 matched_lines.append(n) 52 53 return matched_lines, highlight_words 54 55 56 def make_dict(data, pod_re, objref_dict): 57 """ 58 Given the log file and the failed pod name, returns a dictionary 59 containing the namespace, UID, and other information associated with the pod 60 and a bool indicating if the pod name string is in the log file. 61 62 This dictionary is lifted from the line with the ObjectReference 63 """ 64 pod_in_file = False 65 66 lines = unicode(jinja2.escape(data)).split('\n') 67 for line in lines: 68 if pod_re.search(line): 69 pod_in_file = True 70 objref = regex.objref(line) 71 containerID = regex.containerID(line) 72 if containerID and not objref_dict.get("ContainerID"): 73 objref_dict["ContainerID"] = containerID.group(1) 74 if objref: 75 objref_dict_re = objref.group(1) 76 objref_dict_re = re.sub(r'(\w+):', r'"\1": ', objref_dict_re) 77 objref_dict_re = objref_dict_re.replace('"', '"') 78 objref_dict_re = json.loads(objref_dict_re) 79 objref_dict_re.update(objref_dict) 80 return objref_dict_re, pod_in_file 81 82 return objref_dict, pod_in_file