Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

host-device: Return interface name in result #1147

Merged
merged 1 commit into from
Mar 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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