Skip to content

mz523: skip interstage clean#524

Draft
mzihlmann wants to merge 2 commits into
mainfrom
mz523-skip-interstage-clean
Draft

mz523: skip interstage clean#524
mzihlmann wants to merge 2 commits into
mainfrom
mz523-skip-interstage-clean

Conversation

@mzihlmann

Copy link
Copy Markdown
Collaborator

Fixes #523

Description

In multi-stage builds, kaniko always tears down the filesystem and re-unpacks the base image between stages. When a stage's FROM resolves to the immediately preceding preceding stage, this round-trip is redundant. With this change we introduce FF_KANIKO_SKIP_INTERSTAGE_CLEANUP that allows users to optionally skip redundant inter-stage cleanups.

@mzihlmann mzihlmann changed the title Mz523 skip interstage clean mz523: skip interstage clean Feb 24, 2026
@mzihlmann
mzihlmann force-pushed the mz523-skip-interstage-clean branch 2 times, most recently from f13accc to b3d6383 Compare February 24, 2026 22:54
@mzihlmann
mzihlmann force-pushed the mz523-skip-interstage-clean branch from b3d6383 to a3bb33f Compare February 25, 2026 21:38
@mzihlmann

mzihlmann commented Feb 25, 2026

Copy link
Copy Markdown
Collaborator Author

for now we should not yet skip saving the stages as in 3f85e78

			if i > 0 && kanikoStages[i].BaseImageIndex == kanikoStages[i-1].Index {
+				kanikoStages[i-1].SaveStage = stagesDependencies[kanikoStages[i-1].Index] > 1
				kanikoStages[i-1].Clean = false
				kanikoStages[i].Unpack = false
			}

The reason is that it breaks in newStageBuilder when we try to retrieve the sourceImage.

func newStageBuilder(args *dockerfile.BuildArgs, opts *config.KanikoOptions, stage config.KanikoStage, crossStageDeps map[int][]string, stageNameToIdx map[string]int, fileContext util.FileContext) (*stageBuilder, error) {
	sourceImage, err := image_util.RetrieveSourceImage(stage, opts)
	if err != nil {
		return nil, err
	}

which then breaks as the sourceImage was never saved in the first place.

We faced the same issue in multi-stage cache lookahead, although for a different reason. There we opted to keep the images in memory instead of the filesystem.
bdf99e2

For now, let's not take this second minor optimization and come back to it once we have cache lookahead merged.

Then on the other hand that would be a good way to get part of the change split out of cache lookahead, so it is less daunting for review. Maybe a part 2 to this change.

@mzihlmann

mzihlmann commented Feb 25, 2026

Copy link
Copy Markdown
Collaborator Author

nice! the integration tests caught another very complex issue

            INFO[0000] Building stage 'debian:12.10' [idx: '0', base-idx: '-1'] 
            INFO[0000] Skipping unpacking as no commands require it. 
            INFO[0000] Storing source image from stage 0 at path /kaniko/stages/0 
            INFO[0005] Base image from previous stage 0 found, using saved tar at path /kaniko/stages/0 
            INFO[0005] Building stage 'first-stage' [idx: '2', base-idx: '0'] 
            INFO[0005] Unpacking rootfs as cmd RUN touch test requires it. 
            INFO[0005] Skipping unpacking as no commands require it. 

Each stage can decide whether we need to unpack or not. But now with this logic we rely on the previous stage having unpacked the filesystem for us so we can skip doing that. Of course that can't work if the previous stage can decide that independently.

After multistage cache lookahead this would actually also be pretty easy to implement, as then we can check whether unpack will be required a-priori.

@mzihlmann

Copy link
Copy Markdown
Collaborator Author

so let's just defer his change after #299

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

skip cleanup between stages

1 participant