-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
68 lines (56 loc) · 2.38 KB
/
main.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
package main
import (
"flag"
"fmt"
"net"
"github.com/golang/glog"
api "github.com/morvencao/kube-envoy-xds/envoy/api/v2"
discovery "github.com/morvencao/kube-envoy-xds/envoy/service/discovery/v2"
cache "github.com/morvencao/kube-envoy-xds/pkg/cache"
resource "github.com/morvencao/kube-envoy-xds/pkg/resource"
xds "github.com/morvencao/kube-envoy-xds/pkg/server"
"google.golang.org/grpc"
)
var port = flag.Int("port", 15010, "The gRPC server port.")
var mode = flag.String("mode", "xds", "The mode of envoy management server, can be 'xds' or 'ads'(default value).")
const (
version1 = "v1.0"
version2 = "v2.0"
dataserviceEnvoyNode = "dataservice-envoy"
dataserviceClusterName = "dataservice"
dataserviceEndpointHost = "127.0.0.1"
dataserviceEndpointPort = 9090
dataserviceListenerName = "dataservice-http_listener"
dataserviceListenerHost = "0.0.0.0"
dataserviceListenerPort = 8300
dataserviceTrafficDirection = "ingress"
dataserviceRouteName = "local_route"
dataservicePrefix = "/data"
)
func main() {
flag.Parse()
lis, err := net.Listen("tcp", fmt.Sprintf("0.0.0.0:%v", *port))
if err != nil {
glog.Fatalf("failed to listen: %v", err)
}
cluster := resource.MakeCluster(*mode, dataserviceClusterName)
endpoint := resource.MakeEndpoint(dataserviceClusterName, dataserviceEndpointHost, dataserviceEndpointPort)
listener := resource.MakeHTTPListener(*mode, dataserviceListenerName, dataserviceListenerHost, dataserviceListenerPort, dataserviceTrafficDirection, dataserviceRouteName)
router := resource.MakeRoute(dataserviceClusterName, dataserviceRouteName, dataservicePrefix)
snapshotCache := cache.NewSnapshotCache()
snapshot := resource.NewSnapshot(version1, []resource.Resource{cluster}, []resource.Resource{endpoint}, []resource.Resource{listener}, []resource.Resource{router})
snapshotCache.SetSnapshot(dataserviceEnvoyNode, snapshot)
grpcSvr := grpc.NewServer()
xdsSvr, err := xds.NewXDSServer(snapshotCache)
if err != nil {
glog.Fatalf("failed to create GRPC server: %v", err)
}
api.RegisterClusterDiscoveryServiceServer(grpcSvr, xdsSvr)
api.RegisterEndpointDiscoveryServiceServer(grpcSvr, xdsSvr)
api.RegisterListenerDiscoveryServiceServer(grpcSvr, xdsSvr)
api.RegisterRouteDiscoveryServiceServer(grpcSvr, xdsSvr)
discovery.RegisterAggregatedDiscoveryServiceServer(grpcSvr, xdsSvr)
// start the gRPC server
glog.Info("starting the xDS server...")
grpcSvr.Serve(lis)
}