@@ -26,15 +26,14 @@ import (
2626 "github.com/golang/glog"
2727 "github.com/openshift/origin/pkg/util/proc"
2828 flag "github.com/spf13/pflag"
29- clientset "k8s.io/client-go/1.4/kubernetes"
30- "k8s.io/client-go/1.4/pkg/api"
31- "k8s.io/client-go/1.4/pkg/api/v1"
32- "k8s.io/client-go/1.4/pkg/apis/extensions/v1beta1"
33- "k8s.io/client-go/1.4/pkg/runtime"
34- "k8s.io/client-go/1.4/pkg/watch"
35- restclient "k8s.io/client-go/1.4/rest"
36- "k8s.io/client-go/1.4/tools/cache"
37- "k8s.io/client-go/1.4/tools/clientcmd"
29+ "golang.org/x/net/context"
30+ clientset "k8s.io/client-go/1.5/kubernetes"
31+ "k8s.io/client-go/1.5/pkg/api"
32+ "k8s.io/client-go/1.5/pkg/api/v1"
33+ "k8s.io/client-go/1.5/pkg/apis/extensions/v1beta1"
34+ restclient "k8s.io/client-go/1.5/rest"
35+ "k8s.io/client-go/1.5/tools/cache"
36+ "k8s.io/client-go/1.5/tools/clientcmd"
3837
3938 "github.com/prometheus/client_golang/prometheus"
4039)
@@ -87,7 +86,7 @@ func main() {
8786 glog .Fatalf ("Failed to create client: %v" , err )
8887 }
8988
90- initializeMetrics (kubeClient )
89+ initializeMetricCollection (kubeClient )
9190 metricsServer ()
9291}
9392
@@ -188,54 +187,36 @@ func (l NodeLister) List() (v1.NodeList, error) {
188187 return l ()
189188}
190189
191- // initializeMetrics creates a new controller from the given config.
192- func initializeMetrics (kubeClient clientset.Interface ) {
193- dplStore , dplController := cache .NewNamespaceKeyedIndexerAndReflector (
194- & cache.ListWatch {
195- ListFunc : func (options api.ListOptions ) (runtime.Object , error ) {
196- return kubeClient .Extensions ().Deployments (api .NamespaceAll ).List (options )
197- },
198- WatchFunc : func (options api.ListOptions ) (watch.Interface , error ) {
199- return kubeClient .Extensions ().Deployments (api .NamespaceAll ).Watch (options )
200- },
201- }, & v1beta1.Deployment {}, resyncPeriod )
202-
203- podStore , podController := cache .NewNamespaceKeyedIndexerAndReflector (
204- cache .NewListWatchFromClient (
205- kubeClient .Core ().GetRESTClient (),
206- "pods" ,
207- api .NamespaceAll ,
208- nil ,
209- ), & v1.Pod {}, resyncPeriod )
210-
211- nodeStore , nodeController := cache .NewNamespaceKeyedIndexerAndReflector (
212- cache .NewListWatchFromClient (
213- kubeClient .Core ().GetRESTClient (),
214- "nodes" ,
215- api .NamespaceAll ,
216- nil ,
217- ), & v1.Node {}, resyncPeriod )
218-
219- go dplController .Run ()
220- go podController .Run ()
221- go nodeController .Run ()
190+ // initializeMetricCollection creates and starts informers and initializes and
191+ // registers metrics for collection.
192+ func initializeMetricCollection (kubeClient clientset.Interface ) {
193+ cclient := kubeClient .Core ().GetRESTClient ()
194+ eclient := kubeClient .Extensions ().GetRESTClient ()
195+
196+ dlw := cache .NewListWatchFromClient (eclient , "deployments" , api .NamespaceAll , nil )
197+ plw := cache .NewListWatchFromClient (cclient , "pods" , api .NamespaceAll , nil )
198+ nlw := cache .NewListWatchFromClient (cclient , "nodes" , api .NamespaceAll , nil )
199+
200+ dinf := cache .NewSharedInformer (dlw , & v1beta1.Deployment {}, resyncPeriod )
201+ pinf := cache .NewSharedInformer (plw , & v1.Pod {}, resyncPeriod )
202+ ninf := cache .NewSharedInformer (nlw , & v1.Node {}, resyncPeriod )
222203
223204 dplLister := DeploymentLister (func () (deployments []v1beta1.Deployment , err error ) {
224- for _ , c := range dplStore .List () {
205+ for _ , c := range dinf . GetStore () .List () {
225206 deployments = append (deployments , * (c .(* v1beta1.Deployment )))
226207 }
227208 return deployments , nil
228209 })
229210
230211 podLister := PodLister (func () (pods []v1.Pod , err error ) {
231- for _ , m := range podStore .List () {
212+ for _ , m := range pinf . GetStore () .List () {
232213 pods = append (pods , * m .(* v1.Pod ))
233214 }
234215 return pods , nil
235216 })
236217
237218 nodeLister := NodeLister (func () (machines v1.NodeList , err error ) {
238- for _ , m := range nodeStore .List () {
219+ for _ , m := range ninf . GetStore () .List () {
239220 machines .Items = append (machines .Items , * (m .(* v1.Node )))
240221 }
241222 return machines , nil
@@ -244,4 +225,8 @@ func initializeMetrics(kubeClient clientset.Interface) {
244225 prometheus .MustRegister (& deploymentCollector {store : dplLister })
245226 prometheus .MustRegister (& podCollector {store : podLister })
246227 prometheus .MustRegister (& nodeCollector {store : nodeLister })
228+
229+ go dinf .Run (context .Background ().Done ())
230+ go pinf .Run (context .Background ().Done ())
231+ go ninf .Run (context .Background ().Done ())
247232}
0 commit comments