Skip to content

Commit

Permalink
Add support for COPY --from=<an unrelated image>. (#479)
Browse files Browse the repository at this point in the history
Right now kaniko only supports COPY --from=<another stage>.
This commit adds support for the case where the referenced image is a remote image
in a registry that has not been used as a stage yet in the build.
  • Loading branch information
dlorenc authored Dec 6, 2018
1 parent 539ddef commit 7611ea7
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 27 deletions.
1 change: 1 addition & 0 deletions integration/dockerfiles/Dockerfile_test_multistage
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ FROM fedora@sha256:c4cc32b09c6ae3f1353e7e33a8dda93dc41676b923d6d89afa996b421cc5a
FROM base
ARG file
COPY --from=second /foo ${file}
COPY --from=gcr.io/google-appengine/debian9@sha256:00109fa40230a081f5ecffe0e814725042ff62a03e2d1eae0563f1f82eaeae9b /etc/os-release /new
1 change: 1 addition & 0 deletions pkg/dockerfile/dockerfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ func resolveStages(stages []instructions.Stage) {
if val, ok := nameToIndex[c.From]; ok {
c.From = val
}

}
}
}
Expand Down
52 changes: 44 additions & 8 deletions pkg/executor/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ import (
"strconv"
"time"

"github.com/moby/buildkit/frontend/dockerfile/instructions"

"golang.org/x/sync/errgroup"

"github.com/google/go-containerregistry/pkg/name"
Expand Down Expand Up @@ -337,6 +339,10 @@ func DoBuild(opts *config.KanikoOptions) (v1.Image, error) {
if err != nil {
return nil, err
}
// Some stages may refer to other random images, not previous stages
if err := fetchExtraStages(stages, opts); err != nil {
return nil, err
}
for index, stage := range stages {
sb, err := newStageBuilder(opts, stage)
if err != nil {
Expand Down Expand Up @@ -380,10 +386,10 @@ func DoBuild(opts *config.KanikoOptions) (v1.Image, error) {
return sourceImage, nil
}
if stage.SaveStage {
if err := saveStageAsTarball(index, sourceImage); err != nil {
if err := saveStageAsTarball(strconv.Itoa(index), sourceImage); err != nil {
return nil, err
}
if err := extractImageToDependecyDir(index, sourceImage); err != nil {
if err := extractImageToDependecyDir(strconv.Itoa(index), sourceImage); err != nil {
return nil, err
}
}
Expand All @@ -396,8 +402,38 @@ func DoBuild(opts *config.KanikoOptions) (v1.Image, error) {
return nil, err
}

func extractImageToDependecyDir(index int, image v1.Image) error {
dependencyDir := filepath.Join(constants.KanikoDir, strconv.Itoa(index))
func fetchExtraStages(stages []config.KanikoStage, opts *config.KanikoOptions) error {
for _, s := range stages {
for _, cmd := range s.Commands {
c, ok := cmd.(*instructions.CopyCommand)
if !ok || c.From == "" {
continue
}

// FROMs at this point are guaranteed to be either an integer referring to a previous stage or
// a name of a remote image.
if _, err := strconv.Atoi(c.From); err == nil {
continue
}
// This must be an image name, fetch it.
logrus.Debugf("Found extra base image stage %s", c.From)
sourceImage, err := util.RetrieveRemoteImage(c.From, opts)
if err != nil {
return err
}
if err := saveStageAsTarball(c.From, sourceImage); err != nil {
return err
}
if err := extractImageToDependecyDir(c.From, sourceImage); err != nil {
return err
}
}
}
return nil
}

func extractImageToDependecyDir(name string, image v1.Image) error {
dependencyDir := filepath.Join(constants.KanikoDir, name)
if err := os.MkdirAll(dependencyDir, 0755); err != nil {
return err
}
Expand All @@ -406,16 +442,16 @@ func extractImageToDependecyDir(index int, image v1.Image) error {
return err
}

func saveStageAsTarball(stageIndex int, image v1.Image) error {
func saveStageAsTarball(path string, image v1.Image) error {
destRef, err := name.NewTag("temp/tag", name.WeakValidation)
if err != nil {
return err
}
if err := os.MkdirAll(constants.KanikoIntermediateStagesDir, 0750); err != nil {
tarPath := filepath.Join(constants.KanikoIntermediateStagesDir, path)
logrus.Infof("Storing source image from stage %d at path %s", path, tarPath)
if err := os.MkdirAll(filepath.Dir(tarPath), 0750); err != nil {
return err
}
tarPath := filepath.Join(constants.KanikoIntermediateStagesDir, strconv.Itoa(stageIndex))
logrus.Infof("Storing source image from stage %d at path %s", stageIndex, tarPath)
return tarball.WriteToFile(tarPath, destRef, image)
}

Expand Down
31 changes: 15 additions & 16 deletions pkg/util/image_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ import (
)

var (
// For testing
retrieveRemoteImage = remoteImage
// RetrieveRemoteImage downloads an image from a remote location
RetrieveRemoteImage = remoteImage
retrieveTarImage = tarballImage
)

Expand All @@ -67,21 +67,8 @@ func RetrieveSourceImage(stage config.KanikoStage, opts *config.KanikoOptions) (
return retrieveTarImage(stage.BaseImageIndex)
}

// Next, 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 cachedImage != nil {
return cachedImage, nil
}

if err != nil {
logrus.Warnf("Error while retrieving image from cache: %v", err)
}
}

// Otherwise, initialize image as usual
return retrieveRemoteImage(currentBaseName, opts)
return RetrieveRemoteImage(currentBaseName, opts)
}

// RetrieveConfigFile returns the config file for an image
Expand All @@ -104,6 +91,18 @@ func tarballImage(index int) (v1.Image, error) {

func remoteImage(image string, opts *config.KanikoOptions) (v1.Image, error) {
logrus.Infof("Downloading base image %s", image)
// First, 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, image)

This comment has been minimized.

Copy link
@carlossg

carlossg Dec 8, 2018

Contributor

this causes the infinite loop reported in #482

if cachedImage != nil {
return cachedImage, nil
}

if err != nil {
logrus.Warnf("Error while retrieving image from cache: %v", err)
}
}
ref, err := name.ParseReference(image, name.WeakValidation)
if err != nil {
return nil, err
Expand Down
6 changes: 3 additions & 3 deletions pkg/util/image_util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,14 @@ func Test_StandardImage(t *testing.T) {
if err != nil {
t.Error(err)
}
original := retrieveRemoteImage
original := RetrieveRemoteImage
defer func() {
retrieveRemoteImage = original
RetrieveRemoteImage = original
}()
mock := func(image string, opts *config.KanikoOptions) (v1.Image, error) {
return nil, nil
}
retrieveRemoteImage = mock
RetrieveRemoteImage = mock
actual, err := RetrieveSourceImage(config.KanikoStage{
Stage: stages[0],
}, &config.KanikoOptions{})
Expand Down

0 comments on commit 7611ea7

Please sign in to comment.