Skip to content

Commit

Permalink
fix(cleanup): fix cleanup for Buildah images by switching to tag-base…
Browse files Browse the repository at this point in the history
…d logic

The cleanup process for images built using Buildah was not working as expected due to an issue with digest-based parent lookup, as outlined in Buildah #3866. This issue occurs because Buildah currently does not provide a reliable way to retrieve the digest of an image without publishing it to a registry.

To address this, the cleanup logic has been updated to use image tags instead of digests as the unique identifier for the images. This change ensures that the cleanup process works as expected, even for images built with Buildah.

Signed-off-by: Aleksei Igrychev <aleksei.igrychev@palark.com>
  • Loading branch information
alexey-igrychev committed Nov 25, 2024
1 parent 51f25ca commit 73fcc1b
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 5 deletions.
3 changes: 3 additions & 0 deletions pkg/build/build_phase.go
Original file line number Diff line number Diff line change
Expand Up @@ -998,6 +998,9 @@ func (phase *BuildPhase) prepareStageInstructions(ctx context.Context, img *imag

if stg.HasPrevStage() {
prevBuiltImage := phase.StagesIterator.GetPrevBuiltImage(img, stg)
serviceLabels[imagePkg.WerfParentStageID] = prevBuiltImage.Image.GetStageDesc().StageID.String()

// TODO: remove this legacy logic in v3.
serviceLabels[imagePkg.WerfBaseImageIDLabel] = prevBuiltImage.Image.GetStageDesc().Info.ID
}

Expand Down
21 changes: 16 additions & 5 deletions pkg/cleaning/cleanup.go
Original file line number Diff line number Diff line change
Expand Up @@ -882,11 +882,22 @@ func (m *cleanupManager) getRelativeStageDescSetByStageDesc(targetStageDesc *ima
relativeStageDescSet.Add(currentStageDesc)
currentStageDescSet.Remove(currentStageDesc)

for stageDesc := range stageDescSet.Iter() {
if currentStageDesc.Info.ParentID == stageDesc.Info.ID {
relativeStageDescSet.Add(stageDesc)
currentStageDescSet.Add(stageDesc)
break
relativeStageDescSet.Add(currentStageDesc)
currentStageDescSet.Remove(currentStageDesc)

// Parent stage checking.
{
// TODO: remove this legacy check in v3.
for stageDesc := range stageDescSet.Iter() {
if currentStageDesc.Info.ParentID == stageDesc.Info.ID {
currentStageDescSet.Add(stageDesc)
break
}
}

parentStageDesc := m.stageManager.GetStageDescByStageID(currentStageDesc.Info.Labels[image.WerfParentStageID])
if parentStageDesc != nil {
currentStageDescSet.Add(parentStageDesc)
}
}
}
Expand Down
1 change: 1 addition & 0 deletions pkg/container_backend/buildah_backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -596,6 +596,7 @@ func (backend *BuildahBackend) GetImageInfo(ctx context.Context, ref string, opt
return nil, nil
}

// TODO: remove this legacy logic in v3.
parentID := string(inspect.Docker.Parent)
if parentID == "" {
if id, ok := inspect.Docker.Config.Labels[image.WerfBaseImageIDLabel]; ok { // built with werf
Expand Down
1 change: 1 addition & 0 deletions pkg/docker/image_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ func NewInfoFromInspect(ref string, inspect *types.ImageInspect) *image.Info {
repoDigest = image.ExtractRepoDigest(inspect.RepoDigests, repository)
}

// TODO: remove this legacy logic in v3.
parentID := inspect.Config.Image
if parentID == "" {
if id, ok := inspect.Config.Labels[image.WerfBaseImageIDLabel]; ok { // built with werf
Expand Down
1 change: 1 addition & 0 deletions pkg/docker_registry/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,7 @@ func (api *api) getRepoImageByDesc(ctx context.Context, originalTag string, desc
}
repoImage.Size = totalSize

// TODO: remove this legacy logic in v3.
parentID := configFile.Config.Image
if parentID == "" {
if id, ok := configFile.Config.Labels[image.WerfBaseImageIDLabel]; ok { // built with werf
Expand Down
1 change: 1 addition & 0 deletions pkg/image/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const (
WerfProjectRepoCommitLabel = "werf-project-repo-commit"
WerfImportChecksumLabelPrefix = "werf-import-checksum-"
WerfBaseImageIDLabel = "werf.io/base-image-id"
WerfParentStageID = "werf.io/parent-stage-id"

WerfImportMetadataChecksumLabel = "checksum"
WerfImportMetadataSourceImageIDLabel = "source-image-id"
Expand Down

0 comments on commit 73fcc1b

Please sign in to comment.