Addressed code review comment, removed stuttering variable names
This commit is contained in:
parent
d9022dd7de
commit
bf72328611
|
|
@ -21,7 +21,7 @@ import "github.com/moby/buildkit/frontend/dockerfile/instructions"
|
||||||
// KanikoStage wraps a stage of the Dockerfile and provides extra information
|
// KanikoStage wraps a stage of the Dockerfile and provides extra information
|
||||||
type KanikoStage struct {
|
type KanikoStage struct {
|
||||||
instructions.Stage
|
instructions.Stage
|
||||||
FinalStage bool
|
Final bool
|
||||||
BaseImageStoredLocally bool
|
BaseImageStoredLocally bool
|
||||||
BaseImageIndex int
|
BaseImageIndex int
|
||||||
SaveStage bool
|
SaveStage bool
|
||||||
|
|
|
||||||
|
|
@ -57,7 +57,7 @@ func Stages(opts *config.KanikoOptions) ([]config.KanikoStage, error) {
|
||||||
BaseImageIndex: baseImageIndex(opts, index, stages),
|
BaseImageIndex: baseImageIndex(opts, index, stages),
|
||||||
BaseImageStoredLocally: (baseImageIndex(opts, index, stages) != -1),
|
BaseImageStoredLocally: (baseImageIndex(opts, index, stages) != -1),
|
||||||
SaveStage: saveStage(index, stages),
|
SaveStage: saveStage(index, stages),
|
||||||
FinalStage: index == targetStage,
|
Final: index == targetStage,
|
||||||
})
|
})
|
||||||
if index == targetStage {
|
if index == targetStage {
|
||||||
break
|
break
|
||||||
|
|
|
||||||
|
|
@ -44,8 +44,8 @@ import (
|
||||||
// stageBuilder contains all fields necessary to build one stage of a Dockerfile
|
// stageBuilder contains all fields necessary to build one stage of a Dockerfile
|
||||||
type stageBuilder struct {
|
type stageBuilder struct {
|
||||||
stage config.KanikoStage
|
stage config.KanikoStage
|
||||||
v1.Image
|
image v1.Image
|
||||||
*v1.ConfigFile
|
cf *v1.ConfigFile
|
||||||
*snapshot.Snapshotter
|
*snapshot.Snapshotter
|
||||||
baseImageDigest string
|
baseImageDigest string
|
||||||
}
|
}
|
||||||
|
|
@ -76,8 +76,8 @@ func newStageBuilder(opts *config.KanikoOptions, stage config.KanikoStage) (*sta
|
||||||
}
|
}
|
||||||
return &stageBuilder{
|
return &stageBuilder{
|
||||||
stage: stage,
|
stage: stage,
|
||||||
Image: sourceImage,
|
image: sourceImage,
|
||||||
ConfigFile: imageConfig,
|
cf: imageConfig,
|
||||||
Snapshotter: snapshotter,
|
Snapshotter: snapshotter,
|
||||||
baseImageDigest: digest.String(),
|
baseImageDigest: digest.String(),
|
||||||
}, nil
|
}, nil
|
||||||
|
|
@ -95,36 +95,36 @@ func (s *stageBuilder) extractCachedLayer(layer v1.Image, createdBy string) erro
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *stageBuilder) buildStage(opts *config.KanikoOptions) error {
|
func (s *stageBuilder) build(opts *config.KanikoOptions) error {
|
||||||
// Unpack file system to root
|
// Unpack file system to root
|
||||||
if err := util.GetFSFromImage(constants.RootDir, s.Image); err != nil {
|
if err := util.GetFSFromImage(constants.RootDir, s.image); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
// Take initial snapshot
|
// Take initial snapshot
|
||||||
if err := s.Snapshotter.Init(); err != nil {
|
if err := s.Snapshotter.Init(); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
buildArgs := dockerfile.NewBuildArgs(opts.BuildArgs)
|
args := dockerfile.NewBuildArgs(opts.BuildArgs)
|
||||||
for index, cmd := range s.stage.Commands {
|
for index, cmd := range s.stage.Commands {
|
||||||
finalCmd := index == len(s.stage.Commands)-1
|
finalCmd := index == len(s.stage.Commands)-1
|
||||||
dockerCommand, err := commands.GetCommand(cmd, opts.SrcContext)
|
command, err := commands.GetCommand(cmd, opts.SrcContext)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if dockerCommand == nil {
|
if command == nil {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
logrus.Info(dockerCommand.String())
|
logrus.Info(command.String())
|
||||||
if err := dockerCommand.ExecuteCommand(&s.ConfigFile.Config, buildArgs); err != nil {
|
if err := command.ExecuteCommand(&s.cf.Config, args); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
snapshotFiles := dockerCommand.FilesToSnapshot()
|
files := command.FilesToSnapshot()
|
||||||
var contents []byte
|
var contents []byte
|
||||||
|
|
||||||
// If this is an intermediate stage, we only snapshot for the last command and we
|
// If this is an intermediate stage, we only snapshot for the last command and we
|
||||||
// want to snapshot the entire filesystem since we aren't tracking what was changed
|
// want to snapshot the entire filesystem since we aren't tracking what was changed
|
||||||
// by previous commands.
|
// by previous commands.
|
||||||
if !s.stage.FinalStage {
|
if !s.stage.Final {
|
||||||
if finalCmd {
|
if finalCmd {
|
||||||
contents, err = s.Snapshotter.TakeSnapshotFS()
|
contents, err = s.Snapshotter.TakeSnapshotFS()
|
||||||
}
|
}
|
||||||
|
|
@ -139,15 +139,15 @@ func (s *stageBuilder) buildStage(opts *config.KanikoOptions) error {
|
||||||
// Otherwise, in the final stage we take a snapshot at each command. If we know
|
// Otherwise, in the final stage we take a snapshot at each command. If we know
|
||||||
// the files that were changed, we'll snapshot those explicitly, otherwise we'll
|
// the files that were changed, we'll snapshot those explicitly, otherwise we'll
|
||||||
// check if anything in the filesystem changed.
|
// check if anything in the filesystem changed.
|
||||||
if snapshotFiles != nil {
|
if files != nil {
|
||||||
contents, err = s.Snapshotter.TakeSnapshot(snapshotFiles)
|
contents, err = s.Snapshotter.TakeSnapshot(files)
|
||||||
} else {
|
} else {
|
||||||
contents, err = s.Snapshotter.TakeSnapshotFS()
|
contents, err = s.Snapshotter.TakeSnapshotFS()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("Error taking snapshot of files for command %s: %s", dockerCommand, err)
|
return fmt.Errorf("Error taking snapshot of files for command %s: %s", command, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
util.MoveVolumeWhitelistToWhitelist()
|
util.MoveVolumeWhitelistToWhitelist()
|
||||||
|
|
@ -163,12 +163,12 @@ func (s *stageBuilder) buildStage(opts *config.KanikoOptions) error {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
s.Image, err = mutate.Append(s.Image,
|
s.image, err = mutate.Append(s.image,
|
||||||
mutate.Addendum{
|
mutate.Addendum{
|
||||||
Layer: layer,
|
Layer: layer,
|
||||||
History: v1.History{
|
History: v1.History{
|
||||||
Author: constants.Author,
|
Author: constants.Author,
|
||||||
CreatedBy: dockerCommand.String(),
|
CreatedBy: command.String(),
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
|
|
@ -187,18 +187,18 @@ func DoBuild(opts *config.KanikoOptions) (v1.Image, error) {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
for index, stage := range stages {
|
for index, stage := range stages {
|
||||||
stageBuilder, err := newStageBuilder(opts, stage)
|
sb, err := newStageBuilder(opts, stage)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, errors.Wrap(err, fmt.Sprintf("getting stage builder for stage %d", index))
|
return nil, errors.Wrap(err, fmt.Sprintf("getting stage builder for stage %d", index))
|
||||||
}
|
}
|
||||||
if err := stageBuilder.buildStage(opts); err != nil {
|
if err := sb.build(opts); err != nil {
|
||||||
return nil, errors.Wrap(err, "error building stage")
|
return nil, errors.Wrap(err, "error building stage")
|
||||||
}
|
}
|
||||||
sourceImage, err := mutate.Config(stageBuilder.Image, stageBuilder.ConfigFile.Config)
|
sourceImage, err := mutate.Config(sb.image, sb.cf.Config)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
if stage.FinalStage {
|
if stage.Final {
|
||||||
sourceImage, err = mutate.CreatedAt(sourceImage, v1.Time{Time: time.Now()})
|
sourceImage, err = mutate.CreatedAt(sourceImage, v1.Time{Time: time.Now()})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue