From b75dcd445848abad89e19f78193046a8de86c641 Mon Sep 17 00:00:00 2001 From: Daniel J Walsh Date: Fri, 15 Feb 2019 14:49:12 -0500 Subject: Add registry name to fields returned by varlink image search Cockpit team wants to list the registry name where the image was found. Also fix up SearchImages code to check if the user specified a registry in his call to use that rather then all the registries, This matches podman search command. Signed-off-by: Daniel J Walsh --- pkg/registries/registries.go | 16 ++++++++++++++++ pkg/varlinkapi/images.go | 17 +++++++++++++++-- 2 files changed, 31 insertions(+), 2 deletions(-) (limited to 'pkg') diff --git a/pkg/registries/registries.go b/pkg/registries/registries.go index cbb8b730c..9f4c94533 100644 --- a/pkg/registries/registries.go +++ b/pkg/registries/registries.go @@ -3,10 +3,12 @@ package registries import ( "os" "path/filepath" + "strings" "github.com/containers/image/pkg/sysregistries" "github.com/containers/image/types" "github.com/containers/libpod/pkg/rootless" + "github.com/docker/distribution/reference" "github.com/pkg/errors" ) @@ -49,3 +51,17 @@ func GetInsecureRegistries() ([]string, error) { } return registries, nil } + +// GetRegistry returns the registry name from a string if specified +func GetRegistry(image string) (string, error) { + // It is possible to only have the registry name in the format "myregistry/" + // if so, just trim the "/" from the end and return the registry name + if strings.HasSuffix(image, "/") { + return strings.TrimSuffix(image, "/"), nil + } + imgRef, err := reference.Parse(image) + if err != nil { + return "", err + } + return reference.Domain(imgRef.(reference.Named)), nil +} diff --git a/pkg/varlinkapi/images.go b/pkg/varlinkapi/images.go index b3090d2dd..8deb4cbe2 100644 --- a/pkg/varlinkapi/images.go +++ b/pkg/varlinkapi/images.go @@ -446,9 +446,21 @@ func (i *LibpodAPI) SearchImages(call iopodman.VarlinkCall, query string, limit if tlsVerify != nil { sc.DockerInsecureSkipTLSVerify = types.NewOptionalBool(!*tlsVerify) } - registries, err := sysreg.GetRegistries() + var registries []string + + // Check if search query has a registry in it + registry, err := sysreg.GetRegistry(query) if err != nil { - return call.ReplyErrorOccurred(fmt.Sprintf("unable to get system registries: %q", err)) + return call.ReplyErrorOccurred(fmt.Sprintf("error getting registry from %q: %q", query, err)) + } + if registry != "" { + registries = append(registries, registry) + query = query[len(registry)+1:] + } else { + registries, err = sysreg.GetRegistries() + if err != nil { + return call.ReplyErrorOccurred(fmt.Sprintf("unable to get system registries: %q", err)) + } } var imageResults []iopodman.ImageSearchResult for _, reg := range registries { @@ -468,6 +480,7 @@ func (i *LibpodAPI) SearchImages(call iopodman.VarlinkCall, query string, limit } for _, result := range results { i := iopodman.ImageSearchResult{ + Registry: reg, Description: result.Description, Is_official: result.IsOfficial, Is_automated: result.IsAutomated, -- cgit v1.2.3-54-g00ecf