forked from envoyproxy/xds-relay
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
82 lines (67 loc) · 2.74 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package main
import (
"context"
"crypto/rand"
"fmt"
"math/big"
"net"
"os"
"time"
"google.golang.org/grpc"
clusterservice "github.com/envoyproxy/go-control-plane/envoy/service/cluster/v3"
discovery "github.com/envoyproxy/go-control-plane/envoy/service/discovery/v3"
endpointservice "github.com/envoyproxy/go-control-plane/envoy/service/endpoint/v3"
listenerservice "github.com/envoyproxy/go-control-plane/envoy/service/listener/v3"
routeservice "github.com/envoyproxy/go-control-plane/envoy/service/route/v3"
"github.com/envoyproxy/go-control-plane/pkg/cache/v3"
xds "github.com/envoyproxy/go-control-plane/pkg/server/v3"
gcpresourcev3 "github.com/envoyproxy/go-control-plane/pkg/test/resource/v3"
)
func generateTestSnapshotNewVersion(snapshotCache cache.SnapshotCache) {
// infinite loop where we wait for some time and override objects in the cache
snapshotConfig := gcpresourcev3.TestSnapshot{
Xds: "xds",
UpstreamPort: uint32(12000),
BasePort: uint32(9000),
NumClusters: 3,
NumHTTPListeners: 2,
}
for i := 0; i < 100; i++ {
fmt.Println("wait 10s before generating the new version")
time.Sleep(10 * time.Second)
// Add some randomness in the definition of the new version
newVersion, _ := rand.Int(rand.Reader, big.NewInt(100000))
fmt.Printf("new version = v%d\n", newVersion)
snapshotConfig.Version = fmt.Sprintf("v%d", newVersion)
snapshot := snapshotConfig.Generate()
if err := snapshot.Consistent(); err != nil {
fmt.Printf("snapshot inconsistency: %+v", snapshot)
}
err := snapshotCache.SetSnapshot("envoy-client-1", snapshot)
if err != nil {
fmt.Printf("set snapshot error %q for %+v", err, snapshot)
}
}
fmt.Println("reached the end of the xDS generation data. Exiting the program.")
os.Exit(0)
}
func runServer(snapshotCache cache.SnapshotCache, port int) {
server := xds.NewServer(context.Background(), snapshotCache, nil)
grpcServer := grpc.NewServer()
listener, _ := net.Listen("tcp", fmt.Sprintf(":%d", port))
discovery.RegisterAggregatedDiscoveryServiceServer(grpcServer, server)
endpointservice.RegisterEndpointDiscoveryServiceServer(grpcServer, server)
clusterservice.RegisterClusterDiscoveryServiceServer(grpcServer, server)
routeservice.RegisterRouteDiscoveryServiceServer(grpcServer, server)
listenerservice.RegisterListenerDiscoveryServiceServer(grpcServer, server)
if err := grpcServer.Serve(listener); err != nil {
fmt.Println("something went wrong in the server")
}
}
func main() {
managementServerPort := 18000
snapshotCache := cache.NewSnapshotCache(false, cache.IDHash{}, nil)
// Start producing new versions of the test snapshot cache in a goroutine
go generateTestSnapshotNewVersion(snapshotCache)
runServer(snapshotCache, managementServerPort)
}