Skip to content

Commit b2475b6

Browse files
fix(build): fix incorrect base image when user removes werf labels
Signed-off-by: Aleksei Igrychev <aleksei.igrychev@palark.com>
1 parent 9a80dfd commit b2475b6

File tree

1 file changed

+25
-4
lines changed

1 file changed

+25
-4
lines changed

pkg/build/build_phase.go

+25-4
Original file line numberDiff line numberDiff line change
@@ -851,7 +851,14 @@ func (phase *BuildPhase) findAndFetchStageFromSecondaryStagesStorage(ctx context
851851
stg.SetStageImage(i)
852852

853853
// The stage digest remains the same, but the content digest may differ (e.g., the content digest of git and some user stages depends on the git commit).
854-
stg.SetContentDigest(stageDescCopy.Info.Labels[imagePkg.WerfStageContentDigestLabel])
854+
contentDigest, exist := stageDescCopy.Info.Labels[imagePkg.WerfStageContentDigestLabel]
855+
if exist {
856+
stg.SetContentDigest(contentDigest)
857+
} else if stg.Name() == stage.ImageSpec { // The content digest tag might be missing for the imageSpec stage (removed by the user).
858+
stg.SetContentDigest(stageDescCopy.Info.GetDigest())
859+
} else {
860+
panic(fmt.Sprintf("expected stage %q content digest label to be set!", stg.Name()))
861+
}
855862

856863
logboek.Context(ctx).Default().LogFHighlight("Use previously built image for %s\n", stg.LogDetailedName())
857864
container_backend.LogImageInfo(ctx, stg.GetStageImage().Image, phase.getPrevNonEmptyStageImageSize(), img.ShouldLogPlatform())
@@ -969,7 +976,14 @@ func (phase *BuildPhase) calculateStage(ctx context.Context, img *image.Image, s
969976
foundSuitableStage = true
970977

971978
// The stage digest remains the same, but the content digest may differ (e.g., the content digest of git and some user stages depends on the git commit).
972-
stageContentSig = stageDesc.Info.Labels[imagePkg.WerfStageContentDigestLabel]
979+
contentDigest, exist := stageDesc.Info.Labels[imagePkg.WerfStageContentDigestLabel]
980+
if exist {
981+
stageContentSig = contentDigest
982+
} else if stg.Name() == stage.ImageSpec { // The content digest tag might be missing for the imageSpec stage (removed by the user).
983+
stageContentSig = stageDesc.Info.GetDigest()
984+
} else {
985+
panic(fmt.Sprintf("expected stage %q content digest label to be set!", stg.Name()))
986+
}
973987
} else {
974988
stageContentSig, err = calculateDigest(ctx, fmt.Sprintf("%s-content", stg.Name()), "", stg, phase.Conveyor, calculateDigestOptions{TargetPlatform: img.TargetPlatform})
975989
if err != nil {
@@ -1167,8 +1181,15 @@ func (phase *BuildPhase) atomicBuildStageImage(ctx context.Context, img *image.I
11671181
i.Image.SetStageDesc(stageDesc)
11681182
stg.SetStageImage(i)
11691183

1170-
// The stage digest is equal but stage content digest might be different.
1171-
stg.SetContentDigest(stageDesc.Info.Labels[imagePkg.WerfStageContentDigestLabel])
1184+
// The stage digest remains the same, but the content digest may differ (e.g., the content digest of git and some user stages depends on the git commit).
1185+
contentDigest, exist := stageDesc.Info.Labels[imagePkg.WerfStageContentDigestLabel]
1186+
if exist {
1187+
stg.SetContentDigest(contentDigest)
1188+
} else if stg.Name() == stage.ImageSpec { // The content digest tag might be missing for the imageSpec stage (removed by the user).
1189+
stg.SetContentDigest(stageDesc.Info.GetDigest())
1190+
} else {
1191+
panic(fmt.Sprintf("expected stage %q content digest label to be set!", stg.Name()))
1192+
}
11721193

11731194
return nil
11741195
}

0 commit comments

Comments
 (0)