Skip to content

Commit

Permalink
normalize network implement
Browse files Browse the repository at this point in the history
  • Loading branch information
CMGS committed Oct 13, 2017
1 parent dda0695 commit 3805d11
Show file tree
Hide file tree
Showing 15 changed files with 381 additions and 317 deletions.
3 changes: 1 addition & 2 deletions cluster/calcium/build_image.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ import (
// FIXME in alpine, useradd rename as adduser
const (
fromAsTmpl = "FROM %s as %s"
commonTmpl = `ENV ERU 1
{{ range $k, $v:= .Args -}}
commonTmpl = `{{ range $k, $v:= .Args -}}
{{ printf "ARG %s=%s" $k $v }}
{{ end -}}
{{ range $k, $v:= .Envs -}}
Expand Down
4 changes: 2 additions & 2 deletions cluster/calcium/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"strings"

"github.com/projecteru2/core/network"
"github.com/projecteru2/core/network/calico"
"github.com/projecteru2/core/network/sdn"
"github.com/projecteru2/core/scheduler"
"github.com/projecteru2/core/scheduler/complex"
"github.com/projecteru2/core/source"
Expand Down Expand Up @@ -44,7 +44,7 @@ func New(config types.Config) (*calcium, error) {
}

// set network
titanium := calico.New()
titanium := sdn.New()

// set scm
var scm source.Source
Expand Down
8 changes: 4 additions & 4 deletions cluster/calcium/meta_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"fmt"
"testing"

"github.com/projecteru2/core/network/calico"
"github.com/projecteru2/core/network/sdn"
"github.com/projecteru2/core/scheduler/simple"
"github.com/projecteru2/core/source/gitlab"
"github.com/projecteru2/core/store/mock"
Expand All @@ -15,7 +15,7 @@ import (
func TestListPods(t *testing.T) {
store := &mockstore.MockStore{}
config := types.Config{}
c := &calcium{store: store, config: config, scheduler: simplescheduler.New(), network: calico.New(), source: gitlab.New(config)}
c := &calcium{store: store, config: config, scheduler: simplescheduler.New(), network: sdn.New(), source: gitlab.New(config)}

store.On("GetAllPods").Return([]*types.Pod{
&types.Pod{Name: "pod1", Desc: "desc1"},
Expand All @@ -36,7 +36,7 @@ func TestListPods(t *testing.T) {
func TestAddPod(t *testing.T) {
store := &mockstore.MockStore{}
config := types.Config{}
c := &calcium{store: store, config: config, scheduler: simplescheduler.New(), network: calico.New(), source: gitlab.New(config)}
c := &calcium{store: store, config: config, scheduler: simplescheduler.New(), network: sdn.New(), source: gitlab.New(config)}

store.On("AddPod", "pod1", "", "desc1").Return(&types.Pod{Name: "pod1", Favor: "MEM", Desc: "desc1"}, nil)
store.On("AddPod", "pod2", "", "desc2").Return(nil, fmt.Errorf("Etcd Error"))
Expand All @@ -55,7 +55,7 @@ func TestAddPod(t *testing.T) {
func TestGetPods(t *testing.T) {
store := &mockstore.MockStore{}
config := types.Config{}
c := &calcium{store: store, config: config, scheduler: simplescheduler.New(), network: calico.New(), source: gitlab.New(config)}
c := &calcium{store: store, config: config, scheduler: simplescheduler.New(), network: sdn.New(), source: gitlab.New(config)}

store.On("GetPod", "pod1").Return(&types.Pod{Name: "pod1", Desc: "desc1"}, nil).Once()
store.On("GetPod", "pod2").Return(nil, fmt.Errorf("Not found")).Once()
Expand Down
1 change: 1 addition & 0 deletions cluster/calcium/mock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ const (
mockMemory = int64(8589934592) // 8G
mockID = "f1f9da344e8f8f90f73899ddad02da6cdf2218bbe52413af2bcfef4fba2d22de"
appmemory = int64(268435456) // 0.25 G
driver = "calico"
)

type Map struct {
Expand Down
4 changes: 2 additions & 2 deletions cluster/calcium/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
// 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) {
func (c *calcium) ListNetworks(podname string, driver string) ([]*types.Network, error) {
networks := []*types.Network{}
nodes, err := c.ListPodNodes(podname, false)
if err != nil {
Expand All @@ -24,5 +24,5 @@ func (c *calcium) ListNetworks(podname string) ([]*types.Network, error) {

node := nodes[0]
ctx := utils.ToDockerContext(node.Engine)
return c.network.ListNetworks(ctx)
return c.network.ListNetworks(ctx, driver)
}
2 changes: 1 addition & 1 deletion cluster/calcium/network_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
func TestListNetworks(t *testing.T) {
initMockConfig()

networks, err := mockc.ListNetworks(podname)
networks, err := mockc.ListNetworks(podname, driver)
assert.NoError(t, err)
for _, network := range networks {
t.Log(network.Name)
Expand Down
2 changes: 1 addition & 1 deletion cluster/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ type Cluster interface {
ListPodNodes(podname string, all bool) ([]*types.Node, error)
GetContainer(id string) (*types.Container, error)
GetContainers(ids []string) ([]*types.Container, error)
ListNetworks(podname string) ([]*types.Network, error)
ListNetworks(podname string, driver string) ([]*types.Network, error)

// cluster methods
BuildImage(opts *types.BuildOptions) (chan *types.BuildImageMessage, error)
Expand Down
6 changes: 1 addition & 5 deletions network/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,5 @@ type Network interface {
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
Name() string
ListNetworks(ctx context.Context, driver string) ([]*types.Network, error)
}
19 changes: 4 additions & 15 deletions network/calico/plugin.go → network/sdn/plugin.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package calico
package sdn

import (
"context"
Expand All @@ -15,18 +15,6 @@ import (

type titanium struct{}

// type of the network manager
// if set to "plugin", then it will act like a plugin
// main difference is the order of connect/disconnect
func (t *titanium) Type() string {
return "plugin"
}

// name of the network manager
func (t *titanium) Name() string {
return "calico"
}

// connect to network with ipv4 address
func (t *titanium) ConnectToNetwork(ctx context.Context, containerID, networkID, ipv4 string) error {
if len(containerID) != 64 {
Expand Down Expand Up @@ -73,15 +61,15 @@ func (t *titanium) DisconnectFromNetwork(ctx context.Context, containerID, netwo
}

// list networks from context
func (t *titanium) ListNetworks(ctx context.Context) ([]*types.Network, error) {
func (t *titanium) ListNetworks(ctx context.Context, driver string) ([]*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")
}

filters := enginefilters.NewArgs()
filters.Add("driver", t.Name())
filters.Add("driver", driver)
ns, err := engine.NetworkList(context.Background(), enginetypes.NetworkListOptions{Filters: filters})
if err != nil {
return networks, err
Expand All @@ -97,6 +85,7 @@ func (t *titanium) ListNetworks(ctx context.Context) ([]*types.Network, error) {
return networks, nil
}

//New a titanium obj
func New() *titanium {
return &titanium{}
}
Loading

0 comments on commit 3805d11

Please sign in to comment.