-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
145 lines (128 loc) · 3.62 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
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
package main
import (
"context"
"errors"
"fmt"
"net"
"os"
core "github.com/envoyproxy/go-control-plane/envoy/config/core/v3"
endpoint "github.com/envoyproxy/go-control-plane/envoy/config/endpoint/v3"
discovery "github.com/envoyproxy/go-control-plane/envoy/service/discovery/v3"
endpointservice "github.com/envoyproxy/go-control-plane/envoy/service/endpoint/v3"
cache "github.com/envoyproxy/go-control-plane/pkg/cache/v3"
"github.com/envoyproxy/go-control-plane/pkg/resource/v3"
log "github.com/sirupsen/logrus"
"google.golang.org/grpc"
"google.golang.org/grpc/reflection"
"google.golang.org/protobuf/types/known/anypb"
)
type server struct {
}
func init() {
// Log as JSON instead of the default ASCII formatter.
log.SetFormatter(&log.TextFormatter{})
// Output to stdout instead of the default stderr
// Can be any io.Writer, see below for File example
log.SetOutput(os.Stdout)
// Only log the warning severity or above.
log.SetLevel(log.InfoLevel)
}
func (*server) receive(stream endpointservice.EndpointDiscoveryService_StreamEndpointsServer, reqChannel chan *discovery.DiscoveryRequest) {
for {
req, err := stream.Recv()
if err != nil {
log.Error("Error while receiving message from stream", err)
}
if req.Node == nil || req.Node.Id == "" {
log.Error("Node id is not provided")
}
select {
case reqChannel <- req:
case <-stream.Context().Done():
log.Error("Stream closed")
return
}
}
}
func (s *server) StreamEndpoints(stream endpointservice.EndpointDiscoveryService_StreamEndpointsServer) error {
stop := make(chan struct{})
reqChannel := make(chan *discovery.DiscoveryRequest, 1)
go s.receive(stream, reqChannel)
for {
select {
case _, ok := <-reqChannel:
if !ok {
log.Error("Error receiving request")
return errors.New("Error receiving request")
}
eds, err := cache.MarshalResource(generateEDS())
if err != nil {
log.Error("Error while marhal resource ", err)
}
resp := &discovery.DiscoveryResponse{
TypeUrl: resource.EndpointType,
Resources: []*anypb.Any{
{
Value: eds,
TypeUrl: resource.EndpointType,
},
},
}
err = stream.Send(resp)
if err != nil {
log.Error("Error StreamingEndpoint ", err)
return err
}
case <-stop:
return nil
}
}
}
func (*server) DeltaEndpoints(stream endpointservice.EndpointDiscoveryService_DeltaEndpointsServer) error {
log.Info("Delta service not implemented")
return nil
}
func (*server) FetchEndpoints(ctx context.Context, req *discovery.DiscoveryRequest) (*discovery.DiscoveryResponse, error) {
log.Info("FetchEndpoints service not implemented")
return nil, nil
}
func generateEDS() *endpoint.ClusterLoadAssignment {
return &endpoint.ClusterLoadAssignment{
ClusterName: "hello-endpoint",
Endpoints: []*endpoint.LocalityLbEndpoints{
{
LbEndpoints: []*endpoint.LbEndpoint{
{
HostIdentifier: &endpoint.LbEndpoint_Endpoint{
Endpoint: &endpoint.Endpoint{
Address: &core.Address{
Address: &core.Address_SocketAddress{
SocketAddress: &core.SocketAddress{
Address: "10.5.1.6",
PortSpecifier: &core.SocketAddress_PortValue{
PortValue: 80,
},
},
},
},
},
},
},
},
},
},
}
}
func main() {
grpcServer := grpc.NewServer()
lis, err := net.Listen("tcp", fmt.Sprintf(":%d", 5678))
if err != nil {
log.Error(err)
}
endpointservice.RegisterEndpointDiscoveryServiceServer(grpcServer, &server{})
reflection.Register(grpcServer)
log.Infof("management server listening on %d", 5678)
if err = grpcServer.Serve(lis); err != nil {
log.Error(err)
}
}