58 lines
1.5 KiB
Go
58 lines
1.5 KiB
Go
package client
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"strings"
|
|
|
|
"github.com/containers/image/docker"
|
|
"github.com/containers/image/types"
|
|
"github.com/docker/distribution/reference"
|
|
"github.com/docker/docker/client"
|
|
"github.com/sirupsen/logrus"
|
|
)
|
|
|
|
// GetRegistryTags retrieves all tags of an image from a container registry.
|
|
func GetRegistryTags(img reference.Named) ([]string, error) {
|
|
var tags []string
|
|
|
|
ref, err := docker.ParseReference(fmt.Sprintf("//%s", img))
|
|
if err != nil {
|
|
return tags, fmt.Errorf("failed to parse image %s, saw: %s", img, err.Error())
|
|
}
|
|
|
|
ctx := context.Background()
|
|
tags, err = docker.GetRepositoryTags(ctx, &types.SystemContext{}, ref)
|
|
if err != nil {
|
|
return tags, err
|
|
}
|
|
|
|
return tags, nil
|
|
}
|
|
|
|
// GetTagDigest retrieves an image digest from a container registry.
|
|
func GetTagDigest(cl *client.Client, image reference.Named) (string, error) {
|
|
target := fmt.Sprintf("//%s", reference.Path(image))
|
|
|
|
ref, err := docker.ParseReference(target)
|
|
if err != nil {
|
|
return "", fmt.Errorf("failed to parse image %s, saw: %s", image, err.Error())
|
|
}
|
|
|
|
ctx := context.Background()
|
|
img, err := ref.NewImage(ctx, nil)
|
|
if err != nil {
|
|
logrus.Debugf("failed to query remote registry for %s, saw: %s", image, err.Error())
|
|
return "", fmt.Errorf("unable to read digest for %s", image)
|
|
}
|
|
defer img.Close()
|
|
|
|
digest := img.ConfigInfo().Digest.String()
|
|
|
|
if digest == "" {
|
|
return digest, fmt.Errorf("unable to read digest for %s", image)
|
|
}
|
|
|
|
return strings.Split(digest, ":")[1][:7], nil
|
|
}
|