summaryrefslogtreecommitdiff
path: root/distribution/pull_v2_unix.go
blob: 1c1524bd15af92d002d70dfe04bb065f89b4a249 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
//go:build !windows
// +build !windows

package distribution // import "github.com/docker/docker/distribution"

import (
	"context"
	"sort"

	"github.com/containerd/containerd/platforms"
	"github.com/docker/distribution"
	"github.com/docker/distribution/manifest/manifestlist"
	ocispec "github.com/opencontainers/image-spec/specs-go/v1"
	"github.com/sirupsen/logrus"
)

func (ld *layerDescriptor) open(ctx context.Context) (distribution.ReadSeekCloser, error) {
	blobs := ld.repo.Blobs(ctx)
	return blobs.Open(ctx, ld.digest)
}

func filterManifests(manifests []manifestlist.ManifestDescriptor, p ocispec.Platform) []manifestlist.ManifestDescriptor {
	p = platforms.Normalize(withDefault(p))
	m := platforms.Only(p)
	var matches []manifestlist.ManifestDescriptor
	for _, desc := range manifests {
		descP := toOCIPlatform(desc.Platform)
		if descP == nil || m.Match(*descP) {
			matches = append(matches, desc)
			if descP != nil {
				logrus.Debugf("found match for %s with media type %s, digest %s", platforms.Format(p), desc.MediaType, desc.Digest.String())
			}
		}
	}

	sort.SliceStable(matches, func(i, j int) bool {
		p1 := toOCIPlatform(matches[i].Platform)
		if p1 == nil {
			return false
		}
		p2 := toOCIPlatform(matches[j].Platform)
		if p2 == nil {
			return true
		}
		return m.Less(*p1, *p2)
	})

	return matches
}

// checkImageCompatibility is a Windows-specific function. No-op on Linux
func checkImageCompatibility(imageOS, imageOSVersion string) error {
	return nil
}

func withDefault(p ocispec.Platform) ocispec.Platform {
	def := maximumSpec()
	if p.OS == "" {
		p.OS = def.OS
	}
	if p.Architecture == "" {
		p.Architecture = def.Architecture
		p.Variant = def.Variant
	}
	return p
}

func formatPlatform(platform ocispec.Platform) string {
	if platform.OS == "" {
		platform = platforms.DefaultSpec()
	}
	return platforms.Format(platform)
}