Skip to content

Commit

Permalink
refactor(xds): reduce size of meshroute.DestinationService type (#11375)
Browse files Browse the repository at this point in the history
Signed-off-by: Mike Beaumont <[email protected]>
  • Loading branch information
michaelbeaumont authored Sep 10, 2024
1 parent a6edc3a commit bcab8ea
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 24 deletions.
38 changes: 30 additions & 8 deletions pkg/plugins/policies/core/xds/meshroute/listeners.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,12 @@ func MakeHTTPSplit(
}

type DestinationService struct {
Outbound *xds_types.Outbound
Protocol core_mesh.Protocol
ServiceName string
BackendRef common_api.BackendRef
OutboundInterface mesh_proto.OutboundInterface
Resource *core_model.TypedResourceIdentifier
Tags envoy_tags.Tags
Protocol core_mesh.Protocol
ServiceName string
BackendRef common_api.BackendRef
}

func CollectServices(
Expand Down Expand Up @@ -110,7 +112,12 @@ func collectMeshService(
protocol = port.AppProtocol
}
return &DestinationService{
Outbound: outbound,
OutboundInterface: mesh_proto.OutboundInterface{
DataplaneIP: outbound.GetAddress(),
DataplanePort: outbound.GetPort(),
},
Tags: outbound.LegacyOutbound.GetTags(),
Resource: outbound.Resource,
Protocol: protocol,
ServiceName: ms.DestinationName(port.Port),
BackendRef: common_api.BackendRef{
Expand All @@ -132,7 +139,12 @@ func collectMeshExternalService(
return nil
}
return &DestinationService{
Outbound: outbound,
OutboundInterface: mesh_proto.OutboundInterface{
DataplaneIP: outbound.GetAddress(),
DataplanePort: outbound.GetPort(),
},
Tags: outbound.LegacyOutbound.GetTags(),
Resource: outbound.Resource,
Protocol: core_mesh.Protocol(mes.Spec.Match.Protocol),
ServiceName: mes.DestinationName(uint32(mes.Spec.Match.Port)),
BackendRef: common_api.BackendRef{
Expand Down Expand Up @@ -162,7 +174,12 @@ func collectMeshMultiZoneService(
protocol = port.AppProtocol
}
return &DestinationService{
Outbound: outbound,
OutboundInterface: mesh_proto.OutboundInterface{
DataplaneIP: outbound.GetAddress(),
DataplanePort: outbound.GetPort(),
},
Tags: outbound.LegacyOutbound.GetTags(),
Resource: outbound.Resource,
Protocol: protocol,
ServiceName: svc.DestinationName(port.Port),
BackendRef: common_api.BackendRef{
Expand All @@ -181,7 +198,12 @@ func collectServiceTagService(
) *DestinationService {
serviceName := outbound.LegacyOutbound.GetService()
return &DestinationService{
Outbound: outbound,
OutboundInterface: mesh_proto.OutboundInterface{
DataplaneIP: outbound.GetAddress(),
DataplanePort: outbound.GetPort(),
},
Tags: outbound.LegacyOutbound.GetTags(),
Resource: outbound.Resource,
Protocol: meshCtx.GetServiceProtocol(serviceName),
ServiceName: serviceName,
BackendRef: common_api.BackendRef{
Expand Down
10 changes: 5 additions & 5 deletions pkg/plugins/policies/meshhttproute/plugin/v1alpha1/listeners.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func generateFromService(
svc meshroute_xds.DestinationService,
) (*core_xds.ResourceSet, error) {
tags := svc.BackendRef.Tags
listenerBuilder := envoy_listeners.NewOutboundListenerBuilder(proxy.APIVersion, svc.Outbound.GetAddress(), svc.Outbound.GetPort(), core_xds.SocketAddressProtocolTCP).
listenerBuilder := envoy_listeners.NewOutboundListenerBuilder(proxy.APIVersion, svc.OutboundInterface.DataplaneIP, svc.OutboundInterface.DataplanePort, core_xds.SocketAddressProtocolTCP).
Configure(envoy_listeners.TransparentProxying(proxy.Dataplane.Spec.Networking.GetTransparentProxying())).
Configure(envoy_listeners.TagsMetadata(envoy_tags.Tags(tags).WithoutTags(mesh_proto.MeshTag)))

Expand Down Expand Up @@ -112,7 +112,7 @@ func generateFromService(
Name: listener.GetName(),
Origin: generator.OriginOutbound,
Resource: listener,
ResourceOrigin: svc.Outbound.Resource,
ResourceOrigin: svc.Resource,
Protocol: svc.Protocol,
})

Expand Down Expand Up @@ -164,8 +164,8 @@ func ComputeHTTPRouteConf(toRules rules.ToRules, svc meshroute_xds.DestinationSe
}
}
// check if there is configuration for real MeshService and prioritize it
if svc.Outbound.Resource != nil {
resourceConf := toRules.ResourceRules.Compute(*svc.Outbound.Resource, meshCtx.Resources)
if svc.Resource != nil {
resourceConf := toRules.ResourceRules.Compute(*svc.Resource, meshCtx.Resources)
if resourceConf != nil && len(resourceConf.Conf) != 0 {
conf = pointer.To(resourceConf.Conf[0].(api.PolicyDefault))
for hash := range resourceConf.BackendRefOriginIndex {
Expand Down Expand Up @@ -230,7 +230,7 @@ func prepareRoutes(toRules rules.ToRules, svc meshroute_xds.DestinationService,
defaultBackend.Weight = pointer.To(uint(100))
route.BackendRefs = []core_model.ResolvedBackendRef{{
LegacyBackendRef: &defaultBackend,
Resource: svc.Outbound.Resource,
Resource: svc.Resource,
}}
}
}
Expand Down
14 changes: 5 additions & 9 deletions pkg/plugins/policies/meshtcproute/plugin/v1alpha1/listeners.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
xds_context "github.com/kumahq/kuma/pkg/xds/context"
envoy_common "github.com/kumahq/kuma/pkg/xds/envoy"
envoy_listeners "github.com/kumahq/kuma/pkg/xds/envoy/listeners"
envoy_tags "github.com/kumahq/kuma/pkg/xds/envoy/tags"
"github.com/kumahq/kuma/pkg/xds/generator"
)

Expand All @@ -34,7 +33,7 @@ func generateFromService(

fallbackBackendRef := model.ResolvedBackendRef{
LegacyBackendRef: &svc.BackendRef,
Resource: svc.Outbound.Resource,
Resource: svc.Resource,
}
backendRefs := getBackendRefs(toRulesTCP, toRulesHTTP, svc, protocol, fallbackBackendRef, meshCtx)
if len(backendRefs) == 0 {
Expand All @@ -53,7 +52,7 @@ func generateFromService(
Name: listener.GetName(),
Origin: generator.OriginOutbound,
Resource: listener,
ResourceOrigin: svc.Outbound.Resource,
ResourceOrigin: svc.Resource,
Protocol: protocol,
})
return resources, nil
Expand Down Expand Up @@ -93,17 +92,14 @@ func buildOutboundListener(
svc meshroute_xds.DestinationService,
opts ...envoy_listeners.ListenerBuilderOpt,
) (envoy_common.NamedResource, error) {
var tags envoy_tags.Tags
if svc.Outbound.LegacyOutbound != nil {
tags = svc.Outbound.LegacyOutbound.Tags
}
tags := svc.Tags

// build listener name in format: "outbound:[IP]:[Port]"
// i.e. "outbound:240.0.0.0:80"
builder := envoy_listeners.NewOutboundListenerBuilder(
proxy.APIVersion,
svc.Outbound.GetAddress(),
svc.Outbound.GetPort(),
svc.OutboundInterface.DataplaneIP,
svc.OutboundInterface.DataplanePort,
core_xds.SocketAddressProtocolTCP,
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ func computeConf(toRules core_xds.ToRules, svc meshroute_xds.DestinationService,
}
}
// check if there is configuration for real MeshService and prioritize it
if svc.Outbound.Resource != nil {
resourceConf := toRules.ResourceRules.Compute(*svc.Outbound.Resource, meshCtx.Resources)
if svc.Resource != nil {
resourceConf := toRules.ResourceRules.Compute(*svc.Resource, meshCtx.Resources)
if resourceConf != nil && len(resourceConf.Conf) != 0 {
tcpConf = pointer.To(resourceConf.Conf[0].(api.Rule))
if o, ok := resourceConf.GetBackendRefOrigin(core_xds.EmptyMatches); ok {
Expand Down

0 comments on commit bcab8ea

Please sign in to comment.