Skip to content

Commit 29c417f

Browse files
committed
Added dry run option in removeImage
Signed-off-by: Mark Zvenigorodsky <mark.zvenigorodsky@gmail.com>
1 parent e6e805c commit 29c417f

2 files changed

Lines changed: 18 additions & 11 deletions

File tree

common/libimage/image.go

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,7 @@ func (i *Image) Containers() ([]string, error) {
331331

332332
// removeContainers removes all containers using the image.
333333
func (i *Image) removeContainers(options *RemoveImagesOptions) error {
334-
if !options.Force && !options.ExternalContainers {
334+
if (!options.Force && !options.ExternalContainers) || options.DryRun {
335335
// Nothing to do.
336336
return nil
337337
}
@@ -487,13 +487,15 @@ func (i *Image) removeRecursive(ctx context.Context, rmMap map[string]*RemoveIma
487487

488488
// Only try to untag if we know it's not an ID or digest.
489489
if !byID && !byDigest {
490-
if err := i.Untag(referencedBy); handleError(err) != nil {
491-
return processedIDs, err
490+
if !options.DryRun {
491+
if err := i.Untag(referencedBy); handleError(err) != nil {
492+
return processedIDs, err
493+
}
494+
report.Untagged = append(report.Untagged, referencedBy)
495+
496+
// If there's still tags left, we cannot delete it.
497+
skipRemove = len(i.Names()) > 0
492498
}
493-
report.Untagged = append(report.Untagged, referencedBy)
494-
495-
// If there's still tags left, we cannot delete it.
496-
skipRemove = len(i.Names()) > 0
497499
}
498500
}
499501

@@ -528,11 +530,13 @@ func (i *Image) removeRecursive(ctx context.Context, rmMap map[string]*RemoveIma
528530
parent = nil
529531
}
530532

531-
if _, err := i.runtime.store.DeleteImage(i.ID(), true); handleError(err) != nil {
532-
if errors.Is(err, storage.ErrImageUsedByContainer) {
533-
err = fmt.Errorf("%w: consider listing external containers and force-removing image", err)
533+
if !options.DryRun {
534+
if _, err := i.runtime.store.DeleteImage(i.ID(), true); handleError(err) != nil {
535+
if errors.Is(err, storage.ErrImageUsedByContainer) {
536+
err = fmt.Errorf("%w: consider listing external containers and force-removing image", err)
537+
}
538+
return processedIDs, err
534539
}
535-
return processedIDs, err
536540
}
537541

538542
report.Untagged = append(report.Untagged, i.Names()...)

common/libimage/runtime.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -701,6 +701,9 @@ type RemoveImagesOptions struct {
701701
WithSize bool
702702
// NoPrune will not remove dangling images
703703
NoPrune bool
704+
// DryRun will not remove images but will display image id's
705+
// that would have been removed
706+
DryRun bool
704707
}
705708

706709
// RemoveImages removes images specified by names. If no names are specified,

0 commit comments

Comments
 (0)