From 533bfc0e8b501a4e92626919e53258a8f086852b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?nils=20ma=CC=8Ase=CC=81n?= Date: Sun, 12 Mar 2023 10:44:06 +0100 Subject: [PATCH] fix: always add missing slashes to link names previously, this was only done when marking dependant containers for restart. but since the names should ALWAYS start with a slash, this check could be performed every time the container is iterated. the only downside to this is that it's less performant (as it needs to copy the strings instead of just having the slices point to the same source string), but it should not be noticable. --- internal/actions/update.go | 5 ----- pkg/container/container.go | 9 ++++++++- pkg/container/container_test.go | 11 +++++++++-- 3 files changed, 17 insertions(+), 8 deletions(-) diff --git a/internal/actions/update.go b/internal/actions/update.go index bd3791f..baae47f 100644 --- a/internal/actions/update.go +++ b/internal/actions/update.go @@ -2,7 +2,6 @@ package actions import ( "errors" - "strings" "github.com/containrrr/watchtower/internal/util" "github.com/containrrr/watchtower/pkg/container" @@ -260,10 +259,6 @@ func UpdateImplicitRestart(containers []container.Container) { // container marked for restart func linkedContainerMarkedForRestart(links []string, containers []container.Container) string { for _, linkName := range links { - // Since the container names need to start with '/', let's prepend it if it's missing - if !strings.HasPrefix(linkName, "/") { - linkName = "/" + linkName - } for _, candidate := range containers { if candidate.Name() == linkName && candidate.ToRestart() { return linkName diff --git a/pkg/container/container.go b/pkg/container/container.go index 36abe8c..b52cdf6 100644 --- a/pkg/container/container.go +++ b/pkg/container/container.go @@ -160,7 +160,14 @@ func (c Container) Links() []string { dependsOnLabelValue := c.getLabelValueOrEmpty(dependsOnLabel) if dependsOnLabelValue != "" { - links := strings.Split(dependsOnLabelValue, ",") + for _, link := range strings.Split(dependsOnLabelValue, ",") { + // Since the container names need to start with '/', let's prepend it if it's missing + if !strings.HasPrefix(link, "/") { + link = "/" + link + } + links = append(links, link) + } + return links } diff --git a/pkg/container/container_test.go b/pkg/container/container_test.go index 46cf658..b8d76b0 100644 --- a/pkg/container/container_test.go +++ b/pkg/container/container_test.go @@ -178,14 +178,21 @@ var _ = Describe("the container", func() { "com.centurylinklabs.watchtower.depends-on": "postgres", })) links := c.Links() - Expect(links).To(SatisfyAll(ContainElement("postgres"), HaveLen(1))) + Expect(links).To(SatisfyAll(ContainElement("/postgres"), HaveLen(1))) }) It("should fetch depending containers if there are many", func() { c = MockContainer(WithLabels(map[string]string{ "com.centurylinklabs.watchtower.depends-on": "postgres,redis", })) links := c.Links() - Expect(links).To(SatisfyAll(ContainElement("postgres"), ContainElement("redis"), HaveLen(2))) + Expect(links).To(SatisfyAll(ContainElement("/postgres"), ContainElement("/redis"), HaveLen(2))) + }) + It("should only add slashes to names when they are missing", func() { + c = MockContainer(WithLabels(map[string]string{ + "com.centurylinklabs.watchtower.depends-on": "/postgres,redis", + })) + links := c.Links() + Expect(links).To(SatisfyAll(ContainElement("/postgres"), ContainElement("/redis"))) }) It("should fetch depending containers if label is blank", func() { c = MockContainer(WithLabels(map[string]string{