From ad3aa2d42813917fb326d78663f4f14699ebb29e Mon Sep 17 00:00:00 2001 From: Michael Sauter Date: Sat, 2 Dec 2017 19:50:52 +0100 Subject: [PATCH] Start default network automatically It was not setup properly because Networks() did not return the default network config, it only added it on the fly to connect. --- CHANGELOG.md | 2 ++ crane/container.go | 11 ++++++----- crane/container_test.go | 11 +++++++++++ 3 files changed, 19 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 703fbb0..7f4440e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,8 @@ ## Unreleased +* [Bugfix] Create default network automatically + ## 3.3.2 (2017-11-21) * Remove broken flag `crane start --attach`. This flag was introduced in 3.3.0 but did not work properly, and doesn't add much value anyway. It would make more sense to have this flag on `crane run`, but that is left for another version. diff --git a/crane/container.go b/crane/container.go index 6ea471b..a1927d7 100644 --- a/crane/container.go +++ b/crane/container.go @@ -671,6 +671,12 @@ func (c *container) Networks() map[string]NetworkParameters { panic(StatusError{fmt.Errorf("unknown type: %v", value), 65}) } } + + if cfg.Network("default") != nil { + if _, ok := c.networks["default"]; !ok { + c.networks["default"] = NetworkParameters{} + } + } } return c.networks } @@ -973,11 +979,6 @@ func (c *container) Run(cmds []string, targeted bool, detachFlag bool) { // using the non-prefixed name as an alias func (c *container) connectWithNetworks(adHoc bool) { containerNetworks := c.Networks() - if cfg.Network("default") != nil { - if _, ok := containerNetworks["default"]; !ok { - containerNetworks["default"] = NetworkParameters{} - } - } for name, params := range containerNetworks { networkName := cfg.Network(name).ActualName() args := []string{"network", "connect"} diff --git a/crane/container_test.go b/crane/container_test.go index 30b50d4..6fab5a9 100644 --- a/crane/container_test.go +++ b/crane/container_test.go @@ -214,6 +214,17 @@ func TestActualNet(t *testing.T) { assert.Equal(t, "qux_bar", c.ActualNet()) } +func TestNetworks(t *testing.T) { + var c *container + // automatically has default network configured for every container + c = &container{RawName: "foo"} + cfg = &config{ + containerMap: map[string]Container{"foo": c}, + networkMap: map[string]Network{"default": &network{RawName: "default"}}, + } + assert.Contains(t, c.Networks(), "default") +} + func TestCmd(t *testing.T) { var c *container // String