2018-10-03 18:18:55 +00:00
|
|
|
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"
|
2019-03-22 19:37:14 +00:00
|
|
|
prototypes "github.com/gogo/protobuf/types"
|
2018-10-03 18:18:55 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func createResponse(typeURL string, version, nonce string, resources []proto.Message) (*envoy.DiscoveryResponse, error) {
|
2018-10-09 16:57:26 +00:00
|
|
|
anys := make([]types.Any, 0, len(resources))
|
|
|
|
for _, r := range resources {
|
2018-10-03 18:18:55 +00:00
|
|
|
if r == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if any, ok := r.(*types.Any); ok {
|
2018-10-09 16:57:26 +00:00
|
|
|
anys = append(anys, *any)
|
2018-10-03 18:18:55 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
data, err := proto.Marshal(r)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-10-09 16:57:26 +00:00
|
|
|
anys = append(anys, types.Any{
|
2018-10-03 18:18:55 +00:00
|
|
|
TypeUrl: typeURL,
|
|
|
|
Value: data,
|
2018-10-09 16:57:26 +00:00
|
|
|
})
|
2018-10-03 18:18:55 +00:00
|
|
|
}
|
|
|
|
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
|
|
|
|
}
|
2019-03-22 19:37:14 +00:00
|
|
|
|
|
|
|
func makeUint32Value(n int) *prototypes.UInt32Value {
|
|
|
|
return &prototypes.UInt32Value{Value: uint32(n)}
|
|
|
|
}
|