When prevResults are not returned to loopback plugin, create results to return based on

the lo interface and IP address assigned inside container.

Signed-off-by: Michael Cambria <mcambria@redhat.com>
This commit is contained in:
Michael Cambria 2019-09-06 15:51:34 -04:00
parent 4bb288193c
commit fd42109a06
2 changed files with 50 additions and 6 deletions

View File

@ -55,6 +55,8 @@ func cmdAdd(args *skel.CmdArgs) error {
return err return err
} }
var v4Addr, v6Addr *net.IPNet
args.IfName = "lo" // ignore config, this only works for loopback args.IfName = "lo" // ignore config, this only works for loopback
err = ns.WithNetNSPath(args.Netns, func(_ ns.NetNS) error { err = ns.WithNetNSPath(args.Netns, func(_ ns.NetNS) error {
link, err := netlink.LinkByName(args.IfName) link, err := netlink.LinkByName(args.IfName)
@ -66,6 +68,33 @@ func cmdAdd(args *skel.CmdArgs) error {
if err != nil { if err != nil {
return err // not tested 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 return nil
}) })
@ -79,7 +108,26 @@ func cmdAdd(args *skel.CmdArgs) error {
// loopback should pass it transparently // loopback should pass it transparently
result = conf.PrevResult result = conf.PrevResult
} else { } else {
result = &current.Result{} loopbackInterface := &current.Interface{Name: args.IfName, Mac: "00:00:00:00:00:00", Sandbox: args.Netns}
r := &current.Result{CNIVersion: conf.CNIVersion, Interfaces: []*current.Interface{loopbackInterface}}
if v4Addr != nil {
r.IPs = append(r.IPs, &current.IPConfig{
Version: "4",
Interface: current.Int(0),
Address: *v4Addr,
})
}
if v6Addr != nil {
r.IPs = append(r.IPs, &current.IPConfig{
Version: "6",
Interface: current.Int(0),
Address: *v6Addr,
})
}
result = r
} }
return types.PrintResult(result, conf.CNIVersion) return types.PrintResult(result, conf.CNIVersion)

View File

@ -49,7 +49,7 @@ var _ = Describe("Loopback", func() {
fmt.Sprintf("CNI_ARGS=%s", "none"), fmt.Sprintf("CNI_ARGS=%s", "none"),
fmt.Sprintf("CNI_PATH=%s", "/some/test/path"), 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() { AfterEach(func() {
@ -59,8 +59,6 @@ var _ = Describe("Loopback", func() {
Context("when given a network namespace", func() { Context("when given a network namespace", func() {
It("sets the lo device to UP", func() { It("sets the lo device to UP", func() {
Skip("TODO: add network name")
command.Env = append(environ, fmt.Sprintf("CNI_COMMAND=%s", "ADD")) command.Env = append(environ, fmt.Sprintf("CNI_COMMAND=%s", "ADD"))
session, err := gexec.Start(command, GinkgoWriter, GinkgoWriter) session, err := gexec.Start(command, GinkgoWriter, GinkgoWriter)
@ -81,8 +79,6 @@ var _ = Describe("Loopback", func() {
}) })
It("sets the lo device to DOWN", func() { It("sets the lo device to DOWN", func() {
Skip("TODO: add network name")
command.Env = append(environ, fmt.Sprintf("CNI_COMMAND=%s", "DEL")) command.Env = append(environ, fmt.Sprintf("CNI_COMMAND=%s", "DEL"))
session, err := gexec.Start(command, GinkgoWriter, GinkgoWriter) session, err := gexec.Start(command, GinkgoWriter, GinkgoWriter)