-
Notifications
You must be signed in to change notification settings - Fork 45
/
discovery_response_stream.go
130 lines (115 loc) · 3.62 KB
/
discovery_response_stream.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
package stream
import (
"log"
"strconv"
"time"
cp "github.com/envoyproxy/go-control-plane/envoy/api/v2"
dis "github.com/envoyproxy/go-control-plane/envoy/service/discovery/v2"
"github.com/gogo/protobuf/proto"
"github.com/gogo/protobuf/types"
)
type EndpointDiscoveryResponseStream interface {
SendEDS([]*cp.ClusterLoadAssignment) error
}
type ClusterDiscoveryResponseStream interface {
SendCDS([]*cp.Cluster) error
}
type RouteDiscoveryResponseStream interface {
SendRDS([]*cp.RouteConfiguration) error
}
//DiscoveryResponseStream is an xDS Stream wrapper and wraps grpc stream API and pipes DiscoveryResponse events to it.
type DiscoveryResponseStream interface {
ClusterDiscoveryResponseStream
EndpointDiscoveryResponseStream
RouteDiscoveryResponseStream
}
type responseStream struct {
stream dis.AggregatedDiscoveryService_StreamAggregatedResourcesServer
nonce int64
version int64
}
//Send a CLA on current stream
func (streamer *responseStream) SendEDS(cLAList []*cp.ClusterLoadAssignment) error {
var resources []types.Any
for _, cLA := range cLAList {
data, err := proto.Marshal(cLA)
if err != nil {
return err
}
resources = append(resources, types.Any{
TypeUrl: "type.googleapis.com/envoy.api.v2.ClusterLoadAssignment",
Value: data,
})
}
resp := &cp.DiscoveryResponse{
VersionInfo: strconv.FormatInt(int64(streamer.version), 10),
Resources: resources,
TypeUrl: "type.googleapis.com/envoy.api.v2.ClusterLoadAssignment",
Nonce: strconv.FormatInt(int64(streamer.nonce), 10),
}
streamer.stream.Send(resp)
log.Printf("sent EDS on stream: %v", resp)
streamer.version = time.Now().UnixNano()
streamer.nonce = time.Now().UnixNano()
return nil
}
//Send a Cluster on current stream
func (streamer *responseStream) SendCDS(clusters []*cp.Cluster) error {
if len(clusters) == 0 {
return nil
}
var resources []types.Any
for _, cluster := range clusters {
data, err := proto.Marshal(cluster)
if err != nil {
return err
}
resources = append(resources, types.Any{
TypeUrl: "type.googleapis.com/envoy.api.v2.Cluster",
Value: data,
})
}
resp := &cp.DiscoveryResponse{
VersionInfo: strconv.FormatInt(int64(streamer.version), 10),
Resources: resources,
TypeUrl: "type.googleapis.com/envoy.api.v2.Cluster",
Nonce: strconv.FormatInt(int64(streamer.nonce), 10),
}
streamer.stream.Send(resp)
log.Printf("sent CDS on stream: %v", resp)
streamer.version = time.Now().UnixNano()
streamer.nonce = time.Now().UnixNano()
return nil
}
//Send a Cluster on current stream
func (streamer *responseStream) SendRDS(routeConfig []*cp.RouteConfiguration) error {
if len(routeConfig) == 0 {
return nil
}
var resources []types.Any
for _, route := range routeConfig {
data, err := proto.Marshal(route)
if err != nil {
return err
}
resources = append(resources, types.Any{
TypeUrl: "type.googleapis.com/envoy.api.v2.RouteConfiguration",
Value: data,
})
}
resp := &cp.DiscoveryResponse{
VersionInfo: strconv.FormatInt(int64(streamer.version), 10),
Resources: resources,
TypeUrl: "type.googleapis.com/envoy.api.v2.RouteConfiguration",
Nonce: strconv.FormatInt(int64(streamer.nonce), 10),
}
streamer.stream.Send(resp)
log.Printf("sent RDS on stream: %v", resp)
streamer.version = time.Now().UnixNano()
streamer.nonce = time.Now().UnixNano()
return nil
}
//NewDiscoveryResponseStream creates a DiscoveryResponseStream
func NewDiscoveryResponseStream(stream dis.AggregatedDiscoveryService_StreamAggregatedResourcesServer) DiscoveryResponseStream {
return &responseStream{stream: stream, nonce: time.Now().UnixNano(), version: time.Now().UnixNano()}
}