-
Notifications
You must be signed in to change notification settings - Fork 0
/
resolver.go
115 lines (95 loc) · 2.72 KB
/
resolver.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
package tam_grpc_resolver
import (
"context"
"log"
"sync"
"time"
"google.golang.org/grpc/resolver"
"k8s.io/client-go/informers"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/cache"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
// init function needs for auto-register in resolvers registry
func init() {
resolver.Register(&builder{})
}
type tamResolver struct {
appName string
namespace string
port string
label string
ctx context.Context
cancel context.CancelFunc
wg sync.WaitGroup
cc resolver.ClientConn
clientset *kubernetes.Clientset
}
func (r *tamResolver) ResolveNow(resolver.ResolveNowOptions) {}
// Close closes the resolver.
func (r *tamResolver) Close() {
r.cancel()
r.wg.Wait()
}
func (r *tamResolver) watcher() {
labelOptions := informers.WithTweakListOptions(func(opts *metav1.ListOptions) {
opts.LabelSelector = r.label
})
// create a new instance of sharedInformerFactory
informerFactory := informers.NewSharedInformerFactoryWithOptions(
r.clientset,
1*time.Minute,
informers.WithNamespace(r.namespace),
labelOptions,
)
// using this factory create an informer for `Pods` resources
podsInformer := informerFactory.Core().V1().Pods()
// adds an event handler to the shared informer
podsInformer.Informer().AddEventHandler(cache.ResourceEventHandlerFuncs{
AddFunc: func(obj interface{}) {
if err := r.updatePodAddrs(); err != nil {
log.Fatalf("Update pods IP with ADD event failed: %v", err)
}
},
UpdateFunc: func(oldObj, newObj interface{}) {
if err := r.updatePodAddrs(); err != nil {
log.Fatalf("Update pods IP with UPDATE event failed: %v", err)
}
},
DeleteFunc: func(obj interface{}) {
if err := r.updatePodAddrs(); err != nil {
log.Fatalf("Update pods IP with DELETE event failed: %v", err)
}
},
})
stopCh := make(chan struct{})
defer close(stopCh)
// starts the shared informers that have been created by the factory
informerFactory.Start(stopCh)
// wait for the initial synchronization of the local cache
if !cache.WaitForCacheSync(stopCh, podsInformer.Informer().HasSynced) {
log.Fatalf("failed to sync informer cache")
}
<-r.ctx.Done()
}
func (r *tamResolver) updatePodAddrs() error {
podList, err := r.clientset.CoreV1().Pods(r.namespace).List(
r.ctx,
metav1.ListOptions{
LabelSelector: r.label,
},
)
if err != nil {
return err
}
podAddrs := make([]resolver.Address, 0, len(podList.Items))
for _, pod := range podList.Items {
if pod.Status.Phase == corev1.PodRunning {
addr := pod.Status.PodIP + ":" + r.port
podAddrs = append(podAddrs, resolver.Address{Addr: addr})
}
}
state := resolver.State{Addresses: podAddrs}
return r.cc.UpdateState(state)
}