func createPodWatch()

in pkg/metrics/collector/netlink_metrics.go [126:163]


func createPodWatch() error {
	// Creates the in-cluster config.
	config, err := rest.InClusterConfig()
	if err != nil {
		return err
	}
	config.ContentType = runtime.ContentTypeProtobuf
	// creates the clientset
	clientset, err := kubernetes.NewForConfig(config)
	if err != nil {
		return err
	}
	watchlist := cache.NewListWatchFromClient(
		clientset.CoreV1().RESTClient(),
		"pods",
		v1.NamespaceAll,
		fields.OneTermEqualSelector("spec.nodeName", nodeName),
	)
	_, controller := cache.NewInformerWithOptions(cache.InformerOptions{
		ListerWatcher: watchlist,
		ObjectType:    &v1.Pod{},
		Handler: cache.ResourceEventHandlerFuncs{
			AddFunc:    onPodAdd,
			UpdateFunc: onPodUpdate,
			DeleteFunc: onPodDelete,
		},
	})

	stopper := make(chan struct{})

	go controller.Run(stopper)

	if !cache.WaitForCacheSync(stopper, controller.HasSynced) {
		glog.Infof("Timed out waiting for caches to sync.")
	}

	return nil
}