From fc952e0de94aa4283c677ce6c153b1a766db2fc0 Mon Sep 17 00:00:00 2001 From: Michael Crosby Date: Fri, 14 Feb 2014 18:07:33 -0800 Subject: [PATCH 1/2] Remove container dependency for links Docker-DCO-1.1-Signed-off-by: Michael Crosby (github: crosbymichael) --- container.go | 15 +++++++++++++-- links.go | 23 ++++++++++------------- links_test.go | 37 ++----------------------------------- 3 files changed, 25 insertions(+), 50 deletions(-) diff --git a/container.go b/container.go index 3740a7fb73..d054bcf50d 100644 --- a/container.go +++ b/container.go @@ -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 diff --git a/links.go b/links.go index ff39947a0d..1a198b0814 100644 --- a/links.go +++ b/links.go @@ -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, } diff --git a/links_test.go b/links_test.go index 7b85a8e86d..d84fe04061 100644 --- a/links_test.go +++ b/links_test.go @@ -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) } From 147b09fae9527e760d74b5ef2f9558ee109f4009 Mon Sep 17 00:00:00 2001 From: Michael Crosby Date: Fri, 14 Feb 2014 18:18:16 -0800 Subject: [PATCH 2/2] Move links to sub pkg Docker-DCO-1.1-Signed-off-by: Michael Crosby (github: crosbymichael) --- container.go | 7 ++++--- links.go => links/links.go | 2 +- links_test.go => links/links_test.go | 2 +- 3 files changed, 6 insertions(+), 5 deletions(-) rename links.go => links/links.go (99%) rename links_test.go => links/links_test.go (99%) diff --git a/container.go b/container.go index d054bcf50d..361dfeaf67 100644 --- a/container.go +++ b/container.go @@ -8,6 +8,7 @@ import ( "github.com/dotcloud/docker/engine" "github.com/dotcloud/docker/execdriver" "github.com/dotcloud/docker/graphdriver" + "github.com/dotcloud/docker/links" "github.com/dotcloud/docker/nat" "github.com/dotcloud/docker/pkg/mount" "github.com/dotcloud/docker/pkg/term" @@ -71,7 +72,7 @@ type Container struct { VolumesRW map[string]bool hostConfig *runconfig.HostConfig - activeLinks map[string]*Link + activeLinks map[string]*links.Link } type BindMap struct { @@ -537,7 +538,7 @@ func (container *Container) Start() (err error) { } if len(children) > 0 { - container.activeLinks = make(map[string]*Link, len(children)) + container.activeLinks = make(map[string]*links.Link, len(children)) // If we encounter an error make sure that we rollback any network // config and ip table changes @@ -553,7 +554,7 @@ func (container *Container) Start() (err error) { return fmt.Errorf("Cannot link to a non running container: %s AS %s", child.Name, linkAlias) } - link, err := NewLink( + link, err := links.NewLink( container.NetworkSettings.IPAddress, child.NetworkSettings.IPAddress, linkAlias, diff --git a/links.go b/links/links.go similarity index 99% rename from links.go rename to links/links.go index 1a198b0814..68ac98ee07 100644 --- a/links.go +++ b/links/links.go @@ -1,4 +1,4 @@ -package docker +package links import ( "fmt" diff --git a/links_test.go b/links/links_test.go similarity index 99% rename from links_test.go rename to links/links_test.go index d84fe04061..e66f9bfb78 100644 --- a/links_test.go +++ b/links/links_test.go @@ -1,4 +1,4 @@ -package docker +package links import ( "github.com/dotcloud/docker/nat"