-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathwatch.go
More file actions
57 lines (48 loc) · 1.52 KB
/
watch.go
File metadata and controls
57 lines (48 loc) · 1.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package k8s
import (
"time"
"k8s.io/kubernetes/pkg/api"
"k8s.io/kubernetes/pkg/client/cache"
client "k8s.io/kubernetes/pkg/client/unversioned"
"k8s.io/kubernetes/pkg/controller/framework"
"k8s.io/kubernetes/pkg/labels"
"k8s.io/kubernetes/pkg/runtime"
"k8s.io/kubernetes/pkg/watch"
)
var (
resyncPeriod = 30 * time.Second
)
//PodWatcher is a struct which holds the return values of (k8s.io/kubernetes/pkg/controller/framework).NewIndexerInformer together.
type PodWatcher struct {
Store cache.StoreToPodLister
Controller *framework.Controller
}
//NewPodWatcher creates a new BuildPodWatcher useful to list the pods using a cache which gets updated based on the watch func.
func NewPodWatcher(c *client.Client, ns string) *PodWatcher {
pw := &PodWatcher{}
pw.Store.Store, pw.Controller = framework.NewIndexerInformer(
&cache.ListWatch{
ListFunc: podListFunc(c, ns),
WatchFunc: podWatchFunc(c, ns),
},
&api.Pod{},
resyncPeriod,
framework.ResourceEventHandlerFuncs{},
cache.Indexers{},
)
return pw
}
func podListFunc(c *client.Client, ns string) func(options api.ListOptions) (runtime.Object, error) {
return func(opts api.ListOptions) (runtime.Object, error) {
return c.Pods(ns).List(api.ListOptions{
LabelSelector: labels.Everything(),
})
}
}
func podWatchFunc(c *client.Client, ns string) func(options api.ListOptions) (watch.Interface, error) {
return func(opts api.ListOptions) (watch.Interface, error) {
return c.Pods(ns).Watch(api.ListOptions{
LabelSelector: labels.Everything(),
})
}
}