Skip to content

Commit 60dda1c

Browse files
committed
resolved pr comments
1 parent fd985c7 commit 60dda1c

File tree

2 files changed

+18
-36
lines changed

2 files changed

+18
-36
lines changed

npm/pkg/dataplane/dataplane_windows.go

Lines changed: 12 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -366,7 +366,7 @@ func (dp *DataPlane) getLocalPodEndpoints() ([]*hcn.HostComputeEndpoint, error)
366366
}
367367

368368
// Filtering out any of the same endpoints between endpoints with state attached and attachedSharing
369-
endpoints = removeCommonEndpoints(&endpoints, &endpointsAttached)
369+
endpoints = GetUniqueEndpoints(endpoints, endpointsAttached)
370370

371371
epPointers := make([]*hcn.HostComputeEndpoint, 0, len(endpoints))
372372
for k := range endpoints {
@@ -375,36 +375,22 @@ func (dp *DataPlane) getLocalPodEndpoints() ([]*hcn.HostComputeEndpoint, error)
375375
return epPointers, nil
376376
}
377377

378-
func removeCommonEndpoints(endpoints, endpointsAttached *[]hcn.HostComputeEndpoint) []hcn.HostComputeEndpoint {
379-
// Choose smaller and larger lists based on length for efficiency
380-
smallerEndpointsList, largerEndpointsList := endpoints, endpointsAttached
381-
if len(*endpoints) > len(*endpointsAttached) {
382-
smallerEndpointsList, largerEndpointsList = endpointsAttached, endpoints
383-
}
384-
385-
// Store IDs of smaller list in a map for quick lookup
386-
idMap := make(map[string]struct{}, len(*smallerEndpointsList))
387-
for i := 0; i < len(*smallerEndpointsList); i++ {
388-
ep := &(*smallerEndpointsList)[i]
378+
func GetUniqueEndpoints(endpoints, endpointsAttached []hcn.HostComputeEndpoint) []hcn.HostComputeEndpoint {
379+
// Store IDs of endpoints list in a map for quick lookup
380+
idMap := make(map[string]struct{}, len(endpoints))
381+
for i := 0; i < len(endpoints); i++ {
382+
ep := &(endpoints)[i]
389383
idMap[ep.Id] = struct{}{}
390384
}
391385

392-
// Append endpoints from larger list and remove common IDs from map
393-
result := []hcn.HostComputeEndpoint{}
394-
for i := 0; i < len(*largerEndpointsList); i++ {
395-
ep := (*largerEndpointsList)[i]
396-
result = append(result, ep)
397-
delete(idMap, ep.Id)
398-
}
399-
400-
// Append remaining unique endpoints from smaller list to result
401-
for i := 0; i < len(*smallerEndpointsList); i++ {
402-
ep := (*smallerEndpointsList)[i]
403-
if _, found := idMap[ep.Id]; found {
404-
result = append(result, ep)
386+
// Add endpointsAttached list endpoints in endpoints list if the endpoint is not in the map
387+
for i := 0; i < len(endpointsAttached); i++ {
388+
ep := endpointsAttached[i]
389+
if _, ok := idMap[ep.Id]; !ok {
390+
endpoints = append(endpoints, ep)
405391
}
406392
}
407-
return result
393+
return endpoints
408394
}
409395

410396
// refreshPodEndpoints will refresh all the pod endpoints and create empty netpol references for new endpoints

npm/pkg/dataplane/dataplane_windows_test.go

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package dataplane
22

33
import (
44
"fmt"
5-
"reflect"
65
"sync"
76
"testing"
87
"time"
@@ -12,6 +11,7 @@ import (
1211
"github.com/Azure/azure-container-networking/npm/pkg/dataplane/ipsets"
1312
dptestutils "github.com/Azure/azure-container-networking/npm/pkg/dataplane/testutils"
1413
"github.com/Microsoft/hcsshim/hcn"
14+
"github.com/google/go-cmp/cmp"
1515
"github.com/pkg/errors"
1616
"github.com/stretchr/testify/require"
1717
)
@@ -99,19 +99,19 @@ func TestRemoveCommonEndpoints(t *testing.T) {
9999
name: "1 value same",
100100
endpoints: []hcn.HostComputeEndpoint{{Id: "456901"}, {Id: "123456"}, {Id: "560971"}},
101101
endpointsAttached: []hcn.HostComputeEndpoint{{Id: "567890"}, {Id: "123456"}, {Id: "789012"}},
102-
expected: []hcn.HostComputeEndpoint{{Id: "567890"}, {Id: "123456"}, {Id: "789012"}, {Id: "456901"}, {Id: "560971"}},
102+
expected: []hcn.HostComputeEndpoint{{Id: "456901"}, {Id: "123456"}, {Id: "560971"}, {Id: "567890"}, {Id: "789012"}},
103103
},
104104
{
105105
name: "no values same",
106106
endpoints: []hcn.HostComputeEndpoint{{Id: "456901"}, {Id: "560971"}},
107107
endpointsAttached: []hcn.HostComputeEndpoint{{Id: "567890"}, {Id: "789012"}},
108-
expected: []hcn.HostComputeEndpoint{{Id: "567890"}, {Id: "789012"}, {Id: "456901"}, {Id: "560971"}},
108+
expected: []hcn.HostComputeEndpoint{{Id: "456901"}, {Id: "560971"}, {Id: "567890"}, {Id: "789012"}},
109109
},
110110
{
111111
name: "1 value same",
112112
endpoints: []hcn.HostComputeEndpoint{{Id: "456901"}, {Id: "123456"}, {Id: "560971"}},
113113
endpointsAttached: []hcn.HostComputeEndpoint{{Id: "567890"}, {Id: "123456"}, {Id: "789012"}},
114-
expected: []hcn.HostComputeEndpoint{{Id: "567890"}, {Id: "123456"}, {Id: "789012"}, {Id: "456901"}, {Id: "560971"}},
114+
expected: []hcn.HostComputeEndpoint{{Id: "456901"}, {Id: "123456"}, {Id: "560971"}, {Id: "567890"}, {Id: "789012"}},
115115
},
116116
{
117117
name: "two values same",
@@ -142,14 +142,10 @@ func TestRemoveCommonEndpoints(t *testing.T) {
142142
tt := tt
143143

144144
t.Run(tt.name, func(t *testing.T) {
145-
result := removeCommonEndpoints(&tt.endpoints, &tt.endpointsAttached)
146-
// Use reflect.DeepEqual as a backup if require.Equal doesn't work as expected
147-
if !reflect.DeepEqual(tt.expected, result) {
145+
result := GetUniqueEndpoints(tt.endpoints, tt.endpointsAttached)
146+
if !cmp.Equal(tt.expected, result) {
148147
t.Errorf("Test %s failed: expected %v, got %v", tt.name, tt.expected, result)
149148
}
150-
151-
// Or, if require.Equal works fine, it will display a descriptive error message
152-
require.Equal(t, tt.expected, result, "expected array equals result")
153149
})
154150
}
155151
}

0 commit comments

Comments
 (0)