Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
6 changes: 0 additions & 6 deletions pkg/eni/remote.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"github.com/go-logr/logr"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
k8stypes "k8s.io/apimachinery/pkg/types"
"k8s.io/utils/ptr"
"sigs.k8s.io/controller-runtime/pkg/client"
logf "sigs.k8s.io/controller-runtime/pkg/log"

Expand Down Expand Up @@ -82,11 +81,6 @@ func (l *RemoteIPResource) ToRPC() []*rpc.NetConf {

eniInfo.VfId = info.VfID

switch l.podENI.Annotations[types.ENOApi] {
case types.APIEcsHDeni:
eniInfo.VfType = ptr.To(rpc.VfType_VfTypeVPC)
}

netConf = append(netConf, &rpc.NetConf{
BasicInfo: &rpc.BasicInfo{
PodIP: podIP,
Expand Down
28 changes: 13 additions & 15 deletions pkg/eni/remote_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"k8s.io/apimachinery/pkg/runtime"
"sigs.k8s.io/controller-runtime/pkg/client/fake"

"github.com/AliyunContainerService/terway/rpc"
"github.com/AliyunContainerService/terway/types"
"github.com/AliyunContainerService/terway/types/daemon"

Expand Down Expand Up @@ -74,14 +73,10 @@ func TestToRPC(t *testing.T) {
assert.Equal(t, true, result[0].DefaultRoute)
})

t.Run("test with VfTypeVPC when APIEcsHDeni annotation is set", func(t *testing.T) {
t.Run("test with VfId set in ENIInfo", func(t *testing.T) {
vfID := uint32(5)
l := &RemoteIPResource{
podENI: networkv1beta1.PodENI{
ObjectMeta: metav1.ObjectMeta{
Annotations: map[string]string{
types.ENOApi: types.APIEcsHDeni,
},
},
Spec: networkv1beta1.PodENISpec{
Allocations: []networkv1beta1.Allocation{
{
Expand All @@ -98,7 +93,9 @@ func TestToRPC(t *testing.T) {
},
Status: networkv1beta1.PodENIStatus{
ENIInfos: map[string]networkv1beta1.ENIInfo{
"eni-11": {},
"eni-11": {
VfID: &vfID,
},
},
},
},
Expand All @@ -107,15 +104,13 @@ func TestToRPC(t *testing.T) {
result := l.ToRPC()
assert.NotNil(t, result)
assert.Equal(t, 1, len(result))
assert.Equal(t, rpc.VfType_VfTypeVPC, *result[0].ENIInfo.VfType)
assert.NotNil(t, result[0].ENIInfo.VfId)
assert.Equal(t, uint32(5), *result[0].ENIInfo.VfId)
})

t.Run("test without ENOApi annotation", func(t *testing.T) {
t.Run("test with VfId nil in ENIInfo", func(t *testing.T) {
l := &RemoteIPResource{
podENI: networkv1beta1.PodENI{
ObjectMeta: metav1.ObjectMeta{
Annotations: map[string]string{},
},
Spec: networkv1beta1.PodENISpec{
Allocations: []networkv1beta1.Allocation{
{
Expand All @@ -132,7 +127,9 @@ func TestToRPC(t *testing.T) {
},
Status: networkv1beta1.PodENIStatus{
ENIInfos: map[string]networkv1beta1.ENIInfo{
"eni-11": {},
"eni-11": {
VfID: nil,
},
},
},
},
Expand All @@ -141,8 +138,9 @@ func TestToRPC(t *testing.T) {
result := l.ToRPC()
assert.NotNil(t, result)
assert.Equal(t, 1, len(result))
assert.Nil(t, result[0].ENIInfo.VfType)
assert.Nil(t, result[0].ENIInfo.VfId)
})

}

func TestAllocateReturnsErrorWhenResourceTypeMismatch(t *testing.T) {
Expand Down
18 changes: 14 additions & 4 deletions plugin/driver/vf/vf.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,13 @@ import (
)

const (
defaultNUSAConfigPath = "/var/rdma/eni_topo"
defaultSysfsBasePath = "/sys/bus/pci/devices"
defaultSysfsBasePath = "/sys/bus/pci/devices"
vfBind = "/sys/bus/pci/drivers/virtio-pci"
)

vfBind = "/sys/bus/pci/drivers/virtio-pci"
var (
defaultNUSAConfigPath = "/var/rdma/eni_topo"
HcENIHostConfigPath = "/var/run/hc-eni-host/vf-topo-vpc"
)

type Config struct {
Expand Down Expand Up @@ -48,7 +51,14 @@ func parse(path string, config []byte) (*Configs, error) {

func GetBDFbyVFID(path string, vfID int) (string, error) {
if path == "" {
path = defaultNUSAConfigPath
_, err := os.Stat(defaultNUSAConfigPath)
if err == nil {
path = defaultNUSAConfigPath
} else if os.IsNotExist(err) {
path = HcENIHostConfigPath
} else {
return "", err
}
}
configContent, err := os.ReadFile(path)
if err != nil {
Expand Down
176 changes: 176 additions & 0 deletions plugin/driver/vf/vf_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,182 @@ func TestParse(t *testing.T) {
})
}

// TestGetBDFbyVFID tests the GetBDFbyVFID function
func TestGetBDFbyVFID(t *testing.T) {
t.Run("valid config with matching vfID", func(t *testing.T) {
// Create a temporary directory and config file
tempDir := t.TempDir()
configPath := filepath.Join(tempDir, "vf-config.json")

// Write test config data
configData := `{"eniVFs": [{"pf_id": 1,"vf_id": 9,"bdf": "0000:1:1.6"}]}`
err := os.WriteFile(configPath, []byte(configData), 0644)
require.NoError(t, err)

// Test the function
bdf, err := GetBDFbyVFID(configPath, 9)
assert.NoError(t, err)
assert.Equal(t, "0000:1:1.6", bdf)
})

t.Run("valid config with non-matching vfID", func(t *testing.T) {
// Create a temporary directory and config file
tempDir := t.TempDir()
configPath := filepath.Join(tempDir, "vf-config.json")

// Write test config data
configData := `{"eniVFs": [{"pf_id": 1,"vf_id": 9,"bdf": "0000:1:1.6"}]}`
err := os.WriteFile(configPath, []byte(configData), 0644)
require.NoError(t, err)

// Test the function with non-matching vfID
_, err = GetBDFbyVFID(configPath, 10)
assert.Error(t, err)
assert.Contains(t, err.Error(), "not found specified vfID 10")
})

t.Run("invalid config file", func(t *testing.T) {
// Create a temporary directory and config file
tempDir := t.TempDir()
configPath := filepath.Join(tempDir, "vf-config.json")

// Write invalid config data
configData := `{"eniVFs": [{"pf_id": 1,"vf_id": 9,"bdf": "0000:1:1.6"}`
err := os.WriteFile(configPath, []byte(configData), 0644)
require.NoError(t, err)

// Test the function
_, err = GetBDFbyVFID(configPath, 9)
assert.Error(t, err)
})

t.Run("non-existent config file", func(t *testing.T) {
// Test the function with non-existent config file
_, err := GetBDFbyVFID("/non/existent/path", 9)
assert.Error(t, err)
})

t.Run("empty path with defaultNUSAConfigPath existing", func(t *testing.T) {
// Create a temporary directory and config file
tempDir := t.TempDir()

// Save original value
originalPath := defaultNUSAConfigPath
// Temporarily change the default path to our temp directory
defaultNUSAConfigPath = filepath.Join(tempDir, "eni_topo")

// Make sure the default path doesn't exist
_, err := os.Stat(defaultNUSAConfigPath)
if !os.IsNotExist(err) {
os.Remove(defaultNUSAConfigPath)
}

// Write test config data to the default path
configData := `[{"pf_id": 1,"vf_id": 9,"bdf": "0000:1:1.6"}]`
err = os.WriteFile(defaultNUSAConfigPath, []byte(configData), 0644)
require.NoError(t, err)

// Test the function with empty path
bdf, err := GetBDFbyVFID("", 9)
assert.NoError(t, err)
assert.Equal(t, "0000:1:1.6", bdf)

// Restore original value
defaultNUSAConfigPath = originalPath
})

t.Run("empty path with HcENIHostConfigPath existing", func(t *testing.T) {
// Create a temporary directory and config file
tempDir := t.TempDir()

// Save original values
originalNUSAPath := defaultNUSAConfigPath
originalHCPath := HcENIHostConfigPath

// Temporarily change the paths to our temp directory
defaultNUSAConfigPath = filepath.Join(tempDir, "eni_topo")
HcENIHostConfigPath = filepath.Join(tempDir, "vf-topo-vpc")

// Make sure the defaultNUSAConfigPath doesn't exist
_, err := os.Stat(defaultNUSAConfigPath)
if !os.IsNotExist(err) {
os.Remove(defaultNUSAConfigPath)
}

// Write test config data to the HcENIHostConfigPath
configData := `{"eniVFs": [{"pf_id": 1,"vf_id": 9,"bdf": "0000:1:1.6"}]}`
err = os.WriteFile(HcENIHostConfigPath, []byte(configData), 0644)
require.NoError(t, err)

// Test the function with empty path
bdf, err := GetBDFbyVFID("", 9)
assert.NoError(t, err)
assert.Equal(t, "0000:1:1.6", bdf)

// Restore original values
defaultNUSAConfigPath = originalNUSAPath
HcENIHostConfigPath = originalHCPath
})

t.Run("empty path with neither config file existing", func(t *testing.T) {
// Save original values
originalNUSAPath := defaultNUSAConfigPath
originalHCPath := HcENIHostConfigPath

// Temporarily change the paths to non-existent files
tempDir := t.TempDir()
defaultNUSAConfigPath = filepath.Join(tempDir, "non-existent-eni_topo")
HcENIHostConfigPath = filepath.Join(tempDir, "non-existent-vf-topo-vpc")

// Test the function with empty path
_, err := GetBDFbyVFID("", 9)
assert.Error(t, err)

// Restore original values
defaultNUSAConfigPath = originalNUSAPath
HcENIHostConfigPath = originalHCPath
})

t.Run("multiple VFs with matching vfID", func(t *testing.T) {
// Create a temporary directory and config file
tempDir := t.TempDir()
configPath := filepath.Join(tempDir, "vf-config.json")

// Write test config data with multiple VFs
configData := `{"eniVFs": [{"pf_id": 1,"vf_id": 8,"bdf": "0000:1:1.5"}, {"pf_id": 1,"vf_id": 9,"bdf": "0000:1:1.6"}, {"pf_id": 1,"vf_id": 10,"bdf": "0000:1:1.7"}]}`
err := os.WriteFile(configPath, []byte(configData), 0644)
require.NoError(t, err)

// Test the function
bdf, err := GetBDFbyVFID(configPath, 9)
assert.NoError(t, err)
assert.Equal(t, "0000:1:1.6", bdf)
})

t.Run("eni controller config format", func(t *testing.T) {
// Create a temporary directory and config file
tempDir := t.TempDir()
configPath := filepath.Join(tempDir, "eni-controller-config.json")

// Write test config data in eni controller format
configData := `[{"pf_id": 1,"vf_id": 9,"bdf": "0000:1:1.6"}]`
err := os.WriteFile(configPath, []byte(configData), 0644)
require.NoError(t, err)

// Temporarily change the default path to test eni controller format
originalPath := defaultNUSAConfigPath
defaultNUSAConfigPath = configPath

// Test the function
bdf, err := GetBDFbyVFID(configPath, 9)
assert.NoError(t, err)
assert.Equal(t, "0000:1:1.6", bdf)

// Restore original value
defaultNUSAConfigPath = originalPath
})
}

// TestGetPFBDF tests the getPFBDF function with mocked sysfs structure
func TestGetPFBDF(t *testing.T) {
t.Run("invalid BDF format", func(t *testing.T) {
Expand Down
7 changes: 1 addition & 6 deletions plugin/terway/cni.go
Original file line number Diff line number Diff line change
Expand Up @@ -290,14 +290,9 @@ func parseSetupConf(ctx context.Context, args *skel.CmdArgs, alloc *rpc.NetConf,
if alloc.GetENIInfo() != nil {
mac := alloc.GetENIInfo().GetMAC()
vfID = alloc.GetENIInfo().VfId
vfType := rpc.VfType_VfTypeDefault
if vfID != nil {
// when do setup, this link must present
if alloc.GetENIInfo().VfType != nil {
vfType = *alloc.GetENIInfo().VfType
}

deviceID, err = prepareVF(ctx, int(*vfID), mac, vfType)
deviceID, err = prepareVF(ctx, int(*vfID), mac)
if err != nil {
return nil, err
}
Expand Down
15 changes: 2 additions & 13 deletions plugin/terway/cni_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -466,19 +466,8 @@ func doCmdCheck(ctx context.Context, client rpc.TerwayBackendClient, cmdArgs *cn
return nil
}

func prepareVF(ctx context.Context, id int, mac string, vfType rpc.VfType) (int32, error) {
// vf-topo-vpc
configPath := ""

switch vfType {
case rpc.VfType_VfTypeDefault:
case rpc.VfType_VfTypeVPC:
configPath = "/var/run/hc-eni-host/vf-topo-vpc"
default:
return 0, fmt.Errorf("not support this vf type")
}

deviceID, err := vf.SetupDriverAndGetNetInterface(ctx, id, configPath)
func prepareVF(ctx context.Context, id int, mac string) (int32, error) {
deviceID, err := vf.SetupDriverAndGetNetInterface(ctx, id, "")
if err != nil {
return 0, err
}
Expand Down
2 changes: 1 addition & 1 deletion plugin/terway/cni_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,7 @@ func TestDoCmdAdd(t *testing.T) {
})

// Mock prepareVF function
patches.ApplyFunc(prepareVF, func(ctx context.Context, id int, mac string, vfType rpc.VfType) (int32, error) {
patches.ApplyFunc(prepareVF, func(ctx context.Context, id int, mac string) (int32, error) {
return 1, nil
})

Expand Down
Loading
Loading