From fe0e34e8577026feac6b3d3a42dcc80a4c190043 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tanguy=20=E2=A7=93=20Herrmann?= Date: Sat, 17 Dec 2016 22:50:36 +0100 Subject: [PATCH 1/4] Reuse the network config for the relaunch --- container/client.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/container/client.go b/container/client.go index cf2e589..86e1719 100644 --- a/container/client.go +++ b/container/client.go @@ -8,6 +8,7 @@ import ( log "github.com/Sirupsen/logrus" dockerclient "github.com/docker/docker/client" "github.com/docker/docker/api/types" + "github.com/docker/docker/api/types/network" "golang.org/x/net/context" ) @@ -118,10 +119,11 @@ func (client dockerClient) StartContainer(c Container) error { bg := context.Background(); config := c.runtimeConfig() hostConfig := c.hostConfig() + networkConfig := &network.NetworkingConfig{EndpointsConfig: c.containerInfo.NetworkSettings.Networks} name := c.Name() log.Infof("Starting %s", name) - creation, err := client.api.ContainerCreate(bg, config, hostConfig, nil, name) + creation, err := client.api.ContainerCreate(bg, config, hostConfig, networkConfig, name) if err != nil { return err } From f8a2f80b92da3beae08efd84d2f024cefe738da8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tanguy=20=E2=A7=93=20Herrmann?= Date: Sat, 17 Dec 2016 22:52:54 +0100 Subject: [PATCH 2/4] Make an updated container connects to all the previously connected net With insights from https://github.com/docker/docker/issues/29265 the behaviour is the same as the one from docker-compose * connect to 1 network (at random) at start * disconnect from that network * reconnect to all the network from the previous configuration --- container/client.go | 35 +++++++++++++++++++++++++++++++++-- 1 file changed, 33 insertions(+), 2 deletions(-) diff --git a/container/client.go b/container/client.go index 86e1719..8996c09 100644 --- a/container/client.go +++ b/container/client.go @@ -120,17 +120,48 @@ func (client dockerClient) StartContainer(c Container) error { config := c.runtimeConfig() hostConfig := c.hostConfig() networkConfig := &network.NetworkingConfig{EndpointsConfig: c.containerInfo.NetworkSettings.Networks} + // simpleNetworkConfig is a networkConfig with only 1 network. + // see: https://github.com/docker/docker/issues/29265 + simpleNetworkConfig := func() *network.NetworkingConfig { + oneEndpoint := make(map[string]*network.EndpointSettings) + for k, v := range networkConfig.EndpointsConfig { + oneEndpoint[k] = v + // we only need 1 + break + } + return &network.NetworkingConfig{EndpointsConfig: oneEndpoint} + }() + name := c.Name() log.Infof("Starting %s", name) - creation, err := client.api.ContainerCreate(bg, config, hostConfig, networkConfig, name) + creation, err := client.api.ContainerCreate(bg, config, hostConfig, simpleNetworkConfig, name) if err != nil { return err } log.Debugf("Starting container %s (%s)", name, creation.ID) - return client.api.ContainerStart(bg, creation.ID, types.ContainerStartOptions{}) + err = client.api.ContainerStart(bg, creation.ID, types.ContainerStartOptions{}) + if err != nil { + return err + } + + for k, _ := range simpleNetworkConfig.EndpointsConfig { + err = client.api.NetworkDisconnect(bg, k, creation.ID, true) + if err != nil { + return err + } + } + + for k, v := range networkConfig.EndpointsConfig { + err = client.api.NetworkConnect(bg, k, creation.ID, v) + if err != nil { + return err + } + } + return nil + } func (client dockerClient) RenameContainer(c Container, newName string) error { From 4909b90662d51c590eef9bc2ba7690464436da85 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tanguy=20=E2=A7=93=20Herrmann?= Date: Sat, 17 Dec 2016 22:55:45 +0100 Subject: [PATCH 3/4] go fmt done! --- container/client.go | 13 ++++++------- container/trust.go | 8 ++++---- container/util.go | 4 ---- container/util_test.go | 1 - 4 files changed, 10 insertions(+), 16 deletions(-) diff --git a/container/client.go b/container/client.go index 8996c09..233e753 100644 --- a/container/client.go +++ b/container/client.go @@ -2,13 +2,13 @@ package container import ( "fmt" - "time" "io/ioutil" + "time" log "github.com/Sirupsen/logrus" - dockerclient "github.com/docker/docker/client" "github.com/docker/docker/api/types" "github.com/docker/docker/api/types/network" + dockerclient "github.com/docker/docker/client" "golang.org/x/net/context" ) @@ -77,7 +77,7 @@ func (client dockerClient) ListContainers(fn Filter) ([]Container, error) { } c := Container{containerInfo: &containerInfo, imageInfo: &imageInfo} - if (fn(c)) { + if fn(c) { cs = append(cs, c) } } @@ -116,7 +116,7 @@ func (client dockerClient) StopContainer(c Container, timeout time.Duration) err } func (client dockerClient) StartContainer(c Container) error { - bg := context.Background(); + bg := context.Background() config := c.runtimeConfig() hostConfig := c.hostConfig() networkConfig := &network.NetworkingConfig{EndpointsConfig: c.containerInfo.NetworkSettings.Networks} @@ -178,7 +178,7 @@ func (client dockerClient) IsContainerStale(c Container) (bool, error) { if client.pullImages { log.Debugf("Pulling %s for %s", imageName, c.Name()) - + var opts types.ImagePullOptions // ImagePullOptions can take a RegistryAuth arg to authenticate against a private registry auth, err := EncodedAuth(imageName) if err != nil { @@ -197,7 +197,7 @@ func (client dockerClient) IsContainerStale(c Container) (bool, error) { return false, err } defer response.Close() - + // the pull request will be aborted prematurely unless the response is read _, err = ioutil.ReadAll(response) } @@ -213,7 +213,6 @@ func (client dockerClient) IsContainerStale(c Container) (bool, error) { } else { log.Debugf("No new images found for %s", c.Name()) } - return false, nil } diff --git a/container/trust.go b/container/trust.go index 6871333..db87d84 100644 --- a/container/trust.go +++ b/container/trust.go @@ -2,8 +2,6 @@ package container import ( "errors" - "os" - "strings" log "github.com/Sirupsen/logrus" "github.com/docker/docker/api/types" "github.com/docker/docker/api/types/reference" @@ -11,6 +9,8 @@ import ( "github.com/docker/docker/cliconfig" "github.com/docker/docker/cliconfig/configfile" "github.com/docker/docker/cliconfig/credentials" + "os" + "strings" ) /** @@ -35,7 +35,7 @@ func EncodedEnvAuth(ref string) (string, error) { username := os.Getenv("REPO_USER") password := os.Getenv("REPO_PASS") if username != "" && password != "" { - auth := types.AuthConfig { + auth := types.AuthConfig{ Username: username, Password: password, } @@ -80,7 +80,7 @@ func ParseServerAddress(ref string) (string, error) { } parts := strings.Split(repository, "/") return parts[0], nil - + } // CredentialsStore returns a new credentials store based diff --git a/container/util.go b/container/util.go index 8947c82..3db01d1 100644 --- a/container/util.go +++ b/container/util.go @@ -1,8 +1,5 @@ package container -import ( -) - func sliceEqual(s1, s2 []string) bool { if len(s1) != len(s2) { return false @@ -65,4 +62,3 @@ func structMapSubtract(m1, m2 map[string]struct{}) map[string]struct{} { return m } - diff --git a/container/util_test.go b/container/util_test.go index 9ed551d..c4d5ca7 100644 --- a/container/util_test.go +++ b/container/util_test.go @@ -63,4 +63,3 @@ func TestStructMapSubtract(t *testing.T) { assert.Equal(t, map[string]struct{}{"a": x, "b": x, "c": x}, m1) assert.Equal(t, map[string]struct{}{"a": x, "c": x}, m2) } - From b45fc5a9bad2e4889534a5190acb9f6d9c527e35 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tanguy=20=E2=A7=93=20Herrmann?= Date: Mon, 2 Jan 2017 16:19:51 +0100 Subject: [PATCH 4/4] Fix comment from HoundCI https://github.com/CenturyLinkLabs/watchtower/pull/40#discussion_r94326156 --- container/client.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/container/client.go b/container/client.go index 233e753..3401c1c 100644 --- a/container/client.go +++ b/container/client.go @@ -147,7 +147,7 @@ func (client dockerClient) StartContainer(c Container) error { return err } - for k, _ := range simpleNetworkConfig.EndpointsConfig { + for k := range simpleNetworkConfig.EndpointsConfig { err = client.api.NetworkDisconnect(bg, k, creation.ID, true) if err != nil { return err