Skip to content

Commit 63a4050

Browse files
committed
ENO api fallback to hc-eni-host path
Signed-off-by: bingshen.wbs <[email protected]>
1 parent c1afe28 commit 63a4050

File tree

12 files changed

+661
-1131
lines changed

12 files changed

+661
-1131
lines changed

pkg/eni/remote.go

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"github.com/go-logr/logr"
99
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1010
k8stypes "k8s.io/apimachinery/pkg/types"
11-
"k8s.io/utils/ptr"
1211
"sigs.k8s.io/controller-runtime/pkg/client"
1312
logf "sigs.k8s.io/controller-runtime/pkg/log"
1413

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

8382
eniInfo.VfId = info.VfID
8483

85-
switch l.podENI.Annotations[types.ENOApi] {
86-
case types.APIEcsHDeni:
87-
eniInfo.VfType = ptr.To(rpc.VfType_VfTypeVPC)
88-
}
89-
9084
netConf = append(netConf, &rpc.NetConf{
9185
BasicInfo: &rpc.BasicInfo{
9286
PodIP: podIP,

pkg/eni/remote_test.go

Lines changed: 0 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import (
1212
"k8s.io/apimachinery/pkg/runtime"
1313
"sigs.k8s.io/controller-runtime/pkg/client/fake"
1414

15-
"github.com/AliyunContainerService/terway/rpc"
1615
"github.com/AliyunContainerService/terway/types"
1716
"github.com/AliyunContainerService/terway/types/daemon"
1817

@@ -74,75 +73,6 @@ func TestToRPC(t *testing.T) {
7473
assert.Equal(t, true, result[0].DefaultRoute)
7574
})
7675

77-
t.Run("test with VfTypeVPC when APIEcsHDeni annotation is set", func(t *testing.T) {
78-
l := &RemoteIPResource{
79-
podENI: networkv1beta1.PodENI{
80-
ObjectMeta: metav1.ObjectMeta{
81-
Annotations: map[string]string{
82-
types.ENOApi: types.APIEcsHDeni,
83-
},
84-
},
85-
Spec: networkv1beta1.PodENISpec{
86-
Allocations: []networkv1beta1.Allocation{
87-
{
88-
IPv4: "192.168.1.1",
89-
IPv4CIDR: "192.168.1.0/24",
90-
ENI: networkv1beta1.ENI{
91-
ID: "eni-11",
92-
MAC: "00:00:00:00:00:00",
93-
},
94-
Interface: "eth0",
95-
DefaultRoute: true,
96-
},
97-
},
98-
},
99-
Status: networkv1beta1.PodENIStatus{
100-
ENIInfos: map[string]networkv1beta1.ENIInfo{
101-
"eni-11": {},
102-
},
103-
},
104-
},
105-
}
106-
107-
result := l.ToRPC()
108-
assert.NotNil(t, result)
109-
assert.Equal(t, 1, len(result))
110-
assert.Equal(t, rpc.VfType_VfTypeVPC, *result[0].ENIInfo.VfType)
111-
})
112-
113-
t.Run("test without ENOApi annotation", func(t *testing.T) {
114-
l := &RemoteIPResource{
115-
podENI: networkv1beta1.PodENI{
116-
ObjectMeta: metav1.ObjectMeta{
117-
Annotations: map[string]string{},
118-
},
119-
Spec: networkv1beta1.PodENISpec{
120-
Allocations: []networkv1beta1.Allocation{
121-
{
122-
IPv4: "192.168.1.1",
123-
IPv4CIDR: "192.168.1.0/24",
124-
ENI: networkv1beta1.ENI{
125-
ID: "eni-11",
126-
MAC: "00:00:00:00:00:00",
127-
},
128-
Interface: "eth0",
129-
DefaultRoute: true,
130-
},
131-
},
132-
},
133-
Status: networkv1beta1.PodENIStatus{
134-
ENIInfos: map[string]networkv1beta1.ENIInfo{
135-
"eni-11": {},
136-
},
137-
},
138-
},
139-
}
140-
141-
result := l.ToRPC()
142-
assert.NotNil(t, result)
143-
assert.Equal(t, 1, len(result))
144-
assert.Nil(t, result[0].ENIInfo.VfType)
145-
})
14676
}
14777

14878
func TestAllocateReturnsErrorWhenResourceTypeMismatch(t *testing.T) {

plugin/driver/vf/vf.go

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,13 @@ import (
1313
)
1414

1515
const (
16-
defaultNUSAConfigPath = "/var/rdma/eni_topo"
17-
defaultSysfsBasePath = "/sys/bus/pci/devices"
16+
defaultSysfsBasePath = "/sys/bus/pci/devices"
17+
vfBind = "/sys/bus/pci/drivers/virtio-pci"
18+
)
1819

19-
vfBind = "/sys/bus/pci/drivers/virtio-pci"
20+
var (
21+
defaultNUSAConfigPath = "/var/rdma/eni_topo"
22+
HcENIHostConfigPath = "/var/run/hc-eni-host/vf-topo-vpc"
2023
)
2124

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

4952
func GetBDFbyVFID(path string, vfID int) (string, error) {
5053
if path == "" {
51-
path = defaultNUSAConfigPath
54+
_, err := os.Stat(defaultNUSAConfigPath)
55+
if err == nil {
56+
path = defaultNUSAConfigPath
57+
} else if os.IsNotExist(err) {
58+
path = HcENIHostConfigPath
59+
} else {
60+
return "", err
61+
}
5262
}
5363
configContent, err := os.ReadFile(path)
5464
if err != nil {

plugin/driver/vf/vf_test.go

Lines changed: 176 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,182 @@ func TestParse(t *testing.T) {
4040
})
4141
}
4242

43+
// TestGetBDFbyVFID tests the GetBDFbyVFID function
44+
func TestGetBDFbyVFID(t *testing.T) {
45+
t.Run("valid config with matching vfID", func(t *testing.T) {
46+
// Create a temporary directory and config file
47+
tempDir := t.TempDir()
48+
configPath := filepath.Join(tempDir, "vf-config.json")
49+
50+
// Write test config data
51+
configData := `{"eniVFs": [{"pf_id": 1,"vf_id": 9,"bdf": "0000:1:1.6"}]}`
52+
err := os.WriteFile(configPath, []byte(configData), 0644)
53+
require.NoError(t, err)
54+
55+
// Test the function
56+
bdf, err := GetBDFbyVFID(configPath, 9)
57+
assert.NoError(t, err)
58+
assert.Equal(t, "0000:1:1.6", bdf)
59+
})
60+
61+
t.Run("valid config with non-matching vfID", func(t *testing.T) {
62+
// Create a temporary directory and config file
63+
tempDir := t.TempDir()
64+
configPath := filepath.Join(tempDir, "vf-config.json")
65+
66+
// Write test config data
67+
configData := `{"eniVFs": [{"pf_id": 1,"vf_id": 9,"bdf": "0000:1:1.6"}]}`
68+
err := os.WriteFile(configPath, []byte(configData), 0644)
69+
require.NoError(t, err)
70+
71+
// Test the function with non-matching vfID
72+
_, err = GetBDFbyVFID(configPath, 10)
73+
assert.Error(t, err)
74+
assert.Contains(t, err.Error(), "not found specified vfID 10")
75+
})
76+
77+
t.Run("invalid config file", func(t *testing.T) {
78+
// Create a temporary directory and config file
79+
tempDir := t.TempDir()
80+
configPath := filepath.Join(tempDir, "vf-config.json")
81+
82+
// Write invalid config data
83+
configData := `{"eniVFs": [{"pf_id": 1,"vf_id": 9,"bdf": "0000:1:1.6"}`
84+
err := os.WriteFile(configPath, []byte(configData), 0644)
85+
require.NoError(t, err)
86+
87+
// Test the function
88+
_, err = GetBDFbyVFID(configPath, 9)
89+
assert.Error(t, err)
90+
})
91+
92+
t.Run("non-existent config file", func(t *testing.T) {
93+
// Test the function with non-existent config file
94+
_, err := GetBDFbyVFID("/non/existent/path", 9)
95+
assert.Error(t, err)
96+
})
97+
98+
t.Run("empty path with defaultNUSAConfigPath existing", func(t *testing.T) {
99+
// Create a temporary directory and config file
100+
tempDir := t.TempDir()
101+
102+
// Save original value
103+
originalPath := defaultNUSAConfigPath
104+
// Temporarily change the default path to our temp directory
105+
defaultNUSAConfigPath = filepath.Join(tempDir, "eni_topo")
106+
107+
// Make sure the default path doesn't exist
108+
_, err := os.Stat(defaultNUSAConfigPath)
109+
if !os.IsNotExist(err) {
110+
os.Remove(defaultNUSAConfigPath)
111+
}
112+
113+
// Write test config data to the default path
114+
configData := `[{"pf_id": 1,"vf_id": 9,"bdf": "0000:1:1.6"}]`
115+
err = os.WriteFile(defaultNUSAConfigPath, []byte(configData), 0644)
116+
require.NoError(t, err)
117+
118+
// Test the function with empty path
119+
bdf, err := GetBDFbyVFID("", 9)
120+
assert.NoError(t, err)
121+
assert.Equal(t, "0000:1:1.6", bdf)
122+
123+
// Restore original value
124+
defaultNUSAConfigPath = originalPath
125+
})
126+
127+
t.Run("empty path with HcENIHostConfigPath existing", func(t *testing.T) {
128+
// Create a temporary directory and config file
129+
tempDir := t.TempDir()
130+
131+
// Save original values
132+
originalNUSAPath := defaultNUSAConfigPath
133+
originalHCPath := HcENIHostConfigPath
134+
135+
// Temporarily change the paths to our temp directory
136+
defaultNUSAConfigPath = filepath.Join(tempDir, "eni_topo")
137+
HcENIHostConfigPath = filepath.Join(tempDir, "vf-topo-vpc")
138+
139+
// Make sure the defaultNUSAConfigPath doesn't exist
140+
_, err := os.Stat(defaultNUSAConfigPath)
141+
if !os.IsNotExist(err) {
142+
os.Remove(defaultNUSAConfigPath)
143+
}
144+
145+
// Write test config data to the HcENIHostConfigPath
146+
configData := `{"eniVFs": [{"pf_id": 1,"vf_id": 9,"bdf": "0000:1:1.6"}]}`
147+
err = os.WriteFile(HcENIHostConfigPath, []byte(configData), 0644)
148+
require.NoError(t, err)
149+
150+
// Test the function with empty path
151+
bdf, err := GetBDFbyVFID("", 9)
152+
assert.NoError(t, err)
153+
assert.Equal(t, "0000:1:1.6", bdf)
154+
155+
// Restore original values
156+
defaultNUSAConfigPath = originalNUSAPath
157+
HcENIHostConfigPath = originalHCPath
158+
})
159+
160+
t.Run("empty path with neither config file existing", func(t *testing.T) {
161+
// Save original values
162+
originalNUSAPath := defaultNUSAConfigPath
163+
originalHCPath := HcENIHostConfigPath
164+
165+
// Temporarily change the paths to non-existent files
166+
tempDir := t.TempDir()
167+
defaultNUSAConfigPath = filepath.Join(tempDir, "non-existent-eni_topo")
168+
HcENIHostConfigPath = filepath.Join(tempDir, "non-existent-vf-topo-vpc")
169+
170+
// Test the function with empty path
171+
_, err := GetBDFbyVFID("", 9)
172+
assert.Error(t, err)
173+
174+
// Restore original values
175+
defaultNUSAConfigPath = originalNUSAPath
176+
HcENIHostConfigPath = originalHCPath
177+
})
178+
179+
t.Run("multiple VFs with matching vfID", func(t *testing.T) {
180+
// Create a temporary directory and config file
181+
tempDir := t.TempDir()
182+
configPath := filepath.Join(tempDir, "vf-config.json")
183+
184+
// Write test config data with multiple VFs
185+
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"}]}`
186+
err := os.WriteFile(configPath, []byte(configData), 0644)
187+
require.NoError(t, err)
188+
189+
// Test the function
190+
bdf, err := GetBDFbyVFID(configPath, 9)
191+
assert.NoError(t, err)
192+
assert.Equal(t, "0000:1:1.6", bdf)
193+
})
194+
195+
t.Run("eni controller config format", func(t *testing.T) {
196+
// Create a temporary directory and config file
197+
tempDir := t.TempDir()
198+
configPath := filepath.Join(tempDir, "eni-controller-config.json")
199+
200+
// Write test config data in eni controller format
201+
configData := `[{"pf_id": 1,"vf_id": 9,"bdf": "0000:1:1.6"}]`
202+
err := os.WriteFile(configPath, []byte(configData), 0644)
203+
require.NoError(t, err)
204+
205+
// Temporarily change the default path to test eni controller format
206+
originalPath := defaultNUSAConfigPath
207+
defaultNUSAConfigPath = configPath
208+
209+
// Test the function
210+
bdf, err := GetBDFbyVFID(configPath, 9)
211+
assert.NoError(t, err)
212+
assert.Equal(t, "0000:1:1.6", bdf)
213+
214+
// Restore original value
215+
defaultNUSAConfigPath = originalPath
216+
})
217+
}
218+
43219
// TestGetPFBDF tests the getPFBDF function with mocked sysfs structure
44220
func TestGetPFBDF(t *testing.T) {
45221
t.Run("invalid BDF format", func(t *testing.T) {

plugin/terway/cni.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -290,14 +290,9 @@ func parseSetupConf(ctx context.Context, args *skel.CmdArgs, alloc *rpc.NetConf,
290290
if alloc.GetENIInfo() != nil {
291291
mac := alloc.GetENIInfo().GetMAC()
292292
vfID = alloc.GetENIInfo().VfId
293-
vfType := rpc.VfType_VfTypeDefault
294293
if vfID != nil {
295294
// when do setup, this link must present
296-
if alloc.GetENIInfo().VfType != nil {
297-
vfType = *alloc.GetENIInfo().VfType
298-
}
299-
300-
deviceID, err = prepareVF(ctx, int(*vfID), mac, vfType)
295+
deviceID, err = prepareVF(ctx, int(*vfID), mac)
301296
if err != nil {
302297
return nil, err
303298
}

plugin/terway/cni_linux.go

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -466,19 +466,8 @@ func doCmdCheck(ctx context.Context, client rpc.TerwayBackendClient, cmdArgs *cn
466466
return nil
467467
}
468468

469-
func prepareVF(ctx context.Context, id int, mac string, vfType rpc.VfType) (int32, error) {
470-
// vf-topo-vpc
471-
configPath := ""
472-
473-
switch vfType {
474-
case rpc.VfType_VfTypeDefault:
475-
case rpc.VfType_VfTypeVPC:
476-
configPath = "/var/run/hc-eni-host/vf-topo-vpc"
477-
default:
478-
return 0, fmt.Errorf("not support this vf type")
479-
}
480-
481-
deviceID, err := vf.SetupDriverAndGetNetInterface(ctx, id, configPath)
469+
func prepareVF(ctx context.Context, id int, mac string) (int32, error) {
470+
deviceID, err := vf.SetupDriverAndGetNetInterface(ctx, id, "")
482471
if err != nil {
483472
return 0, err
484473
}

plugin/terway/cni_linux_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -398,7 +398,7 @@ func TestDoCmdAdd(t *testing.T) {
398398
})
399399

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

0 commit comments

Comments
 (0)