-
Notifications
You must be signed in to change notification settings - Fork 23
/
entities.go
278 lines (233 loc) · 7.79 KB
/
entities.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
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
package entities
import (
"context"
"errors"
"sync"
"go.uber.org/fx"
"google.golang.org/protobuf/proto"
entitiesv1 "github.com/fluxninja/aperture/api/v2/gen/proto/go/aperture/discovery/entities/v1"
"github.com/fluxninja/aperture/v2/pkg/config"
"github.com/fluxninja/aperture/v2/pkg/log"
"github.com/fluxninja/aperture/v2/pkg/net/grpcgateway"
"github.com/fluxninja/aperture/v2/pkg/notifiers"
)
// Module sets up Entities with Fx.
func Module() fx.Option {
return fx.Options(
notifiers.TrackersConstructor{Name: "entity_trackers_private"}.Annotate(),
fx.Provide(provideEntities),
grpcgateway.RegisterHandler{Handler: entitiesv1.RegisterEntitiesServiceHandlerFromEndpoint}.Annotate(),
fx.Invoke(RegisterEntitiesService),
)
}
// Entity is an immutable wrapper over *entitiesv1.Entity.
type Entity struct {
immutableEntity *entitiesv1.Entity
}
// NewEntity creates a new immutable entity from the copy of given entity.
func NewEntity(entity *entitiesv1.Entity) Entity {
return Entity{immutableEntity: proto.Clone(entity).(*entitiesv1.Entity)}
}
// NewEntityFromImmutable creates a new immutable entity, assuming given entity is immutable.
//
// This allows avoiding a copy compared to NewEntity.
func NewEntityFromImmutable(entity *entitiesv1.Entity) Entity {
return Entity{immutableEntity: entity}
}
// Clone returns a mutable copy of the entity.
func (e Entity) Clone() *entitiesv1.Entity {
return proto.Clone(e.immutableEntity).(*entitiesv1.Entity)
}
// UID returns the entity's UID.
func (e Entity) UID() string { return e.immutableEntity.Uid }
// IPAddress returns the entity's IP address.
func (e Entity) IPAddress() string { return e.immutableEntity.IpAddress }
// Name returns the entity's name.
func (e Entity) Name() string { return e.immutableEntity.Name }
// Namespace returns the entity's namespace.
func (e Entity) Namespace() string { return e.immutableEntity.Namespace }
// NodeName returns the entity's node name.
func (e Entity) NodeName() string { return e.immutableEntity.NodeName }
// Services returns list of services the entity belongs to.
//
// The returned slice must not be modified.
func (e Entity) Services() []string { return e.immutableEntity.Services }
// Entities maps IP addresses and Entity names to entities.
type Entities struct {
sync.RWMutex
byIP map[string]Entity
byName map[string]Entity
}
// EntityTrackers allows to register a service discovery for entity cache
//
// Intended to be used during FX initialization.
type EntityTrackers struct {
trackers notifiers.Trackers
hasDiscovery bool
}
// RegisterServiceDiscovery registers service discovery for entity cache and
// returns an EventWriter to push discovery events into.
//
// Keys passed to EventWriter should not be prefixed.
//
// Should be called at FX provide/invoke stage.
func (et *EntityTrackers) RegisterServiceDiscovery(name string) notifiers.EventWriter {
et.hasDiscovery = true
return notifiers.NewPrefixedEventWriter(name+".", et.trackers)
}
// HasDiscovery returns whether RegisterServiceDiscovery was called before.
func (et *EntityTrackers) HasDiscovery() bool { return et.hasDiscovery }
// Watcher returns watcher that watches all events from registered service discoveries.
func (et *EntityTrackers) Watcher() notifiers.Watcher { return et.trackers }
// FxIn are the parameters for ProvideEntities.
type FxIn struct {
fx.In
Lifecycle fx.Lifecycle
EntityTrackers notifiers.Trackers `name:"entity_trackers_private"`
}
// provideEntities creates Entity Cache.
func provideEntities(in FxIn) (*Entities, *EntityTrackers, error) {
entityCache := NewEntities()
// create a ConfigPrefixNotifier
configPrefixNotifier, err := notifiers.NewUnmarshalPrefixNotifier("",
entityCache.processUpdate,
config.NewProtobufUnmarshaller,
)
if err != nil {
log.Error().Err(err).Msg("failed to create config prefix notifier")
return nil, nil, err
}
in.Lifecycle.Append(fx.Hook{
OnStart: func(context.Context) error {
err := in.EntityTrackers.AddPrefixNotifier(configPrefixNotifier)
if err != nil {
log.Error().Err(err).Msg("failed to add config prefix notifier")
return err
}
return nil
},
OnStop: func(context.Context) error {
err := in.EntityTrackers.RemovePrefixNotifier(configPrefixNotifier)
if err != nil {
log.Error().Err(err).Msg("failed to remove prefix notifier")
return err
}
return nil
},
})
return entityCache, &EntityTrackers{trackers: in.EntityTrackers}, nil
}
func (e *Entities) processUpdate(event notifiers.Event, unmarshaller config.Unmarshaller) {
log.Trace().Str("event", event.String()).Msg("Updating entity")
entityProto := &entitiesv1.Entity{}
if err := unmarshaller.Unmarshal(entityProto); err != nil {
log.Error().Err(err).Msg("Failed to unmarshal entity")
return
}
entity := NewEntityFromImmutable(entityProto)
ip := entity.IPAddress()
name := entity.Name()
switch event.Type {
case notifiers.Write:
log.Trace().Str("entity", entity.UID()).Str("ip", ip).Str("name", name).Msg("new entity")
e.Put(entity)
case notifiers.Remove:
log.Trace().Str("entity", entity.UID()).Str("ip", ip).Str("name", name).Msg("removing entity")
e.Remove(entity)
}
}
// NewEntities creates a new, empty Entities.
func NewEntities() *Entities {
return &Entities{
byIP: make(map[string]Entity),
byName: make(map[string]Entity),
}
}
// PutForTest maps given IP address and name to the entity it currently represents.
func (e *Entities) PutForTest(entity *entitiesv1.Entity) {
e.Put(NewEntity(entity))
}
// Put maps given IP address and name to the entity it currently represents.
func (e *Entities) Put(entity Entity) {
e.Lock()
defer e.Unlock()
entityIP := entity.IPAddress()
if entityIP != "" {
// FIXME: would be nice to store Entity directly in the map, but that
// would require removing the reusal of proto-generated structs as
// containers.
e.byIP[entityIP] = entity
}
entityName := entity.Name()
if entityName != "" {
e.byName[entityName] = entity
}
}
// GetByIP retrieves entity with a given IP address.
func (e *Entities) GetByIP(entityIP string) (Entity, error) {
return e.getFromMap(e.byIP, entityIP)
}
// GetByName retrieves entity with a given name.
func (e *Entities) GetByName(entityName string) (Entity, error) {
return e.getFromMap(e.byName, entityName)
}
func (e *Entities) getFromMap(m map[string]Entity, k string) (Entity, error) {
e.RLock()
defer e.RUnlock()
if len(m) == 0 {
return Entity{}, errNoEntities
}
v, ok := m[k]
if !ok {
return Entity{}, errNotFound
}
return v, nil
}
var (
errNotFound = errors.New("entity not found")
errNoEntities = errors.New("entity not found (empty cache)")
)
// Clear removes all entities from the cache.
func (e *Entities) Clear() {
e.Lock()
defer e.Unlock()
e.byIP = make(map[string]Entity)
e.byName = make(map[string]Entity)
}
// Remove removes entity from the cache and returns `true` if any of IP address
// or name mapping exists.
// If no such entity was found, returns `false`.
func (e *Entities) Remove(entity Entity) bool {
e.Lock()
defer e.Unlock()
entityIP := entity.IPAddress()
_, okByIP := e.byIP[entityIP]
if okByIP {
delete(e.byIP, entityIP)
}
entityName := entity.Name()
_, okByName := e.byName[entityName]
if okByName {
delete(e.byName, entityName)
}
return okByIP || okByName
}
// GetEntities returns *entitiesv1.EntitiyCache entities.
func (e *Entities) GetEntities() *entitiesv1.Entities {
e.RLock()
defer e.RUnlock()
// Not sure what caller will do with the result, let's clone
return &entitiesv1.Entities{
EntitiesByIpAddress: cloneEntitiesMap(e.byIP),
EntitiesByName: cloneEntitiesMap(e.byName),
}
}
func cloneEntitiesMap(m map[string]Entity) *entitiesv1.Entities_Entities {
clones := make(map[string]*entitiesv1.Entity, len(m))
for k, entity := range m {
clones[k] = entity.Clone()
}
return &entitiesv1.Entities_Entities{
Entities: clones,
}
}