-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathwatch.go
More file actions
70 lines (61 loc) · 1.96 KB
/
watch.go
File metadata and controls
70 lines (61 loc) · 1.96 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
58
59
60
61
62
63
64
65
66
67
68
69
70
package k8s
import (
"context"
"k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/watch"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/cache"
"time"
)
var (
resyncPeriod = 30 * time.Second
)
type StoreToPodLister struct {
cache.Store
}
//PodWatcher is a struct which holds the return values of (k8s.io/kubernetes/pkg/controller/framework).NewIndexerInformer together.
type PodWatcher struct {
Store StoreToPodLister
Controller cache.Controller
}
func (s *StoreToPodLister) List(selector labels.Selector) (pods []*v1.Pod, err error) {
// TODO: it'd be great to just call
// s.Pods(api.NamespaceAll).List(selector), however then we'd have to
// remake the list.Items as a []*api.Pod. So leave this separate for
// now.
for _, m := range s.Store.List() {
pod := m.(*v1.Pod)
if selector.Matches(labels.Set(pod.Labels)) {
pods = append(pods, pod)
}
}
return pods, nil
}
//NewPodWatcher creates a new BuildPodWatcher useful to list the pods using a cache which gets updated based on the watch func.
func NewPodWatcher(c kubernetes.Clientset, ns string) *PodWatcher {
pw := &PodWatcher{}
pw.Store.Store, pw.Controller = cache.NewIndexerInformer(
&cache.ListWatch{
ListFunc: podListFunc(c, ns),
WatchFunc: podWatchFunc(c, ns),
},
&v1.Pod{},
resyncPeriod,
cache.ResourceEventHandlerFuncs{},
cache.Indexers{},
)
return pw
}
func podListFunc(c kubernetes.Clientset, ns string) func(options metav1.ListOptions) (runtime.Object, error) {
return func(opts metav1.ListOptions) (runtime.Object, error) {
return c.CoreV1().Pods(ns).List(context.TODO(), metav1.ListOptions{})
}
}
func podWatchFunc(c kubernetes.Clientset, ns string) func(options metav1.ListOptions) (watch.Interface, error) {
return func(opts metav1.ListOptions) (watch.Interface, error) {
return c.CoreV1().Pods(ns).Watch(context.TODO(), metav1.ListOptions{})
}
}