Skip to content

Commit

Permalink
refactor network interface, will response subnets (#240)
Browse files Browse the repository at this point in the history
  • Loading branch information
CMGS committed Aug 11, 2020
1 parent cd44595 commit fd80ffd
Show file tree
Hide file tree
Showing 13 changed files with 401 additions and 371 deletions.
8 changes: 4 additions & 4 deletions cluster/calcium/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,17 @@ func (c *Calcium) ListNetworks(ctx context.Context, podname string, driver strin
}

// ConnectNetwork connect to a network
func (c *Calcium) ConnectNetwork(ctx context.Context, network, target, ipv4, ipv6 string) error {
func (c *Calcium) ConnectNetwork(ctx context.Context, network, target, ipv4, ipv6 string) ([]string, error) {
container, err := c.GetContainer(ctx, target)
if err != nil {
return err
return nil, err
}

return container.Engine.NetworkConnect(ctx, network, target, ipv4, ipv6)
}

// DisConnectNetwork connect to a network
func (c *Calcium) DisConnectNetwork(ctx context.Context, network, target string, force bool) error {
// DisconnectNetwork connect to a network
func (c *Calcium) DisconnectNetwork(ctx context.Context, network, target string, force bool) error {
container, err := c.GetContainer(ctx, target)
if err != nil {
return err
Expand Down
10 changes: 5 additions & 5 deletions cluster/calcium/network_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,11 @@ func TestConnectNetwork(t *testing.T) {
container := &types.Container{Engine: engine}

store.On("GetContainer", mock.Anything, mock.Anything).Return(nil, types.ErrBadMeta).Once()
err := c.ConnectNetwork(ctx, "network", "123", "", "")
_, err := c.ConnectNetwork(ctx, "network", "123", "", "")
assert.Error(t, err)
store.On("GetContainer", mock.Anything, mock.Anything).Return(container, nil)
engine.On("NetworkConnect", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(nil)
err = c.ConnectNetwork(ctx, "network", "123", "", "")
engine.On("NetworkConnect", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return([]string{}, nil)
_, err = c.ConnectNetwork(ctx, "network", "123", "", "")
assert.NoError(t, err)
}

Expand All @@ -69,10 +69,10 @@ func TestDisConnectNetwork(t *testing.T) {
container := &types.Container{Engine: engine}

store.On("GetContainer", mock.Anything, mock.Anything).Return(nil, types.ErrBadMeta).Once()
err := c.DisConnectNetwork(ctx, "network", "123", true)
err := c.DisconnectNetwork(ctx, "network", "123", true)
assert.Error(t, err)
store.On("GetContainer", mock.Anything, mock.Anything).Return(container, nil)
engine.On("NetworkDisconnect", mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(nil)
err = c.DisConnectNetwork(ctx, "network", "123", true)
err = c.DisconnectNetwork(ctx, "network", "123", true)
assert.NoError(t, err)
}
4 changes: 2 additions & 2 deletions cluster/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ const (
type Cluster interface {
// meta networks
ListNetworks(ctx context.Context, podname string, driver string) ([]*enginetypes.Network, error)
ConnectNetwork(ctx context.Context, network, target, ipv4, ipv6 string) error
DisConnectNetwork(ctx context.Context, network, target string, force bool) error
ConnectNetwork(ctx context.Context, network, target, ipv4, ipv6 string) ([]string, error)
DisconnectNetwork(ctx context.Context, network, target string, force bool) error
// meta pod
AddPod(ctx context.Context, podname, desc string) (*types.Pod, error)
RemovePod(ctx context.Context, podname string) error
Expand Down
35 changes: 20 additions & 15 deletions cluster/mocks/Cluster.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 14 additions & 3 deletions engine/docker/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,23 @@ import (
)

// NetworkConnect connect to a network
func (e *Engine) NetworkConnect(ctx context.Context, network, target, ipv4, ipv6 string) error {
func (e *Engine) NetworkConnect(ctx context.Context, network, target, ipv4, ipv6 string) ([]string, error) {
config, err := e.makeIPV4EndpointSetting(ipv4)
if err != nil {
return err
return nil, err
}
return e.client.NetworkConnect(ctx, network, target, config)
if err := e.client.NetworkConnect(ctx, network, target, config); err != nil {
return nil, err
}
container, err := e.client.ContainerInspect(ctx, target)
if err != nil {
return nil, err
}
ns := container.NetworkSettings.Networks[network]
if ns == nil {
return []string{}, nil
}
return []string{ns.IPAddress}, nil
}

// NetworkDisconnect disconnect from a network
Expand Down
2 changes: 1 addition & 1 deletion engine/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ type API interface {
ExecResize(ctx context.Context, execID string, height, width uint) (err error)
ExecExitCode(ctx context.Context, execID string) (int, error)

NetworkConnect(ctx context.Context, network, target, ipv4, ipv6 string) error
NetworkConnect(ctx context.Context, network, target, ipv4, ipv6 string) ([]string, error)
NetworkDisconnect(ctx context.Context, network, target string, force bool) error
NetworkList(ctx context.Context, drivers []string) ([]*enginetypes.Network, error)

Expand Down
19 changes: 14 additions & 5 deletions engine/mocks/API.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion engine/mocks/fakeengine/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func MakeClient(ctx context.Context, config coretypes.Config, nodename, endpoint
e.On("ExecResize", mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(nil)
e.On("ExecExitCode", mock.Anything, execID).Return(0, nil)
// network
e.On("NetworkConnect", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(nil)
e.On("NetworkConnect", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return([]string{}, nil)
e.On("NetworkDisconnect", mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(nil)
e.On("NetworkList", mock.Anything, mock.Anything).Return([]*enginetypes.Network{{
Name: "mock-network", Subnets: []string{"1.1.1.1/8", "2.2.2.2/8"},
Expand Down
2 changes: 1 addition & 1 deletion engine/systemd/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
)

// NetworkConnect connects target netloc
func (s *SSHClient) NetworkConnect(ctx context.Context, network, target, ipv4, ipv6 string) (err error) {
func (s *SSHClient) NetworkConnect(ctx context.Context, network, target, ipv4, ipv6 string) (subnets []string, err error) {
err = types.ErrEngineNotImplemented
return
}
Expand Down
2 changes: 1 addition & 1 deletion engine/virt/virt.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ func (v *Virt) ExecResize(ctx context.Context, execID string, height, width uint
}

// NetworkConnect connects to a network.
func (v *Virt) NetworkConnect(ctx context.Context, network, target, ipv4, ipv6 string) (err error) {
func (v *Virt) NetworkConnect(ctx context.Context, network, target, ipv4, ipv6 string) (subnet []string, err error) {
log.Warnf("NetworkConnect does not implement")
return
}
Expand Down
Loading

0 comments on commit fd80ffd

Please sign in to comment.