-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
image_util.go
122 lines (105 loc) · 3.88 KB
/
image_util.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
/*
Copyright 2018 Google LLC
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 image
import (
"fmt"
"path/filepath"
"strconv"
"github.com/GoogleContainerTools/kaniko/pkg/cache"
"github.com/GoogleContainerTools/kaniko/pkg/config"
"github.com/GoogleContainerTools/kaniko/pkg/constants"
"github.com/GoogleContainerTools/kaniko/pkg/image/remote"
"github.com/GoogleContainerTools/kaniko/pkg/timing"
"github.com/GoogleContainerTools/kaniko/pkg/util"
"github.com/google/go-containerregistry/pkg/name"
v1 "github.com/google/go-containerregistry/pkg/v1"
"github.com/google/go-containerregistry/pkg/v1/empty"
"github.com/google/go-containerregistry/pkg/v1/tarball"
"github.com/sirupsen/logrus"
)
var (
// RetrieveRemoteImage downloads an image from a remote location
RetrieveRemoteImage = remote.RetrieveRemoteImage
retrieveTarImage = tarballImage
)
// RetrieveSourceImage returns the base image of the stage at index
func RetrieveSourceImage(stage config.KanikoStage, opts *config.KanikoOptions) (v1.Image, error) {
t := timing.Start("Retrieving Source Image")
defer timing.DefaultRun.Stop(t)
var buildArgs []string
for _, marg := range stage.MetaArgs {
for _, arg := range marg.Args {
buildArgs = append(buildArgs, fmt.Sprintf("%s=%s", arg.Key, arg.ValueString()))
}
}
buildArgs = append(buildArgs, opts.BuildArgs...)
currentBaseName, err := util.ResolveEnvironmentReplacement(stage.BaseName, buildArgs, false)
if err != nil {
return nil, err
}
// First, check if the base image is a scratch image
if currentBaseName == constants.NoBaseImage {
logrus.Info("No base image, nothing to extract")
return empty.Image, nil
}
// Next, check if the base image of the current stage is built from a previous stage
// If so, retrieve the image from the stored tarball
if stage.BaseImageStoredLocally {
return retrieveTarImage(stage.BaseImageIndex)
}
// Finally, check if local caching is enabled
// If so, look in the local cache before trying the remote registry
if opts.Cache && opts.CacheDir != "" {
cachedImage, err := cachedImage(opts, currentBaseName)
if err != nil {
switch {
case cache.IsNotFound(err):
logrus.Debugf("Image %v not found in cache", currentBaseName)
case cache.IsExpired(err):
logrus.Debugf("Image %v found in cache but was expired", currentBaseName)
default:
logrus.Errorf("Error while retrieving image from cache: %v %v", currentBaseName, err)
}
} else if cachedImage != nil {
return cachedImage, nil
}
}
// Otherwise, initialize image as usual
return RetrieveRemoteImage(currentBaseName, opts.RegistryOptions, opts.CustomPlatform)
}
func tarballImage(index int) (v1.Image, error) {
tarPath := filepath.Join(config.KanikoIntermediateStagesDir, strconv.Itoa(index))
logrus.Infof("Base image from previous stage %d found, using saved tar at path %s", index, tarPath)
return tarball.ImageFromPath(tarPath, nil)
}
func cachedImage(opts *config.KanikoOptions, image string) (v1.Image, error) {
ref, err := name.ParseReference(image, name.WeakValidation)
if err != nil {
return nil, err
}
var cacheKey string
if d, ok := ref.(name.Digest); ok {
cacheKey = d.DigestStr()
} else {
image, err := remote.RetrieveRemoteImage(image, opts.RegistryOptions, opts.CustomPlatform)
if err != nil {
return nil, err
}
d, err := image.Digest()
if err != nil {
return nil, err
}
cacheKey = d.String()
}
return cache.LocalSource(&opts.CacheOptions, cacheKey)
}