Skip to content

Commit

Permalink
host-device: Return interface name in result
Browse files Browse the repository at this point in the history
Signed-off-by: Sriram Yagnaraman <sriram.yagnaraman@ericsson.com>
  • Loading branch information
sriramy committed Feb 20, 2025
1 parent 6e7fb60 commit 12522ab
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 14 deletions.
14 changes: 9 additions & 5 deletions plugins/main/host-device/host-device.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,11 @@ func cmdAdd(args *skel.CmdArgs) error {
defer containerNs.Close()

result := &current.Result{}
result.Interfaces = []*current.Interface{{
Name: args.IfName,
Sandbox: containerNs.Path(),
}}

var contDev netlink.Link
if !cfg.DPDKMode {
hostDev, err := getLink(cfg.Device, cfg.HWAddr, cfg.KernelPath, cfg.PCIAddr, cfg.auxDevice)
Expand All @@ -143,11 +148,10 @@ func cmdAdd(args *skel.CmdArgs) error {
return fmt.Errorf("failed to move link %v", err)
}

result.Interfaces = []*current.Interface{{
Name: contDev.Attrs().Name,
Mac: contDev.Attrs().HardwareAddr.String(),
Sandbox: containerNs.Path(),
}}
// Override the device name with the name in the container namespace
result.Interfaces[0].Name = contDev.Attrs().Name
// Set the MAC address of the interface
result.Interfaces[0].Mac = contDev.Attrs().HardwareAddr.String()
}

if cfg.IPAM.Type == "" {
Expand Down
33 changes: 24 additions & 9 deletions plugins/main/host-device/host-device_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ func buildOneConfig(name, cniVersion string, orig *Net, prevResult types.Result)

type tester interface {
expectInterfaces(result types.Result, name, mac, sandbox string)
expectDpdkInterfaceIP(result types.Result, ipAddress string)
expectDpdkInterfaceIP(result types.Result, name, sandbox, ipAddress string)
}

type testerBase struct{}
Expand Down Expand Up @@ -256,11 +256,16 @@ func (t *testerV10x) expectInterfaces(result types.Result, name, mac, sandbox st
}))
}

func (t *testerV10x) expectDpdkInterfaceIP(result types.Result, ipAddress string) {
func (t *testerV10x) expectDpdkInterfaceIP(result types.Result, name, sandbox, ipAddress string) {
// check that the result was sane
res, err := types100.NewResultFromResult(result)
Expect(err).NotTo(HaveOccurred())
Expect(res.Interfaces).To(BeEmpty())
Expect(res.Interfaces).To(Equal([]*types100.Interface{
{
Name: name,
Sandbox: sandbox,
},
}))
Expect(res.IPs).To(HaveLen(1))
Expect(res.IPs[0].Address.String()).To(Equal(ipAddress))
}
Expand All @@ -278,11 +283,16 @@ func (t *testerV04x) expectInterfaces(result types.Result, name, mac, sandbox st
}))
}

func (t *testerV04x) expectDpdkInterfaceIP(result types.Result, ipAddress string) {
func (t *testerV04x) expectDpdkInterfaceIP(result types.Result, name, sandbox, ipAddress string) {
// check that the result was sane
res, err := types040.NewResultFromResult(result)
Expect(err).NotTo(HaveOccurred())
Expect(res.Interfaces).To(BeEmpty())
Expect(res.Interfaces).To(Equal([]*types040.Interface{
{
Name: name,
Sandbox: sandbox,
},
}))
Expect(res.IPs).To(HaveLen(1))
Expect(res.IPs[0].Address.String()).To(Equal(ipAddress))
}
Expand All @@ -300,11 +310,16 @@ func (t *testerV03x) expectInterfaces(result types.Result, name, mac, sandbox st
}))
}

func (t *testerV03x) expectDpdkInterfaceIP(result types.Result, ipAddress string) {
func (t *testerV03x) expectDpdkInterfaceIP(result types.Result, name, sandbox, ipAddress string) {
// check that the result was sane
res, err := types040.NewResultFromResult(result)
Expect(err).NotTo(HaveOccurred())
Expect(res.Interfaces).To(BeEmpty())
Expect(res.Interfaces).To(Equal([]*types040.Interface{
{
Name: name,
Sandbox: sandbox,
},
}))
Expect(res.IPs).To(HaveLen(1))
Expect(res.IPs[0].Address.String()).To(Equal(ipAddress))
}
Expand Down Expand Up @@ -598,7 +613,7 @@ var _ = Describe("base functionality", func() {

// check that the result was sane
t := newTesterByVersion(ver)
t.expectDpdkInterfaceIP(resI, targetIP)
t.expectDpdkInterfaceIP(resI, cniName, targetNS.Path(), targetIP)

// call CmdDel
_ = originalNS.Do(func(ns.NetNS) error {
Expand Down Expand Up @@ -870,7 +885,7 @@ var _ = Describe("base functionality", func() {

// check that the result was sane
t := newTesterByVersion(ver)
t.expectDpdkInterfaceIP(resI, targetIP)
t.expectDpdkInterfaceIP(resI, cniName, targetNS.Path(), targetIP)

// call CmdCheck
n := &Net{}
Expand Down

0 comments on commit 12522ab

Please sign in to comment.