2018-10-03 18:18:55 +00:00
|
|
|
package xds
|
|
|
|
|
|
|
|
import (
|
2021-02-26 22:23:15 +00:00
|
|
|
envoy_core_v3 "github.com/envoyproxy/go-control-plane/envoy/config/core/v3"
|
|
|
|
envoy_discovery_v3 "github.com/envoyproxy/go-control-plane/envoy/service/discovery/v3"
|
|
|
|
envoy_matcher_v3 "github.com/envoyproxy/go-control-plane/envoy/type/matcher/v3"
|
2021-02-22 21:00:15 +00:00
|
|
|
|
2020-06-23 20:19:56 +00:00
|
|
|
"github.com/golang/protobuf/proto"
|
|
|
|
"github.com/golang/protobuf/ptypes/any"
|
|
|
|
"github.com/golang/protobuf/ptypes/wrappers"
|
2018-10-03 18:18:55 +00:00
|
|
|
)
|
|
|
|
|
2021-02-26 22:23:15 +00:00
|
|
|
func createResponse(typeURL string, version, nonce string, resources []proto.Message) (*envoy_discovery_v3.DiscoveryResponse, error) {
|
2020-06-23 20:19:56 +00:00
|
|
|
anys := make([]*any.Any, 0, len(resources))
|
2018-10-09 16:57:26 +00:00
|
|
|
for _, r := range resources {
|
2018-10-03 18:18:55 +00:00
|
|
|
if r == nil {
|
|
|
|
continue
|
|
|
|
}
|
2020-06-23 20:19:56 +00:00
|
|
|
if any, ok := r.(*any.Any); ok {
|
|
|
|
anys = append(anys, any)
|
2018-10-03 18:18:55 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
data, err := proto.Marshal(r)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-06-23 20:19:56 +00:00
|
|
|
anys = append(anys, &any.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
|
|
|
}
|
2021-02-26 22:23:15 +00:00
|
|
|
resp := &envoy_discovery_v3.DiscoveryResponse{
|
2018-10-03 18:18:55 +00:00
|
|
|
VersionInfo: version,
|
|
|
|
Resources: anys,
|
|
|
|
TypeUrl: typeURL,
|
|
|
|
Nonce: nonce,
|
|
|
|
}
|
|
|
|
return resp, nil
|
|
|
|
}
|
|
|
|
|
2021-02-26 22:23:15 +00:00
|
|
|
func makeAddress(ip string, port int) *envoy_core_v3.Address {
|
|
|
|
return &envoy_core_v3.Address{
|
|
|
|
Address: &envoy_core_v3.Address_SocketAddress{
|
|
|
|
SocketAddress: &envoy_core_v3.SocketAddress{
|
2018-10-03 18:18:55 +00:00
|
|
|
Address: ip,
|
2021-02-26 22:23:15 +00:00
|
|
|
PortSpecifier: &envoy_core_v3.SocketAddress_PortValue{
|
2018-10-03 18:18:55 +00:00
|
|
|
PortValue: uint32(port),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-23 20:19:56 +00:00
|
|
|
func makeUint32Value(n int) *wrappers.UInt32Value {
|
|
|
|
return &wrappers.UInt32Value{Value: uint32(n)}
|
2018-10-03 18:18:55 +00:00
|
|
|
}
|
2019-03-22 19:37:14 +00:00
|
|
|
|
2020-06-23 20:19:56 +00:00
|
|
|
func makeBoolValue(n bool) *wrappers.BoolValue {
|
|
|
|
return &wrappers.BoolValue{Value: n}
|
2019-07-02 03:10:51 +00:00
|
|
|
}
|
2020-08-27 17:20:58 +00:00
|
|
|
|
2021-02-26 22:23:15 +00:00
|
|
|
func makeEnvoyRegexMatch(patt string) *envoy_matcher_v3.RegexMatcher {
|
|
|
|
return &envoy_matcher_v3.RegexMatcher{
|
|
|
|
EngineType: &envoy_matcher_v3.RegexMatcher_GoogleRe2{
|
|
|
|
GoogleRe2: &envoy_matcher_v3.RegexMatcher_GoogleRE2{},
|
2020-08-27 17:20:58 +00:00
|
|
|
},
|
|
|
|
Regex: patt,
|
|
|
|
}
|
|
|
|
}
|