mirror of https://github.com/status-im/consul.git
55 lines
1.2 KiB
Go
55 lines
1.2 KiB
Go
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, len(resources))
|
|
for i, r := range resources {
|
|
if r == nil {
|
|
continue
|
|
}
|
|
if any, ok := r.(*types.Any); ok {
|
|
anys[i] = *any
|
|
continue
|
|
}
|
|
data, err := proto.Marshal(r)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
anys[i] = 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
|
|
}
|