/
watcher.go
100 lines (84 loc) · 1.82 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
package configmap
import (
"errors"
"time"
"github.com/micro/go-micro/v2/config/source"
v12 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/fields"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/cache"
)
type watcher struct {
opts source.Options
name string
namespace string
client *kubernetes.Clientset
st cache.Store
ct cache.Controller
ch chan *source.ChangeSet
exit chan bool
stop chan struct{}
}
func newWatcher(n, ns string, c *kubernetes.Clientset, opts source.Options) (source.Watcher, error) {
w := &watcher{
opts: opts,
name: n,
namespace: ns,
client: c,
ch: make(chan *source.ChangeSet),
exit: make(chan bool),
stop: make(chan struct{}),
}
lw := cache.NewListWatchFromClient(w.client.CoreV1().RESTClient(), "configmaps", w.namespace, fields.OneTermEqualSelector("metadata.name", w.name))
st, ct := cache.NewInformer(
lw,
&v12.ConfigMap{},
time.Second*30,
cache.ResourceEventHandlerFuncs{
UpdateFunc: w.handle,
},
)
go ct.Run(w.stop)
w.ct = ct
w.st = st
return w, nil
}
func (w *watcher) handle(oldCmp interface{}, newCmp interface{}) {
if newCmp == nil {
return
}
data := makeMap(newCmp.(*v12.ConfigMap).Data)
b, err := w.opts.Encoder.Encode(data)
if err != nil {
return
}
cs := &source.ChangeSet{
Format: w.opts.Encoder.String(),
Source: w.name,
Data: b,
Timestamp: newCmp.(*v12.ConfigMap).CreationTimestamp.Time,
}
cs.Checksum = cs.Sum()
w.ch <- cs
}
// Next
func (w *watcher) Next() (*source.ChangeSet, error) {
select {
case cs := <-w.ch:
return cs, nil
case <-w.exit:
return nil, errors.New("watcher stopped")
}
}
// Stop
func (w *watcher) Stop() error {
select {
case <-w.exit:
return nil
case <-w.stop:
return nil
default:
close(w.exit)
}
return nil
}