Skip to content

Commit

Permalink
Add ability to specify registry port in image name
Browse files Browse the repository at this point in the history
Now registry.domain.com:8080/myteam/image is supported
  • Loading branch information
Alexey Miroshkin committed Sep 1, 2016
1 parent 77d718d commit c0b0cfa
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 8 deletions.
22 changes: 19 additions & 3 deletions docker/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"net/http"
"net/http/httputil"
"regexp"
"strconv"
"strings"
)

Expand Down Expand Up @@ -40,22 +41,37 @@ func NewImage(qname, user, password string) (*Image, error) {
registry := dockerHub
tag := "latest"
name := ""
port := ""

regIndex := strings.Index(qname, "/")
if regIndex != -1 {
regCandidate := qname[:regIndex]
portIndex := strings.Index(regCandidate, ":")
if portIndex != -1 {
var err error
port = regCandidate[portIndex+1:]
regCandidate = regCandidate[:portIndex]
_, err = strconv.Atoi(port)
if err != nil {
fmt.Printf("bad registry port value %q\n", port)
port = ""
}
}
addrs, err := net.LookupHost(regCandidate)
if err != nil || len(addrs) == 0 {
regIndex = -1
} else {
registry = regCandidate
if port != "" {
registry = fmt.Sprintf("%s:%s", registry, port)
}
}
}

tagIndex := strings.Index(qname, ":")
if tagIndex != -1 {
name = qname[regIndex+1 : tagIndex]
tagIndex := strings.LastIndex(qname, ":")
if tagIndex != -1 && tagIndex > regIndex {
tag = qname[tagIndex+1 : len(qname)]
name = qname[regIndex+1 : tagIndex]
} else {
name = qname[regIndex+1 : len(qname)]
}
Expand Down
23 changes: 18 additions & 5 deletions docker/docker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,30 @@ func TestNewImage(t *testing.T) {
tag string
}{
"full": {
image: "docker-registry.optiopay.com/nginx:1b29e1531c",
registry: "https://docker-registry.optiopay.com/v2",
image: "docker-registry.domain.com:8080/nginx:1b29e1531c",
registry: "https://docker-registry.domain.com:8080/v2",
name: "nginx",
tag: "1b29e1531c",
},
"regular": {
image: "docker-registry.domain.com/nginx:1b29e1531c",
registry: "https://docker-registry.domain.com/v2",
name: "nginx",
tag: "1b29e1531c",
},
"no_tag": {
image: "docker-registry.optiopay.com/nginx",
registry: "https://docker-registry.optiopay.com/v2",
image: "docker-registry.domain.com/nginx",
registry: "https://docker-registry.domain.com/v2",
name: "nginx",
tag: "latest",
},
"no_tag_with_port": {
image: "docker-registry.domain.com:8080/nginx",
registry: "https://docker-registry.domain.com:8080/v2",
name: "nginx",
tag: "latest",
},

"no_registry": {
image: "skynetservices/skydns:2.3",
registry: "https://registry-1.docker.io/v2",
Expand Down Expand Up @@ -68,7 +81,7 @@ func TestPull(t *testing.T) {
}))
defer ts.Close()

image, err := NewImage("docker-registry.optiopay.com/nginx:1b29e1531c", "", "")
image, err := NewImage("docker-registry.domain.com/nginx:1b29e1531c", "", "")
image.Registry = ts.URL
err = image.Pull()
if err != nil {
Expand Down

0 comments on commit c0b0cfa

Please sign in to comment.