forked from buildpacks/lifecycle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
extender.go
448 lines (410 loc) · 12.8 KB
/
extender.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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
package lifecycle
import (
"context"
"fmt"
"io"
"os"
"path/filepath"
"strconv"
"strings"
"time"
"github.com/BurntSushi/toml"
"github.com/buildpacks/imgutil"
v1 "github.com/google/go-containerregistry/pkg/v1"
"github.com/google/go-containerregistry/pkg/v1/empty"
"github.com/google/go-containerregistry/pkg/v1/mutate"
"github.com/google/uuid"
"golang.org/x/sync/errgroup"
"github.com/buildpacks/lifecycle/buildpack"
"github.com/buildpacks/lifecycle/internal/extend"
"github.com/buildpacks/lifecycle/internal/selective"
"github.com/buildpacks/lifecycle/launch"
"github.com/buildpacks/lifecycle/layers"
"github.com/buildpacks/lifecycle/log"
)
type Extender struct {
AppDir string // explicitly ignored by the Dockerfile applier, also the Dockefile build context
ExtendedDir string // output directory for extended image layers
GeneratedDir string // input Dockerfiles are found here
ImageRef string // the image to extend
LayersDir string // explicitly ignored by the Dockerfile applier
PlatformDir string // explicitly ignored by the Dockerfile applier
CacheTTL time.Duration // a platform input
DockerfileApplier DockerfileApplier // uses kaniko, BuildKit, or other to apply the provided Dockerfile to the provided image
Extensions []buildpack.GroupElement // extensions are ordered from group.toml
}
//go:generate mockgen -package testmock -destination testmock/dockerfile_applier.go github.com/buildpacks/lifecycle DockerfileApplier
type DockerfileApplier interface {
ImageFor(reference string) (v1.Image, error)
Apply(dockerfile extend.Dockerfile, toBaseImage v1.Image, withBuildOptions extend.Options, logger log.Logger) (v1.Image, error)
Cleanup() error
}
type ExtenderFactory struct {
apiVerifier BuildpackAPIVerifier
configHandler ConfigHandler
}
func NewExtenderFactory(apiVerifier BuildpackAPIVerifier, configHandler ConfigHandler) *ExtenderFactory {
return &ExtenderFactory{
apiVerifier: apiVerifier,
configHandler: configHandler,
}
}
func (f *ExtenderFactory) NewExtender(
analyzedPath string,
appDir string,
extendedDir string,
generatedDir string,
groupPath string,
layersDir string,
platformDir string,
cacheTTL time.Duration,
dockerfileApplier DockerfileApplier,
kind string,
logger log.Logger,
) (*Extender, error) {
extender := &Extender{
AppDir: appDir,
ExtendedDir: extendedDir,
GeneratedDir: generatedDir,
LayersDir: layersDir,
PlatformDir: platformDir,
CacheTTL: cacheTTL,
DockerfileApplier: dockerfileApplier,
}
if err := f.setImageRef(extender, kind, analyzedPath, logger); err != nil {
return nil, err
}
if err := f.setExtensions(extender, groupPath, logger); err != nil {
return nil, err
}
return extender, nil
}
func (f *ExtenderFactory) setImageRef(extender *Extender, kind, path string, logr log.Logger) error {
analyzedMD, err := f.configHandler.ReadAnalyzed(path, logr)
if err != nil {
return err
}
if kind == "build" {
if analyzedMD.BuildImage != nil {
extender.ImageRef = analyzedMD.BuildImage.Reference
}
} else if kind == "run" {
if analyzedMD.RunImage != nil {
extender.ImageRef = analyzedMD.RunImage.Reference
}
}
return nil
}
func (f *ExtenderFactory) setExtensions(extender *Extender, path string, logger log.Logger) error {
_, groupExt, err := f.configHandler.ReadGroup(path)
if err != nil {
return fmt.Errorf("reading group: %w", err)
}
for i := range groupExt {
groupExt[i].Extension = true
}
if err = f.verifyAPIs(groupExt, logger); err != nil {
return err
}
extender.Extensions = groupExt
return nil
}
func (f *ExtenderFactory) verifyAPIs(groupExt []buildpack.GroupElement, logger log.Logger) error {
for _, groupEl := range groupExt {
if err := f.apiVerifier.VerifyBuildpackAPI(groupEl.Kind(), groupEl.String(), groupEl.API, logger); err != nil {
return err
}
}
return nil
}
func (e *Extender) Extend(kind string, logger log.Logger) error {
switch kind {
case buildpack.DockerfileKindBuild:
return e.extendBuild(logger)
case buildpack.DockerfileKindRun:
return e.extendRun(logger)
default:
return nil
}
}
func (e *Extender) extendBuild(logger log.Logger) error {
origBaseImage, err := e.DockerfileApplier.ImageFor(e.ImageRef)
if err != nil {
return fmt.Errorf("getting build image to extend: %w", err)
}
extendedImage, err := e.extend(buildpack.DockerfileKindBuild, origBaseImage, logger)
if err != nil {
return fmt.Errorf("extending build image: %w", err)
}
if err = setImageEnvVarsInCurrentContext(extendedImage); err != nil {
return fmt.Errorf("setting environment variables from extended image in current context: %w", err)
}
return e.DockerfileApplier.Cleanup()
}
func setImageEnvVarsInCurrentContext(image v1.Image) error {
configFile, err := image.ConfigFile()
if err != nil || configFile == nil {
return fmt.Errorf("getting config for extended image: %w", err)
}
for _, env := range configFile.Config.Env {
parts := strings.Split(env, "=")
if len(parts) != 2 {
return fmt.Errorf("parsing env '%s': expected format 'key=value'", env)
}
if err := os.Setenv(parts[0], parts[1]); err != nil {
return fmt.Errorf("setting env: %w", err)
}
}
return nil
}
func (e *Extender) extendRun(logger log.Logger) error {
origBaseImage, err := e.DockerfileApplier.ImageFor(e.ImageRef)
if err != nil {
return fmt.Errorf("getting run image to extend: %w", err)
}
origTopLayer, err := topLayer(origBaseImage)
if err != nil {
return fmt.Errorf("getting original run image top layer: %w", err)
}
extendedImage, err := e.extend(buildpack.DockerfileKindRun, origBaseImage, logger)
if err != nil {
return fmt.Errorf("extending run image: %w", err)
}
if err = e.saveSelective(extendedImage, origTopLayer); err != nil {
return fmt.Errorf("copying selective image to output directory: %w", err)
}
return e.DockerfileApplier.Cleanup()
}
func topLayer(image v1.Image) (string, error) {
manifest, err := image.Manifest()
if err != nil {
return "", fmt.Errorf("getting image manifest: %w", err)
}
layers := manifest.Layers
if len(layers) == 0 {
return "", nil
}
layer := layers[len(layers)-1]
return layer.Digest.String(), nil
}
func (e *Extender) saveSelective(image v1.Image, origTopLayerHash string) error {
// save sparse image (manifest and config)
imageHash, err := image.Digest()
if err != nil {
return fmt.Errorf("getting image hash: %w", err)
}
toPath := filepath.Join(e.ExtendedDir, "run", imageHash.String())
layoutPath, err := selective.Write(toPath, empty.Index) // FIXME: this should use the imgutil layout/sparse package instead, but for some reason sparse.NewImage().Save() fails when the provided base image is already sparse
if err != nil {
return fmt.Errorf("initializing selective image: %w", err)
}
if err = layoutPath.AppendImage(image); err != nil {
return fmt.Errorf("saving selective image: %w", err)
}
// get all image layers (we will only copy those following the original top layer)
layers, err := image.Layers()
if err != nil {
return fmt.Errorf("getting image layers: %w", err)
}
var (
currentHash v1.Hash
needsCopying bool
)
if origTopLayerHash == "" { // if the original base image had no layers, copy all the layers
needsCopying = true
}
group, _ := errgroup.WithContext(context.TODO())
for _, currentLayer := range layers {
currentHash, err = currentLayer.Digest()
if err != nil {
return fmt.Errorf("getting layer hash: %w", err)
}
switch {
case needsCopying:
currentLayer := currentLayer // allow use in closure
group.Go(func() error {
return copyLayer(currentLayer, toPath)
})
case currentHash.String() == origTopLayerHash:
needsCopying = true
continue
default:
continue
}
}
return group.Wait()
}
func copyLayer(layer v1.Layer, toSparseImage string) error {
digest, err := layer.Digest()
if err != nil {
return err
}
f, err := os.Create(filepath.Join(toSparseImage, "blobs", digest.Algorithm, digest.Hex))
if err != nil {
return err
}
defer f.Close()
rc, err := layer.Compressed()
if err != nil {
return err
}
defer rc.Close()
_, err = io.Copy(f, rc)
return err
}
const (
argBuildID = "build_id"
argUserID = "user_id"
argGroupID = "group_id"
)
func (e *Extender) extend(kind string, baseImage v1.Image, logger log.Logger) (v1.Image, error) {
defer log.NewMeasurement("Extender", logger)()
logger.Debugf("Extending base image for %s: %s", kind, e.ImageRef)
dockerfiles, err := e.dockerfilesFor(kind, logger)
if err != nil {
return nil, fmt.Errorf("getting %s.Dockerfiles: %w", kind, err)
}
var (
configFile *v1.ConfigFile
rebasable = true // we don't require the initial base image to have io.buildpacks.rebasable=true
workingHistory []v1.History
)
// get config
baseImage, err = imgutil.OverrideHistoryIfNeeded(baseImage)
if err != nil {
return nil, err
}
configFile, err = baseImage.ConfigFile()
if err != nil {
return nil, err
}
workingHistory = configFile.History
buildOptions := e.extendOptions()
userID, groupID := userFrom(*configFile)
origUserID := userID
for _, dockerfile := range dockerfiles {
dockerfile.Args = append([]extend.Arg{
{Name: argBuildID, Value: uuid.New().String()},
{Name: argUserID, Value: userID},
{Name: argGroupID, Value: groupID},
}, dockerfile.Args...)
// apply Dockerfile
if baseImage, err = e.DockerfileApplier.Apply(
dockerfile,
baseImage,
buildOptions,
logger,
); err != nil {
return nil, fmt.Errorf("applying Dockerfile to image: %w", err)
}
// update rebasable, history in config, and user/group IDs
configFile, err = baseImage.ConfigFile()
if err != nil || configFile == nil {
return nil, fmt.Errorf("getting image config: %w", err)
}
// rebasable
if !rebasable || !isRebasable(configFile) {
rebasable = false
}
if configFile.Config.Labels == nil {
configFile.Config.Labels = map[string]string{}
}
configFile.Config.Labels[RebasableLabel] = fmt.Sprintf("%t", rebasable)
// history
newHistory := imgutil.NormalizedHistory(configFile.History, len(configFile.RootFS.DiffIDs))
for i := len(workingHistory); i < len(newHistory); i++ {
workingHistory = append(
workingHistory,
v1.History{
CreatedBy: fmt.Sprintf(layers.ExtensionLayerName, newHistory[i].CreatedBy, dockerfile.ExtensionID),
},
)
}
configFile.History = workingHistory
prevUserID := userID
userID, groupID = userFrom(*configFile)
if isRoot(userID) {
logger.Warnf("Extension from %s changed the user ID from %s to %s; this must not be the final user ID (a following extension must reset the user).", dockerfile.Path, prevUserID, userID)
}
}
if isRoot(userID) && kind == "run" {
return baseImage, fmt.Errorf("the final user ID is 0 (root); please add another extension that resets the user to non-root")
}
if userID != origUserID {
logger.Warnf("The original user ID was %s but the final extension left the user ID set to %s.", origUserID, userID)
}
if kind == buildpack.DockerfileKindBuild {
return baseImage, nil
}
return mutate.ConfigFile(baseImage, configFile)
}
func userFrom(config v1.ConfigFile) (string, string) {
user := strings.Split(config.Config.User, ":")
if len(user) < 2 {
return config.Config.User, ""
}
return user[0], user[1]
}
func isRoot(userID string) bool {
return userID == "0" || userID == "root"
}
const RebasableLabel = "io.buildpacks.rebasable"
func isRebasable(config *v1.ConfigFile) bool {
val, ok := config.Config.Labels[RebasableLabel]
if !ok {
// label unset
return false
}
b, err := strconv.ParseBool(val)
if err != nil {
// label not parsable
return false
}
return b
}
func (e *Extender) dockerfilesFor(kind string, logger log.Logger) ([]extend.Dockerfile, error) {
var dockerfiles []extend.Dockerfile
for _, ext := range e.Extensions {
dockerfile, err := e.dockerfileFor(kind, ext.ID)
if err != nil {
return nil, err
}
if dockerfile != nil {
logger.Debugf("Found %s Dockerfile for extension '%s'", kind, ext.ID)
dockerfiles = append(dockerfiles, *dockerfile)
}
}
return dockerfiles, nil
}
func (e *Extender) dockerfileFor(kind, extID string) (*extend.Dockerfile, error) {
dockerfilePath := filepath.Join(e.GeneratedDir, kind, launch.EscapeID(extID), "Dockerfile")
if _, err := os.Stat(dockerfilePath); err != nil {
return nil, nil
}
configPath := filepath.Join(e.GeneratedDir, kind, launch.EscapeID(extID), "extend-config.toml")
var config extend.Config
_, err := toml.DecodeFile(configPath, &config)
if err != nil {
if !os.IsNotExist(err) {
return nil, err
}
}
var args []extend.Arg
if kind == buildpack.DockerfileKindBuild {
args = config.Build.Args
} else {
args = config.Run.Args
}
return &extend.Dockerfile{
ExtensionID: extID,
Path: dockerfilePath,
Args: args,
}, nil
}
func (e *Extender) extendOptions() extend.Options {
return extend.Options{
BuildContext: e.AppDir,
CacheTTL: e.CacheTTL,
IgnorePaths: []string{e.AppDir, e.LayersDir, e.PlatformDir},
}
}