types: make Result an interface and move existing Result to separate package

This commit is contained in:
Dan Williams
2016-11-09 15:11:18 -06:00
parent cb4cd0e12c
commit befb95977c
29 changed files with 500 additions and 131 deletions

View File

@ -29,6 +29,7 @@ import (
"github.com/containernetworking/cni/pkg/skel"
"github.com/containernetworking/cni/pkg/types"
"github.com/containernetworking/cni/pkg/types/current"
"github.com/coreos/go-systemd/activation"
)
@ -50,7 +51,7 @@ func newDHCP() *DHCP {
// Allocate acquires an IP from a DHCP server for a specified container.
// The acquired lease will be maintained until Release() is called.
func (d *DHCP) Allocate(args *skel.CmdArgs, result *types.Result) error {
func (d *DHCP) Allocate(args *skel.CmdArgs, result *current.Result) error {
conf := types.NetConf{}
if err := json.Unmarshal(args.StdinData, &conf); err != nil {
return fmt.Errorf("error parsing netconf: %v", err)
@ -70,7 +71,7 @@ func (d *DHCP) Allocate(args *skel.CmdArgs, result *types.Result) error {
d.setLease(args.ContainerID, conf.Name, l)
result.IP4 = &types.IPConfig{
result.IP4 = &current.IPConfig{
IP: *ipn,
Gateway: l.Gateway(),
Routes: l.Routes(),

View File

@ -21,7 +21,7 @@ import (
"path/filepath"
"github.com/containernetworking/cni/pkg/skel"
"github.com/containernetworking/cni/pkg/types"
"github.com/containernetworking/cni/pkg/types/current"
"github.com/containernetworking/cni/pkg/version"
)
@ -36,8 +36,8 @@ func main() {
}
func cmdAdd(args *skel.CmdArgs) error {
result := types.Result{}
if err := rpcCall("DHCP.Allocate", args, &result); err != nil {
result := &current.Result{}
if err := rpcCall("DHCP.Allocate", args, result); err != nil {
return err
}
return result.Print()

View File

@ -20,7 +20,7 @@ import (
"net"
"github.com/containernetworking/cni/pkg/ip"
"github.com/containernetworking/cni/pkg/types"
"github.com/containernetworking/cni/pkg/types/current"
"github.com/containernetworking/cni/plugins/ipam/host-local/backend"
)
@ -129,7 +129,7 @@ func validateRangeIP(ip net.IP, ipnet *net.IPNet, start net.IP, end net.IP) erro
}
// Returns newly allocated IP along with its config
func (a *IPAllocator) Get(id string) (*types.IPConfig, error) {
func (a *IPAllocator) Get(id string) (*current.IPConfig, error) {
a.store.Lock()
defer a.store.Unlock()
@ -163,7 +163,7 @@ func (a *IPAllocator) Get(id string) (*types.IPConfig, error) {
}
if reserved {
return &types.IPConfig{
return &current.IPConfig{
IP: net.IPNet{IP: requestedIP, Mask: a.conf.Subnet.Mask},
Gateway: gw,
Routes: a.conf.Routes,
@ -184,7 +184,7 @@ func (a *IPAllocator) Get(id string) (*types.IPConfig, error) {
return nil, err
}
if reserved {
return &types.IPConfig{
return &current.IPConfig{
IP: net.IPNet{IP: cur, Mask: a.conf.Subnet.Mask},
Gateway: gw,
Routes: a.conf.Routes,

View File

@ -17,6 +17,7 @@ package allocator
import (
"fmt"
"github.com/containernetworking/cni/pkg/types"
"github.com/containernetworking/cni/pkg/types/current"
fakestore "github.com/containernetworking/cni/plugins/ipam/host-local/backend/testing"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
@ -30,7 +31,7 @@ type AllocatorTestCase struct {
lastIP string
}
func (t AllocatorTestCase) run() (*types.IPConfig, error) {
func (t AllocatorTestCase) run() (*current.IPConfig, error) {
subnet, err := types.ParseCIDR(t.subnet)
if err != nil {
return nil, err

View File

@ -24,6 +24,7 @@ import (
"github.com/containernetworking/cni/pkg/skel"
"github.com/containernetworking/cni/pkg/testutils"
"github.com/containernetworking/cni/pkg/types"
"github.com/containernetworking/cni/pkg/types/current"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
@ -62,11 +63,14 @@ var _ = Describe("host-local Operations", func() {
}
// Allocate the IP
result, _, err := testutils.CmdAddWithResult(nspath, ifname, []byte(conf), func() error {
r, _, err := testutils.CmdAddWithResult(nspath, ifname, []byte(conf), func() error {
return cmdAdd(args)
})
Expect(err).NotTo(HaveOccurred())
result, err := current.GetResult(r)
Expect(err).NotTo(HaveOccurred())
expectedAddress, err := types.ParseCIDR("10.1.2.2/24")
Expect(err).NotTo(HaveOccurred())
expectedAddress.IP = expectedAddress.IP.To16()
@ -124,11 +128,14 @@ var _ = Describe("host-local Operations", func() {
}
// Allocate the IP
result, _, err := testutils.CmdAddWithResult(nspath, ifname, []byte(conf), func() error {
r, _, err := testutils.CmdAddWithResult(nspath, ifname, []byte(conf), func() error {
return cmdAdd(args)
})
Expect(err).NotTo(HaveOccurred())
result, err := current.GetResult(r)
Expect(err).NotTo(HaveOccurred())
ipFilePath := filepath.Join(tmpDir, "mynet", result.IP4.IP.IP.String())
contents, err := ioutil.ReadFile(ipFilePath)
Expect(err).NotTo(HaveOccurred())

View File

@ -19,7 +19,7 @@ import (
"github.com/containernetworking/cni/plugins/ipam/host-local/backend/disk"
"github.com/containernetworking/cni/pkg/skel"
"github.com/containernetworking/cni/pkg/types"
"github.com/containernetworking/cni/pkg/types/current"
"github.com/containernetworking/cni/pkg/version"
)
@ -33,7 +33,7 @@ func cmdAdd(args *skel.CmdArgs) error {
return err
}
r := types.Result{}
r := &current.Result{}
if ipamConf.ResolvConf != "" {
dns, err := parseResolvConf(ipamConf.ResolvConf)
@ -54,11 +54,10 @@ func cmdAdd(args *skel.CmdArgs) error {
return err
}
ipConf, err := allocator.Get(args.ContainerID)
r.IP4, err = allocator.Get(args.ContainerID)
if err != nil {
return err
}
r.IP4 = ipConf
return r.Print()
}

View File

@ -27,6 +27,7 @@ import (
"github.com/containernetworking/cni/pkg/ns"
"github.com/containernetworking/cni/pkg/skel"
"github.com/containernetworking/cni/pkg/types"
"github.com/containernetworking/cni/pkg/types/current"
"github.com/containernetworking/cni/pkg/utils"
"github.com/containernetworking/cni/pkg/version"
"github.com/vishvananda/netlink"
@ -234,7 +235,12 @@ func cmdAdd(args *skel.CmdArgs) error {
}
// run the IPAM plugin and get back the config to apply
result, err := ipam.ExecAdd(n.IPAM.Type, args.StdinData)
r, err := ipam.ExecAdd(n.IPAM.Type, args.StdinData)
if err != nil {
return err
}
result, err := current.GetResult(r)
if err != nil {
return err
}

View File

@ -25,6 +25,7 @@ import (
"github.com/containernetworking/cni/pkg/ns"
"github.com/containernetworking/cni/pkg/skel"
"github.com/containernetworking/cni/pkg/types"
"github.com/containernetworking/cni/pkg/types/current"
"github.com/containernetworking/cni/pkg/version"
"github.com/vishvananda/netlink"
)
@ -125,10 +126,15 @@ func cmdAdd(args *skel.CmdArgs) error {
}
// run the IPAM plugin and get back the config to apply
result, err := ipam.ExecAdd(n.IPAM.Type, args.StdinData)
r, err := ipam.ExecAdd(n.IPAM.Type, args.StdinData)
if err != nil {
return err
}
result, err := current.GetResult(r)
if err != nil {
return err
}
if result.IP4 == nil {
return errors.New("IPAM plugin returned missing IPv4 config")
}

View File

@ -17,7 +17,7 @@ package main
import (
"github.com/containernetworking/cni/pkg/ns"
"github.com/containernetworking/cni/pkg/skel"
"github.com/containernetworking/cni/pkg/types"
"github.com/containernetworking/cni/pkg/types/current"
"github.com/containernetworking/cni/pkg/version"
"github.com/vishvananda/netlink"
)
@ -41,7 +41,7 @@ func cmdAdd(args *skel.CmdArgs) error {
return err // not tested
}
result := types.Result{}
result := current.Result{}
return result.Print()
}

View File

@ -25,6 +25,7 @@ import (
"github.com/containernetworking/cni/pkg/ns"
"github.com/containernetworking/cni/pkg/skel"
"github.com/containernetworking/cni/pkg/types"
"github.com/containernetworking/cni/pkg/types/current"
"github.com/containernetworking/cni/pkg/utils/sysctl"
"github.com/containernetworking/cni/pkg/version"
"github.com/vishvananda/netlink"
@ -141,10 +142,15 @@ func cmdAdd(args *skel.CmdArgs) error {
}
// run the IPAM plugin and get back the config to apply
result, err := ipam.ExecAdd(n.IPAM.Type, args.StdinData)
r, err := ipam.ExecAdd(n.IPAM.Type, args.StdinData)
if err != nil {
return err
}
result, err := current.GetResult(r)
if err != nil {
return err
}
if result.IP4 == nil {
return errors.New("IPAM plugin returned missing IPv4 config")
}

View File

@ -29,6 +29,7 @@ import (
"github.com/containernetworking/cni/pkg/ns"
"github.com/containernetworking/cni/pkg/skel"
"github.com/containernetworking/cni/pkg/types"
"github.com/containernetworking/cni/pkg/types/current"
"github.com/containernetworking/cni/pkg/utils"
"github.com/containernetworking/cni/pkg/version"
)
@ -46,7 +47,7 @@ type NetConf struct {
MTU int `json:"mtu"`
}
func setupContainerVeth(netns, ifName string, mtu int, pr *types.Result) (string, error) {
func setupContainerVeth(netns, ifName string, mtu int, pr *current.Result) (string, error) {
// The IPAM result will be something like IP=192.168.3.5/24, GW=192.168.3.1.
// What we want is really a point-to-point link but veth does not support IFF_POINTOPONT.
// Next best thing would be to let it ARP but set interface to 192.168.3.5/32 and
@ -102,7 +103,7 @@ func setupContainerVeth(netns, ifName string, mtu int, pr *types.Result) (string
}
for _, r := range []netlink.Route{
netlink.Route{
{
LinkIndex: contVeth.Attrs().Index,
Dst: &net.IPNet{
IP: pr.IP4.Gateway,
@ -111,7 +112,7 @@ func setupContainerVeth(netns, ifName string, mtu int, pr *types.Result) (string
Scope: netlink.SCOPE_LINK,
Src: pr.IP4.IP.IP,
},
netlink.Route{
{
LinkIndex: contVeth.Attrs().Index,
Dst: &net.IPNet{
IP: pr.IP4.IP.IP.Mask(pr.IP4.IP.Mask),
@ -132,7 +133,7 @@ func setupContainerVeth(netns, ifName string, mtu int, pr *types.Result) (string
return hostVethName, err
}
func setupHostVeth(vethName string, ipConf *types.IPConfig) error {
func setupHostVeth(vethName string, ipConf *current.IPConfig) error {
// hostVeth moved namespaces and may have a new ifindex
veth, err := netlink.LinkByName(vethName)
if err != nil {
@ -172,7 +173,11 @@ func cmdAdd(args *skel.CmdArgs) error {
}
// run the IPAM plugin and get back the config to apply
result, err := ipam.ExecAdd(conf.IPAM.Type, args.StdinData)
r, err := ipam.ExecAdd(conf.IPAM.Type, args.StdinData)
if err != nil {
return err
}
result, err := current.GetResult(r)
if err != nil {
return err
}

View File

@ -27,6 +27,7 @@ import (
"github.com/containernetworking/cni/pkg/ns"
"github.com/containernetworking/cni/pkg/skel"
"github.com/containernetworking/cni/pkg/types"
"github.com/containernetworking/cni/pkg/types/current"
"github.com/containernetworking/cni/pkg/version"
)
@ -67,7 +68,7 @@ func cmdAdd(args *skel.CmdArgs) error {
return err
}
result := types.Result{}
result := current.Result{}
return result.Print()
}

View File

@ -31,14 +31,15 @@ import (
"github.com/containernetworking/cni/pkg/skel"
"github.com/containernetworking/cni/pkg/types"
"github.com/containernetworking/cni/pkg/types/current"
"github.com/containernetworking/cni/pkg/version"
noop_debug "github.com/containernetworking/cni/plugins/test/noop/debug"
)
type NetConf struct {
types.NetConf
DebugFile string `json:"debugFile"`
PrevResult *types.Result `json:"prevResult,omitempty"`
DebugFile string `json:"debugFile"`
PrevResult *current.Result `json:"prevResult,omitempty"`
}
func loadConf(bytes []byte) (*NetConf, error) {
@ -121,7 +122,12 @@ func debugBehavior(args *skel.CmdArgs, command string) error {
return errors.New(debug.ReportError)
} else if debug.ReportResult == "PASSTHROUGH" || debug.ReportResult == "INJECT-DNS" {
if debug.ReportResult == "INJECT-DNS" {
netConf.PrevResult.DNS.Nameservers = []string{"1.2.3.4"}
newResult, err := current.NewResultFromResult(netConf.PrevResult)
if err != nil {
return err
}
newResult.DNS.Nameservers = []string{"1.2.3.4"}
netConf.PrevResult = newResult
}
newResult, err := json.Marshal(netConf.PrevResult)
if err != nil {