Skip to content

Commit

Permalink
add list network api
Browse files Browse the repository at this point in the history
  • Loading branch information
tonicmuroq committed Sep 22, 2016
1 parent d7a1256 commit f8300fb
Show file tree
Hide file tree
Showing 11 changed files with 443 additions and 148 deletions.
28 changes: 28 additions & 0 deletions cluster/calcium/network.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package calcium

import (
"fmt"

"gitlab.ricebook.net/platform/core/types"
"gitlab.ricebook.net/platform/core/utils"
)

// list networks for podname
// just get one node from podname
// and call docker network ls
// only get those driven by network driver
func (c *calcium) ListNetworks(podname string) ([]*types.Network, error) {
networks := []*types.Network{}
nodes, err := c.ListPodNodes(podname)
if err != nil {
return networks, err
}

if len(nodes) == 0 {
return networks, fmt.Errorf("Pod %s has no nodes", podname)
}

node := nodes[0]
ctx := utils.ToDockerContext(node.Engine)
return c.network.ListNetworks(ctx)
}
1 change: 1 addition & 0 deletions cluster/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ type Cluster interface {
ListPodNodes(podname string) ([]*types.Node, error)
GetContainer(id string) (*types.Container, error)
GetContainers(ids []string) ([]*types.Container, error)
ListNetworks(podname string) ([]*types.Network, error)

// cluster methods
BuildImage(repository, version, uid, artifact string) (chan *types.BuildImageMessage, error)
Expand Down
17 changes: 17 additions & 0 deletions devtools/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,23 @@ def get_pod_nodes(ctx, name):
click.echo(node)


@cli.command('pod:networks')
@click.argument('name')
@click.pass_context
def get_pod_networks(ctx, name):
stub = _get_stub(ctx)
opts = pb.GetPodOptions(name=name)

try:
r = stub.ListNetworks(opts, 5)
except AbortionError as e:
click.echo(click.style('abortion error: %s' % e.details, fg='red', bold=True))
ctx.exit(-1)

for n in r.networks:
click.echo('%s: %s' % (n.name, ','.join(n.subnets)))


@cli.command('node:get')
@click.argument('podname')
@click.argument('nodename')
Expand Down
210 changes: 161 additions & 49 deletions devtools/core_pb2.py

Large diffs are not rendered by default.

36 changes: 34 additions & 2 deletions network/calico/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ import (
"net"

log "github.com/Sirupsen/logrus"
enginetypes "github.com/docker/engine-api/types"
enginenetwork "github.com/docker/engine-api/types/network"
"gitlab.ricebook.net/platform/core/types"
"gitlab.ricebook.net/platform/core/utils"
"golang.org/x/net/context"
)
Expand Down Expand Up @@ -33,7 +35,7 @@ func (t *titanium) ConnectToNetwork(ctx context.Context, containerID, networkID,

engine, ok := utils.FromDockerContext(ctx)
if !ok {
return fmt.Errorf("Not actually a `engineapi.Client` for value engine in context", containerID)
return fmt.Errorf("Not actually a `engineapi.Client` for value engine in context")
}

config := &enginenetwork.EndpointSettings{
Expand Down Expand Up @@ -63,13 +65,43 @@ func (t *titanium) DisconnectFromNetwork(ctx context.Context, containerID, netwo

engine, ok := utils.FromDockerContext(ctx)
if !ok {
return fmt.Errorf("Not actually a `engineapi.Client` for value engine in context", containerID)
return fmt.Errorf("Not actually a `engineapi.Client` for value engine in context")
}

log.Debugf("Disconnect %q from %q", containerID, networkID)
return engine.NetworkDisconnect(context.Background(), networkID, containerID, false)
}

// list networks from context
func (t *titanium) ListNetworks(ctx context.Context) ([]*types.Network, error) {
networks := []*types.Network{}
engine, ok := utils.FromDockerContext(ctx)
if !ok {
return networks, fmt.Errorf("Not actually a `engineapi.Client` for value engine in context")
}

ns, err := engine.NetworkList(context.Background(), enginetypes.NetworkListOptions{})
if err != nil {
return networks, err
}

driver := t.Name()
for _, n := range ns {
// TODO 之后可以用filter driver=xxx
// 但是现在版本的API还没有给出
if n.Driver != driver {
continue
}

subnets := []string{}
for _, config := range n.IPAM.Config {
subnets = append(subnets, config.Subnet)
}
networks = append(networks, &types.Network{Name: n.Name, Subnets: subnets})
}
return networks, nil
}

func New() *titanium {
return &titanium{}
}
7 changes: 6 additions & 1 deletion network/network.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
package network

import "golang.org/x/net/context"
import (
"gitlab.ricebook.net/platform/core/types"
"golang.org/x/net/context"
)

type Network interface {
// connect and disconnect
ConnectToNetwork(ctx context.Context, containerID, networkID, ipv4 string) error
DisconnectFromNetwork(ctx context.Context, containerID, networkID string) error
// list networks
ListNetworks(ctx context.Context) ([]*types.Network, error)
// type and name to identify the network manager
// this will determine when to call connect/disconnect
Type() string
Expand Down
Loading

0 comments on commit f8300fb

Please sign in to comment.