Skip to content

Commit

Permalink
add host-side interface info to cni.Result
Browse files Browse the repository at this point in the history
This commit adds host-side interface name and
mac address to cni.Result according to CNI Specification
(https://www.cni.dev/docs/spec/#section-5-result-types)

Signed-off-by: czybjtu <smartczy@outlook.com>
  • Loading branch information
nayihz authored and tklauser committed Jun 29, 2023
1 parent 7be6bc2 commit 2b3d8a6
Showing 1 changed file with 11 additions and 7 deletions.
18 changes: 11 additions & 7 deletions plugins/cilium-cni/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,8 @@ func cmdAdd(args *skel.CmdArgs) (err error) {
ep.DatapathConfiguration.ExternalIpam = true
}

res := &cniTypesV1.Result{}

switch conf.DatapathMode {
case datapathOption.DatapathModeVeth:
var (
Expand All @@ -504,6 +506,11 @@ func cmdAdd(args *skel.CmdArgs) (err error) {
}
}()

res.Interfaces = append(res.Interfaces, &cniTypesV1.Interface{
Name: veth.Attrs().Name,
Mac: veth.Attrs().HardwareAddr.String(),
})

if err = netlink.LinkSetNsFd(peer, int(netNs.Fd())); err != nil {
return fmt.Errorf("unable to move veth pair '%v' to netns: %s", peer, err)
}
Expand All @@ -518,8 +525,6 @@ func cmdAdd(args *skel.CmdArgs) (err error) {
HostAddr: ipam.HostAddressing,
}

res := &cniTypesV1.Result{}

if !ipv6IsEnabled(ipam) && !ipv4IsEnabled(ipam) {
return fmt.Errorf("IPAM did not provide IPv4 or IPv6 address")
}
Expand All @@ -537,6 +542,8 @@ func cmdAdd(args *skel.CmdArgs) (err error) {
if err != nil {
return fmt.Errorf("unable to prepare IP addressing for '%s': %s", ep.Addressing.IPV6, err)
}
// set the addresses interface index to that of the container-side veth
ipConfig.Interface = cniTypesV1.Int(len(res.Interfaces))
res.IPs = append(res.IPs, ipConfig)
res.Routes = append(res.Routes, routes...)
}
Expand All @@ -550,6 +557,8 @@ func cmdAdd(args *skel.CmdArgs) (err error) {
if err != nil {
return fmt.Errorf("unable to prepare IP addressing for '%s': %s", ep.Addressing.IPV4, err)
}
// set the addresses interface index to that of the container-side veth
ipConfig.Interface = cniTypesV1.Int(len(res.Interfaces))
res.IPs = append(res.IPs, ipConfig)
res.Routes = append(res.Routes, routes...)
}
Expand Down Expand Up @@ -581,11 +590,6 @@ func cmdAdd(args *skel.CmdArgs) (err error) {
Sandbox: args.Netns,
})

// Add to the result the Interface as index of Interfaces
for i := range res.Interfaces {
res.IPs[i].Interface = cniTypesV1.Int(i)
}

// Specify that endpoint must be regenerated synchronously. See GH-4409.
ep.SyncBuildEndpoint = true
if err = c.EndpointCreate(ep); err != nil {
Expand Down

0 comments on commit 2b3d8a6

Please sign in to comment.