Skip to content

Commit

Permalink
fix: cni tolerate eni not found
Browse files Browse the repository at this point in the history
Signed-off-by: l1b0k <[email protected]>
  • Loading branch information
l1b0k committed Jan 21, 2025
1 parent 6af5b5a commit ce214a7
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 2 deletions.
14 changes: 14 additions & 0 deletions pkg/link/interface_linux_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
5 changes: 4 additions & 1 deletion plugin/datapath/ipvlan_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down
11 changes: 11 additions & 0 deletions plugin/datapath/ipvlan_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
3 changes: 3 additions & 0 deletions plugin/datapath/policy_router_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
11 changes: 11 additions & 0 deletions plugin/datapath/policy_router_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
7 changes: 6 additions & 1 deletion plugin/terway/cni.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
}
}

Expand Down

0 comments on commit ce214a7

Please sign in to comment.