/
node.go
265 lines (243 loc) · 7.15 KB
/
node.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
package provider
import (
"fmt"
"time"
"github.com/gogo/protobuf/types"
"github.com/moolen/bent/envoy/api/v2"
"github.com/moolen/bent/envoy/api/v2/cluster"
"github.com/moolen/bent/envoy/api/v2/core"
"github.com/moolen/bent/envoy/api/v2/endpoint"
"github.com/moolen/bent/envoy/api/v2/route"
_type "github.com/moolen/bent/envoy/type"
"github.com/moolen/bent/pkg/cache"
log "github.com/sirupsen/logrus"
)
// Node holds the actual envoy-related data
// and acts only as a proxy type
type Node struct {
Name string
clusters map[string]*v2.Cluster
endpoints map[string]*v2.ClusterLoadAssignment
routes map[string]*v2.RouteConfiguration
vhostExists map[string]map[string]struct{}
listeners []*v2.Listener
}
// NewNode constructs a new node
func NewNode(name string) *Node {
return &Node{
Name: name,
clusters: make(map[string]*v2.Cluster),
endpoints: make(map[string]*v2.ClusterLoadAssignment),
routes: make(map[string]*v2.RouteConfiguration),
vhostExists: make(map[string]map[string]struct{}),
}
}
// AddEndpoints initializes a ClusterLoadAssigment and appends a bunch of endpoints to it
func (n *Node) AddEndpoints(cluster string, eps ...Endpoint) {
if n.endpoints[cluster] == nil {
n.endpoints[cluster] = &v2.ClusterLoadAssignment{
ClusterName: cluster,
Endpoints: []endpoint.LocalityLbEndpoints{
{
LbEndpoints: []endpoint.LbEndpoint{},
},
},
}
}
n.endpoints[cluster].Endpoints[0].LbEndpoints = append(
n.endpoints[cluster].Endpoints[0].LbEndpoints,
createEnvoyEndpoint(eps)...,
)
}
// AddCluster initializes a cluster and calls AddEndpoints
func (n *Node) AddCluster(clusters ...Cluster) {
for _, c := range clusters {
if n.clusters[c.Name] == nil {
n.clusters[c.Name] = createEnvoyCluster(c)
}
n.AddEndpoints(c.Name, c.Endpoints...)
}
}
// AddListener adds a bunch of listeners
func (n *Node) AddListener(lis ...*v2.Listener) {
n.listeners = append(n.listeners, lis...)
}
// AddRoute initializes a route config and appends a bunch of vhosts to it
// a vhost is unique per route and must not be duplicated
// this function takes care of it
func (n *Node) AddRoute(routeName string, vhosts ...route.VirtualHost) {
if n.routes[routeName] == nil {
n.routes[routeName] = &v2.RouteConfiguration{
Name: routeName,
}
}
for _, vhost := range vhosts {
if n.vhostExists[routeName] == nil {
n.vhostExists[routeName] = make(map[string]struct{})
}
// have we seen a vhost already? no? -> append
if _, ok := n.vhostExists[routeName][vhost.Name]; !ok {
n.vhostExists[routeName][vhost.Name] = struct{}{}
n.routes[routeName].VirtualHosts = append(
n.routes[routeName].VirtualHosts,
vhost,
)
}
}
}
func createEnvoyEndpoint(endpoints []Endpoint) []endpoint.LbEndpoint {
var envoyEndpoints []endpoint.LbEndpoint
for _, ep := range endpoints {
cfg := ep.Config()
log.Debugf("ep config: %#v", cfg)
envoyEndpoints = append(envoyEndpoints, endpoint.LbEndpoint{
LoadBalancingWeight: &types.UInt32Value{Value: cfg.Weight},
Metadata: &core.Metadata{},
HostIdentifier: &endpoint.LbEndpoint_Endpoint{
Endpoint: &endpoint.Endpoint{
Address: &core.Address{
Address: &core.Address_SocketAddress{
SocketAddress: &core.SocketAddress{
Protocol: core.TCP,
Address: ep.Address,
PortSpecifier: &core.SocketAddress_PortValue{
PortValue: ep.Port,
},
},
},
},
},
},
})
}
return envoyEndpoints
}
func createEnvoyCluster(c Cluster) *v2.Cluster {
clusterCfg := c.Config()
log.Debugf("cluster config: %#v", clusterCfg)
// if nil, the endpoint port is used
var healthCheckPort *types.UInt32Value
if clusterCfg.HealthCheck.Port != 0 {
healthCheckPort = &types.UInt32Value{Value: clusterCfg.HealthCheck.Port}
}
cb := &cluster.CircuitBreakers_Thresholds{
Priority: core.RoutingPriority_DEFAULT,
MaxConnections: &types.UInt32Value{Value: clusterCfg.CircuitBreaker.MaxConnections},
MaxPendingRequests: &types.UInt32Value{Value: clusterCfg.CircuitBreaker.MaxPendingRequests},
MaxRequests: &types.UInt32Value{Value: clusterCfg.CircuitBreaker.MaxRequests},
MaxRetries: &types.UInt32Value{Value: clusterCfg.CircuitBreaker.MaxRetries},
}
cluster := &v2.Cluster{
Name: c.Name,
ConnectTimeout: 1 * time.Second,
Type: v2.Cluster_EDS,
DnsLookupFamily: v2.Cluster_V4_ONLY,
LbPolicy: v2.Cluster_ROUND_ROBIN,
CircuitBreakers: &cluster.CircuitBreakers{
Thresholds: []*cluster.CircuitBreakers_Thresholds{cb},
},
// TODO: implement od
// OutlierDetection: &cluster.OutlierDetection{},
HealthChecks: []*core.HealthCheck{
{
Timeout: &clusterCfg.HealthCheck.Timeout,
Interval: &clusterCfg.HealthCheck.Interval,
UnhealthyThreshold: &types.UInt32Value{Value: 3},
HealthyThreshold: &types.UInt32Value{Value: 3},
AltPort: healthCheckPort,
HealthChecker: &core.HealthCheck_HttpHealthCheck_{
HttpHealthCheck: &core.HealthCheck_HttpHealthCheck{
Path: clusterCfg.HealthCheck.Path,
ExpectedStatuses: []*_type.Int64Range{
{
Start: clusterCfg.HealthCheck.ExpectedStatusLower,
End: clusterCfg.HealthCheck.ExpectedStatusUpper,
},
},
},
},
},
},
EdsClusterConfig: &v2.Cluster_EdsClusterConfig{
EdsConfig: createXDSConfigSource(),
},
}
return cluster
}
func createXDSConfigSource() *core.ConfigSource {
return &core.ConfigSource{
ConfigSourceSpecifier: &core.ConfigSource_ApiConfigSource{
ApiConfigSource: &core.ApiConfigSource{
ApiType: core.ApiConfigSource_GRPC,
GrpcServices: []*core.GrpcService{
{
TargetSpecifier: &core.GrpcService_EnvoyGrpc_{
EnvoyGrpc: &core.GrpcService_EnvoyGrpc{
ClusterName: "xds",
},
},
},
},
},
},
}
}
// VHostConfig defines the VHost target cluster
type VHostConfig struct {
Hostname string
Cluster string
}
func createEnvoyVHost(cfg VHostConfig) route.VirtualHost {
vhost := route.VirtualHost{
Name: fmt.Sprintf("vhost_%s", cfg.Hostname),
Domains: []string{
cfg.Hostname,
fmt.Sprintf("%s:%d", cfg.Hostname, defaultIngressTrafficPort),
},
Routes: []route.Route{
{
Match: route.RouteMatch{
PathSpecifier: &route.RouteMatch_Prefix{
Prefix: "/",
},
},
Action: &route.Route_Route{
Route: &route.RouteAction{
ClusterSpecifier: &route.RouteAction_Cluster{
Cluster: cfg.Cluster,
},
},
},
},
},
}
return vhost
}
// Endpoints returns the endpoints as cache.Resources
func (n *Node) Endpoints() (eps []cache.Resource) {
for _, ep := range n.endpoints {
eps = append(eps, ep)
}
return
}
// Clusters returns the clusters as cache.Resources
func (n *Node) Clusters() (cls []cache.Resource) {
for _, c := range n.clusters {
cls = append(cls, c)
}
return
}
// Routes returns the routes as cache.Resources
func (n *Node) Routes() (rs []cache.Resource) {
for _, r := range n.routes {
rs = append(rs, r)
}
return
}
// Listeners returns the listeners as cache.Resources
func (n *Node) Listeners() (ls []cache.Resource) {
for _, l := range n.listeners {
ls = append(ls, l)
}
return ls
}