This repository has been archived by the owner on Feb 8, 2021. It is now read-only.
/
fetch_digest.go
129 lines (116 loc) · 3.67 KB
/
fetch_digest.go
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package distribution
import (
"errors"
"time"
dockerdist "github.com/docker/distribution"
"github.com/docker/distribution/digest"
"github.com/docker/distribution/manifest/manifestlist"
"github.com/docker/distribution/manifest/schema1"
"github.com/docker/distribution/manifest/schema2"
"github.com/docker/distribution/registry/client"
mobydist "github.com/docker/docker/distribution"
"github.com/docker/docker/reference"
"github.com/docker/docker/registry"
"github.com/docker/engine-api/types"
"golang.org/x/net/context"
)
func GetDigestFromTag(svc *registry.Service, ref reference.Named, metaHeaders map[string][]string, authConfig *types.AuthConfig) (digest.Digest, error) {
var (
ok bool
err error
tagged reference.NamedTagged
ctx context.Context
canceller context.CancelFunc
endpoints []registry.APIEndpoint
repoInfo *registry.RepositoryInfo
dgst digest.Digest
)
tagged, ok = ref.(reference.NamedTagged)
if !ok {
return "", errors.New("not a tagged reference")
}
endpoints, repoInfo, err = lookupV2Endpoints(svc, ref)
if err != nil || len(endpoints) == 0 {
return "", err
}
ctx, canceller = context.WithTimeout(context.Background(), 2*time.Minute)
defer func() {
if err != nil {
canceller()
}
}()
for _, endpoint := range endpoints {
var manSrv dockerdist.ManifestService
manSrv, err = getSvcWithV2Endpoint(ctx, repoInfo, endpoint, metaHeaders, authConfig)
if err != nil {
err = nil
continue
}
dgst, err = getDigest(manSrv, ctx, tagged.Tag())
if err != nil {
err = nil
continue
}
return dgst, nil
}
return "", errors.New("no available endpoint")
}
func lookupV2Endpoints(svc *registry.Service, ref reference.Named) ([]registry.APIEndpoint, *registry.RepositoryInfo, error) {
repoInfo, err := svc.ResolveRepository(ref)
if err != nil {
return nil, nil, err
}
//This method should be called after a successful tagged pull, therefor we do not need to
//validate the ref name again. In case we want to use this before pull, we should add
// `validateRepoName()` back here.
endpoints, err := svc.LookupPullEndpoints(repoInfo)
if err != nil {
return nil, nil, err
}
res := make([]registry.APIEndpoint, 0, len(endpoints))
for _, endpoint := range endpoints {
if endpoint.Version != registry.APIVersion2 {
continue
}
res = append(res, endpoint)
}
return res, repoInfo, nil
}
func getSvcWithV2Endpoint(ctx context.Context, repoInfo *registry.RepositoryInfo, endpoint registry.APIEndpoint, metaHeaders map[string][]string, authConfig *types.AuthConfig) (dockerdist.ManifestService, error) {
repo, _, err := mobydist.NewV2Repository(ctx, repoInfo, endpoint, metaHeaders, authConfig, "pull")
if err != nil {
return nil, err
}
manSvc, err := repo.Manifests(ctx)
if err != nil {
return nil, err
}
return manSvc, nil
}
func getDigest(manSvc dockerdist.ManifestService, ctx context.Context, tag string) (digest.Digest, error) {
manifest, err := manSvc.Get(ctx, "", client.WithTag(tag))
if err != nil {
return "", err
}
return manifest2Digist(manifest)
}
func manifest2Digist(mfst dockerdist.Manifest) (digest.Digest, error) {
switch v := mfst.(type) {
case *schema1.SignedManifest:
return digest.FromBytes(v.Canonical), nil
case *schema2.DeserializedManifest:
_, canonical, err := v.Payload()
if err != nil {
return "", err
}
return digest.FromBytes(canonical), nil
case *manifestlist.DeserializedManifestList: // TODO: I think we should process the mfst list one by one, but let's begin from here.
_, canonical, err := v.Payload()
if err != nil {
return "", err
}
return digest.FromBytes(canonical), nil
default:
return "", errors.New("unsupported manifest format")
}
}