forked from hashicorp/consul
-
Notifications
You must be signed in to change notification settings - Fork 0
/
response.go
54 lines (50 loc) · 1.21 KB
/
response.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
package xds
import (
envoy "github.com/envoyproxy/go-control-plane/envoy/api/v2"
envoycore "github.com/envoyproxy/go-control-plane/envoy/api/v2/core"
"github.com/gogo/protobuf/proto"
"github.com/gogo/protobuf/types"
)
func createResponse(typeURL string, version, nonce string, resources []proto.Message) (*envoy.DiscoveryResponse, error) {
anys := make([]types.Any, 0, len(resources))
for _, r := range resources {
if r == nil {
continue
}
if any, ok := r.(*types.Any); ok {
anys = append(anys, *any)
continue
}
data, err := proto.Marshal(r)
if err != nil {
return nil, err
}
anys = append(anys, types.Any{
TypeUrl: typeURL,
Value: data,
})
}
resp := &envoy.DiscoveryResponse{
VersionInfo: version,
Resources: anys,
TypeUrl: typeURL,
Nonce: nonce,
}
return resp, nil
}
func makeAddress(ip string, port int) envoycore.Address {
return envoycore.Address{
Address: &envoycore.Address_SocketAddress{
SocketAddress: &envoycore.SocketAddress{
Address: ip,
PortSpecifier: &envoycore.SocketAddress_PortValue{
PortValue: uint32(port),
},
},
},
}
}
func makeAddressPtr(ip string, port int) *envoycore.Address {
a := makeAddress(ip, port)
return &a
}