Skip to content
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
87 changes: 76 additions & 11 deletions daemon/mgr/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"net"
"path"
"strings"

apitypes "github.com/alibaba/pouch/apis/types"
"github.com/alibaba/pouch/daemon/config"
Expand Down Expand Up @@ -149,26 +150,90 @@ func (nm *NetworkManager) List(ctx context.Context, labels map[string]string) ([
return net, nil
}

// Get returns the information of network that specified name/id.
func (nm *NetworkManager) Get(ctx context.Context, name string) (*types.Network, error) {
// GetNetworkByName returns the information of network that specified name.
func (nm *NetworkManager) GetNetworkByName(name string) (*types.Network, error) {
n, err := nm.controller.NetworkByName(name)
if err != nil {
if err == libnetwork.ErrNoSuchNetwork(name) {
return nil, errors.Wrap(errtypes.ErrNotfound, err.Error())
}
return nil, err
}
return &types.Network{
Name: n.Name(),
ID: n.ID(),
Type: n.Type(),
Network: n,
}, nil
}

if n != nil {
// GetNetworksByPartialID returns a list of networks that ID starts with the given prefix.
func (nm *NetworkManager) GetNetworksByPartialID(partialID string) []*types.Network {
var matchedNetworks []*types.Network

walker := func(nw libnetwork.Network) bool {
if strings.HasPrefix(nw.ID(), partialID) {
matchedNetwork := &types.Network{
Name: nw.Name(),
ID: nw.ID(),
Type: nw.Type(),
Network: nw,
}
matchedNetworks = append(matchedNetworks, matchedNetwork)
}
return false
}
nm.controller.WalkNetworks(walker)
return matchedNetworks
}

// GetNetworkByPartialID returns the information of network that ID starts with the given prefix.
// If there are not matching networks, it fails with ErrNotfound.
// If there are multiple matching networks, it fails with ErrTooMany.
func (nm *NetworkManager) GetNetworkByPartialID(partialID string) (*types.Network, error) {
network, err := nm.controller.NetworkByID(partialID)
if err == nil {
return &types.Network{
Name: name,
ID: n.ID(),
Type: n.Type(),
Network: n,
Name: network.Name(),
ID: network.ID(),
Type: network.Type(),
Network: network,
}, nil
}
if !isNoSuchNetworkError(err) {
return nil, err
}
matchedNetworks := nm.GetNetworksByPartialID(partialID)
if len(matchedNetworks) == 0 {
return nil, errors.Wrap(errtypes.ErrNotfound, "network: "+partialID)
}
if len(matchedNetworks) > 1 {
return nil, errors.Wrap(errtypes.ErrTooMany, "network: "+partialID)
}
return matchedNetworks[0], nil
}

return nil, nil
// isNoSuchNetworkError looks up the error type and returns a bool if it is ErrNoSuchNetwork or not.
func isNoSuchNetworkError(err error) bool {
_, ok := err.(libnetwork.ErrNoSuchNetwork)
return ok
}

// Get returns the information of network for specified string that represent network name or ID.
// If network name is given, the network with same name is returned.
// If prefix of network ID is given, the network with same prefix is returned.
func (nm *NetworkManager) Get(ctx context.Context, idName string) (*types.Network, error) {
n, err := nm.GetNetworkByName(idName)
if err != nil && !isNoSuchNetworkError(err) {
return nil, err
}

if n != nil {
return n, nil
}

n, err = nm.GetNetworkByPartialID(idName)
if err != nil {
return nil, err
}
return n, err
}

// EndpointCreate is used to create network endpoint.
Expand Down
13 changes: 12 additions & 1 deletion test/cli_network_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,25 @@ func (suite *PouchNetworkSuite) SetUpSuite(c *check.C) {
// TestNetworkInspectFormat tests the inspect format of network works.
func (suite *PouchNetworkSuite) TestNetworkInspectFormat(c *check.C) {
output := command.PouchRun("network", "inspect", "bridge").Stdout()
result := &types.ContainerJSON{}
result := &types.NetworkInspectResp{}
if err := json.Unmarshal([]byte(output), result); err != nil {
c.Errorf("failed to decode inspect output: %v", err)
}

// inspect network name
output = command.PouchRun("network", "inspect", "-f", "{{.Name}}", "bridge").Stdout()
c.Assert(output, check.Equals, "bridge\n")

output = command.PouchRun("network", "inspect", "bridge").Stdout()
network := &types.NetworkInspectResp{}
if err := json.Unmarshal([]byte(output), network); err != nil {
c.Errorf("failed to decode inspect output: %v", err)
}
networkID := network.ID

// inspect network name by ID
output = command.PouchRun("network", "inspect", "-f", "{{.Name}}", networkID).Stdout()
c.Assert(output, check.Equals, "bridge\n")
}

// TestNetworkDefault tests the creation of default bridge/none/host network.
Expand Down