1
0
Fork 0
mirror of https://github.com/moby/moby.git synced 2022-11-09 12:21:53 -05:00

Remove container dependency for links

Docker-DCO-1.1-Signed-off-by: Michael Crosby <michael@crosbymichael.com> (github: crosbymichael)
This commit is contained in:
Michael Crosby 2014-02-14 18:07:33 -08:00
parent a7ecc3ea11
commit fc952e0de9
3 changed files with 25 additions and 50 deletions

View file

@ -548,8 +548,19 @@ func (container *Container) Start() (err error) {
container.activeLinks = nil
}
for p, child := range children {
link, err := NewLink(container, child, p, runtime.eng)
for linkAlias, child := range children {
if !child.State.IsRunning() {
return fmt.Errorf("Cannot link to a non running container: %s AS %s", child.Name, linkAlias)
}
link, err := NewLink(
container.NetworkSettings.IPAddress,
child.NetworkSettings.IPAddress,
linkAlias,
child.Config.Env,
child.Config.ExposedPorts,
runtime.eng)
if err != nil {
rollback()
return err

View file

@ -18,26 +18,23 @@ type Link struct {
eng *engine.Engine
}
func NewLink(parent, child *Container, name string, eng *engine.Engine) (*Link, error) {
if parent.ID == child.ID {
return nil, fmt.Errorf("Cannot link to self: %s == %s", parent.ID, child.ID)
}
if !child.State.IsRunning() {
return nil, fmt.Errorf("Cannot link to a non running container: %s AS %s", child.Name, name)
}
func NewLink(parentIP, childIP, name string, env []string, exposedPorts map[nat.Port]struct{}, eng *engine.Engine) (*Link, error) {
ports := make([]nat.Port, len(child.Config.ExposedPorts))
var i int
for p := range child.Config.ExposedPorts {
var (
i int
ports = make([]nat.Port, len(exposedPorts))
)
for p := range exposedPorts {
ports[i] = p
i++
}
l := &Link{
Name: name,
ChildIP: child.NetworkSettings.IPAddress,
ParentIP: parent.NetworkSettings.IPAddress,
ChildEnvironment: child.Config.Env,
ChildIP: childIP,
ParentIP: parentIP,
ChildEnvironment: env,
Ports: ports,
eng: eng,
}

View file

@ -2,37 +2,15 @@ package docker
import (
"github.com/dotcloud/docker/nat"
"github.com/dotcloud/docker/runconfig"
"strings"
"testing"
)
func newMockLinkContainer(id string, ip string) *Container {
return &Container{
Config: &runconfig.Config{},
ID: id,
NetworkSettings: &NetworkSettings{
IPAddress: ip,
},
}
}
func TestLinkNew(t *testing.T) {
toID := GenerateID()
fromID := GenerateID()
from := newMockLinkContainer(fromID, "172.0.17.2")
from.Config.Env = []string{}
from.State = State{Running: true}
ports := make(nat.PortSet)
ports[nat.Port("6379/tcp")] = struct{}{}
from.Config.ExposedPorts = ports
to := newMockLinkContainer(toID, "172.0.17.3")
link, err := NewLink(to, from, "/db/docker", nil)
link, err := NewLink("172.0.17.3", "172.0.17.2", "/db/docker", nil, ports, nil)
if err != nil {
t.Fatal(err)
}
@ -60,21 +38,10 @@ func TestLinkNew(t *testing.T) {
}
func TestLinkEnv(t *testing.T) {
toID := GenerateID()
fromID := GenerateID()
from := newMockLinkContainer(fromID, "172.0.17.2")
from.Config.Env = []string{"PASSWORD=gordon"}
from.State = State{Running: true}
ports := make(nat.PortSet)
ports[nat.Port("6379/tcp")] = struct{}{}
from.Config.ExposedPorts = ports
to := newMockLinkContainer(toID, "172.0.17.3")
link, err := NewLink(to, from, "/db/docker", nil)
link, err := NewLink("172.0.17.3", "172.0.17.2", "/db/docker", []string{"PASSWORD=gordon"}, ports, nil)
if err != nil {
t.Fatal(err)
}