diff options
author | baude <bbaude@redhat.com> | 2017-11-13 12:40:21 -0600 |
---|---|---|
committer | Atomic Bot <atomic-devel@projectatomic.io> | 2017-11-14 21:14:13 +0000 |
commit | 5cfd7a313fcae7c748b5bae84de779b28d4ea01b (patch) | |
tree | 310b7b4d63f989415d5dcf1696a127c2f3d0ce0a /libpod | |
parent | 7df322123290ee600812312421b98cfb97031e9f (diff) | |
download | podman-5cfd7a313fcae7c748b5bae84de779b28d4ea01b.tar.gz podman-5cfd7a313fcae7c748b5bae84de779b28d4ea01b.tar.bz2 podman-5cfd7a313fcae7c748b5bae84de779b28d4ea01b.zip |
Address run/create performance issues
Fixed the logic where we observed different performance
results when running an image by its fqname vs a shortname. In
the case of the latter, we resolve the name without using the
network.
Signed-off-by: baude <bbaude@redhat.com>
Closes: #37
Approved by: rhatdan
Diffstat (limited to 'libpod')
-rw-r--r-- | libpod/runtime_img.go | 80 | ||||
-rw-r--r-- | libpod/util.go | 10 |
2 files changed, 80 insertions, 10 deletions
diff --git a/libpod/runtime_img.go b/libpod/runtime_img.go index 8a5258e75..0fa003799 100644 --- a/libpod/runtime_img.go +++ b/libpod/runtime_img.go @@ -127,6 +127,7 @@ type Image struct { Transport string beenDecomposed bool PullName string + LocalName string } // NewImage creates a new image object based on its name @@ -139,9 +140,21 @@ func (r *Runtime) NewImage(name string) Image { // GetImageID returns the image ID of the image func (k *Image) GetImageID() (string, error) { + // If the ID field is already populated, then + // return it. if k.ID != "" { return k.ID, nil } + // If we have the name of the image locally, then + // get the image and returns its ID + if k.LocalName != "" { + img, err := k.runtime.GetImage(k.LocalName) + if err == nil { + return img.ID, nil + } + } + // If neither the ID is known and no local name + // is know, we search it out. image, _ := k.GetFQName() img, err := k.runtime.GetImage(image) if err != nil { @@ -270,26 +283,73 @@ func (k *Image) Decompose() error { return nil } -// HasImageLocal returns a bool true if the image is already pulled -func (k *Image) HasImageLocal() bool { +func getTags(nameInput string) (reference.NamedTagged, bool, error) { + inputRef, err := reference.Parse(nameInput) + if err != nil { + return nil, false, errors.Wrapf(err, "unable to obtain tag from input name") + } + tagged, isTagged := inputRef.(reference.NamedTagged) + + return tagged, isTagged, nil +} + +// GetLocalImageName returns the name of the image if it is local. +// It will return an empty string and error if not found. +func (k *Image) GetLocalImageName() (string, error) { _, err := k.runtime.GetImage(k.Name) - if err == nil { - return true + if err != nil { + return "", errors.Wrapf(err, "unable to obtain local image") } - fqname, _ := k.GetFQName() + localImages, err := k.runtime.GetImages(&ImageFilterParams{}) + if err != nil { + return "", errors.Wrapf(err, "unable to obtain local image") + } + _, isTagged, err := getTags(k.Name) + if err != nil { + return "", err + } + for _, image := range localImages { + for _, name := range image.Names { + imgRef, err := reference.Parse(name) + if err != nil { + continue + } + var imageName string + imageNameOnly := reference.Path(imgRef.(reference.Named)) + if isTagged { + imageNameTag, _, err := getTags(name) + if err != nil { + continue + } + imageName = fmt.Sprintf("%s:%s", imageNameOnly, imageNameTag.Tag()) + } else { + imageName = imageNameOnly + } - _, err = k.runtime.GetImage(fqname) - if err == nil { - return true + if imageName == k.Name { + return name, nil + } + imageSplit := strings.Split(imageName, "/") + baseName := imageSplit[len(imageSplit)-1] + if baseName == k.Name { + return name, nil + } + } } - return false + fqname, _ := k.GetFQName() + return fqname, nil } // HasLatest determines if we have the latest image local func (k *Image) HasLatest() (bool, error) { - if !k.HasImageLocal() { + localName, err := k.GetLocalImageName() + if err != nil { + return false, err + } + if localName == "" { return false, nil } + fqname, err := k.GetFQName() if err != nil { return false, err diff --git a/libpod/util.go b/libpod/util.go index 0270af07c..61546f23e 100644 --- a/libpod/util.go +++ b/libpod/util.go @@ -1,8 +1,10 @@ package libpod import ( + "fmt" "os" "path/filepath" + "time" ) // WriteFile writes a provided string to a provided path @@ -32,3 +34,11 @@ func StringInSlice(s string, sl []string) bool { } return false } + +// FuncTimer helps measure the execution time of a function +// For debug purposes, do not leave in code +// used like defer FuncTimer("foo") +func FuncTimer(funcName string) { + elapsed := time.Since(time.Now()) + fmt.Printf("%s executed in %d ms\n", funcName, elapsed) +} |