Skip to content

Commit

Permalink
fix: always add missing slashes to link names (#1588)
Browse files Browse the repository at this point in the history
  • Loading branch information
piksel authored Mar 12, 2023
1 parent bbbe041 commit 9470bf8
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 8 deletions.
5 changes: 0 additions & 5 deletions internal/actions/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package actions

import (
"errors"
"strings"

"github.com/containrrr/watchtower/internal/util"
"github.com/containrrr/watchtower/pkg/container"
Expand Down Expand Up @@ -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
Expand Down
9 changes: 8 additions & 1 deletion pkg/container/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
11 changes: 9 additions & 2 deletions pkg/container/container_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down

0 comments on commit 9470bf8

Please sign in to comment.