/
cache_images.go
337 lines (288 loc) · 8.72 KB
/
cache_images.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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
/*
Copyright 2016 The Kubernetes Authors All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package machine
import (
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
"golang.org/x/sync/errgroup"
"k8s.io/minikube/pkg/minikube/assets"
"k8s.io/minikube/pkg/minikube/bootstrapper"
"k8s.io/minikube/pkg/minikube/config"
"k8s.io/minikube/pkg/minikube/constants"
"k8s.io/minikube/pkg/minikube/sshutil"
"github.com/containers/image/copy"
"github.com/containers/image/docker"
"github.com/containers/image/docker/archive"
"github.com/containers/image/signature"
"github.com/containers/image/types"
"github.com/golang/glog"
"github.com/pkg/errors"
)
const tempLoadDir = "/tmp"
var getWindowsVolumeName = getWindowsVolumeNameCmd
func CacheImagesForBootstrapper(version string, clusterBootstrapper string) error {
images := bootstrapper.GetCachedImageList(version, clusterBootstrapper)
if err := CacheImages(images, constants.ImageCacheDir); err != nil {
return errors.Wrapf(err, "Caching images for %s", clusterBootstrapper)
}
return nil
}
// CacheImages will cache images on the host
//
// The cache directory currently caches images using the imagename_tag
// For example, k8s.gcr.io/kube-addon-manager:v6.5 would be
// stored at $CACHE_DIR/k8s.gcr.io/kube-addon-manager_v6.5
func CacheImages(images []string, cacheDir string) error {
var g errgroup.Group
for _, image := range images {
image := image
g.Go(func() error {
dst := filepath.Join(cacheDir, image)
dst = sanitizeCacheDir(dst)
if err := CacheImage(image, dst); err != nil {
return errors.Wrapf(err, "caching image %s", dst)
}
return nil
})
}
if err := g.Wait(); err != nil {
return errors.Wrap(err, "caching images")
}
glog.Infoln("Successfully cached all images.")
return nil
}
func LoadImages(cmd bootstrapper.CommandRunner, images []string, cacheDir string) error {
var g errgroup.Group
for _, image := range images {
image := image
g.Go(func() error {
src := filepath.Join(cacheDir, image)
src = sanitizeCacheDir(src)
if err := LoadFromCacheBlocking(cmd, src); err != nil {
return errors.Wrapf(err, "loading image %s", src)
}
return nil
})
}
if err := g.Wait(); err != nil {
return errors.Wrap(err, "loading cached images")
}
glog.Infoln("Successfully loaded all cached images.")
return nil
}
func CacheAndLoadImages(images []string) error {
if err := CacheImages(images, constants.ImageCacheDir); err != nil {
return err
}
api, err := NewAPIClient()
if err != nil {
return err
}
defer api.Close()
h, err := api.Load(config.GetMachineName())
if err != nil {
return err
}
client, err := sshutil.NewSSHClient(h.Driver)
if err != nil {
return err
}
cmdRunner, err := bootstrapper.NewSSHRunner(client), nil
if err != nil {
return err
}
return LoadImages(cmdRunner, images, constants.ImageCacheDir)
}
// # ParseReference cannot have a : in the directory path
func sanitizeCacheDir(image string) string {
if runtime.GOOS == "windows" && hasWindowsDriveLetter(image) {
// not sanitize Windows drive letter.
return image[:2] + strings.Replace(image[2:], ":", "_", -1)
}
return strings.Replace(image, ":", "_", -1)
}
func hasWindowsDriveLetter(s string) bool {
if len(s) < 3 {
return false
}
drive := s[:3]
for _, b := range "CDEFGHIJKLMNOPQRSTUVWXYZAB" {
if d := string(b) + ":"; drive == d+`\` || drive == d+`/` {
return true
}
}
return false
}
// Replace a drive letter to a volume name.
func replaceWinDriveLetterToVolumeName(s string) (string, error) {
vname, err := getWindowsVolumeName(s[:1])
if err != nil {
return "", err
}
path := vname + s[3:]
if _, err := os.Stat(filepath.Dir(path)); err != nil {
return "", err
}
return path, nil
}
func getWindowsVolumeNameCmd(d string) (string, error) {
cmd := exec.Command("wmic", "volume", "where", "DriveLetter = '"+d+":'", "get", "DeviceID")
stdout, err := cmd.Output()
if err != nil {
return "", err
}
outs := strings.Split(strings.Replace(string(stdout), "\r", "", -1), "\n")
var vname string
for _, l := range outs {
s := strings.TrimSpace(l)
if strings.HasPrefix(s, `\\?\Volume{`) && strings.HasSuffix(s, `}\`) {
vname = s
break
}
}
if vname == "" {
return "", errors.New("failed to get a volume GUID")
}
return vname, nil
}
func LoadFromCacheBlocking(cmd bootstrapper.CommandRunner, src string) error {
glog.Infoln("Loading image from cache at ", src)
filename := filepath.Base(src)
for {
if _, err := os.Stat(src); err == nil {
break
}
}
dst := filepath.Join(tempLoadDir, filename)
f, err := assets.NewFileAsset(src, tempLoadDir, filename, "0777")
if err != nil {
return errors.Wrapf(err, "creating copyable file asset: %s", filename)
}
if err := cmd.Copy(f); err != nil {
return errors.Wrap(err, "transferring cached image")
}
dockerLoadCmd := "docker load -i " + dst
if err := cmd.Run(dockerLoadCmd); err != nil {
return errors.Wrapf(err, "loading docker image: %s", dst)
}
if err := cmd.Run("sudo rm -rf " + dst); err != nil {
return errors.Wrap(err, "deleting temp docker image location")
}
glog.Infof("Successfully loaded image %s from cache", src)
return nil
}
func DeleteFromImageCacheDir(images []string) error {
for _, image := range images {
path := filepath.Join(constants.ImageCacheDir, image)
path = sanitizeCacheDir(path)
glog.Infoln("Deleting image in cache at ", path)
if err := os.Remove(path); err != nil {
return err
}
}
return cleanImageCacheDir()
}
func cleanImageCacheDir() error {
err := filepath.Walk(constants.ImageCacheDir, func(path string, info os.FileInfo, err error) error {
// If error is not nil, it's because the path was already deleted and doesn't exist
// Move on to next path
if err != nil {
return nil
}
// Check if path is directory
if !info.IsDir() {
return nil
}
// If directory is empty, delete it
entries, err := ioutil.ReadDir(path)
if err != nil {
return err
}
if len(entries) == 0 {
if err = os.Remove(path); err != nil {
return err
}
}
return nil
})
return err
}
func getSrcRef(image string) (types.ImageReference, error) {
srcRef, err := docker.ParseReference("//" + image)
if err != nil {
return nil, errors.Wrap(err, "parsing docker image src ref")
}
return srcRef, nil
}
func getDstRef(image, dst string) (types.ImageReference, error) {
if runtime.GOOS == "windows" && hasWindowsDriveLetter(dst) {
// ParseReference does not support a Windows drive letter.
// Therefore, will replace the drive letter to a volume name.
var err error
if dst, err = replaceWinDriveLetterToVolumeName(dst); err != nil {
return nil, errors.Wrap(err, "parsing docker archive dst ref: replace a Win drive letter to a volume name")
}
}
return _getDstRef(image, dst)
}
func _getDstRef(image, dst string) (types.ImageReference, error) {
dstRef, err := archive.ParseReference(dst + ":" + image)
if err != nil {
return nil, errors.Wrap(err, "parsing docker archive dst ref")
}
return dstRef, nil
}
func CacheImage(image, dst string) error {
glog.Infof("Attempting to cache image: %s at %s\n", image, dst)
if _, err := os.Stat(dst); err == nil {
return nil
}
if err := os.MkdirAll(filepath.Dir(dst), 0777); err != nil {
return errors.Wrapf(err, "making cache image directory: %s", dst)
}
srcRef, err := getSrcRef(image)
if err != nil {
return errors.Wrap(err, "creating docker image src ref")
}
dstRef, err := getDstRef(image, dst)
if err != nil {
return errors.Wrap(err, "creating docker archive dst ref")
}
policy := &signature.Policy{Default: []signature.PolicyRequirement{signature.NewPRInsecureAcceptAnything()}}
policyContext, err := signature.NewPolicyContext(policy)
if err != nil {
return errors.Wrap(err, "getting policy context")
}
tmp, err := ioutil.TempDir("", "")
if err != nil {
return errors.Wrap(err, "making temp dir")
}
defer os.RemoveAll(tmp)
sourceCtx := &types.SystemContext{
// By default, the image library will try to look at /etc/docker/certs.d
// As a non-root user, this would result in a permissions error,
// so, we skip this step by just looking in a newly created tmpdir.
DockerCertPath: tmp,
}
err = copy.Image(policyContext, dstRef, srcRef, ©.Options{
SourceCtx: sourceCtx,
})
if err != nil {
return errors.Wrap(err, "copying image")
}
return nil
}