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.
This commit is contained in:
Dan Williams
2017-06-22 13:24:56 -05:00
parent 121362601b
commit 0da6780449
12 changed files with 107 additions and 27 deletions

View 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")

View 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

View 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}

View 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 {

View 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}

View 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}

View 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":

View 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

View 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() {

View 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.