@@ -2,14 +2,12 @@ package main
22
33import  (
44	"flag" 
5- 	"fmt" 
65	"net/http" 
76	"regexp" 
87	"strings" 
98
109	docker_client "docker.io/go-docker" 
1110	docker_types "docker.io/go-docker/api/types" 
12- 	docker_filters "docker.io/go-docker/api/types/filters" 
1311	"github.com/golang/glog" 
1412	"github.com/prometheus/client_golang/prometheus" 
1513	"github.com/prometheus/client_golang/prometheus/promhttp" 
@@ -18,10 +16,8 @@ import (
1816	"k8s.io/node-problem-detector/pkg/systemlogmonitor/logwatchers/types" 
1917)
2018
21- const  (
22- 	OOMMatchExpression    =  ".*killed as a result of limit of.*" 
23- 	PodExtractExpression  =  "^.+/pod(\\ w+\\ -\\ w+\\ -\\ w+\\ -\\ w+\\ -\\ w+)/.+$" 
24- 	PodUIDLabel           =  "io.kubernetes.pod.uid" 
19+ var  (
20+ 	kmesgRE  =  regexp .MustCompile ("/pod(\\ w+\\ -\\ w+\\ -\\ w+\\ -\\ w+\\ -\\ w+)/([a-f0-9]+) killed as a result of limit of /kubepods" )
2521)
2622
2723var  (
@@ -74,66 +70,43 @@ func main() {
7470	}
7571
7672	for  log  :=  range  logCh  {
77- 		podUID  :=  getPodUIDFromLog (log .Message )
78- 		if  podUID  !=  ""  {
79- 			container , err  :=  getContainerFromPod (podUID , dockerClient )
80- 
73+ 		podUID , containerID  :=  getContainerIDFromLog (log .Message )
74+ 		if  containerID  !=  ""  {
75+ 			container , err  :=  getContainer (containerID , dockerClient )
8176			if  err  !=  nil  {
82- 				glog .Warningf ("Could not get container for pod UID  %s: %v" , podUID , err )
77+ 				glog .Warningf ("Could not get container %s  for pod %s: %v" ,  containerID , podUID , err )
8378			} else  {
84- 				prometheusCount (container )
79+ 				prometheusCount (container . Config . Labels )
8580			}
8681		}
8782	}
8883}
8984
90- func  getPodUIDFromLog (log  string ) string  {
91- 	match , err  :=  regexp .MatchString (OOMMatchExpression , log )
92- 	if  err  !=  nil  {
93- 		return  "" 
94- 	}
95- 
96- 	var  ret  []string 
97- 	if  match  {
98- 		re  :=  regexp .MustCompile (PodExtractExpression )
99- 		ret  =  re .FindStringSubmatch (log )
100- 		if  len (ret ) ==  2  {
101- 			return  ret [1 ]
102- 		}
85+ func  getContainerIDFromLog (log  string ) (string , string ) {
86+ 	if  matches  :=  kmesgRE .FindStringSubmatch (log ); matches  !=  nil  {
87+ 		return  matches [1 ], matches [2 ]
10388	}
10489
105- 	return  "" 
90+ 	return  "" ,  "" 
10691}
10792
108- func  getContainerFromPod (podUID  string , cli  * docker_client.Client ) (docker_types.Container , error ) {
109- 	filters  :=  docker_filters .NewArgs ()
110- 	filters .Add ("label" , fmt .Sprintf ("%s=%s" , PodUIDLabel , podUID ))
111- 	filters .Add ("label" , fmt .Sprintf ("%s=%s" , "io.kubernetes.docker.type" , "container" ))
112- 
113- 	listOpts  :=  docker_types.ContainerListOptions {
114- 		Filters : filters ,
115- 	}
116- 
117- 	containers , err  :=  cli .ContainerList (context .Background (), listOpts )
93+ func  getContainer (containerID  string , cli  * docker_client.Client ) (docker_types.ContainerJSON , error ) {
94+ 	container , err  :=  cli .ContainerInspect (context .Background (), containerID )
11895	if  err  !=  nil  {
119- 		return  docker_types.Container {}, err 
120- 	}
121- 
122- 	if  len (containers ) <  1  {
123- 		return  docker_types.Container {}, fmt .Errorf ("There should be at least one container with UID %s" , podUID )
96+ 		return  docker_types.ContainerJSON {}, err 
12497	}
98+ 	return  container , nil 
12599
126- 	return  containers [0 ], nil 
127100}
128101
129- func  prometheusCount (container  docker_types. Container ) {
102+ func  prometheusCount (containerLabels   map [ string ] string ) {
130103	var  counter  prometheus.Counter 
131104	var  err  error 
132105
133106	var  labels  map [string ]string 
134107	labels  =  make (map [string ]string )
135108	for  key , label  :=  range  prometheusContainerLabels  {
136- 		labels [label ] =  container . Labels [key ]
109+ 		labels [label ] =  containerLabels [key ]
137110	}
138111
139112	glog .V (5 ).Infof ("Labels: %v\n " , labels )
0 commit comments