This repository has been archived by the owner on Jan 21, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
event.go
84 lines (70 loc) · 2.55 KB
/
event.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
package event
import (
"errors"
"code.cloudfoundry.org/lager"
"code.cloudfoundry.org/routing-api/models"
"code.cloudfoundry.org/tcp-emitter/routing_table/schema/endpoint"
)
type RoutingEventType string
const (
RouteRegistrationEvent RoutingEventType = "RouteRegistrationEvent"
RouteUnregistrationEvent RoutingEventType = "RouteUnregistrationEvent"
)
type RoutingEvent struct {
EventType RoutingEventType
Key endpoint.RoutingKey
Entry endpoint.RoutableEndpoints
}
type RoutingEvents []RoutingEvent
func (r RoutingEvent) Valid() bool {
if len(r.Entry.Endpoints) == 0 {
return false
}
if len(r.Entry.ExternalEndpoints) == 0 {
return false
}
for _, externalEndpoint := range r.Entry.ExternalEndpoints {
if externalEndpoint.Port == 0 {
return false
}
}
return true
}
func (routingEvents RoutingEvents) ToMappingRequests(logger lager.Logger, ttl int) ([]models.TcpRouteMapping, []models.TcpRouteMapping) {
registrationEvents := RoutingEvents{}
unregistrationEvents := RoutingEvents{}
for _, routingEvent := range routingEvents {
if !routingEvent.Valid() {
logger.Error("invalid-routing-event", errors.New("Invalid routing event"), lager.Data{"routing-event-key": routingEvent.Key})
continue
}
if routingEvent.EventType == RouteRegistrationEvent {
registrationEvents = append(registrationEvents, routingEvent)
} else if routingEvent.EventType == RouteUnregistrationEvent {
unregistrationEvents = append(unregistrationEvents, routingEvent)
}
}
registrationMappingRequests := buildMappingRequests(registrationEvents, ttl)
unregistrationMappingRequests := buildMappingRequests(unregistrationEvents, ttl)
return registrationMappingRequests, unregistrationMappingRequests
}
func buildMappingRequests(routingEvents RoutingEvents, ttl int) []models.TcpRouteMapping {
mappingRequests := make([]models.TcpRouteMapping, 0)
for _, routingEvent := range routingEvents {
mappingRequest := mapRoutingEvent(routingEvent, ttl)
if mappingRequest != nil {
mappingRequests = append(mappingRequests, (*mappingRequest)...)
}
}
return mappingRequests
}
func mapRoutingEvent(routingEvent RoutingEvent, ttl int) *[]models.TcpRouteMapping {
mappingRequests := make([]models.TcpRouteMapping, 0)
for _, externalEndpoint := range routingEvent.Entry.ExternalEndpoints {
for _, endpoint := range routingEvent.Entry.Endpoints {
mappingRequests = append(mappingRequests, models.NewTcpRouteMapping(externalEndpoint.RouterGroupGUID, uint16(externalEndpoint.Port),
endpoint.Host, uint16(endpoint.Port), ttl))
}
}
return &mappingRequests
}