Skip to content

Commit

Permalink
Merge pull request #2231 from katiewasnothere/kabaldau/sort_endpoints
Browse files Browse the repository at this point in the history
Sort the endpoints such that eth0 is first
  • Loading branch information
katiewasnothere authored Aug 14, 2024
2 parents a8ef0c4 + 07fea0d commit ba6e8ea
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 0 deletions.
23 changes: 23 additions & 0 deletions internal/uvm/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"context"
"fmt"
"os"
"slices"
"strings"

"github.com/Microsoft/go-winio"
"github.com/Microsoft/go-winio/pkg/guid"
Expand Down Expand Up @@ -36,6 +38,20 @@ var (
ErrNICNotFound = errors.New("NIC not found in network namespace")
)

func sortEndpoints(endpoints []*hns.HNSEndpoint) {
cmp := func(a, b *hns.HNSEndpoint) int {
if strings.HasSuffix(a.Name, "eth0") {
return -1
}
if strings.HasSuffix(b.Name, "eth0") {
return 1
}
return 0
}

slices.SortStableFunc(endpoints, cmp)
}

// In this function we take the namespace ID of the namespace that was created for this
// UVM. We hot add the namespace. We get the endpoints associated with this namespace
// and then hot add those endpoints.
Expand All @@ -59,6 +75,13 @@ func (uvm *UtilityVM) SetupNetworkNamespace(ctx context.Context, nsid string) er
return err
}

// Sort the endpoints.
// In some scenarios, multiple endpoints may be added to a pod. When this happens
// we need to know which endpoint to add first as the eth0 interface in the guest.
// Since we don't have CNI results here, instead look if any endpoints have the
// name eth0 at the end. If so, add that endpoint first. Otherwise this is a noop.
sortEndpoints(endpoints)

if err = uvm.AddEndpointsToNS(ctx, nsidInsideUVM, endpoints); err != nil {
// Best effort clean up the NS
if removeErr := uvm.RemoveNetNS(ctx, nsidInsideUVM); removeErr != nil {
Expand Down
69 changes: 69 additions & 0 deletions internal/uvm/network_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
//go:build windows

package uvm

import (
"fmt"
"testing"

"github.com/Microsoft/hcsshim/internal/hns"
)

func Test_SortEndpoints(t *testing.T) {
type config struct {
endpointNames []string
targetName string
}
tests := []config{
{
endpointNames: []string{"eth1", "eth0"},
targetName: "eth0",
},
{
endpointNames: []string{"eth0", "eth1"},
targetName: "eth0",
},
{
endpointNames: []string{"eth1", "name-eth0"},
targetName: "name-eth0",
},
{
endpointNames: []string{"name-eth098", "name-eth0"},
targetName: "name-eth0",
},
{
endpointNames: []string{"name-eth0", "name-eth098"},
targetName: "name-eth0",
},
{
endpointNames: []string{"random-ifname", "another-random-ifname"},
// ordering shouldn't change so the first entry should still be first
targetName: "random-ifname",
},
{
endpointNames: []string{"eth0-name", "name-eth0"},
targetName: "name-eth0",
},
{
endpointNames: []string{},
},
}
for i, test := range tests {
t.Run(fmt.Sprint(t.Name(), i), func(st *testing.T) {
endpoints := []*hns.HNSEndpoint{}
for _, n := range test.endpointNames {
e := &hns.HNSEndpoint{
Name: n,
}
endpoints = append(endpoints, e)
}

sortEndpoints(endpoints)
if len(test.endpointNames) != 0 {
if endpoints[0].Name != test.targetName {
st.Fatalf("expected endpoint sorting to return endpoint with name %s first, instead got %s", test.targetName, endpoints[0].Name)
}
}
})
}
}

0 comments on commit ba6e8ea

Please sign in to comment.