pkg,plugins: update for Result struct Interface index changes
authorDan Williams <dcbw@redhat.com>
Thu, 22 Jun 2017 18:24:56 +0000 (13:24 -0500)
committerDan Williams <dcbw@redhat.com>
Thu, 29 Jun 2017 18:40:36 +0000 (13:40 -0500)
It's a pointer now, so we need to use the helper function to set
the field and also test for nil before accessing it.

12 files changed:
pkg/ipam/ipam.go
pkg/ipam/ipam_test.go
plugins/ipam/host-local/host_local_test.go
plugins/main/bridge/bridge.go
plugins/main/ipvlan/ipvlan.go
plugins/main/macvlan/macvlan.go
plugins/main/ptp/ptp.go
plugins/main/vlan/vlan.go
plugins/meta/portmap/main.go
plugins/meta/portmap/portmap_integ_test.go
plugins/meta/portmap/portmap_test.go
plugins/sample/main.go

index 54f80c0..0685679 100644 (file)
@@ -53,7 +53,11 @@ func ConfigureIface(ifName string, res *current.Result) error {
 
        var v4gw, v6gw net.IP
        for _, ipc := range res.IPs {
-               if int(ipc.Interface) >= len(res.Interfaces) || res.Interfaces[ipc.Interface].Name != ifName {
+               if ipc.Interface == nil {
+                       continue
+               }
+               intIdx := *ipc.Interface
+               if intIdx < 0 || intIdx >= len(res.Interfaces) || res.Interfaces[intIdx].Name != ifName {
                        // IP address is for a different interface
                        return fmt.Errorf("failed to add IP addr %v to %q: invalid interface index", ipc, ifName)
                }
index 844867f..cf85117 100644 (file)
@@ -109,13 +109,13 @@ var _ = Describe("IPAM Operations", func() {
                        IPs: []*current.IPConfig{
                                {
                                        Version:   "4",
-                                       Interface: 0,
+                                       Interface: current.Int(0),
                                        Address:   *ipv4,
                                        Gateway:   ipgw4,
                                },
                                {
                                        Version:   "6",
-                                       Interface: 0,
+                                       Interface: current.Int(0),
                                        Address:   *ipv6,
                                        Gateway:   ipgw6,
                                },
@@ -226,7 +226,7 @@ var _ = Describe("IPAM Operations", func() {
        })
 
        It("returns an error when the interface index doesn't match the link name", func() {
-               result.IPs[0].Interface = 1
+               result.IPs[0].Interface = current.Int(1)
                err := originalNS.Do(func(ns.NetNS) error {
                        return ConfigureIface(LINK_NAME, result)
                })
@@ -234,7 +234,15 @@ var _ = Describe("IPAM Operations", func() {
        })
 
        It("returns an error when the interface index is too big", func() {
-               result.IPs[0].Interface = 2
+               result.IPs[0].Interface = current.Int(2)
+               err := originalNS.Do(func(ns.NetNS) error {
+                       return ConfigureIface(LINK_NAME, result)
+               })
+               Expect(err).To(HaveOccurred())
+       })
+
+       It("returns an error when the interface index is too small", func() {
+               result.IPs[0].Interface = current.Int(-1)
                err := originalNS.Do(func(ns.NetNS) error {
                        return ConfigureIface(LINK_NAME, result)
                })
@@ -255,4 +263,37 @@ var _ = Describe("IPAM Operations", func() {
                })
                Expect(err).To(HaveOccurred())
        })
+
+       It("does not panic when interface is not specified", func() {
+               result = &current.Result{
+                       Interfaces: []*current.Interface{
+                               {
+                                       Name:    "eth0",
+                                       Mac:     "00:11:22:33:44:55",
+                                       Sandbox: "/proc/3553/ns/net",
+                               },
+                               {
+                                       Name:    "fake0",
+                                       Mac:     "00:33:44:55:66:77",
+                                       Sandbox: "/proc/1234/ns/net",
+                               },
+                       },
+                       IPs: []*current.IPConfig{
+                               {
+                                       Version: "4",
+                                       Address: *ipv4,
+                                       Gateway: ipgw4,
+                               },
+                               {
+                                       Version: "6",
+                                       Address: *ipv6,
+                                       Gateway: ipgw6,
+                               },
+                       },
+               }
+               err := originalNS.Do(func(ns.NetNS) error {
+                       return ConfigureIface(LINK_NAME, result)
+               })
+               Expect(err).NotTo(HaveOccurred())
+       })
 })
index 68942ea..30b73c8 100644 (file)
@@ -86,27 +86,25 @@ var _ = Describe("host-local Operations", func() {
                // Gomega is cranky about slices with different caps
                Expect(*result.IPs[0]).To(Equal(
                        current.IPConfig{
-                               Version:   "4",
-                               Interface: 0,
-                               Address:   mustCIDR("10.1.2.2/24"),
-                               Gateway:   net.ParseIP("10.1.2.1"),
+                               Version: "4",
+                               Address: mustCIDR("10.1.2.2/24"),
+                               Gateway: net.ParseIP("10.1.2.1"),
                        }))
 
                Expect(*result.IPs[1]).To(Equal(
                        current.IPConfig{
-                               Version:   "6",
-                               Interface: 0,
-                               Address:   mustCIDR("2001:db8:1::2/64"),
-                               Gateway:   net.ParseIP("2001:db8:1::1"),
+                               Version: "6",
+                               Address: mustCIDR("2001:db8:1::2/64"),
+                               Gateway: net.ParseIP("2001:db8:1::1"),
                        },
                ))
                Expect(len(result.IPs)).To(Equal(2))
 
                Expect(result.Routes).To(Equal([]*types.Route{
-                       &types.Route{Dst: mustCIDR("0.0.0.0/0"), GW: nil},
-                       &types.Route{Dst: mustCIDR("::/0"), GW: nil},
-                       &types.Route{Dst: mustCIDR("192.168.0.0/16"), GW: net.ParseIP("1.1.1.1")},
-                       &types.Route{Dst: mustCIDR("2001:db8:2::0/64"), GW: net.ParseIP("2001:db8:3::1")},
+                       {Dst: mustCIDR("0.0.0.0/0"), GW: nil},
+                       {Dst: mustCIDR("::/0"), GW: nil},
+                       {Dst: mustCIDR("192.168.0.0/16"), GW: net.ParseIP("1.1.1.1")},
+                       {Dst: mustCIDR("2001:db8:2::0/64"), GW: net.ParseIP("2001:db8:3::1")},
                }))
 
                ipFilePath1 := filepath.Join(tmpDir, "mynet", "10.1.2.2")
index 797409e..4e5bb1e 100644 (file)
@@ -100,7 +100,7 @@ func calcGateways(result *current.Result, n *NetConf) (*gwInfo, *gwInfo, error)
                defaultNet.Mask = net.IPMask(defaultNet.IP)
 
                // All IPs currently refer to the container interface
-               ipc.Interface = 2
+               ipc.Interface = current.Int(2)
 
                // If not provided, calculate the gateway address corresponding
                // to the selected IP address
index b9c7b1b..09924ef 100644 (file)
@@ -159,7 +159,7 @@ func cmdAdd(args *skel.CmdArgs) error {
        }
        for _, ipc := range result.IPs {
                // All addresses belong to the ipvlan interface
-               ipc.Interface = 0
+               ipc.Interface = current.Int(0)
        }
 
        result.Interfaces = []*current.Interface{ipvlanInterface}
index 98e46a4..3b69194 100644 (file)
@@ -179,7 +179,7 @@ func cmdAdd(args *skel.CmdArgs) error {
 
        for _, ipc := range result.IPs {
                // All addresses apply to the container macvlan interface
-               ipc.Interface = 0
+               ipc.Interface = current.Int(0)
        }
 
        err = netns.Do(func(_ ns.NetNS) error {
index 42b2670..12a7722 100644 (file)
@@ -75,7 +75,7 @@ func setupContainerVeth(netns ns.NetNS, ifName string, mtu int, pr *current.Resu
 
                for _, ipc := range pr.IPs {
                        // All addresses apply to the container veth interface
-                       ipc.Interface = 1
+                       ipc.Interface = current.Int(1)
                }
 
                pr.Interfaces = []*current.Interface{hostInterface, containerInterface}
index b9aa4fe..7f527b5 100644 (file)
@@ -148,7 +148,7 @@ func cmdAdd(args *skel.CmdArgs) error {
        }
        for _, ipc := range result.IPs {
                // All addresses belong to the vlan interface
-               ipc.Interface = 0
+               ipc.Interface = current.Int(0)
        }
 
        result.Interfaces = []*current.Interface{vlanInterface}
index c58db6a..6424a77 100644 (file)
@@ -164,9 +164,14 @@ func parseConfig(stdin []byte, ifName string) (*PortMapConf, error) {
                        }
 
                        // Skip known non-sandbox interfaces
-                       intIdx := ip.Interface
-                       if intIdx >= 0 && intIdx < len(conf.PrevResult.Interfaces) && conf.PrevResult.Interfaces[intIdx].Name != ifName {
-                               continue
+                       if ip.Interface != nil {
+                               intIdx := *ip.Interface
+                               if intIdx >= 0 &&
+                                       intIdx < len(conf.PrevResult.Interfaces) &&
+                                       (conf.PrevResult.Interfaces[intIdx].Name != ifName ||
+                                               conf.PrevResult.Interfaces[intIdx].Sandbox == "") {
+                                       continue
+                               }
                        }
                        switch ip.Version {
                        case "6":
index bcdbcfd..158b905 100644 (file)
@@ -134,7 +134,8 @@ var _ = Describe("portmap integration tests", func() {
                var contIP net.IP
 
                for _, ip := range result.IPs {
-                       if result.Interfaces[ip.Interface].Sandbox == "" {
+                       intfIndex := *ip.Interface
+                       if result.Interfaces[intfIndex].Sandbox == "" {
                                continue
                        }
                        contIP = ip.Address.IP
index e67907f..cf5ed5f 100644 (file)
@@ -124,6 +124,34 @@ var _ = Describe("portmapping configuration", func() {
                        _, err := parseConfig(configBytes, "container")
                        Expect(err).To(MatchError("Invalid host port number: 0"))
                })
+
+               It("Does not fail on missing prevResult interface index", func() {
+                       configBytes := []byte(`{
+       "name": "test",
+       "type": "portmap",
+       "cniVersion": "0.3.1",
+       "runtimeConfig": {
+               "portMappings": [
+                       { "hostPort": 8080, "containerPort": 80, "protocol": "tcp"}
+               ]
+       },
+       "conditionsV4": ["a", "b"],
+       "prevResult": {
+               "interfaces": [
+                       {"name": "host"}
+               ],
+               "ips": [
+                       {
+                               "version": "4",
+                               "address": "10.0.0.1/24",
+                               "gateway": "10.0.0.1"
+                       }
+               ]
+       }
+}`)
+                       _, err := parseConfig(configBytes, "container")
+                       Expect(err).NotTo(HaveOccurred())
+               })
        })
 
        Describe("Generating chains", func() {
index e55953c..1abdc16 100644 (file)
@@ -106,7 +106,10 @@ func cmdAdd(args *skel.CmdArgs) error {
                }
        } else {
                for _, ip := range conf.PrevResult.IPs {
-                       intIdx := ip.Interface
+                       if ip.Interface == nil {
+                               continue
+                       }
+                       intIdx := *ip.Interface
                        // Every IP is indexed in to the interfaces array, with "-1" standing
                        // for an unknown interface (which we'll assume to be Container-side
                        // Skip all IPs we know belong to an interface with the wrong name.