mirror of
https://github.com/containrrr/watchtower.git
synced 2025-12-13 21:56:38 +01:00
fix(registry): image name parsing behavior (#1526)
Co-authored-by: nils måsén <nils@piksel.se>
This commit is contained in:
parent
aa50d12389
commit
25fdb40312
12 changed files with 249 additions and 294 deletions
|
|
@ -1,36 +1,28 @@
|
|||
package helpers
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
url2 "net/url"
|
||||
"github.com/docker/distribution/reference"
|
||||
)
|
||||
|
||||
// ConvertToHostname strips a url from everything but the hostname part
|
||||
func ConvertToHostname(url string) (string, string, error) {
|
||||
urlWithSchema := fmt.Sprintf("x://%s", url)
|
||||
u, err := url2.Parse(urlWithSchema)
|
||||
if err != nil {
|
||||
return "", "", err
|
||||
}
|
||||
hostName := u.Hostname()
|
||||
port := u.Port()
|
||||
// domains for Docker Hub, the default registry
|
||||
const (
|
||||
DefaultRegistryDomain = "docker.io"
|
||||
DefaultRegistryHost = "index.docker.io"
|
||||
LegacyDefaultRegistryDomain = "index.docker.io"
|
||||
)
|
||||
|
||||
return hostName, port, err
|
||||
}
|
||||
|
||||
// NormalizeRegistry makes sure variations of DockerHubs registry
|
||||
func NormalizeRegistry(registry string) (string, error) {
|
||||
hostName, port, err := ConvertToHostname(registry)
|
||||
// GetRegistryAddress parses an image name
|
||||
// and returns the address of the specified registry
|
||||
func GetRegistryAddress(imageRef string) (string, error) {
|
||||
normalizedRef, err := reference.ParseNormalizedNamed(imageRef)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
if hostName == "registry-1.docker.io" || hostName == "docker.io" {
|
||||
hostName = "index.docker.io"
|
||||
}
|
||||
address := reference.Domain(normalizedRef)
|
||||
|
||||
if port != "" {
|
||||
return fmt.Sprintf("%s:%s", hostName, port), nil
|
||||
if address == DefaultRegistryDomain {
|
||||
address = DefaultRegistryHost
|
||||
}
|
||||
return hostName, nil
|
||||
return address, nil
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,9 +1,10 @@
|
|||
package helpers
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
. "github.com/onsi/ginkgo"
|
||||
. "github.com/onsi/gomega"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestHelpers(t *testing.T) {
|
||||
|
|
@ -12,20 +13,25 @@ func TestHelpers(t *testing.T) {
|
|||
}
|
||||
|
||||
var _ = Describe("the helpers", func() {
|
||||
|
||||
When("converting an url to a hostname", func() {
|
||||
It("should return docker.io given docker.io/containrrr/watchtower:latest", func() {
|
||||
host, port, err := ConvertToHostname("docker.io/containrrr/watchtower:latest")
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
Expect(host).To(Equal("docker.io"))
|
||||
Expect(port).To(BeEmpty())
|
||||
Describe("GetRegistryAddress", func() {
|
||||
It("should return error if passed empty string", func() {
|
||||
_, err := GetRegistryAddress("")
|
||||
Expect(err).To(HaveOccurred())
|
||||
})
|
||||
})
|
||||
When("normalizing the registry information", func() {
|
||||
It("should return index.docker.io given docker.io", func() {
|
||||
out, err := NormalizeRegistry("docker.io/containrrr/watchtower:latest")
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
Expect(out).To(Equal("index.docker.io"))
|
||||
It("should return index.docker.io for image refs with no explicit registry", func() {
|
||||
Expect(GetRegistryAddress("watchtower")).To(Equal("index.docker.io"))
|
||||
Expect(GetRegistryAddress("containrrr/watchtower")).To(Equal("index.docker.io"))
|
||||
})
|
||||
It("should return index.docker.io for image refs with docker.io domain", func() {
|
||||
Expect(GetRegistryAddress("docker.io/watchtower")).To(Equal("index.docker.io"))
|
||||
Expect(GetRegistryAddress("docker.io/containrrr/watchtower")).To(Equal("index.docker.io"))
|
||||
})
|
||||
It("should return the host if passed an image name containing a local host", func() {
|
||||
Expect(GetRegistryAddress("henk:80/watchtower")).To(Equal("henk:80"))
|
||||
Expect(GetRegistryAddress("localhost/watchtower")).To(Equal("localhost"))
|
||||
})
|
||||
It("should return the server address if passed a fully qualified image name", func() {
|
||||
Expect(GetRegistryAddress("github.com/containrrr/config")).To(Equal("github.com"))
|
||||
})
|
||||
})
|
||||
})
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue