From ce214a780f57ab06f4863c26a7ed5a656a4dfdc2 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 c043a73a..b288c0da 100644 --- a/plugin/datapath/ipvlan_linux.go +++ b/plugin/datapath/ipvlan_linux.go @@ -323,7 +323,7 @@ func (d *IPvlanDriver) Teardown(ctx context.Context, cfg *types.TeardownCfg, net 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 { @@ -338,6 +338,9 @@ func (d *IPvlanDriver) Teardown(ctx context.Context, cfg *types.TeardownCfg, net } 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 823a2297..50c2c47c 100644 --- a/plugin/datapath/ipvlan_linux_test.go +++ b/plugin/datapath/ipvlan_linux_test.go @@ -304,4 +304,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 d073ea01..6decb0dc 100644 --- a/plugin/datapath/policy_router_linux.go +++ b/plugin/datapath/policy_router_linux.go @@ -476,6 +476,9 @@ func (d *PolicyRoute) Teardown(ctx context.Context, cfg *types.TeardownCfg, netN } } + 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 4aa48aef..2806b9f0 100644 --- a/plugin/datapath/policy_router_linux_test.go +++ b/plugin/datapath/policy_router_linux_test.go @@ -202,4 +202,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 59a76c2b..d120f2ed 100644 --- a/plugin/terway/cni.go +++ b/plugin/terway/cni.go @@ -418,7 +418,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 + } + } } }