-
Notifications
You must be signed in to change notification settings - Fork 1
/
endpoint_cache.go
159 lines (142 loc) · 4.04 KB
/
endpoint_cache.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
package k8s
import (
"context"
"fmt"
"time"
log "github.com/sirupsen/logrus"
v1 "k8s.io/api/core/v1"
"k8s.io/client-go/tools/cache"
)
type EndpointCache struct {
endpoints chan *v1.Endpoints
indexer cache.Indexer
controller cache.Controller
}
func NewEndpointCache(source cache.ListerWatcher, syncInterval time.Duration, bufferSize int) *EndpointCache {
endpoints := make(chan *v1.Endpoints, bufferSize)
endpointHandler := &endpointHandler{endpoints}
indexer, controller := cache.NewIndexerInformer(source, &v1.Endpoints{}, syncInterval, endpointHandler, cache.Indexers{
indexByIP: endpointIPIndex,
})
EndpointCache := &EndpointCache{
endpoints: endpoints,
indexer: indexer,
controller: controller,
}
return EndpointCache
}
func (s *EndpointCache) getMetadataForIP(ip string) (map[string]string, error) {
items, err := s.indexer.ByIndex(indexByIP, ip)
if err != nil {
return nil, err
}
if len(items) == 0 {
return nil, fmt.Errorf("endpoint for %s not found", ip)
}
for _, obj := range items {
ep := obj.(*v1.Endpoints)
return ep.ObjectMeta.Labels, nil
}
return nil, fmt.Errorf("multiple endpoints found")
}
// GetMetadataByIP returns the Endpoint metadata
func (s *EndpointCache) GetMetadataByIP(ip string) (map[string]string, error) {
return s.getMetadataForIP(ip)
}
func (s *EndpointCache) getByIP(ip string) (*v1.Endpoints, error) {
items, err := s.indexer.ByIndex(indexByIP, ip)
if err != nil {
return nil, err
}
if len(items) == 0 {
return nil, fmt.Errorf("endpoint for %s not found", ip)
}
for _, obj := range items {
ep := obj.(*v1.Endpoints)
return ep, nil
}
return nil, fmt.Errorf("multiple endpoints found")
}
// GetByIP returns the Endpoint
func (s *EndpointCache) GetByIP(ip string) (*v1.Endpoints, error) {
return s.getByIP(ip)
}
const (
indexByIP = "byIP"
)
func endpointIPIndex(obj interface{}) ([]string, error) {
endpoints := obj.(*v1.Endpoints)
var out []string
for _, sub := range endpoints.Subsets {
for _, addr := range sub.Addresses {
out = append(out, addr.IP)
}
}
return out, nil
}
// Run starts the controller processing updates. Blocks until the cache has synced
func (s *EndpointCache) Run(ctx context.Context) error {
go s.controller.Run(ctx.Done())
log.Infof("started cache controller")
ok := cache.WaitForCacheSync(ctx.Done(), s.controller.HasSynced)
if !ok {
return fmt.Errorf("error waiting for sync")
}
return nil
}
type endpointHandler struct {
endpoints chan<- *v1.Endpoints
}
func (o *endpointHandler) announce(endpoint *v1.Endpoints) {
logger := log.WithFields(EndpointFields(endpoint))
select {
case o.endpoints <- endpoint:
logger.Debugf("announced endpoint")
default:
logger.Warnf("endpoint announcement full, dropping")
}
}
func (o *endpointHandler) OnAdd(obj interface{}) {
ep, isEp := obj.(*v1.Endpoints)
if !isEp {
log.Errorf("OnAdd unexpected object: %+v", obj)
return
}
log.WithFields(EndpointFields(ep)).Debugf("added ep")
o.announce(ep)
}
func (o *endpointHandler) OnDelete(obj interface{}) {
ep, isEp := obj.(*v1.Endpoints)
if !isEp {
deletedObj, isDeleted := obj.(cache.DeletedFinalStateUnknown)
if !isDeleted {
log.Errorf("OnDelete unexpected object: %+v", obj)
return
}
ep, isEp = deletedObj.Obj.(*v1.Endpoints)
if !isEp {
log.Errorf("OnDelete unexpected DeletedFinalStateUnknown object: %+v", deletedObj.Obj)
}
log.WithFields(EndpointFields(ep)).Debugf("deleted ep")
return
}
log.WithFields(EndpointFields(ep)).Debugf("deleted ep")
return
}
func (o *endpointHandler) OnUpdate(old, new interface{}) {
ep, isEp := new.(*v1.Endpoints)
if !isEp {
log.Errorf("OnUpdate unexpected object: %+v", new)
return
}
log.WithFields(EndpointFields(ep)).Debugf("updated ep")
}
func EndpointFields(endpoints *v1.Endpoints) log.Fields {
return log.Fields{
"endpoints.namespace": endpoints.ObjectMeta.Namespace,
"endpoints.name": endpoints.ObjectMeta.Name,
"endpoints.addr": endpoints.Subsets,
"resource.version": endpoints.ObjectMeta.ResourceVersion,
"generation.metadata": endpoints.ObjectMeta.Generation,
}
}