-
Notifications
You must be signed in to change notification settings - Fork 88
/
temp_registry.go
220 lines (185 loc) · 6.43 KB
/
temp_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
package registry
import (
_ "embed"
"fmt"
"io/ioutil"
"net/http"
"os"
"os/exec"
"strings"
"time"
containersmanifest "github.com/containers/image/v5/manifest"
"github.com/containers/image/v5/transports/alltransports"
containerstypes "github.com/containers/image/v5/types"
"github.com/distribution/distribution/v3/reference"
"github.com/phayes/freeport"
"github.com/pkg/errors"
"github.com/replicatedhq/kots/pkg/docker/types"
"github.com/replicatedhq/kots/pkg/kotsutil"
"github.com/replicatedhq/kots/pkg/logger"
)
//go:embed assets/temp-registry-config.yml
var tempRegistryConfigYML string
type TempRegistry struct {
process *os.Process
port string
}
// Start will spin up a docker registry service in the background on a random port.
// Will use the given "rootDir" as storage.
// Caller is responsible for stopping the registry.
func (r *TempRegistry) Start(rootDir string) (finalError error) {
if r.port != "" {
return errors.Errorf("registry is already running on port %s", r.port)
}
defer func() {
if finalError != nil {
r.Stop()
}
}()
fp, err := freeport.GetFreePort()
if err != nil {
return errors.Wrap(err, "failed to get free port")
}
freePort := fmt.Sprintf("%d", fp)
configYMLCopy := strings.Replace(tempRegistryConfigYML, "__ROOT_DIR__", rootDir, 1)
configYMLCopy = strings.Replace(configYMLCopy, "__PORT__", freePort, 1)
configFile, err := ioutil.TempFile("", "registryconfig")
if err != nil {
return errors.Wrap(err, "failed to create temp file for config")
}
if err := ioutil.WriteFile(configFile.Name(), []byte(configYMLCopy), 0644); err != nil {
return errors.Wrap(err, "failed to write config to temp file")
}
defer os.RemoveAll(configFile.Name())
// We use the KOTS CLI as a wrapper to start the docker registry service because:
// - We can't directly run the official docker registry binary because it doesn't necessarily exist when pushing images from the host.
// - We need to be able to control stdout and stderr and stop the process later, but the registry go module doesn't give control over that.
// - The KOTS CLI binary exists inside the kotsadm pod and/or will be used to push images from the host.
cmd := exec.Command(kotsutil.GetKOTSBinPath(), "docker-registry", "serve", configFile.Name())
if err := cmd.Start(); err != nil {
return errors.Wrap(err, "failed to start")
}
r.port = freePort
r.process = cmd.Process
if err := r.WaitForReady(time.Second * 30); err != nil {
return errors.Wrap(err, "failed to wait for registry to become ready")
}
return nil
}
func (r *TempRegistry) Stop() {
if r.process != nil {
if err := r.process.Signal(os.Interrupt); err != nil {
logger.Debugf("Failed to stop registry process on port %s", r.port)
}
}
r.port = ""
r.process = nil
}
func (r *TempRegistry) WaitForReady(timeout time.Duration) error {
start := time.Now()
for {
url := fmt.Sprintf("http://localhost:%s", r.port)
newRequest, err := http.NewRequest("GET", url, nil)
if err == nil {
resp, err := http.DefaultClient.Do(newRequest)
if err == nil {
if resp.StatusCode == http.StatusOK {
return nil
}
}
}
time.Sleep(time.Second)
if time.Since(start) > timeout {
return errors.Errorf("Timeout waiting for registry to become ready on port %s", r.port)
}
}
}
func (r *TempRegistry) GetImageLayers(image string) ([]types.Layer, error) {
imageRef, err := reference.ParseDockerRef(image)
if err != nil {
return nil, errors.Wrapf(err, "failed to normalize image %s", image)
}
tagOrDigest := ""
if can, ok := imageRef.(reference.Canonical); ok {
tagOrDigest = can.Digest().String()
} else if tagged, ok := imageRef.(reference.Tagged); ok {
tagOrDigest = tagged.Tag()
} else {
tagOrDigest = "latest"
}
imageParts := strings.Split(reference.TrimNamed(imageRef).Name(), "/") // strip tag and digest
imageName := imageParts[len(imageParts)-1] // strip hostname and repo if any
layers, err := r.getImageLayers(imageName, tagOrDigest)
if err != nil {
return nil, errors.Wrapf(err, "failed to get layers for image %s and tag/digest %s", imageName, tagOrDigest)
}
return layers, nil
}
func (r *TempRegistry) getImageLayers(imageName string, tagOrDigest string) ([]types.Layer, error) {
url := fmt.Sprintf("http://localhost:%s/v2/%s/manifests/%s", r.port, imageName, tagOrDigest)
newRequest, err := http.NewRequest("GET", url, nil)
if err != nil {
return nil, errors.Wrap(err, "failed to create http request")
}
resp, err := http.DefaultClient.Do(newRequest)
if err != nil {
return nil, errors.Wrap(err, "failed to execute http request")
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, errors.Errorf("unexpected status code %d", resp.StatusCode)
}
b, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, errors.Wrap(err, "failed to read server response")
}
layers := []types.Layer{}
mimeType := containersmanifest.GuessMIMEType(b)
if containersmanifest.MIMETypeIsMultiImage(mimeType) {
// this is a multi-arch image, read layers for each architecture
list, err := containersmanifest.ListFromBlob(b, mimeType)
if err != nil {
return nil, errors.Wrap(err, "failed to list manifests from blob")
}
for _, digest := range list.Instances() {
mLayers, err := r.getImageLayers(imageName, digest.String())
if err != nil {
return nil, errors.Wrapf(err, "failed to get layers for %s", digest.String())
}
layers = append(layers, mLayers...)
}
} else {
manifest, err := containersmanifest.FromBlob(b, mimeType)
if err != nil {
return nil, errors.Wrap(err, "failed to get manifest from blob")
}
for _, i := range manifest.LayerInfos() {
if i.EmptyLayer {
continue
}
layers = append(layers, types.Layer{
Digest: i.Digest.String(),
Size: i.Size,
})
}
}
return layers, nil
}
func (r *TempRegistry) SrcRef(image string) (containerstypes.ImageReference, error) {
parsed, err := reference.ParseDockerRef(image)
if err != nil {
return nil, errors.Wrapf(err, "failed to normalize image %s", image)
}
normalizedImage := parsed.String()
imageParts := strings.Split(normalizedImage, "/")
lastPart := imageParts[len(imageParts)-1]
srcRef, err := alltransports.ParseImageName(fmt.Sprintf("docker://localhost:%s/%s", r.port, lastPart))
if err != nil {
return nil, errors.Wrap(err, "failed to parse source image name")
}
return srcRef, nil
}
// This is only used for integration tests
func (r *TempRegistry) OverridePort(port string) {
r.port = port
}