Skip to content

Commit

Permalink
fix(staged-dockerfile): allow scratch base image
Browse files Browse the repository at this point in the history
Signed-off-by: Timofey Kirillov <timofey.kirillov@flant.com>
  • Loading branch information
distorhead committed Mar 23, 2023
1 parent 70b77ce commit 5bf6a27
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 16 deletions.
2 changes: 1 addition & 1 deletion pkg/build/build_phase.go
Expand Up @@ -583,7 +583,7 @@ func (phase *BuildPhase) fetchBaseImageForStage(ctx context.Context, img *image.
return phase.Conveyor.StorageManager.FetchStage(ctx, phase.Conveyor.ContainerBackend, phase.StagesIterator.PrevBuiltStage)
} else {
if err := img.FetchBaseImage(ctx); err != nil {
return fmt.Errorf("unable to fetch base image %s for stage %s: %w", img.GetBaseStageImage().Image.Name(), stg.LogDetailedName(), err)
return fmt.Errorf("unable to fetch base image %q for stage %s: %w", img.GetBaseStageImage().Image.Name(), stg.LogDetailedName(), err)
}
}
return nil
Expand Down
42 changes: 27 additions & 15 deletions pkg/build/image/image.go
Expand Up @@ -250,25 +250,33 @@ func (i *Image) SetupBaseImage(ctx context.Context, storageManager manager.Stora

if i.IsDockerfileImage && i.DockerfileImageConfig.Staged {
var info *image.Info
var err error
info, err = storageManager.GetImageInfo(ctx, i.baseImageReference, storageOpts)
if isUnsupportedMediaTypeError(err) {
if err := logboek.Context(ctx).Default().LogProcess("Pulling base image %s", i.baseStageImage.Image.Name()).
Options(func(options types.LogProcessOptionsInterface) {
options.Style(style.Highlight())
}).
DoError(func() error {
return i.ContainerBackend.PullImageFromRegistry(ctx, i.baseStageImage.Image)
}); err != nil {
return err
}

if i.baseImageReference != "scratch" {
var err error
info, err = storageManager.GetImageInfo(ctx, i.baseImageReference, storageOpts)
if err != nil {
if isUnsupportedMediaTypeError(err) {
if err := logboek.Context(ctx).Default().LogProcess("Pulling base image %s", i.baseStageImage.Image.Name()).
Options(func(options types.LogProcessOptionsInterface) {
options.Style(style.Highlight())
}).
DoError(func() error {
return i.ContainerBackend.PullImageFromRegistry(ctx, i.baseStageImage.Image)
}); err != nil {
return err
}

info, err = storageManager.GetImageInfo(ctx, i.baseImageReference, storageOpts)
if err != nil {
return fmt.Errorf("unable to get base image %q manifest: %w", i.baseImageReference, err)
}
} else if err != nil {
return fmt.Errorf("unable to get base image %q manifest: %w", i.baseImageReference, err)
}
} else if err != nil {
return fmt.Errorf("unable to get base image %q manifest: %w", i.baseImageReference, err)
} else {
info = &image.Info{
Name: i.baseImageReference,
Env: nil,
}
}

i.baseStageImage.Image.SetStageDescription(&image.StageDescription{
Expand Down Expand Up @@ -310,6 +318,10 @@ func (i *Image) GetBaseImageReference() string {
func (i *Image) FetchBaseImage(ctx context.Context) error {
switch i.baseImageType {
case ImageFromRegistryAsBaseImage:
if i.baseStageImage.Image.Name() == "scratch" {
return nil
}

// TODO: Refactor, move manifest fetching into SetupBaseImage, only pull image in FetchBaseImage method

if info, err := i.ContainerBackend.GetImageInfo(ctx, i.baseStageImage.Image.Name(), container_backend.GetImageInfoOpts{}); err != nil {
Expand Down

0 comments on commit 5bf6a27

Please sign in to comment.