forked from nstehr/bobcaygeon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
control.go
300 lines (262 loc) · 8.3 KB
/
control.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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
package control
import (
"fmt"
"log"
"net"
"time"
apiv2 "github.com/envoyproxy/go-control-plane/envoy/api/v2"
"github.com/envoyproxy/go-control-plane/envoy/api/v2/core"
"github.com/envoyproxy/go-control-plane/envoy/api/v2/endpoint"
"github.com/envoyproxy/go-control-plane/envoy/api/v2/listener"
"github.com/envoyproxy/go-control-plane/envoy/api/v2/route"
hcm "github.com/envoyproxy/go-control-plane/envoy/config/filter/network/http_connection_manager/v2"
discoveryv3 "github.com/envoyproxy/go-control-plane/envoy/service/discovery/v3"
"github.com/envoyproxy/go-control-plane/pkg/cache/v3"
xds "github.com/envoyproxy/go-control-plane/pkg/server/v3"
// "github.com/envoyproxy/go-control-plane/pkg/util"
"github.com/gogo/protobuf/types"
"google.golang.org/grpc"
)
const (
clusterName = "mgmt-cluster"
listenerName = "listener_0"
listenerPort = 9211
envoyNodeName = "bcg-frontend-envoy" // needs to match what is in the envoy config
)
type hash struct {
}
// ID function
func (h hash) ID(node *core.Node) string {
if node == nil {
return "unknown"
}
return node.Id
}
// ControlPlane represents an Envoy control plane we use to dynamically add endpoints
type ControlPlane struct {
apiPort int
snapshotCache cache.SnapshotCache
cacheVersion int32
cluster *api.Cluster
listener *api.Listener
endpoints *api.ClusterLoadAssignment
}
// MgmtEndpoint represents a single endpoint of a bcg-mgmt server that will become proxied by us
type MgmtEndpoint struct {
Host string
Port uint32
}
// NewControlPlane will instantiate a control plane
func NewControlPlane(apiPort int) *ControlPlane {
cp := &ControlPlane{apiPort: apiPort,
// snapshotCache: cache.NewSnapshotCache(false, hash{}, nil),
cacheVersion: 1}
// build our initial dyanmic configuration
// TODO: could probably just use the EDS service,
// and everything else be static...
la := buildEndpoints([]MgmtEndpoint{})
c := buildCluster()
l := buildListener()
cp.cluster = c
cp.listener = l
cp.endpoints = la
cp.newSnapshot()
return cp
}
// Start starts up a control plane to respond to Envoy XDS requests
func (cp *ControlPlane) Start() {
server := xds.NewServer(cp.snapshotCache, nil)
grpcServer := grpc.NewServer()
lis, _ := net.Listen("tcp", fmt.Sprintf(":%d", cp.apiPort))
discoveryv3.RegisterAggregatedDiscoveryServiceServer(grpcServer, server)
apiv2.RegisterEndpointDiscoveryServiceServer(grpcServer, server)
apiv2.RegisterClusterDiscoveryServiceServer(grpcServer, server)
apiv2.RegisterRouteDiscoveryServiceServer(grpcServer, server)
apiv2.RegisterListenerDiscoveryServiceServer(grpcServer, server)
grpcServer.Serve(lis)
}
// UpdateEndpoints will tell Envoy to direct traffic to the MgmtEndpoints
func (cp *ControlPlane) UpdateEndpoints(mgmtEndpoints []MgmtEndpoint) {
cp.endpoints = buildEndpoints(mgmtEndpoints)
cp.newSnapshot()
}
// AddEndpoint adds a single endpoint
func (cp *ControlPlane) AddEndpoint(mgmtEndpoint MgmtEndpoint) {
l := cp.endpoints.Endpoints[0].LbEndpoints
l = append(l, buildEndpoint(mgmtEndpoint))
cp.endpoints.Endpoints[0].LbEndpoints = l
cp.newSnapshot()
}
// RemoveEndpoint remove a single endpoint
func (cp *ControlPlane) RemoveEndpoint(mgmtEndpoint MgmtEndpoint) {
l := cp.endpoints.Endpoints[0].LbEndpoints
foundIndex := -1
for i, e := range l {
addr := e.Endpoint.Address.GetSocketAddress().Address
port := e.Endpoint.Address.GetSocketAddress().GetPortValue()
if addr == mgmtEndpoint.Host && port == mgmtEndpoint.Port {
foundIndex = i
break
}
}
if foundIndex >= 0 {
// remove the list of endpoints
l = append(l[:foundIndex], l[foundIndex+1:]...)
cp.endpoints.Endpoints[0].LbEndpoints = l
cp.newSnapshot()
}
}
func (cp *ControlPlane) newSnapshot() {
snapshot, err := cache.NewSnapshot(
fmt.Sprint(cp.cacheVersion), []cache.ResourceSnapshot{cp.endpoints},
[]cache.ResourceSnapshot{cp.cluster}, nil, []cache.Resource{cp.listener})
if err != nil {
log.Fatal(err)
}
_ = cp.snapshotCache.SetSnapshot(envoyNodeName, snapshot)
cp.cacheVersion = cp.cacheVersion + 1
}
func buildCluster() *api.Cluster {
//EDS, or endpoint discovery service is what we can use to dynamically add endpoints
// that will be proxied. Here we create a source, point it to our endpoint that
/// represents our control plane
edsSource := &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_cluster"},
},
}},
},
},
}
// creates a cluster. A cluster in envoy terms represents a collection
// of endpoints that can be called. We tell our cluster that
// endpoints will be dynamically discovered, letting us control
// service discovery
clust := &api.Cluster{
Name: clusterName,
ConnectTimeout: 250 * time.Millisecond,
Type: api.Cluster_EDS,
EdsClusterConfig: &api.Cluster_EdsClusterConfig{EdsConfig: edsSource},
Http2ProtocolOptions: &core.Http2ProtocolOptions{},
}
return clust
}
func buildListener() *api.Listener {
// Listener is the top level object for envoy config
l := api.Listener{}
l.Name = listenerName
// address of our proxy that callers will access
l.Address = &core.Address{
Address: &core.Address_SocketAddress{
SocketAddress: &core.SocketAddress{
Protocol: core.TCP,
Address: "0.0.0.0",
PortSpecifier: &core.SocketAddress_PortValue{
PortValue: listenerPort,
},
},
},
}
r := route.Route{}
r.Match = &route.RouteMatch{
PathSpecifier: &route.RouteMatch_Prefix{
Prefix: "/",
},
}
t := 0 * time.Second
// this route action specifies to direct traffic we receive on this
// listener to the specified cluster
r.Action = &route.Route_Route{
Route: &route.RouteAction{
ClusterSpecifier: &route.RouteAction_Cluster{
Cluster: clusterName,
},
MaxGrpcTimeout: &t,
},
}
// classic CORS...
cors := &route.CorsPolicy{}
cors.AllowOrigin = []string{"*"}
cors.AllowMethods = "GET, PUT, DELETE, POST, OPTIONS"
cors.AllowHeaders = "keep-alive,user-agent,cache-control,content-type,content-transfer-encoding,custom-header-1,x-accept-content-transfer-encoding,x-accept-response-streaming,x-user-agent,x-grpc-web,grpc-timeout"
cors.MaxAge = "1728000"
cors.ExposeHeaders = "custom-header-1,grpc-status,grpc-message"
cors.Enabled = &types.BoolValue{Value: true}
v := route.VirtualHost{}
v.Name = "local_service"
v.Domains = []string{"*"}
v.Routes = []*route.Route{&r}
v.Cors = cors
// the filters are the interesting part here
// setups our handling of grpc-web and CORS
manager := &hcm.HttpConnectionManager{
CodecType: hcm.AUTO,
StatPrefix: "ingress_http",
RouteSpecifier: &hcm.HttpConnectionManager_RouteConfig{
RouteConfig: &api.RouteConfiguration{
Name: "local_route",
VirtualHosts: []*route.VirtualHost{&v},
},
},
HttpFilters: []*hcm.HttpFilter{{
Name: util.GRPCWeb,
},
{
Name: util.CORS,
},
{
Name: util.Router,
},
},
}
pbst, err := util.MessageToStruct(manager)
if err != nil {
log.Println("error: ", err)
}
l.FilterChains = []*listener.FilterChain{{
Filters: []*listener.Filter{{
Name: util.HTTPConnectionManager,
ConfigType: &listener.Filter_Config{
Config: pbst,
},
}},
}}
return &l
}
func buildEndpoints(mgmtEndpoints []MgmtEndpoint) *api.ClusterLoadAssignment {
// endpoints are members of the Envoy cluster that will respond to API calls
// in our case, they are the API endpoints of the bcg-mgmt servers
var endpoints []endpoint.LbEndpoint
for _, mgmtEndpoint := range mgmtEndpoints {
lbEndpoint := buildEndpoint(mgmtEndpoint)
endpoints = append(endpoints, lbEndpoint)
}
la := &api.ClusterLoadAssignment{
ClusterName: clusterName,
Endpoints: []*endpoint.LocalityLbEndpoints{{
LbEndpoints: endpoints,
}},
}
return la
}
func buildEndpoint(mgmtEndpoint MgmtEndpoint) endpoint.LbEndpoint {
return endpoint.LbEndpoint{
Endpoint: &endpoint.Endpoint{
Address: &core.Address{
Address: &core.Address_SocketAddress{
SocketAddress: &core.SocketAddress{
Protocol: core.TCP,
Address: mgmtEndpoint.Host,
PortSpecifier: &core.SocketAddress_PortValue{
PortValue: mgmtEndpoint.Port,
},
},
},
},
},
}
}