Skip to content

Commit

Permalink
check BuildHTTPRoutesForVirtualService first (#33838)
Browse files Browse the repository at this point in the history
  • Loading branch information
hzxuzhonghu committed Jul 8, 2021
1 parent fb62ce5 commit c8dc38e
Showing 1 changed file with 36 additions and 33 deletions.
69 changes: 36 additions & 33 deletions pilot/pkg/networking/core/v1alpha3/route/route.go
Expand Up @@ -26,7 +26,7 @@ import (
xdshttpfault "github.com/envoyproxy/go-control-plane/envoy/extensions/filters/http/fault/v3"
matcher "github.com/envoyproxy/go-control-plane/envoy/type/matcher/v3"
xdstype "github.com/envoyproxy/go-control-plane/envoy/type/v3"
wellknown "github.com/envoyproxy/go-control-plane/pkg/wellknown"
"github.com/envoyproxy/go-control-plane/pkg/wellknown"
"github.com/golang/protobuf/ptypes/any"
"github.com/golang/protobuf/ptypes/duration"
"github.com/golang/protobuf/ptypes/wrappers"
Expand Down Expand Up @@ -95,38 +95,13 @@ func BuildSidecarVirtualHostWrapper(node *model.Proxy, push *model.PushContext,
}

// compute services missing virtual service configs
missing := make(map[host.Name]struct{})
for fqdn := range serviceRegistry {
missing[fqdn] = struct{}{}
}
for _, wrapper := range out {
for _, service := range wrapper.Services {
delete(missing, service.Hostname)
delete(serviceRegistry, service.Hostname)
}
}

// append default hosts for the service missing virtual services
for hn := range missing {
svc := serviceRegistry[hn]
for _, port := range svc.Ports {
if port.Protocol.IsHTTP() || util.IsProtocolSniffingEnabledForPort(port) {
cluster := model.BuildSubsetKey(model.TrafficDirectionOutbound, "", svc.Hostname, port.Port)
traceOperation := traceOperation(string(svc.Hostname), port.Port)
httpRoute := BuildDefaultHTTPOutboundRoute(node, cluster, traceOperation)

// if this host has no virtualservice, the consistentHash on its destinationRule will be useless
if hashPolicy := getHashPolicyByService(node, push, svc, port); hashPolicy != nil {
httpRoute.GetRoute().HashPolicy = []*route.RouteAction_HashPolicy{hashPolicy}
}
out = append(out, VirtualHostWrapper{
Port: port.Port,
Services: []*model.Service{svc},
Routes: []*route.Route{httpRoute},
})
}
}
}

out = append(out, buildSidecarVirtualHostsForService(node, push, serviceRegistry)...)
return out
}

Expand Down Expand Up @@ -183,6 +158,12 @@ func buildSidecarVirtualHostsForVirtualService(
virtualService config.Config,
serviceRegistry map[host.Name]*model.Service,
listenPort int) []VirtualHostWrapper {
meshGateway := map[string]bool{constants.IstioMeshGateway: true}
routes, err := BuildHTTPRoutesForVirtualService(node, push, virtualService, serviceRegistry, listenPort, meshGateway)
if err != nil || len(routes) == 0 {
return nil
}

hosts, servicesInVirtualService := separateVSHostsAndServices(virtualService, serviceRegistry)

// Now group these services by port so that we can infer the destination.port if the user
Expand All @@ -208,12 +189,7 @@ func buildSidecarVirtualHostsForVirtualService(
// the current code is written.
serviceByPort[80] = nil
}
meshGateway := map[string]bool{constants.IstioMeshGateway: true}
out := make([]VirtualHostWrapper, 0, len(serviceByPort))
routes, err := BuildHTTPRoutesForVirtualService(node, push, virtualService, serviceRegistry, listenPort, meshGateway)
if err != nil || len(routes) == 0 {
return out
}
for port, service := range serviceByPort {
out = append(out, VirtualHostWrapper{
Port: port,
Expand All @@ -226,6 +202,33 @@ func buildSidecarVirtualHostsForVirtualService(
return out
}

func buildSidecarVirtualHostsForService(
node *model.Proxy,
push *model.PushContext,
serviceRegistry map[host.Name]*model.Service) []VirtualHostWrapper {
out := make([]VirtualHostWrapper, 0)
for _, svc := range serviceRegistry {
for _, port := range svc.Ports {
if port.Protocol.IsHTTP() || util.IsProtocolSniffingEnabledForPort(port) {
cluster := model.BuildSubsetKey(model.TrafficDirectionOutbound, "", svc.Hostname, port.Port)
traceOperation := traceOperation(string(svc.Hostname), port.Port)
httpRoute := BuildDefaultHTTPOutboundRoute(node, cluster, traceOperation)

// if this host has no virtualservice, the consistentHash on its destinationRule will be useless
if hashPolicy := getHashPolicyByService(node, push, svc, port); hashPolicy != nil {
httpRoute.GetRoute().HashPolicy = []*route.RouteAction_HashPolicy{hashPolicy}
}
out = append(out, VirtualHostWrapper{
Port: port.Port,
Services: []*model.Service{svc},
Routes: []*route.Route{httpRoute},
})
}
}
}
return out
}

// GetDestinationCluster generates a cluster name for the route, or error if no cluster
// can be found. Called by translateRule to determine if
func GetDestinationCluster(destination *networking.Destination, service *model.Service, listenerPort int) string {
Expand Down

0 comments on commit c8dc38e

Please sign in to comment.