-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmanifest.go
156 lines (125 loc) · 3.27 KB
/
manifest.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package client
import (
"encoding/json"
"io"
"io/ioutil"
"net/http"
"path"
ispec "github.com/opencontainers/image-spec/specs-go/v1"
)
// ManifestExists confirms the existance of a manifest in a remote registry.
func (r *Registry) ManifestExists(img Image) error {
c := r.client
req := new(http.Request)
req.Method = "HEAD"
req.URL = r.Host
req.URL.Path = manifestEndpoint(img.Repository, img.Reference)
req.Header = make(http.Header)
resp, err := c.RoundTrip(req)
if err != nil {
return err
}
if resp.StatusCode != http.StatusOK {
return ErrManifestNotExist
}
return nil
}
// GetManifests returns an image index and a slice of manifests from a remote
// registry. If the manifest is an Image Index, all manifests referenced in the
// index will be downloaded and returned. If a nil Index is returned, there will
// only be one manifest in the slice.
func (r *Registry) GetManifests(img Image) (*ispec.Index, *[]ispec.Manifest, error) {
c := r.client
req := new(http.Request)
req.Method = "GET"
req.URL = r.Host
req.URL.Path = manifestEndpoint(img.Repository, img.Reference)
req.Header = make(http.Header)
req.Header["Accept"] = []string{
ispec.MediaTypeImageIndex,
ispec.MediaTypeImageManifest,
}
resp, err := c.RoundTrip(req)
if err != nil {
return nil, nil, err
}
defer resp.Body.Close()
var idx *ispec.Index
manifests := make([]ispec.Manifest, 0)
contentType := resp.Header.Get("Content-Type")
switch contentType {
case ispec.MediaTypeImageIndex:
err := validateIndex(resp.Body)
if err != nil {
return nil, nil, err
}
parseIndex(resp.Body, idx)
case ispec.MediaTypeImageManifest:
err := validateManifest(resp.Body)
if err != nil {
return nil, nil, err
}
var m *ispec.Manifest
parseManifest(resp.Body, m)
manifests = append(manifests, *m)
default:
return nil, nil, ErrUnknownMediaType
}
return idx, &manifests, nil
}
func (r *Registry) getManifest(img Image) (*ispec.Manifest, error) {
c := r.client
req := new(http.Request)
req.Method = "GET"
req.URL = r.Host
req.URL.Path = manifestEndpoint(img.Repository, img.Reference)
req.Header = make(http.Header)
req.Header["Accept"] = []string{ispec.MediaTypeImageManifest}
resp, err := c.RoundTrip(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
b, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
if resp.StatusCode != http.StatusOK {
var errorResp *ErrorResponse
if err := json.Unmarshal(b, errorResp); err != nil {
return nil, ErrParseJSON
}
return nil, errorResp
}
var m *ispec.Manifest
if err := parseManifest(resp.Body, m); err != nil {
return nil, err
}
return m, nil
}
func (r *Registry) manifestsFromIndex(idx ispec.Index) (*[]ispec.Manifest, error) {
return nil, nil
}
func parseManifest(data io.Reader, manifest *ispec.Manifest) error {
b, err := ioutil.ReadAll(data)
if err != nil {
return err
}
if err := json.Unmarshal(b, manifest); err != nil {
return ErrParseJSON
}
return nil
}
func parseIndex(data io.Reader, idx *ispec.Index) error {
b, err := ioutil.ReadAll(data)
if err != nil {
return err
}
if err := json.Unmarshal(b, idx); err != nil {
return ErrParseJSON
}
return nil
}
func manifestEndpoint(repo, reference string) string {
return path.Join("/v2", repo, "manifests", reference)
}