Refactoring code around image ref generation

Signed-off-by: Nishant Totla <nishanttotla@gmail.com>
This commit is contained in:
Nishant Totla 2016-05-26 10:17:53 -07:00
parent 8a29fad06f
commit e782541d99
No known key found for this signature in database
GPG Key ID: 7EA5781C9B3D0C19
7 changed files with 32 additions and 42 deletions

View File

@ -678,14 +678,8 @@ func postImagesCreate(c *context, w http.ResponseWriter, r *http.Request) {
if err == nil {
json.Unmarshal(buf, &authConfig)
}
if tag := r.Form.Get("tag"); tag != "" {
if tagHasDigest(tag) {
image += "@" + tag
} else {
image += ":" + tag
}
}
tag := r.Form.Get("tag")
image := getImageRef(image, tag)
var errorMessage string
errorFound := false
@ -724,7 +718,8 @@ func postImagesCreate(c *context, w http.ResponseWriter, r *http.Request) {
}
sendJSONMessage(wf, what, status)
}
c.cluster.Import(source, repo, tag, r.Body, callback)
ref := getImageRef(repo, tag)
c.cluster.Import(source, ref, tag, r.Body, callback)
if errorFound {
sendErrorJSONMessage(wf, 1, errorMessage)
}
@ -1178,8 +1173,9 @@ func postTagImage(c *context, w http.ResponseWriter, r *http.Request) {
tag := r.Form.Get("tag")
force := boolValue(r, "force")
ref := getImageRef(repo, tag)
// call cluster tag image
if err := c.cluster.TagImage(name, repo, tag, force); err != nil {
if err := c.cluster.TagImage(name, ref, force); err != nil {
if strings.HasPrefix(err.Error(), "No such image") {
httpError(w, err.Error(), http.StatusNotFound)
return

View File

@ -320,3 +320,17 @@ func int64ValueOrZero(r *http.Request, k string) int64 {
func tagHasDigest(tag string) bool {
return strings.Contains(tag, ":")
}
// TODO(nishanttotla): There might be a better way to pass a ref string than construct it here
// getImageRef returns a string containing the registry reference given a repo and tag
func getImageRef(repo, tag string) string {
ref := repo
if tag != "" {
if tagHasDigest(tag) {
ref += "@" + tag
} else {
ref += ":" + tag
}
}
return ref
}

View File

@ -63,7 +63,7 @@ type Cluster interface {
// `callback` can be called multiple time
// `where` is where it is being imported
// `status` is the current status, like "", "in progress" or "imported"
Import(source string, repository string, tag string, imageReader io.Reader, callback func(where, status string, err error))
Import(source string, ref string, tag string, imageReader io.Reader, callback func(where, status string, err error))
// Load images
// `callback` can be called multiple time
@ -98,5 +98,5 @@ type Cluster interface {
BuildImage(io.Reader, *types.ImageBuildOptions, io.Writer) error
// Tag an image
TagImage(IDOrName string, repo string, tag string, force bool) error
TagImage(IDOrName string, ref string, force bool) error
}

View File

@ -1045,7 +1045,7 @@ func (e *Engine) Load(reader io.Reader) error {
}
// Import image
func (e *Engine) Import(source string, repository string, tag string, imageReader io.Reader) error {
func (e *Engine) Import(source string, ref string, tag string, imageReader io.Reader) error {
importSrc := types.ImageImportSource{
Source: imageReader,
SourceName: source,
@ -1053,17 +1053,7 @@ func (e *Engine) Import(source string, repository string, tag string, imageReade
opts := types.ImageImportOptions{
Tag: tag,
}
// TODO(nishanttotla): There might be a better way to pass a ref string than construct it here
// generate ref string
ref := repository
if tag != "" {
if strings.Contains(tag, ":") {
ref += "@" + tag
} else {
ref += ":" + tag
}
}
_, err := e.apiClient.ImageImport(context.Background(), importSrc, ref, opts)
e.CheckConnectionErr(err)
if err != nil {
@ -1281,22 +1271,12 @@ func (e *Engine) BuildImage(buildContext io.Reader, buildImage *types.ImageBuild
}
// TagImage tags an image
func (e *Engine) TagImage(IDOrName string, repo string, tag string, force bool) error {
func (e *Engine) TagImage(IDOrName string, ref string, force bool) error {
// send tag request to docker engine
opts := types.ImageTagOptions{
Force: force,
}
// TODO(nishanttotla): There might be a better way to pass a ref string than construct it here
// generate ref string
ref := repo
if tag != "" {
if strings.Contains(tag, ":") {
ref += "@" + tag
} else {
ref += ":" + tag
}
}
err := e.apiClient.ImageTag(context.Background(), IDOrName, ref, opts)
e.CheckConnectionErr(err)
if err != nil {

View File

@ -375,7 +375,7 @@ func (c *Cluster) Load(imageReader io.Reader, callback func(where, status string
}
// Import image
func (c *Cluster) Import(source string, repository string, tag string, imageReader io.Reader, callback func(what, status string, err error)) {
func (c *Cluster) Import(source string, ref string, tag string, imageReader io.Reader, callback func(what, status string, err error)) {
}
@ -675,6 +675,6 @@ func (c *Cluster) BuildImage(buildContext io.Reader, buildImage *types.ImageBuil
}
// TagImage tags an image
func (c *Cluster) TagImage(IDOrName string, repo string, tag string, force bool) error {
func (c *Cluster) TagImage(IDOrName string, ref string, force bool) error {
return errNotSupported
}

View File

@ -663,7 +663,7 @@ func (c *Cluster) Load(imageReader io.Reader, callback func(where, status string
}
// Import image
func (c *Cluster) Import(source string, repository string, tag string, imageReader io.Reader, callback func(what, status string, err error)) {
func (c *Cluster) Import(source string, ref string, tag string, imageReader io.Reader, callback func(what, status string, err error)) {
var wg sync.WaitGroup
c.RLock()
pipeWriters := []*io.PipeWriter{}
@ -679,7 +679,7 @@ func (c *Cluster) Import(source string, repository string, tag string, imageRead
defer reader.Close()
// call engine import
err := engine.Import(source, repository, tag, reader)
err := engine.Import(source, ref, tag, reader)
if callback != nil {
if err != nil {
callback(engine.Name, "", err)
@ -932,7 +932,7 @@ func (c *Cluster) BuildImage(buildContext io.Reader, buildImage *types.ImageBuil
}
// TagImage tags an image
func (c *Cluster) TagImage(IDOrName string, repo string, tag string, force bool) error {
func (c *Cluster) TagImage(IDOrName string, ref string, force bool) error {
c.RLock()
defer c.RUnlock()
@ -943,7 +943,7 @@ func (c *Cluster) TagImage(IDOrName string, repo string, tag string, force bool)
for _, image := range e.Images() {
if image.Match(IDOrName, true) {
found = true
err := image.Engine.TagImage(IDOrName, repo, tag, force)
err := image.Engine.TagImage(IDOrName, ref, force)
if err != nil {
errs = append(errs, fmt.Sprintf("%s: %s", image.Engine.Name, err.Error()))
continue

View File

@ -265,6 +265,6 @@ func TestTagImage(t *testing.T) {
// tag image
apiClient.On("ImageTag", mock.Anything, mock.Anything, mock.Anything, mock.AnythingOfType("types.ImageTagOptions")).Return(nil).Once()
assert.Nil(t, c.TagImage("busybox", "test_busybox", "latest", false))
assert.NotNil(t, c.TagImage("busybox_not_exists", "test_busybox", "latest", false))
assert.Nil(t, c.TagImage("busybox", "test_busybox:latest", false))
assert.NotNil(t, c.TagImage("busybox_not_exists", "test_busybox:latest", false))
}