Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[NPM Lite] Querying L1VH + Non-L1VH Endpoints #3086

Merged
merged 32 commits into from
Nov 6, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
ae849be
Added logic to make 2 hns calls for 2 different endpoint states
rejain456 Oct 22, 2024
6e56bba
added querying to l1vh hns only if npm lite is enabled
rejain456 Oct 24, 2024
98a5d1e
added logging line for debugging
rejain456 Oct 24, 2024
ddc83b0
updated config
rejain456 Oct 24, 2024
382c150
removed logging lines
rejain456 Oct 24, 2024
1ef66e3
fixing go lint err
rejain456 Oct 25, 2024
70fb5c4
refactored based on pr comments
rejain456 Oct 25, 2024
605a498
replaced with errors.Wrap and fixed a logging statement
rejain456 Oct 25, 2024
6dde355
added if condition with logic
rejain456 Oct 25, 2024
4abb1c9
changed errl1vh to err
rejain456 Oct 25, 2024
f19fd62
added omments
rejain456 Oct 25, 2024
a79e44f
added logging lines for debugging
rejain456 Oct 25, 2024
79b54fb
added npm lite enabled log debugging
rejain456 Oct 25, 2024
65714fb
spacing
rejain456 Oct 25, 2024
7b2e422
syntax
rejain456 Oct 25, 2024
79d19b8
added logs for debugging
rejain456 Oct 25, 2024
27b76cd
optimizing api load
rejain456 Oct 25, 2024
7728b31
added function to remove common endpoints
rejain456 Oct 29, 2024
b6e07fb
added logging for debugging
rejain456 Oct 29, 2024
b1b941e
removed npm lite check
rejain456 Oct 30, 2024
ae341d5
removed all the debugging comments
rejain456 Oct 31, 2024
c5b18be
added extra unit test cases
rejain456 Oct 31, 2024
3c237ba
added additional unit tests
rejain456 Oct 31, 2024
3089651
removed protobuf code
rejain456 Oct 31, 2024
15a1182
fixed comment
rejain456 Nov 4, 2024
fd985c7
fixed a spelling error
rejain456 Nov 4, 2024
60dda1c
resolved pr comments
rejain456 Nov 4, 2024
36f0d74
updated a comment
rejain456 Nov 4, 2024
ab7038d
revised comment
rejain456 Nov 4, 2024
2c66d63
resolved further pr comments
rejain456 Nov 4, 2024
10d116f
changed back to for loop from range
rejain456 Nov 4, 2024
cd17bd3
Merge branch 'master' into jainriya/hnsEndpointFixL1VH
rejain456 Nov 5, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 14 additions & 28 deletions npm/pkg/dataplane/dataplane_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func (dp *DataPlane) initializeDataPlane() error {
}
dp.endpointQuery.query.Filter = string(filter)

// Filter out any endpoints that are not in "Attached" State. All running Windows L1VH pods with networking must be in this state.
// Filter out any endpoints that are not in "Attached" State. All running Windows pods on L1VH with networking must be in this state.
filterMapAttached := map[string]uint16{"State": hcnEndpointStateAttached}
filterAttached, err := json.Marshal(filterMapAttached)
if err != nil {
Expand Down Expand Up @@ -365,8 +365,8 @@ func (dp *DataPlane) getLocalPodEndpoints() ([]*hcn.HostComputeEndpoint, error)
return nil, errors.Wrap(err, "failed to get local pod endpoints in state: attachedSharing")
}

// Filtering out any of the same endpoints between endpoints with state attached and attachedSharing
endpoints = removeCommonEndpoints(&endpoints, &endpointsAttached)
// Get endpoints unique to endpoints and endpointsAttached
endpoints = GetUniqueEndpoints(endpoints, endpointsAttached)

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

func removeCommonEndpoints(endpoints, endpointsAttached *[]hcn.HostComputeEndpoint) []hcn.HostComputeEndpoint {
// Choose smaller and larger lists based on length for efficiency
smallerEndpointsList, largerEndpointsList := endpoints, endpointsAttached
if len(*endpoints) > len(*endpointsAttached) {
smallerEndpointsList, largerEndpointsList = endpointsAttached, endpoints
}

// Store IDs of smaller list in a map for quick lookup
idMap := make(map[string]struct{}, len(*smallerEndpointsList))
for i := 0; i < len(*smallerEndpointsList); i++ {
ep := &(*smallerEndpointsList)[i]
func GetUniqueEndpoints(endpoints, endpointsAttached []hcn.HostComputeEndpoint) []hcn.HostComputeEndpoint {
// Store IDs of endpoints list in a map for quick lookup
idMap := make(map[string]struct{}, len(endpoints))
for i := 0; i < len(endpoints); i++ {
rejain456 marked this conversation as resolved.
Show resolved Hide resolved
ep := &(endpoints)[i]
rejain456 marked this conversation as resolved.
Show resolved Hide resolved
idMap[ep.Id] = struct{}{}
}

// Append endpoints from larger list and remove common IDs from map
result := []hcn.HostComputeEndpoint{}
for i := 0; i < len(*largerEndpointsList); i++ {
ep := (*largerEndpointsList)[i]
result = append(result, ep)
delete(idMap, ep.Id)
}

// Append remaining unique endpoints from smaller list to result
for i := 0; i < len(*smallerEndpointsList); i++ {
ep := (*smallerEndpointsList)[i]
if _, found := idMap[ep.Id]; found {
result = append(result, ep)
// Add endpointsAttached list endpoints in endpoints list if the endpoint is not in the map
for i := 0; i < len(endpointsAttached); i++ {
ep := endpointsAttached[i]
if _, ok := idMap[ep.Id]; !ok {
endpoints = append(endpoints, ep)
}
}
return result
return endpoints
}

// refreshPodEndpoints will refresh all the pod endpoints and create empty netpol references for new endpoints
Expand Down
16 changes: 6 additions & 10 deletions npm/pkg/dataplane/dataplane_windows_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package dataplane

import (
"fmt"
"reflect"
"sync"
"testing"
"time"
Expand All @@ -12,6 +11,7 @@ import (
"github.com/Azure/azure-container-networking/npm/pkg/dataplane/ipsets"
dptestutils "github.com/Azure/azure-container-networking/npm/pkg/dataplane/testutils"
"github.com/Microsoft/hcsshim/hcn"
"github.com/google/go-cmp/cmp"
"github.com/pkg/errors"
"github.com/stretchr/testify/require"
)
Expand Down Expand Up @@ -99,19 +99,19 @@ func TestRemoveCommonEndpoints(t *testing.T) {
name: "1 value same",
endpoints: []hcn.HostComputeEndpoint{{Id: "456901"}, {Id: "123456"}, {Id: "560971"}},
endpointsAttached: []hcn.HostComputeEndpoint{{Id: "567890"}, {Id: "123456"}, {Id: "789012"}},
expected: []hcn.HostComputeEndpoint{{Id: "567890"}, {Id: "123456"}, {Id: "789012"}, {Id: "456901"}, {Id: "560971"}},
expected: []hcn.HostComputeEndpoint{{Id: "456901"}, {Id: "123456"}, {Id: "560971"}, {Id: "567890"}, {Id: "789012"}},
rejain456 marked this conversation as resolved.
Show resolved Hide resolved
},
{
name: "no values same",
endpoints: []hcn.HostComputeEndpoint{{Id: "456901"}, {Id: "560971"}},
endpointsAttached: []hcn.HostComputeEndpoint{{Id: "567890"}, {Id: "789012"}},
expected: []hcn.HostComputeEndpoint{{Id: "567890"}, {Id: "789012"}, {Id: "456901"}, {Id: "560971"}},
expected: []hcn.HostComputeEndpoint{{Id: "456901"}, {Id: "560971"}, {Id: "567890"}, {Id: "789012"}},
},
{
name: "1 value same",
endpoints: []hcn.HostComputeEndpoint{{Id: "456901"}, {Id: "123456"}, {Id: "560971"}},
endpointsAttached: []hcn.HostComputeEndpoint{{Id: "567890"}, {Id: "123456"}, {Id: "789012"}},
expected: []hcn.HostComputeEndpoint{{Id: "567890"}, {Id: "123456"}, {Id: "789012"}, {Id: "456901"}, {Id: "560971"}},
expected: []hcn.HostComputeEndpoint{{Id: "456901"}, {Id: "123456"}, {Id: "560971"}, {Id: "567890"}, {Id: "789012"}},
},
{
name: "two values same",
Expand Down Expand Up @@ -142,14 +142,10 @@ func TestRemoveCommonEndpoints(t *testing.T) {
tt := tt

t.Run(tt.name, func(t *testing.T) {
result := removeCommonEndpoints(&tt.endpoints, &tt.endpointsAttached)
// Use reflect.DeepEqual as a backup if require.Equal doesn't work as expected
if !reflect.DeepEqual(tt.expected, result) {
result := GetUniqueEndpoints(tt.endpoints, tt.endpointsAttached)
if !cmp.Equal(tt.expected, result) {
t.Errorf("Test %s failed: expected %v, got %v", tt.name, tt.expected, result)
}

// Or, if require.Equal works fine, it will display a descriptive error message
require.Equal(t, tt.expected, result, "expected array equals result")
})
}
}
Expand Down
Loading