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()
}