forked from alibaba/kt-connect
-
Notifications
You must be signed in to change notification settings - Fork 2
/
watcher.go
206 lines (163 loc) · 4.69 KB
/
watcher.go
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
package cluster
import (
"errors"
"time"
"k8s.io/apimachinery/pkg/util/runtime"
"k8s.io/client-go/tools/cache"
"k8s.io/client-go/informers"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/client-go/kubernetes"
v1 "k8s.io/client-go/listers/core/v1"
"k8s.io/client-go/rest"
)
var (
errTimeout = errors.New("timed out waiting for caches to sync")
)
//Watcher Kubernetes resource watch
type Watcher struct {
Client kubernetes.Interface
Config *rest.Config
NamespaceLister v1.NamespaceLister
PodLister v1.PodLister
ServiceLister v1.ServiceLister
EndpointsLister v1.EndpointsLister
}
// Construct for watcher
func Construct(client kubernetes.Interface, config *rest.Config) (w Watcher, err error) {
w = Watcher{Client: client, Config: config}
namespaceLister, err := w.Namespaces(wait.NeverStop)
if err != nil {
return
}
w.NamespaceLister = namespaceLister
podListener, err := w.Pods(wait.NeverStop)
if err != nil {
return
}
w.PodLister = podListener
serviceLister, err := w.Services(wait.NeverStop)
if err != nil {
return
}
w.ServiceLister = serviceLister
endpointLister, err := w.Endpoints(wait.NeverStop)
if err != nil {
return
}
w.EndpointsLister = endpointLister
return
}
// PodListenerWithNamespace PodListener
func PodListenerWithNamespace(client kubernetes.Interface, namespace string, stopCh <-chan struct{}) (lister v1.PodLister, err error) {
w := Watcher{Client: client}
lister, err = w.PodsWithNamespace(namespace, stopCh)
if err != nil {
return
}
return
}
func informerFactoryWithNamespace(w *Watcher, namespace string) (factory informers.SharedInformerFactory) {
resyncPeriod := 30 * time.Minute
factory = informers.NewSharedInformerFactoryWithOptions(w.Client, resyncPeriod, informers.WithNamespace(namespace))
return
}
func informerFactory(w *Watcher) (factory informers.SharedInformerFactory) {
resyncPeriod := 30 * time.Minute
factory = informers.NewSharedInformerFactory(w.Client, resyncPeriod)
return
}
// Endpoints informer of endpoints
func (w *Watcher) Endpoints(stopCh <-chan struct{}) (lister v1.EndpointsLister, err error) {
factory := informerFactory(w)
informerFactory := factory.Core().V1().Endpoints()
informer := informerFactory.Informer()
defer runtime.HandleCrash()
factory.Start(stopCh)
if !cache.WaitForCacheSync(stopCh, informer.HasSynced) {
err = errTimeout
runtime.HandleError(err)
return
}
lister = informerFactory.Lister()
return
}
// Namespaces informer of namespace
func (w *Watcher) Namespaces(stopCh <-chan struct{}) (lister v1.NamespaceLister, err error) {
factory := informerFactory(w)
informerFactory := factory.Core().V1().Namespaces()
informer := informerFactory.Informer()
defer runtime.HandleCrash()
factory.Start(stopCh)
if !cache.WaitForCacheSync(stopCh, informer.HasSynced) {
err = errTimeout
runtime.HandleError(err)
return
}
lister = informerFactory.Lister()
return
}
func podDeleted(obj interface{}) {
// pod, ok := obj.(*api.Pod)
// if ok {
// fmt.Printf("Pod deleted: %s\n", pod.ObjectMeta.Name)
// } else {
// fmt.Printf("Pod deleted event: %s\n", obj)
// }
}
// PodsWithNamespace watch pods change
func (w *Watcher) PodsWithNamespace(namespace string, stopCh <-chan struct{}) (lister v1.PodLister, err error) {
factory := informerFactoryWithNamespace(w, namespace)
podInformer := factory.Core().V1().Pods()
informer := podInformer.Informer()
defer runtime.HandleCrash()
factory.Start(stopCh)
if !cache.WaitForCacheSync(stopCh, informer.HasSynced) {
err = errTimeout
runtime.HandleError(err)
return
}
informer.AddEventHandler(
cache.ResourceEventHandlerFuncs{
// AddFunc: podCreated,
DeleteFunc: podDeleted,
},
)
lister = podInformer.Lister()
return
}
// Pods watch pods change
func (w *Watcher) Pods(stopCh <-chan struct{}) (lister v1.PodLister, err error) {
factory := informerFactory(w)
podInformer := factory.Core().V1().Pods()
informer := podInformer.Informer()
defer runtime.HandleCrash()
factory.Start(stopCh)
if !cache.WaitForCacheSync(stopCh, informer.HasSynced) {
err = errTimeout
runtime.HandleError(err)
return
}
informer.AddEventHandler(
cache.ResourceEventHandlerFuncs{
// AddFunc: podCreated,
DeleteFunc: podDeleted,
},
)
lister = podInformer.Lister()
return
}
// Services informer of service
func (w *Watcher) Services(stopCh <-chan struct{}) (lister v1.ServiceLister, err error) {
factory := informerFactory(w)
serviceformer := factory.Core().V1().Services()
informer := serviceformer.Informer()
defer runtime.HandleCrash()
factory.Start(stopCh)
if !cache.WaitForCacheSync(stopCh, informer.HasSynced) {
err = errTimeout
runtime.HandleError(err)
return
}
lister = serviceformer.Lister()
return
}