-
Notifications
You must be signed in to change notification settings - Fork 55
/
registry.go
222 lines (181 loc) · 4.91 KB
/
registry.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
package task
import (
"fmt"
"io"
"net"
"net/http"
"strings"
v1 "github.com/google/go-containerregistry/pkg/v1"
"github.com/google/go-containerregistry/pkg/v1/tarball"
"github.com/julienschmidt/httprouter"
"github.com/sirupsen/logrus"
)
type ImageArg struct {
Image v1.Image
ArgName string
}
type LocalRegistry map[string]ImageArg
func LoadRegistry(imagePaths map[string]string) (LocalRegistry, error) {
images := LocalRegistry{}
for name, path := range imagePaths {
image, err := tarball.ImageFromPath(path, nil)
if err != nil {
return nil, fmt.Errorf("image from path: %w", err)
}
images[strings.ToLower(name)] = ImageArg{Image: image, ArgName: name}
}
return images, nil
}
func ServeRegistry(reg LocalRegistry) (string, error) {
router := httprouter.New()
router.GET("/v2/:name/manifests/:ref", reg.GetManifest)
router.GET("/v2/:name/blobs/:digest", reg.GetBlob)
router.NotFound = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
logrus.WithFields(logrus.Fields{
"method": r.Method,
"path": r.URL.Path,
}).Warnf("unknown request")
})
listener, err := net.Listen("tcp", ":0")
if err != nil {
return "", fmt.Errorf("listen: %w", err)
}
go http.Serve(listener, router)
_, port, err := net.SplitHostPort(listener.Addr().String())
if err != nil {
return "", fmt.Errorf("split registry host/port: %w", err)
}
return port, nil
}
func (registry LocalRegistry) BuildArgs(port string) []string {
var buildArgs []string
for name, image := range registry {
buildArgs = append(buildArgs, fmt.Sprintf("%s=localhost:%s/%s", image.ArgName, port, name))
}
return buildArgs
}
func (registry LocalRegistry) GetManifest(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
name := p.ByName("name")
ref := p.ByName("ref")
logrus.WithFields(logrus.Fields{
"accept": r.Header["Accept"],
}).Debugf("get manifest for %s at %s", name, ref)
img, found := registry[name]
if !found {
w.WriteHeader(http.StatusNotFound)
return
}
image := img.Image
mt, err := image.MediaType()
if err != nil {
logrus.Errorf("failed to get media type: %s", err)
w.WriteHeader(http.StatusInternalServerError)
return
}
blob, err := image.RawManifest()
if err != nil {
logrus.Errorf("failed to get manifest: %s", err)
w.WriteHeader(http.StatusInternalServerError)
return
}
digest, err := image.Digest()
if err != nil {
logrus.Errorf("failed to get digest: %s", err)
w.WriteHeader(http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", string(mt))
w.Header().Set("Content-Length", fmt.Sprintf("%d", len(blob)))
w.Header().Set("Docker-Content-Digest", digest.String())
if r.Method == "HEAD" {
return
}
_, err = w.Write(blob)
if err != nil {
logrus.Errorf("write manifest blob: %s", err)
return
}
}
func (registry LocalRegistry) GetBlob(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
name := p.ByName("name")
dig := p.ByName("digest")
logrus.WithFields(logrus.Fields{
"accept": r.Header["Accept"],
}).Debugf("get blob %s", dig)
img, found := registry[name]
if !found {
w.WriteHeader(http.StatusNotFound)
return
}
image := img.Image
hash, err := v1.NewHash(dig)
if err != nil {
logrus.Errorf("failed to parse digest: %s", err)
w.WriteHeader(http.StatusInternalServerError)
return
}
cfgHash, err := image.ConfigName()
if err != nil {
logrus.Errorf("failed to get config hash: %s", err)
w.WriteHeader(http.StatusInternalServerError)
return
}
if hash == cfgHash {
manifest, err := image.Manifest()
if err != nil {
logrus.Errorf("get image manifest: %s", err)
return
}
cfgBlob, err := image.RawConfigFile()
if err != nil {
logrus.Errorf("failed to get config file: %s", err)
w.WriteHeader(http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", string(manifest.Config.MediaType))
w.Header().Set("Content-Length", fmt.Sprintf("%d", len(cfgBlob)))
if r.Method == "HEAD" {
return
}
_, err = w.Write(cfgBlob)
if err != nil {
logrus.Errorf("write config blob: %s", err)
return
}
return
}
layer, err := image.LayerByDigest(hash)
if err != nil {
logrus.Errorf("failed to get layer: %s", err)
w.WriteHeader(http.StatusInternalServerError)
return
}
size, err := layer.Size()
if err != nil {
logrus.Errorf("failed to get layer size: %s", err)
w.WriteHeader(http.StatusInternalServerError)
return
}
mt, err := layer.MediaType()
if err != nil {
logrus.Errorf("failed to get layer media type: %s", err)
w.WriteHeader(http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", string(mt))
w.Header().Set("Content-Length", fmt.Sprintf("%d", size))
if r.Method == "HEAD" {
return
}
blob, err := layer.Compressed()
if err != nil {
logrus.Errorf("failed to read layer: %s", err)
w.WriteHeader(http.StatusInternalServerError)
return
}
_, err = io.Copy(w, blob)
if err != nil {
logrus.Errorf("write blob: %s", err)
return
}
}