mirror of https://github.com/docker/docs.git
Merge pull request #4162 from crosbymichael/movelinks
Move links functionality into pkg
This commit is contained in:
commit
e9db157bee
20
container.go
20
container.go
|
@ -8,6 +8,7 @@ import (
|
||||||
"github.com/dotcloud/docker/engine"
|
"github.com/dotcloud/docker/engine"
|
||||||
"github.com/dotcloud/docker/execdriver"
|
"github.com/dotcloud/docker/execdriver"
|
||||||
"github.com/dotcloud/docker/graphdriver"
|
"github.com/dotcloud/docker/graphdriver"
|
||||||
|
"github.com/dotcloud/docker/links"
|
||||||
"github.com/dotcloud/docker/nat"
|
"github.com/dotcloud/docker/nat"
|
||||||
"github.com/dotcloud/docker/pkg/mount"
|
"github.com/dotcloud/docker/pkg/mount"
|
||||||
"github.com/dotcloud/docker/pkg/term"
|
"github.com/dotcloud/docker/pkg/term"
|
||||||
|
@ -71,7 +72,7 @@ type Container struct {
|
||||||
VolumesRW map[string]bool
|
VolumesRW map[string]bool
|
||||||
hostConfig *runconfig.HostConfig
|
hostConfig *runconfig.HostConfig
|
||||||
|
|
||||||
activeLinks map[string]*Link
|
activeLinks map[string]*links.Link
|
||||||
}
|
}
|
||||||
|
|
||||||
type BindMap struct {
|
type BindMap struct {
|
||||||
|
@ -537,7 +538,7 @@ func (container *Container) Start() (err error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(children) > 0 {
|
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
|
// If we encounter an error make sure that we rollback any network
|
||||||
// config and ip table changes
|
// config and ip table changes
|
||||||
|
@ -548,8 +549,19 @@ func (container *Container) Start() (err error) {
|
||||||
container.activeLinks = nil
|
container.activeLinks = nil
|
||||||
}
|
}
|
||||||
|
|
||||||
for p, child := range children {
|
for linkAlias, child := range children {
|
||||||
link, err := NewLink(container, child, p, runtime.eng)
|
if !child.State.IsRunning() {
|
||||||
|
return fmt.Errorf("Cannot link to a non running container: %s AS %s", child.Name, linkAlias)
|
||||||
|
}
|
||||||
|
|
||||||
|
link, err := links.NewLink(
|
||||||
|
container.NetworkSettings.IPAddress,
|
||||||
|
child.NetworkSettings.IPAddress,
|
||||||
|
linkAlias,
|
||||||
|
child.Config.Env,
|
||||||
|
child.Config.ExposedPorts,
|
||||||
|
runtime.eng)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
rollback()
|
rollback()
|
||||||
return err
|
return err
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
package docker
|
package links
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
@ -18,26 +18,23 @@ type Link struct {
|
||||||
eng *engine.Engine
|
eng *engine.Engine
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewLink(parent, child *Container, name string, eng *engine.Engine) (*Link, error) {
|
func NewLink(parentIP, childIP, name string, env []string, exposedPorts map[nat.Port]struct{}, 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)
|
|
||||||
}
|
|
||||||
|
|
||||||
ports := make([]nat.Port, len(child.Config.ExposedPorts))
|
var (
|
||||||
var i int
|
i int
|
||||||
for p := range child.Config.ExposedPorts {
|
ports = make([]nat.Port, len(exposedPorts))
|
||||||
|
)
|
||||||
|
|
||||||
|
for p := range exposedPorts {
|
||||||
ports[i] = p
|
ports[i] = p
|
||||||
i++
|
i++
|
||||||
}
|
}
|
||||||
|
|
||||||
l := &Link{
|
l := &Link{
|
||||||
Name: name,
|
Name: name,
|
||||||
ChildIP: child.NetworkSettings.IPAddress,
|
ChildIP: childIP,
|
||||||
ParentIP: parent.NetworkSettings.IPAddress,
|
ParentIP: parentIP,
|
||||||
ChildEnvironment: child.Config.Env,
|
ChildEnvironment: env,
|
||||||
Ports: ports,
|
Ports: ports,
|
||||||
eng: eng,
|
eng: eng,
|
||||||
}
|
}
|
|
@ -1,38 +1,16 @@
|
||||||
package docker
|
package links
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/dotcloud/docker/nat"
|
"github.com/dotcloud/docker/nat"
|
||||||
"github.com/dotcloud/docker/runconfig"
|
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
func newMockLinkContainer(id string, ip string) *Container {
|
|
||||||
return &Container{
|
|
||||||
Config: &runconfig.Config{},
|
|
||||||
ID: id,
|
|
||||||
NetworkSettings: &NetworkSettings{
|
|
||||||
IPAddress: ip,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestLinkNew(t *testing.T) {
|
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 := make(nat.PortSet)
|
||||||
|
|
||||||
ports[nat.Port("6379/tcp")] = struct{}{}
|
ports[nat.Port("6379/tcp")] = struct{}{}
|
||||||
|
|
||||||
from.Config.ExposedPorts = ports
|
link, err := NewLink("172.0.17.3", "172.0.17.2", "/db/docker", nil, ports, nil)
|
||||||
|
|
||||||
to := newMockLinkContainer(toID, "172.0.17.3")
|
|
||||||
|
|
||||||
link, err := NewLink(to, from, "/db/docker", nil)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
@ -60,21 +38,10 @@ func TestLinkNew(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestLinkEnv(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 := make(nat.PortSet)
|
||||||
|
|
||||||
ports[nat.Port("6379/tcp")] = struct{}{}
|
ports[nat.Port("6379/tcp")] = struct{}{}
|
||||||
|
|
||||||
from.Config.ExposedPorts = ports
|
link, err := NewLink("172.0.17.3", "172.0.17.2", "/db/docker", []string{"PASSWORD=gordon"}, ports, nil)
|
||||||
|
|
||||||
to := newMockLinkContainer(toID, "172.0.17.3")
|
|
||||||
|
|
||||||
link, err := NewLink(to, from, "/db/docker", nil)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
Loading…
Reference in New Issue