Newer
Older
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
package kubernetes
import (
"context"
"encoding/base64"
"encoding/json"
"fmt"
"os"
"time"
"github.com/apache/dubbo-go/common/logger"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/watch"
"k8s.io/client-go/informers"
"k8s.io/client-go/tools/cache"
perrors "github.com/pkg/errors"
v1 "k8s.io/api/core/v1"
informerscorev1 "k8s.io/client-go/informers/core/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"k8s.io/client-go/util/workqueue"
)
const (
// kubernetes suggest resync
defaultResync = 5 * time.Minute
)
// dubboRegistryController
// work like a kubernetes controller
type dubboRegistryController struct {
ctx context.Context
cancel context.CancelFunc
// current pod config
needWatchedNamespaceList []string
namespace string
name string
cfg *rest.Config
watcherSet WatcherSet
// kubernetes
kc kubernetes.Interface
namespacedInformerFactory map[string]informers.SharedInformerFactory
namespacedPodInformers map[string]informerscorev1.PodInformer
queue workqueue.Interface //shared by namespaced informers
}
// read dubbo-registry controller config
func (c *dubboRegistryController) readConfig() error {
// read in-cluster config
cfg, err := rest.InClusterConfig()
if err != nil {
return perrors.WithMessage(err, "get in-cluster config")
}
c.cfg = cfg
// read current pod name && namespace
c.name = os.Getenv(podNameKey)
if len(c.name) == 0 {
return perrors.New("read value from env by key (HOSTNAME)")
}
c.namespace = os.Getenv(nameSpaceKey)
if len(c.namespace) == 0 {
return perrors.New("read value from env by key (NAMESPACE)")
}
return nil
}
func (c *dubboRegistryController) initNamespacedPodInformer(ns string) {
informersFactory := informers.NewSharedInformerFactoryWithOptions(
c.kc,
defaultResync,
informers.WithNamespace(ns),
informers.WithTweakListOptions(func(options *metav1.ListOptions) {
labelSelector := &metav1.LabelSelector{
MatchLabels: map[string]string{DubboIOLabelKey: DubboIOLabelValue},
}
labelMap, err := metav1.LabelSelectorAsMap(labelSelector)
if err != nil {
panic(err)
}
options.LabelSelector = labels.SelectorFromSet(labelMap).String()
}),
)
podInformer := informersFactory.Core().V1().Pods()
podInformer.Informer().AddEventHandler(cache.ResourceEventHandlerFuncs{
AddFunc: c.addPod,
UpdateFunc: c.updatePod,
DeleteFunc: c.deletePod,
})
c.namespacedInformerFactory[ns] = informersFactory
c.namespacedPodInformers[ns] = podInformer
}
func (c *dubboRegistryController) init() error {
// init kubernetes client
var err error
c.kc, err = kubernetes.NewForConfig(c.cfg)
if err != nil {
return perrors.WithMessage(err, "new kubernetes client from config")
}
c.queue = workqueue.New()
// init all watch needed pod-informer
for _, watchedNS := range c.needWatchedNamespaceList {
c.initNamespacedPodInformer(watchedNS)
}
return nil
}
func newDubboRegistryController() error {
c := &dubboRegistryController{}
if err := c.readConfig(); err != nil {
return perrors.WithMessage(err, "dubbo registry controller read config")
}
if err := c.init(); err != nil {
return perrors.WithMessage(err, "dubbo registry controller init")
}
go c.run()
return nil
}
type kubernetesEvent struct {
p *v1.Pod
t watch.EventType
}
func (c *dubboRegistryController) addPod(obj interface{}) {
p, ok := obj.(*v1.Pod)
if !ok {
logger.Warnf("pod-informer got object %T not *v1.Pod", obj)
return
}
c.queue.Add(&kubernetesEvent{
t: watch.Added,
p: p,
})
}
func (c *dubboRegistryController) updatePod(oldObj, newObj interface{}) {
op, ok := oldObj.(*v1.Pod)
if !ok {
logger.Warnf("pod-informer got object %T not *v1.Pod", oldObj)
return
}
np, ok := newObj.(*v1.Pod)
if !ok {
logger.Warnf("pod-informer got object %T not *v1.Pod", newObj)
return
}
if op.GetResourceVersion() == np.GetResourceVersion() {
return
}
c.queue.Add(&kubernetesEvent{
p: np,
t: watch.Modified,
})
}
func (c *dubboRegistryController) deletePod(obj interface{}) {
p, ok := obj.(*v1.Pod)
if !ok {
logger.Warnf("pod-informer got object %T not *v1.Pod", obj)
return
}
c.queue.Add(&kubernetesEvent{
p: p,
t: watch.Deleted,
})
}
func (c *dubboRegistryController) Run() {
for _, factory := range c.namespacedInformerFactory {
go factory.Start(c.ctx.Done())
}
}
func (c *dubboRegistryController) stop() {
c.cancel()
}
func (c *dubboRegistryController) run() {
defer c.queue.ShutDown()
for ns, podInformer := range c.namespacedPodInformers {
if !cache.WaitForCacheSync(c.ctx.Done(), podInformer.Informer().HasSynced) {
logger.Errorf("wait for cache sync finish @namespace %s fail", ns)
return
}
}
// start work
go c.work()
// block wait
<-c.ctx.Done()
}
func (c *dubboRegistryController) work() {
defer logger.Debugf("dubbo registry controller work stopped")
for c.processNextWorkItem() {
}
}
// processNextWorkItem process work-queue elements
func (c *dubboRegistryController) processNextWorkItem() bool {
item, shutdown := c.queue.Get()
if shutdown {
return false
}
defer c.queue.Done(item)
o := item.(*kubernetesEvent)
c.handleWatchedPodEvent(o.p, o.t)
return true
}
// handleWatchedPodEvent
// handle watched pod event
func (c *dubboRegistryController) handleWatchedPodEvent(p *v1.Pod, eventType watch.EventType) {
for ak, av := range p.GetAnnotations() {
// not dubbo interest annotation
if ak != DubboIOAnnotationKey {
continue
}
ol, err := c.unmarshalRecord(av)
if err != nil {
logger.Errorf("there a pod with dubbo annotation, but unmarshal dubbo value %v", err)
return
}
for _, o := range ol {
switch eventType {
case watch.Added:
// if pod is added, the record always be create
o.EventType = Create
case watch.Modified:
o.EventType = Update
case watch.Deleted:
o.EventType = Delete
default:
logger.Errorf("no valid kubernetes event-type (%s) ", eventType)
return
}
logger.Debugf("prepare to put object (%#v) to kubernetes-watcherSet", o)
if err := c.watcherSet.Put(o); err != nil {
logger.Errorf("put (%#v) to cache watcherSet: %v ", o, err)
return
}
}
}
}
// unmarshalRecord
// unmarshal the kubernetes dubbo annotation value
func (c *dubboRegistryController) unmarshalRecord(record string) ([]*WatcherEvent, error) {
if len(record) == 0 {
// []*WatcherEvent is nil.
return nil, nil
}
rawMsg, err := base64.URLEncoding.DecodeString(record)
if err != nil {
return nil, perrors.WithMessagef(err, "decode record (%s)", record)
}
var out []*WatcherEvent
if err := json.Unmarshal(rawMsg, &out); err != nil {
return nil, perrors.WithMessage(err, "decode json")
}
return out, nil
}