add http head based digest comparison to avoid dockerhub rate limits

This commit is contained in:
Simon Aronsson 2020-12-06 13:21:04 +01:00 committed by GitHub
parent c8bd484b9e
commit cb62b16369
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
23 changed files with 1476 additions and 57 deletions

View file

@ -0,0 +1,36 @@
package helpers
import (
"fmt"
url2 "net/url"
)
// 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()
return hostName, port, err
}
// NormalizeRegistry makes sure variations of DockerHubs registry
func NormalizeRegistry(registry string) (string, error) {
hostName, port, err := ConvertToHostname(registry)
if err != nil {
return "", err
}
if hostName == "registry-1.docker.io" || hostName == "docker.io" {
hostName = "index.docker.io"
}
if port != "" {
return fmt.Sprintf("%s:%s", hostName, port), nil
}
return hostName, nil
}

View file

@ -0,0 +1,31 @@
package helpers
import (
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"testing"
)
func TestHelpers(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Helper Suite")
}
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())
})
})
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"))
})
})
})