From 2f7e3d2da17ae619869e412864c86e5cbdf362c6 Mon Sep 17 00:00:00 2001 From: l1b0k Date: Tue, 21 Jan 2025 11:06:28 +0800 Subject: [PATCH] fix: cni tolerate eni not found Signed-off-by: l1b0k --- pkg/link/interface_linux_test.go | 14 ++++++++++++++ plugin/datapath/ipvlan_linux.go | 5 ++++- plugin/datapath/ipvlan_linux_test.go | 11 +++++++++++ plugin/datapath/policy_router_linux.go | 3 +++ plugin/datapath/policy_router_linux_test.go | 11 +++++++++++ plugin/terway/cni.go | 7 ++++++- 6 files changed, 49 insertions(+), 2 deletions(-) create mode 100644 pkg/link/interface_linux_test.go diff --git a/pkg/link/interface_linux_test.go b/pkg/link/interface_linux_test.go new file mode 100644 index 00000000..b39a39ee --- /dev/null +++ b/pkg/link/interface_linux_test.go @@ -0,0 +1,14 @@ +package link + +import ( + "errors" + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestGetDeviceNumber(t *testing.T) { + id, err := GetDeviceNumber("00") + assert.True(t, errors.Is(err, ErrNotFound)) + assert.Equal(t, int32(0), id) +} diff --git a/plugin/datapath/ipvlan_linux.go b/plugin/datapath/ipvlan_linux.go index 2050cdbd..6906f152 100644 --- a/plugin/datapath/ipvlan_linux.go +++ b/plugin/datapath/ipvlan_linux.go @@ -322,7 +322,7 @@ func (d *IPvlanDriver) Teardown(cfg *types.TeardownCfg, netNS ns.NetNS) error { return err } - if cfg.EnableNetworkPriority { + if cfg.EnableNetworkPriority && cfg.ENIIndex > 0 { link, err := netlink.LinkByIndex(cfg.ENIIndex) if err != nil { if _, ok := err.(netlink.LinkNotFoundError); !ok { @@ -337,6 +337,9 @@ func (d *IPvlanDriver) Teardown(cfg *types.TeardownCfg, netNS ns.NetNS) error { } err = func() error { + if cfg.ENIIndex <= 0 { + return nil + } link, err := netlink.LinkByIndex(cfg.ENIIndex) if err != nil { if _, ok := err.(netlink.LinkNotFoundError); !ok { diff --git a/plugin/datapath/ipvlan_linux_test.go b/plugin/datapath/ipvlan_linux_test.go index 50fefef5..a875216b 100644 --- a/plugin/datapath/ipvlan_linux_test.go +++ b/plugin/datapath/ipvlan_linux_test.go @@ -303,4 +303,15 @@ func TestDataPathIPvlanL2(t *testing.T) { }) assert.NoError(t, err) assert.Equal(t, 0, len(routes)) + + err = d.Teardown(context.Background(), &types2.TeardownCfg{ + HostVETHName: cfg.HostVETHName, + ContainerIfName: cfg.ContainerIfName, + ContainerIPNet: &types.IPNetSet{ + IPv4: containerIPNet, + IPv6: containerIPNetIPv6, + }, + ENIIndex: 0, + }, containerNS) + assert.NoError(t, err) } diff --git a/plugin/datapath/policy_router_linux.go b/plugin/datapath/policy_router_linux.go index 6bd22a9d..44c0fd0b 100644 --- a/plugin/datapath/policy_router_linux.go +++ b/plugin/datapath/policy_router_linux.go @@ -462,6 +462,9 @@ func (d *PolicyRoute) Teardown(cfg *types.TeardownCfg, netNS ns.NetNS) error { } } + if cfg.ENIIndex <= 0 { + return nil + } link, err := netlink.LinkByIndex(cfg.ENIIndex) if err != nil { if _, ok := err.(netlink.LinkNotFoundError); !ok { diff --git a/plugin/datapath/policy_router_linux_test.go b/plugin/datapath/policy_router_linux_test.go index 25269bfd..a1dca12f 100644 --- a/plugin/datapath/policy_router_linux_test.go +++ b/plugin/datapath/policy_router_linux_test.go @@ -201,4 +201,15 @@ func TestDataPathPolicyRoute(t *testing.T) { for _, r := range rules { t.Logf("%s %#v ", r, r) } + + err = d.Teardown(context.Background(), &types.TeardownCfg{ + HostVETHName: cfg.HostVETHName, + ContainerIfName: cfg.ContainerIfName, + ContainerIPNet: &terwayTypes.IPNetSet{ + IPv4: containerIPNet, + IPv6: containerIPNetIPv6, + }, + ENIIndex: 0, + }, containerNS) + assert.NoError(t, err) } diff --git a/plugin/terway/cni.go b/plugin/terway/cni.go index e32c3f62..62fc9ed6 100644 --- a/plugin/terway/cni.go +++ b/plugin/terway/cni.go @@ -428,7 +428,12 @@ func parseTearDownConf(alloc *rpc.NetConf, conf *types.CNIConf, ipType rpc.IPTyp if alloc.GetENIInfo() != nil { mac := alloc.GetENIInfo().GetMAC() if mac != "" { - eniIndex, _ = link.GetDeviceNumber(mac) + eniIndex, err = link.GetDeviceNumber(mac) + if err != nil { + if !errors.Is(err, link.ErrNotFound) { + return nil, err + } + } } }