From 04db93c77014ec838f0651d1051da0d73802b9d2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?nils=20m=C3=A5s=C3=A9n?= Date: Sat, 22 Jan 2022 17:40:37 +0100 Subject: [PATCH] fix: add missing portmap when needed (#1183) --- pkg/container/container.go | 33 +++++++++++++++++---------------- pkg/container/container_test.go | 8 +++++--- 2 files changed, 22 insertions(+), 19 deletions(-) diff --git a/pkg/container/container.go b/pkg/container/container.go index 666f33bd5..82ae20572 100644 --- a/pkg/container/container.go +++ b/pkg/container/container.go @@ -10,6 +10,7 @@ import ( "github.com/docker/docker/api/types" dockercontainer "github.com/docker/docker/api/types/container" + "github.com/docker/go-connections/nat" ) // NewContainer returns a new Container instance instantiated with the @@ -191,24 +192,22 @@ func (c Container) PreUpdateTimeout() int { // PostUpdateTimeout checks whether a container has a specific timeout set // for how long the post-update command is allowed to run. This value is expressed - // either as an integer, in minutes, or as 0 which will allow the command/script - // to run indefinitely. Users should be cautious with the 0 option, as that - // could result in watchtower waiting forever. - func (c Container) PostUpdateTimeout() int { - var minutes int - var err error - - val := c.getLabelValueOrEmpty(postUpdateTimeoutLabel) - - minutes, err = strconv.Atoi(val) - if err != nil || val == "" { - return 1 - } +// either as an integer, in minutes, or as 0 which will allow the command/script +// to run indefinitely. Users should be cautious with the 0 option, as that +// could result in watchtower waiting forever. +func (c Container) PostUpdateTimeout() int { + var minutes int + var err error - return minutes - } + val := c.getLabelValueOrEmpty(postUpdateTimeoutLabel) + minutes, err = strconv.Atoi(val) + if err != nil || val == "" { + return 1 + } + return minutes +} // StopSignal returns the custom stop signal (if any) that is encoded in the // container's metadata. If the container has not specified a custom stop @@ -319,8 +318,10 @@ func (c Container) VerifyConfiguration() error { return errorInvalidConfig } + // Instead of returning an error here, we just create an empty map + // This should allow for updating containers where the exposed ports are missing if len(hostConfig.PortBindings) > 0 && containerConfig.ExposedPorts == nil { - return errorNoExposedPorts + containerConfig.ExposedPorts = make(map[nat.Port]struct{}) } return nil diff --git a/pkg/container/container_test.go b/pkg/container/container_test.go index 03658bacb..1a4e956cc 100644 --- a/pkg/container/container_test.go +++ b/pkg/container/container_test.go @@ -50,11 +50,13 @@ var _ = Describe("the container", func() { }) }) When("verifying a container with port bindings, but no exposed ports", func() { - It("should return an error", func() { + It("should make the config compatible with updating", func() { c := mockContainerWithPortBindings("80/tcp") c.containerInfo.Config.ExposedPorts = nil - err := c.VerifyConfiguration() - Expect(err).To(Equal(errorNoExposedPorts)) + Expect(c.VerifyConfiguration()).To(Succeed()) + + Expect(c.containerInfo.Config.ExposedPorts).ToNot(BeNil()) + Expect(c.containerInfo.Config.ExposedPorts).To(BeEmpty()) }) }) When("verifying a container with port bindings and exposed ports is non-nil", func() {