mirror of
https://github.com/moby/moby.git
synced 2022-11-09 12:21:53 -05:00
Add methods to walk Endpoints and Networks
- From Network and Controller interfaces, respectively Signed-off-by: Alessandro Boch <aboch@docker.com>
This commit is contained in:
parent
2b3644edd5
commit
1c7c0f371b
2 changed files with 135 additions and 21 deletions
|
@ -74,14 +74,6 @@ func TestBridge(t *testing.T) {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
epList := network.Endpoints()
|
|
||||||
if len(epList) != 1 {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
if ep != epList[0] {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := ep.Delete(); err != nil {
|
if err := ep.Delete(); err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
@ -299,3 +291,83 @@ func TestUnknownEndpoint(t *testing.T) {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestNetworkEndpointsWalkers(t *testing.T) {
|
||||||
|
defer netutils.SetupTestNetNS(t)()
|
||||||
|
controller := libnetwork.New()
|
||||||
|
netType := "bridge"
|
||||||
|
|
||||||
|
option := options.Generic{}
|
||||||
|
err := controller.ConfigureNetworkDriver(netType, option)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create network 1 and add 2 endpoint: ep11, ep12
|
||||||
|
net1, err := controller.NewNetwork(netType, "network1", "")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
ep11, err := net1.CreateEndpoint("ep11", "sbox1", nil)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
ep12, err := net1.CreateEndpoint("ep12", "sbox2", nil)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test list methods on net1
|
||||||
|
epList1 := net1.Endpoints()
|
||||||
|
if len(epList1) != 2 {
|
||||||
|
t.Fatalf("Endpoints() returned wrong number of elements: %d instead of 2", len(epList1))
|
||||||
|
}
|
||||||
|
// endpoint order is not guaranteed
|
||||||
|
for _, e := range epList1 {
|
||||||
|
if e != ep11 && e != ep12 {
|
||||||
|
t.Fatal("Endpoints() did not return all the expected elements")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test Endpoint Walk method
|
||||||
|
var epName string
|
||||||
|
var epWanted libnetwork.Endpoint
|
||||||
|
wlk := func(ep libnetwork.Endpoint) bool {
|
||||||
|
if ep.Name() == epName {
|
||||||
|
epWanted = ep
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
// Look for ep1 on network1
|
||||||
|
epName = "ep11"
|
||||||
|
net1.WalkEndpoints(wlk)
|
||||||
|
if epWanted == nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if ep11 != epWanted {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test Network Walk method
|
||||||
|
var netName string
|
||||||
|
var netWanted libnetwork.Network
|
||||||
|
nwWlk := func(nw libnetwork.Network) bool {
|
||||||
|
if nw.Name() == netName {
|
||||||
|
netWanted = nw
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
// Look for network named "network1"
|
||||||
|
netName = "network1"
|
||||||
|
controller.WalkNetworks(nwWlk)
|
||||||
|
if netWanted == nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if net1 != netWanted {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -52,9 +52,16 @@ import (
|
||||||
type NetworkController interface {
|
type NetworkController interface {
|
||||||
// ConfigureNetworkDriver applies the passed options to the driver instance for the specified network type
|
// ConfigureNetworkDriver applies the passed options to the driver instance for the specified network type
|
||||||
ConfigureNetworkDriver(networkType string, options interface{}) error
|
ConfigureNetworkDriver(networkType string, options interface{}) error
|
||||||
|
|
||||||
// Create a new network. The options parameter carries network specific options.
|
// Create a new network. The options parameter carries network specific options.
|
||||||
// Labels support will be added in the near future.
|
// Labels support will be added in the near future.
|
||||||
NewNetwork(networkType, name string, options interface{}) (Network, error)
|
NewNetwork(networkType, name string, options interface{}) (Network, error)
|
||||||
|
|
||||||
|
// Networks returns the list of Network(s) managed by this controller.
|
||||||
|
Networks() []Network
|
||||||
|
|
||||||
|
// WalkNetworks uses the provided function to walk the Network(s) managed by this controller.
|
||||||
|
WalkNetworks(walker NetworkWalker)
|
||||||
}
|
}
|
||||||
|
|
||||||
// A Network represents a logical connectivity zone that containers may
|
// A Network represents a logical connectivity zone that containers may
|
||||||
|
@ -74,13 +81,20 @@ type Network interface {
|
||||||
// Labels support will be added in the near future.
|
// Labels support will be added in the near future.
|
||||||
CreateEndpoint(name string, sboxKey string, options interface{}) (Endpoint, error)
|
CreateEndpoint(name string, sboxKey string, options interface{}) (Endpoint, error)
|
||||||
|
|
||||||
// Endpoints returns the list of Endpoint in this network.
|
// Endpoints returns the list of Endpoint(s) in this network.
|
||||||
Endpoints() []Endpoint
|
Endpoints() []Endpoint
|
||||||
|
|
||||||
|
// WalkEndpoints uses the provided function to walk the Endpoints
|
||||||
|
WalkEndpoints(walker EndpointWalker)
|
||||||
|
|
||||||
// Delete the network.
|
// Delete the network.
|
||||||
Delete() error
|
Delete() error
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// NetworkWalker is a client provided function which will be used to walk the Networks.
|
||||||
|
// When the function returns true, the walk will stop.
|
||||||
|
type NetworkWalker func(nw Network) bool
|
||||||
|
|
||||||
// Endpoint represents a logical connection between a network and a sandbox.
|
// Endpoint represents a logical connection between a network and a sandbox.
|
||||||
type Endpoint interface {
|
type Endpoint interface {
|
||||||
// A system generated id for this endpoint.
|
// A system generated id for this endpoint.
|
||||||
|
@ -99,12 +113,9 @@ type Endpoint interface {
|
||||||
Delete() error
|
Delete() error
|
||||||
}
|
}
|
||||||
|
|
||||||
type endpoint struct {
|
// EndpointWalker is a client provided function which will be used to walk the Endpoints.
|
||||||
name string
|
// When the function returns true, the walk will stop.
|
||||||
id types.UUID
|
type EndpointWalker func(ep Endpoint) bool
|
||||||
network *network
|
|
||||||
sandboxInfo *sandbox.Info
|
|
||||||
}
|
|
||||||
|
|
||||||
type network struct {
|
type network struct {
|
||||||
ctrlr *controller
|
ctrlr *controller
|
||||||
|
@ -116,6 +127,13 @@ type network struct {
|
||||||
sync.Mutex
|
sync.Mutex
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type endpoint struct {
|
||||||
|
name string
|
||||||
|
id types.UUID
|
||||||
|
network *network
|
||||||
|
sandboxInfo *sandbox.Info
|
||||||
|
}
|
||||||
|
|
||||||
type networkTable map[types.UUID]*network
|
type networkTable map[types.UUID]*network
|
||||||
type endpointTable map[types.UUID]*endpoint
|
type endpointTable map[types.UUID]*endpoint
|
||||||
|
|
||||||
|
@ -179,6 +197,26 @@ func (c *controller) NewNetwork(networkType, name string, options interface{}) (
|
||||||
return network, nil
|
return network, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c *controller) Networks() []Network {
|
||||||
|
c.Lock()
|
||||||
|
defer c.Unlock()
|
||||||
|
|
||||||
|
list := make([]Network, 0, len(c.networks))
|
||||||
|
for _, n := range c.networks {
|
||||||
|
list = append(list, n)
|
||||||
|
}
|
||||||
|
|
||||||
|
return list
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *controller) WalkNetworks(walker NetworkWalker) {
|
||||||
|
for _, n := range c.Networks() {
|
||||||
|
if walker(n) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func (n *network) Name() string {
|
func (n *network) Name() string {
|
||||||
return n.name
|
return n.name
|
||||||
}
|
}
|
||||||
|
@ -248,18 +286,22 @@ func (n *network) CreateEndpoint(name string, sboxKey string, options interface{
|
||||||
func (n *network) Endpoints() []Endpoint {
|
func (n *network) Endpoints() []Endpoint {
|
||||||
n.Lock()
|
n.Lock()
|
||||||
defer n.Unlock()
|
defer n.Unlock()
|
||||||
|
list := make([]Endpoint, 0, len(n.endpoints))
|
||||||
list := make([]Endpoint, len(n.endpoints))
|
|
||||||
|
|
||||||
idx := 0
|
|
||||||
for _, e := range n.endpoints {
|
for _, e := range n.endpoints {
|
||||||
list[idx] = e
|
list = append(list, e)
|
||||||
idx++
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return list
|
return list
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (n *network) WalkEndpoints(walker EndpointWalker) {
|
||||||
|
for _, e := range n.Endpoints() {
|
||||||
|
if walker(e) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func (ep *endpoint) ID() string {
|
func (ep *endpoint) ID() string {
|
||||||
return string(ep.id)
|
return string(ep.id)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue