From 0da6780449c10a2d2d8ba650e6663a3d93e086c5 Mon Sep 17 00:00:00 2001 From: Dan Williams Date: Thu, 22 Jun 2017 13:24:56 -0500 Subject: [PATCH] pkg,plugins: update for Result struct Interface index changes 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. --- pkg/ipam/ipam.go | 6 ++- pkg/ipam/ipam_test.go | 49 ++++++++++++++++++++-- plugins/ipam/host-local/host_local_test.go | 22 +++++----- plugins/main/bridge/bridge.go | 2 +- plugins/main/ipvlan/ipvlan.go | 2 +- plugins/main/macvlan/macvlan.go | 2 +- plugins/main/ptp/ptp.go | 2 +- plugins/main/vlan/vlan.go | 2 +- plugins/meta/portmap/main.go | 11 +++-- plugins/meta/portmap/portmap_integ_test.go | 3 +- plugins/meta/portmap/portmap_test.go | 28 +++++++++++++ plugins/sample/main.go | 5 ++- 12 files changed, 107 insertions(+), 27 deletions(-) diff --git a/pkg/ipam/ipam.go b/pkg/ipam/ipam.go index 54f80c0e..0685679d 100644 --- a/pkg/ipam/ipam.go +++ b/pkg/ipam/ipam.go @@ -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) } diff --git a/pkg/ipam/ipam_test.go b/pkg/ipam/ipam_test.go index 844867f2..cf851175 100644 --- a/pkg/ipam/ipam_test.go +++ b/pkg/ipam/ipam_test.go @@ -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 = ¤t.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()) + }) }) diff --git a/plugins/ipam/host-local/host_local_test.go b/plugins/ipam/host-local/host_local_test.go index 68942ea1..30b73c81 100644 --- a/plugins/ipam/host-local/host_local_test.go +++ b/plugins/ipam/host-local/host_local_test.go @@ -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") diff --git a/plugins/main/bridge/bridge.go b/plugins/main/bridge/bridge.go index 797409e1..4e5bb1e4 100644 --- a/plugins/main/bridge/bridge.go +++ b/plugins/main/bridge/bridge.go @@ -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 diff --git a/plugins/main/ipvlan/ipvlan.go b/plugins/main/ipvlan/ipvlan.go index b9c7b1b3..09924ef3 100644 --- a/plugins/main/ipvlan/ipvlan.go +++ b/plugins/main/ipvlan/ipvlan.go @@ -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} diff --git a/plugins/main/macvlan/macvlan.go b/plugins/main/macvlan/macvlan.go index 98e46a40..3b69194b 100644 --- a/plugins/main/macvlan/macvlan.go +++ b/plugins/main/macvlan/macvlan.go @@ -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 { diff --git a/plugins/main/ptp/ptp.go b/plugins/main/ptp/ptp.go index 42b26707..12a77221 100644 --- a/plugins/main/ptp/ptp.go +++ b/plugins/main/ptp/ptp.go @@ -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} diff --git a/plugins/main/vlan/vlan.go b/plugins/main/vlan/vlan.go index b9aa4fee..7f527b55 100644 --- a/plugins/main/vlan/vlan.go +++ b/plugins/main/vlan/vlan.go @@ -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} diff --git a/plugins/meta/portmap/main.go b/plugins/meta/portmap/main.go index c58db6aa..6424a777 100644 --- a/plugins/meta/portmap/main.go +++ b/plugins/meta/portmap/main.go @@ -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": diff --git a/plugins/meta/portmap/portmap_integ_test.go b/plugins/meta/portmap/portmap_integ_test.go index bcdbcfd9..158b9057 100644 --- a/plugins/meta/portmap/portmap_integ_test.go +++ b/plugins/meta/portmap/portmap_integ_test.go @@ -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 diff --git a/plugins/meta/portmap/portmap_test.go b/plugins/meta/portmap/portmap_test.go index e67907f8..cf5ed5fb 100644 --- a/plugins/meta/portmap/portmap_test.go +++ b/plugins/meta/portmap/portmap_test.go @@ -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() { diff --git a/plugins/sample/main.go b/plugins/sample/main.go index e55953c9..1abdc165 100644 --- a/plugins/sample/main.go +++ b/plugins/sample/main.go @@ -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.