From 459e35391648fd477c277bc11b91fc094f9ef1e4 Mon Sep 17 00:00:00 2001 From: "Addo.Zhang" Date: Tue, 5 Jan 2021 05:24:15 +0800 Subject: [PATCH] remove duplicated address_prefix (#2239) Signed-off-by: addozhang Co-authored-by: Edu Serra --- pkg/envoy/lds/inmesh.go | 18 +++++++++++++++++- pkg/envoy/lds/inmesh_test.go | 27 +++++++++++++++++++++++++++ 2 files changed, 44 insertions(+), 1 deletion(-) diff --git a/pkg/envoy/lds/inmesh.go b/pkg/envoy/lds/inmesh.go index cdf64f1699..7095dea5b5 100644 --- a/pkg/envoy/lds/inmesh.go +++ b/pkg/envoy/lds/inmesh.go @@ -274,9 +274,25 @@ func (lb *listenerBuilder) getOutboundFilterChainMatchForService(dstSvc service. return nil, err } + contains := func(s []string, t string) bool { + for _, a := range s { + if a == t { + return true + } + } + return false + } + + var endpointSet []string for _, endp := range endpoints { + if !contains(endpointSet, endp.IP.String()) { + endpointSet = append(endpointSet, endp.IP.String()) + } + } + + for _, ip := range endpointSet { filterMatch.PrefixRanges = append(filterMatch.PrefixRanges, &xds_core.CidrRange{ - AddressPrefix: endp.IP.String(), + AddressPrefix: ip, PrefixLen: &wrapperspb.UInt32Value{ Value: singleIpv4Mask, }, diff --git a/pkg/envoy/lds/inmesh_test.go b/pkg/envoy/lds/inmesh_test.go index ea72abad6e..56f7f4a435 100644 --- a/pkg/envoy/lds/inmesh_test.go +++ b/pkg/envoy/lds/inmesh_test.go @@ -356,6 +356,33 @@ func TestGetOutboundFilterChainMatchForService(t *testing.T) { expectedFilterChainMatch: nil, expectError: true, }, + + { + // test case 5 + name: "outbound TCP filter chain for service with duplicated endpoints", + endpoints: []endpoint.Endpoint{ + { + IP: net.IPv4(192, 168, 10, 1), + Port: 8080, + }, + { + IP: net.IPv4(192, 168, 10, 1), + Port: 8090, + }, + }, + appProtocol: tcpAppProtocol, + expectedFilterChainMatch: &xds_listener.FilterChainMatch{ + PrefixRanges: []*xds_core.CidrRange{ + { + AddressPrefix: "192.168.10.1", + PrefixLen: &wrapperspb.UInt32Value{ + Value: 32, + }, + }, + }, + }, + expectError: false, + }, } for i, tc := range testCases {