abra/pkg/client/registry.go

58 lines
1.5 KiB
Go
Raw Normal View History

2021-08-06 13:40:23 +00:00
package client
import (
"context"
"fmt"
2021-08-10 10:21:42 +00:00
"strings"
"github.com/containers/image/docker"
"github.com/containers/image/types"
2021-08-10 10:21:42 +00:00
"github.com/docker/distribution/reference"
2021-12-12 00:52:28 +00:00
"github.com/docker/docker/client"
"github.com/sirupsen/logrus"
2021-08-06 13:40:23 +00:00
)
// 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))
2021-08-10 10:21:42 +00:00
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)
2021-08-10 10:21:42 +00:00
if err != nil {
return tags, err
2021-08-10 10:21:42 +00:00
}
return tags, nil
2021-08-10 10:21:42 +00:00
}
// 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))
2021-08-10 10:21:42 +00:00
ref, err := docker.ParseReference(target)
2021-08-10 10:21:42 +00:00
if err != nil {
return "", fmt.Errorf("failed to parse image %s, saw: %s", image, err.Error())
2021-08-10 10:21:42 +00:00
}
ctx := context.Background()
img, err := ref.NewImage(ctx, nil)
2021-08-10 10:21:42 +00:00
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)
2021-08-10 10:21:42 +00:00
}
defer img.Close()
2021-08-10 10:21:42 +00:00
digest := img.ConfigInfo().Digest.String()
2021-08-10 10:21:42 +00:00
if digest == "" {
return digest, fmt.Errorf("unable to read digest for %s", image)
2021-08-10 10:21:42 +00:00
}
return strings.Split(digest, ":")[1][:7], nil
2021-08-10 10:21:42 +00:00
}