added code for parsing Docker image names and returning owner, name and tag

This commit is contained in:
Brad Rydzewski 2014-03-23 23:15:36 -07:00
parent fb49a2c6e9
commit 1c5aebe9fb
2 changed files with 91 additions and 0 deletions

View file

@ -5,6 +5,7 @@ import (
"crypto/sha1" "crypto/sha1"
"fmt" "fmt"
"io" "io"
"strings"
) )
// createUID is a helper function that will // createUID is a helper function that will
@ -26,3 +27,57 @@ func createRandom() []byte {
} }
return k return k
} }
// list of service aliases and their full, canonical names
var defaultServices = map[string]string{
"cassandra": "relateiq/cassandra:latest",
"couchdb": "bradrydzewski/couchdb:1.5",
"elasticsearch": "bradrydzewski/elasticsearch:0.90",
"memcached": "bradrydzewski/memcached",
"mongodb": "bradrydzewski/mongodb:2.4",
"mysql": "bradrydzewski/mysql:5.5",
"neo4j": "bradrydzewski/neo4j:1.9",
"postgres": "bradrydzewski/postgres:9.1",
"redis": "bradrydzewski/redis:2.8",
"rabbitmq": "bradrydzewski/rabbitmq:3.2",
"riak": "guillermo/riak:latest",
"zookeeper": "jplock/zookeeper:3.4.5",
}
// parseImageName parses a Docker image name, in the format owner/name:tag,
// and returns each segment.
//
// If the owner is blank, it is assumed to be an official drone image,
// and will be prefixed with the appropriate owner name.
//
// If the tag is empty, it is assumed to be the latest version.
func parseImageName(image string) (owner, name, tag string) {
owner = "bradrydzewski" // this will eventually change to drone
name = image
tag = "latest"
// first we check to see if the image name is an alias
// for a known service.
//
// TODO I'm not a huge fan of this code here. Maybe it
// should get handled when the yaml is parsed, and
// convert the image and service names in the yaml
// to fully qualified names?
if cname, ok := defaultServices[image]; ok {
name = cname
}
parts := strings.Split(name, "/")
if len(parts) == 2 {
owner = parts[0]
name = parts[1]
}
parts = strings.Split(name, ":")
if len(parts) == 2 {
name = parts[0]
tag = parts[1]
}
return
}

36
pkg/build/util_test.go Normal file
View file

@ -0,0 +1,36 @@
package build
import "testing"
func TestParseImageName(t *testing.T) {
images := []struct {
owner string
name string
tag string
cname string
}{
// full image name with all 3 sections present
{"johnsmith", "redis", "2.8", "johnsmith/redis:2.8"},
// image name with no tag specified
{"johnsmith", "redis", "latest", "johnsmith/redis"},
// image name with no owner specified
{"bradrydzewski", "redis", "2.8", "redis:2.8"},
// image name with ownly name specified
{"bradrydzewski", "redis2", "latest", "redis2"},
// image name that is a known alias
{"relateiq", "cassandra", "latest", "cassandra"},
}
for _, img := range images {
owner, name, tag := parseImageName(img.cname)
if owner != img.owner {
t.Errorf("Expected image %s with owner %s, got %s", img.cname, img.owner, owner)
}
if name != img.name {
t.Errorf("Expected image %s with name %s, got %s", img.cname, img.name, name)
}
if tag != img.tag {
t.Errorf("Expected image %s with tag %s, got %s", img.cname, img.tag, tag)
}
}
}