Skip to content

Commit

Permalink
PR review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
ishustava committed Sep 7, 2023
1 parent a281bdc commit 0f9e54b
Show file tree
Hide file tree
Showing 21 changed files with 968 additions and 280 deletions.
4 changes: 4 additions & 0 deletions internal/mesh/exports.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
package mesh

import (
"github.com/hashicorp/consul/internal/controller"
"github.com/hashicorp/consul/internal/mesh/internal/controllers"
"github.com/hashicorp/consul/internal/mesh/internal/controllers/sidecarproxy"
"github.com/hashicorp/consul/internal/mesh/internal/controllers/sidecarproxy/status"
"github.com/hashicorp/consul/internal/mesh/internal/types"
"github.com/hashicorp/consul/internal/resource"
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ type Builder struct {
proxyCfg *pbmesh.ProxyConfiguration
trustDomain string
localDatacenter string

outboundListenerBuilder *ListenerBuilder
}

func New(id *pbresource.ID,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
package builder

import (
"google.golang.org/protobuf/types/known/wrapperspb"

"github.com/hashicorp/consul/agent/connect"
"github.com/hashicorp/consul/envoyextensions/xdscommon"
"github.com/hashicorp/consul/internal/mesh/internal/types/intermediate"
Expand All @@ -12,7 +14,6 @@ import (
pbmesh "github.com/hashicorp/consul/proto-public/pbmesh/v1alpha1"
"github.com/hashicorp/consul/proto-public/pbmesh/v1alpha1/pbproxystate"
"github.com/hashicorp/consul/proto-public/pbresource"
"google.golang.org/protobuf/types/known/wrapperspb"
)

func (b *Builder) BuildDestinations(destinations []*intermediate.Destination) *Builder {
Expand Down Expand Up @@ -56,11 +57,9 @@ func (b *Builder) buildExplicitDestination(destination *intermediate.Destination
addEndpointsRef(clusterName, destination.ServiceEndpoints.Resource.Id, meshPortName)
}
}

return b
}

func (b *Builder) buildImplicitDestination(destination *intermediate.Destination) *Builder {
func (b *Builder) buildImplicitDestination(destination *intermediate.Destination) {
serviceRef := resource.Reference(destination.ServiceEndpoints.Resource.Owner, "")
clusterName := DestinationClusterName(serviceRef, b.localDatacenter, b.trustDomain)
statPrefix := DestinationStatPrefix(serviceRef, b.localDatacenter)
Expand All @@ -74,13 +73,13 @@ func (b *Builder) buildImplicitDestination(destination *intermediate.Destination
meshPortName := findMeshPort(destination.ServiceEndpoints.Endpoints.Endpoints[0].Ports)

for _, port := range destination.ServiceEndpoints.Endpoints.Endpoints[0].Ports {
b.addRouterWithIPMatch(clusterName, statPrefix, port.Protocol, destination.VirtualIPs).
b.outboundListenerBuilder.
addRouterWithIPMatch(clusterName, statPrefix, port.Protocol, destination.VirtualIPs).
buildListener().
addCluster(clusterName, destination.Identities).
addEndpointsRef(clusterName, destination.ServiceEndpoints.Resource.Id, meshPortName)
}
}

return b
}

func (b *Builder) addOutboundDestinationListener(explicit *pbmesh.Upstream) *ListenerBuilder {
Expand Down Expand Up @@ -113,7 +112,7 @@ func (b *Builder) addOutboundDestinationListener(explicit *pbmesh.Upstream) *Lis
return b.NewListenerBuilder(listener)
}

func (b *Builder) addOutboundListener(port uint32) *Builder {
func (b *Builder) addOutboundListener(port uint32) *ListenerBuilder {
listener := &pbproxystate.Listener{
Name: xdscommon.OutboundListenerName,
Direction: pbproxystate.Direction_DIRECTION_OUTBOUND,
Expand All @@ -126,16 +125,19 @@ func (b *Builder) addOutboundListener(port uint32) *Builder {
Capabilities: []pbproxystate.Capability{pbproxystate.Capability_CAPABILITY_TRANSPARENT},
}

return b.addListener(listener)
lb := b.NewListenerBuilder(listener)

// Save outbound listener builder so we can use it in the future.
b.outboundListenerBuilder = lb

return lb
}

func (l *ListenerBuilder) addRouter(clusterName, statPrefix string, protocol pbcatalog.Protocol) *ListenerBuilder {
return b.addRouterWithIPMatch(clusterName, statPrefix, protocol, nil)
return l.addRouterWithIPMatch(clusterName, statPrefix, protocol, nil)
}

func (b *Builder) addRouterWithIPMatch(clusterName, statPrefix string, protocol pbcatalog.Protocol, vips []string) *Builder {
listener := b.getLastBuiltListener()

func (l *ListenerBuilder) addRouterWithIPMatch(clusterName, statPrefix string, protocol pbcatalog.Protocol, vips []string) *ListenerBuilder {
// For explicit destinations, we have no filter chain match, and filters are based on port protocol.
router := &pbproxystate.Router{}
switch protocol {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,19 +101,24 @@ func TestBuildExplicitDestinations(t *testing.T) {
Build()

actual := protoToJSON(t, proxyTmpl)
expected := goldenValue(t, name, actual, *update)
expected := golden.Get(t, actual, name)

require.JSONEq(t, expected, actual)
}
}

func TestBuildImplicitDestinations(t *testing.T) {
api1Endpoints := resourcetest.Resource(catalog.ServiceEndpointsType, "api-1").
WithOwner(resourcetest.Resource(catalog.ServiceType, "api-1").ID()).
WithOwner(
resourcetest.Resource(catalog.ServiceType, "api-1").
WithTenancy(resource.DefaultNamespacedTenancy()).ID()).
WithTenancy(resource.DefaultNamespacedTenancy()).
WithData(t, endpointsData).Build()

api2Endpoints := resourcetest.Resource(catalog.ServiceEndpointsType, "api-2").
WithOwner(resourcetest.Resource(catalog.ServiceType, "api-2").ID()).
WithOwner(resourcetest.Resource(catalog.ServiceType, "api-2").
WithTenancy(resource.DefaultNamespacedTenancy()).ID()).
WithTenancy(resource.DefaultNamespacedTenancy()).
WithData(t, endpointsData).Build()

api1Identity := &pbresource.Reference{
Expand Down
Loading

0 comments on commit 0f9e54b

Please sign in to comment.