finish rds proxy

This commit is contained in:
凡羊羊 2019-10-07 16:56:19 +08:00
parent e1ee002c0e
commit f86420e8dc
2 changed files with 60 additions and 34 deletions

View File

@ -24,8 +24,8 @@ import (
"time"
"github.com/Sirupsen/logrus"
"github.com/envoyproxy/go-control-plane/envoy/api/v2"
apiv2 "github.com/envoyproxy/go-control-plane/envoy/api/v2"
v2 "github.com/envoyproxy/go-control-plane/envoy/api/v2"
"github.com/envoyproxy/go-control-plane/envoy/api/v2/auth"
"github.com/envoyproxy/go-control-plane/envoy/api/v2/cluster"
"github.com/envoyproxy/go-control-plane/envoy/api/v2/core"
@ -38,7 +38,7 @@ import (
configratelimit "github.com/envoyproxy/go-control-plane/envoy/config/ratelimit/v2"
_type "github.com/envoyproxy/go-control-plane/envoy/type"
"github.com/envoyproxy/go-control-plane/pkg/util"
"github.com/goodrain/rainbond/node/core/envoy/v1"
v1 "github.com/goodrain/rainbond/node/core/envoy/v1"
corev1 "k8s.io/api/core/v1"
)
@ -262,8 +262,8 @@ func CreateRouteVirtualHost(name string, domains []string, rateLimits []*route.R
return pvh
}
//CreateRoute create http route
func CreateRoute(host, clusterName, prefix string, headers []*route.HeaderMatcher, weight uint32) *route.Route {
//CreateRouteWithHostRewrite create route with hostRewrite
func CreateRouteWithHostRewrite(host, clusterName, prefix string, headers []*route.HeaderMatcher, weight uint32) *route.Route {
var rout *route.Route
if host != "" {
var hostRewriteSpecifier *route.RouteAction_HostRewrite
@ -295,30 +295,40 @@ func CreateRoute(host, clusterName, prefix string, headers []*route.HeaderMatche
},
},
}
} else {
rout = &route.Route{
Match: route.RouteMatch{
PathSpecifier: &route.RouteMatch_Prefix{
Prefix: prefix,
},
Headers: headers,
if err := rout.Validate(); err != nil {
logrus.Errorf("route http route config validate failure %s", err.Error())
return nil
}
}
return rout
}
//CreateRoute create http route
func CreateRoute(clusterName, prefix string, headers []*route.HeaderMatcher, weight uint32) *route.Route {
var rout *route.Route
rout = &route.Route{
Match: route.RouteMatch{
PathSpecifier: &route.RouteMatch_Prefix{
Prefix: prefix,
},
Action: &route.Route_Route{
Route: &route.RouteAction{
ClusterSpecifier: &route.RouteAction_WeightedClusters{
WeightedClusters: &route.WeightedCluster{
Clusters: []*route.WeightedCluster_ClusterWeight{
&route.WeightedCluster_ClusterWeight{
Name: clusterName,
Weight: ConversionUInt32(weight),
},
Headers: headers,
},
Action: &route.Route_Route{
Route: &route.RouteAction{
ClusterSpecifier: &route.RouteAction_WeightedClusters{
WeightedClusters: &route.WeightedCluster{
Clusters: []*route.WeightedCluster_ClusterWeight{
&route.WeightedCluster_ClusterWeight{
Name: clusterName,
Weight: ConversionUInt32(weight),
},
},
},
Priority: core.RoutingPriority_DEFAULT,
},
Priority: core.RoutingPriority_DEFAULT,
},
}
},
}
if err := rout.Validate(); err != nil {
@ -476,7 +486,7 @@ func CreateDNSLoadAssignment(serviceAlias, namespace, domain string, service *co
HostIdentifier: &endpoint.LbEndpoint_Endpoint{
Endpoint: &endpoint.Endpoint{
Address: &envoyAddress,
HealthCheckConfig: &endpoint.Endpoint_HealthCheckConfig{PortValue:uint32(port)},
HealthCheckConfig: &endpoint.Endpoint_HealthCheckConfig{PortValue: uint32(port)},
},
},
})

View File

@ -108,6 +108,30 @@ func upstreamListener(serviceAlias, namespace string, dependsServices []*api_mod
listennerName := fmt.Sprintf("%s_%s_%s_%d", namespace, serviceAlias, GetServiceAliasByService(service), ListenPort)
destService := ListennerConfig[listennerName]
statPrefix := fmt.Sprintf("%s_%s", serviceAlias, GetServiceAliasByService(service))
if domain, ok := service.Annotations["domain"]; ok && domain != "" {
protocol := service.Labels["port_protocol"]
if protocol == "https" || protocol == "http" {
logrus.Debugf("create domain listener, domain service protocol: %s", service.Labels["port_protocol"])
host := service.Annotations["domain"]
route := envoyv2.CreateRouteWithHostRewrite(host, clusterName, "/", nil, 0)
if route != nil {
pvh := envoyv2.CreateRouteVirtualHost(fmt.Sprintf("%s_%s_%s_%d", namespace, serviceAlias, GetServiceAliasByService(service), port), []string{"*"}, nil, *route)
if pvh != nil {
plds := envoyv2.CreateHTTPListener(fmt.Sprintf("%s_%s_http_%d", namespace, serviceAlias, port), envoyv2.DefaultLocalhostListenerAddress, fmt.Sprintf("%s_%d", serviceAlias, port), uint32(port), nil, *pvh)
if plds != nil {
ldsL = append(ldsL, plds)
} else {
logrus.Warnf("create listenner %s failure", fmt.Sprintf("%s_%s_http_%d", namespace, serviceAlias, port))
}
} else {
logrus.Warnf("create route virtual host of domain listener %s failure", fmt.Sprintf("%s_%s_http_%d", namespace, serviceAlias, port))
}
}
continue
}
}
// Unique by listen port
if _, ok := portMap[ListenPort]; !ok {
//listener name depend listner port
@ -121,16 +145,12 @@ func upstreamListener(serviceAlias, namespace string, dependsServices []*api_mod
}
portMap[ListenPort] = len(ldsL) - 1
}
portProtocol, _ := service.Labels["port_protocol"]
if destService != nil && destService.Protocol != "" {
logrus.Debugf("use destService.Protocol is : %s", destService.Protocol)
portProtocol = destService.Protocol
}
if domain, ok := service.Annotations["domain"]; ok && domain != "" {
logrus.Debugf("use domain service protocol: %s", service.Labels["port_protocol"])
portProtocol = service.Labels["port_protocol"]
}
if portProtocol != "" {
//TODO: support more protocol
switch portProtocol {
@ -159,11 +179,7 @@ func upstreamListener(serviceAlias, namespace string, dependsServices []*api_mod
}
}
host := ""
if domain, ok := service.Annotations["domain"]; ok && domain != "" {
host = domain
}
route := envoyv2.CreateRoute(host, clusterName, options.Prefix, headerMatchers, options.Weight)
route := envoyv2.CreateRoute(clusterName, options.Prefix, headerMatchers, options.Weight)
if route != nil {
pvh := envoyv2.CreateRouteVirtualHost(fmt.Sprintf("%s_%s_%s_%d", namespace, serviceAlias,
GetServiceAliasByService(service), port), options.Domains, nil, *route)
@ -222,7 +238,7 @@ func downstreamListener(serviceAlias, namespace string, ports []*api_model.BaseP
},
}
}
route := envoyv2.CreateRoute("", clusterName, "/", nil, 100)
route := envoyv2.CreateRoute(clusterName, "/", nil, 100)
if route == nil {
logrus.Warning("create route cirtual route failure")
continue