diff --git a/plugins/main/loopback/loopback.go b/plugins/main/loopback/loopback.go index 9b954b26..2c4a047c 100644 --- a/plugins/main/loopback/loopback.go +++ b/plugins/main/loopback/loopback.go @@ -55,6 +55,8 @@ func cmdAdd(args *skel.CmdArgs) error { return err } + var v4Addr, v6Addr *net.IPNet + args.IfName = "lo" // ignore config, this only works for loopback err = ns.WithNetNSPath(args.Netns, func(_ ns.NetNS) error { link, err := netlink.LinkByName(args.IfName) @@ -66,6 +68,33 @@ func cmdAdd(args *skel.CmdArgs) error { if err != nil { return err // not tested } + v4Addrs, err := netlink.AddrList(link, netlink.FAMILY_V4) + if err != nil { + return err // not tested + } + if len(v4Addrs) != 0 { + v4Addr = v4Addrs[0].IPNet + // sanity check that this is a loopback address + for _, addr := range v4Addrs { + if !addr.IP.IsLoopback() { + return fmt.Errorf("loopback interface found with non-loopback address %q", addr.IP) + } + } + } + + v6Addrs, err := netlink.AddrList(link, netlink.FAMILY_V6) + if err != nil { + return err // not tested + } + if len(v6Addrs) != 0 { + v6Addr = v6Addrs[0].IPNet + // sanity check that this is a loopback address + for _, addr := range v4Addrs { + if !addr.IP.IsLoopback() { + return fmt.Errorf("loopback interface found with non-loopback address %q", addr.IP) + } + } + } return nil }) @@ -79,7 +108,26 @@ func cmdAdd(args *skel.CmdArgs) error { // loopback should pass it transparently result = conf.PrevResult } else { - result = ¤t.Result{} + loopbackInterface := ¤t.Interface{Name: args.IfName, Mac: "00:00:00:00:00:00", Sandbox: args.Netns} + r := ¤t.Result{CNIVersion: conf.CNIVersion, Interfaces: []*current.Interface{loopbackInterface}} + + if v4Addr != nil { + r.IPs = append(r.IPs, ¤t.IPConfig{ + Version: "4", + Interface: current.Int(0), + Address: *v4Addr, + }) + } + + if v6Addr != nil { + r.IPs = append(r.IPs, ¤t.IPConfig{ + Version: "6", + Interface: current.Int(0), + Address: *v6Addr, + }) + } + + result = r } return types.PrintResult(result, conf.CNIVersion) diff --git a/plugins/main/loopback/loopback_test.go b/plugins/main/loopback/loopback_test.go index 4bfd6e0b..ca47e3d3 100644 --- a/plugins/main/loopback/loopback_test.go +++ b/plugins/main/loopback/loopback_test.go @@ -49,7 +49,7 @@ var _ = Describe("Loopback", func() { fmt.Sprintf("CNI_ARGS=%s", "none"), fmt.Sprintf("CNI_PATH=%s", "/some/test/path"), } - command.Stdin = strings.NewReader(`{ "cniVersion": "0.1.0" }`) + command.Stdin = strings.NewReader(`{ "name": "loopback-test", "cniVersion": "0.1.0" }`) }) AfterEach(func() { @@ -59,8 +59,6 @@ var _ = Describe("Loopback", func() { Context("when given a network namespace", func() { It("sets the lo device to UP", func() { - - Skip("TODO: add network name") command.Env = append(environ, fmt.Sprintf("CNI_COMMAND=%s", "ADD")) session, err := gexec.Start(command, GinkgoWriter, GinkgoWriter) @@ -81,8 +79,6 @@ var _ = Describe("Loopback", func() { }) It("sets the lo device to DOWN", func() { - - Skip("TODO: add network name") command.Env = append(environ, fmt.Sprintf("CNI_COMMAND=%s", "DEL")) session, err := gexec.Start(command, GinkgoWriter, GinkgoWriter)