Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Honor Network and IPAM plugin #1607

Merged
merged 2 commits into from
Oct 12, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions manager/allocator/networkallocator/drivers_ipam.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package networkallocator

import (
"github.com/docker/libnetwork/drvregistry"
"github.com/docker/libnetwork/ipamapi"
builtinIpam "github.com/docker/libnetwork/ipams/builtin"
nullIpam "github.com/docker/libnetwork/ipams/null"
remoteIpam "github.com/docker/libnetwork/ipams/remote"
)

func initIPAMDrivers(r *drvregistry.DrvRegistry) error {
for _, fn := range [](func(ipamapi.Callback, interface{}, interface{}) error){
builtinIpam.Init,
remoteIpam.Init,
nullIpam.Init,
} {
if err := fn(r, nil, nil); err != nil {
return err
}
}

return nil
}
13 changes: 13 additions & 0 deletions manager/allocator/networkallocator/drivers_linux.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package networkallocator

import (
"github.com/docker/libnetwork/drivers/overlay/ovmanager"
"github.com/docker/libnetwork/drivers/remote"
)

func getInitializers() []initializer {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need a definition of this for other non-linux OSes

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

return []initializer{
{remote.Init, "remote"},
{ovmanager.Init, "overlay"},
}
}
7 changes: 7 additions & 0 deletions manager/allocator/networkallocator/drivers_unsupported.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// +build !linux

package networkallocator

func getInitializers() []initializer {
return nil
}
58 changes: 40 additions & 18 deletions manager/allocator/networkallocator/networkallocator.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@ import (
"fmt"
"net"

"github.com/docker/docker/pkg/plugins"
"github.com/docker/libnetwork/datastore"
"github.com/docker/libnetwork/driverapi"
"github.com/docker/libnetwork/drivers/overlay/ovmanager"
"github.com/docker/libnetwork/drvregistry"
"github.com/docker/libnetwork/ipamapi"
builtinIpam "github.com/docker/libnetwork/ipams/builtin"
nullIpam "github.com/docker/libnetwork/ipams/null"
"github.com/docker/swarmkit/api"
"github.com/docker/swarmkit/log"
"github.com/pkg/errors"
Expand All @@ -23,10 +22,6 @@ const (
DefaultDriver = "overlay"
)

var (
defaultDriverInitFunc = ovmanager.Init
)

// NetworkAllocator acts as the controller for all network related operations
// like managing network and IPAM drivers and also creating and
// deleting networks and the associated resources.
Expand Down Expand Up @@ -68,6 +63,11 @@ type network struct {
endpoints map[string]string
}

type initializer struct {
fn drvregistry.InitFunc
ntype string
}

// New returns a new NetworkAllocator handle
func New() (*NetworkAllocator, error) {
na := &NetworkAllocator{
Expand All @@ -84,18 +84,12 @@ func New() (*NetworkAllocator, error) {
return nil, err
}

// Add the manager component of overlay driver to the registry.
if err := reg.AddDriver(DefaultDriver, defaultDriverInitFunc, nil); err != nil {
if err := initializeDrivers(reg); err != nil {
return nil, err
}

for _, fn := range [](func(ipamapi.Callback, interface{}, interface{}) error){
builtinIpam.Init,
nullIpam.Init,
} {
if err := fn(reg, nil, nil); err != nil {
return nil, err
}
if err = initIPAMDrivers(reg); err != nil {
return nil, err
}

pa, err := newPortAllocator()
Expand Down Expand Up @@ -631,14 +625,33 @@ func (na *NetworkAllocator) resolveDriver(n *api.Network) (driverapi.Driver, str
dName = n.Spec.DriverConfig.Name
}

d, _ := na.drvRegistry.Driver(dName)
d, drvcap := na.drvRegistry.Driver(dName)
if d == nil {
return nil, "", fmt.Errorf("could not resolve network driver %s", dName)
var err error
err = na.loadDriver(dName)
if err != nil {
return nil, "", err
}

d, drvcap = na.drvRegistry.Driver(dName)
if d == nil {
return nil, "", fmt.Errorf("could not resolve network driver %s", dName)
}

}

if drvcap.DataScope != datastore.GlobalScope {
return nil, "", fmt.Errorf("swarm can allocate network resources only for global scoped networks. network driver (%s) is scoped %s", dName, drvcap.DataScope)
}

return d, dName, nil
}

func (na *NetworkAllocator) loadDriver(name string) error {
_, err := plugins.Get(name, driverapi.NetworkPluginEndpointType)
return err
}

// Resolve the IPAM driver
func (na *NetworkAllocator) resolveIPAM(n *api.Network) (ipamapi.Ipam, string, error) {
dName := ipamapi.DefaultIPAM
Expand Down Expand Up @@ -746,3 +759,12 @@ func (na *NetworkAllocator) allocatePools(n *api.Network) (map[string]string, er

return pools, nil
}

func initializeDrivers(reg *drvregistry.DrvRegistry) error {
for _, i := range getInitializers() {
if err := reg.AddDriver(i.ntype, i.fn, nil); err != nil {
return err
}
}
return nil
}
10 changes: 0 additions & 10 deletions manager/controlapi/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@ import (
"fmt"
"net"

"github.com/docker/libnetwork/ipamapi"
"github.com/docker/swarmkit/api"
"github.com/docker/swarmkit/identity"
"github.com/docker/swarmkit/manager/allocator/networkallocator"
"github.com/docker/swarmkit/manager/state/store"
"golang.org/x/net/context"
"google.golang.org/grpc"
Expand Down Expand Up @@ -60,10 +58,6 @@ func validateIPAM(ipam *api.IPAMOptions) error {
return err
}

if ipam.Driver != nil && ipam.Driver.Name != ipamapi.DefaultIPAM {
return grpc.Errorf(codes.InvalidArgument, "invalid IPAM specified")
}

for _, ipamConf := range ipam.Configs {
if err := validateIPAMConfiguration(ipamConf); err != nil {
return err
Expand All @@ -86,10 +80,6 @@ func validateNetworkSpec(spec *api.NetworkSpec) error {
return err
}

if spec.DriverConfig != nil && spec.DriverConfig.Name != networkallocator.DefaultDriver {
return grpc.Errorf(codes.InvalidArgument, "invalid driver specified")
}

if err := validateIPAM(spec.IPAM); err != nil {
return err
}
Expand Down
28 changes: 0 additions & 28 deletions manager/controlapi/network_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,34 +144,6 @@ func TestValidateIPAMConfiguration(t *testing.T) {
assert.NoError(t, err)
}

func TestValidateIPAM(t *testing.T) {
assert.NoError(t, validateIPAM(nil))
ipam := &api.IPAMOptions{
Driver: &api.Driver{
Name: "external",
},
}

err := validateIPAM(ipam)
assert.Error(t, err)
assert.Equal(t, codes.InvalidArgument, grpc.Code(err))
}

func TestValidateNetworkSpec(t *testing.T) {
err := validateNetworkSpec(nil)
assert.Error(t, err)
assert.Equal(t, codes.InvalidArgument, grpc.Code(err))

spec := createNetworkSpec("invalid_driver")
spec.DriverConfig = &api.Driver{
Name: "external",
}

err = validateNetworkSpec(spec)
assert.Error(t, err)
assert.Equal(t, codes.InvalidArgument, grpc.Code(err))
}

func TestCreateNetwork(t *testing.T) {
ts := newTestServer(t)
defer ts.Stop()
Expand Down
2 changes: 1 addition & 1 deletion vendor.conf
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ github.com/docker/go-connections 34b5052da6b11e27f5f2e357b38b571ddddd3928
github.com/docker/go-events 37d35add5005832485c0225ec870121b78fcff1c
github.com/docker/go-units 954fed01cc617c55d838fa2230073f2cb17386c8
github.com/docker/libkv 9fd56606e928ff1f309808f5d5a0b7a2ef73f9a8
github.com/docker/libnetwork 7b26d7e3009f57409cbd15b5cd42e240c1a13510
github.com/docker/libnetwork 7b74403be4241aea5b01b56adab5eab82a80698b
github.com/opencontainers/runc 8e8d01d38d7b4fb0a35bf89b72bc3e18c98882d7

github.com/davecgh/go-spew 5215b55f46b2b919f50a1df0eaa5886afe4e3b3d
Expand Down
Loading