Example #1
0
// RegistryListTags returns the list of images instances obtained from all tags existing in the registry
func RegistryListTags(image *imagename.ImageName, auth *docker.AuthConfigurations) (images []*imagename.ImageName, err error) {
	var (
		name     = image.Name
		registry = image.Registry
	)

	regAuth, err := GetAuthForRegistry(auth, image)
	if err != nil {
		return nil, fmt.Errorf("Failed to get auth token for registry: %s, make sure you are properly logged in using `docker login` or have AWS credentials set in case of using ECR", image)
	}

	// XXX: AWS ECR Registry API v2 does not support listing tags
	// wo we just return a single image tag if it exists and no wildcards used
	if image.IsECR() {
		log.Debugf("ECR detected %s", registry)
		if !image.IsStrict() {
			return nil, fmt.Errorf("Amazon ECR does not support tags listing, therefore image wildcards are not supported, sorry: %s", image)
		}
		if exists, err := ecrImageExists(image, regAuth); err != nil {
			return nil, err
		} else if exists {
			log.Debugf("ECR image %s found in the registry", image)
			images = append(images, image)
		}
		return
	}

	if registry == "" {
		registry = "registry-1.docker.io"
		if !strings.Contains(name, "/") {
			name = "library/" + name
		}
	}

	var (
		tg  = tags{}
		url = fmt.Sprintf("https://%s/v2/%s/tags/list?page_size=9999&page=1", registry, name)
	)

	log.Debugf("Listing image tags from the remote registry %s", url)

	if err := registryGet(url, regAuth, &tg); err != nil {
		return nil, err
	}

	log.Debugf("Got %d tags from the remote registry for image %s", len(tg.Tags), image)

	for _, t := range tg.Tags {
		candidate := imagename.New(image.NameWithRegistry(), t)
		if image.Contains(candidate) || image.Tag == candidate.Tag {
			images = append(images, candidate)
		}
	}

	return
}
Example #2
0
// ListTags returns the list of parsed tags existing for given image name on S3
func (s *StorageS3) ListTags(imageName string) (images []*imagename.ImageName, err error) {
	image := imagename.NewFromString(imageName)

	params := &s3.ListObjectsInput{
		Bucket:  aws.String(image.Registry),
		MaxKeys: aws.Int64(1000),
		Prefix:  aws.String(image.Name),
	}

	resp, err := s.s3.ListObjects(params)
	if err != nil {
		return nil, err
	}

	for _, s3Obj := range resp.Contents {
		split := strings.Split(*s3Obj.Key, "/")
		if len(split) < 2 {
			continue
		}

		imgName := strings.Join(split[:len(split)-1], "/")
		imgName = fmt.Sprintf("s3.amazonaws.com/%s/%s", image.Registry, imgName)

		tag := strings.TrimSuffix(split[len(split)-1], ".tar")
		candidate := imagename.New(imgName, tag)

		if candidate.Name != image.Name {
			continue
		}

		if image.Contains(candidate) || image.Tag == candidate.Tag {
			images = append(images, candidate)
		}
	}

	return
}