ImagesToPrune returns the set of images that should be removed.
(ctx context.Context, opts ImagePruneOptions)
| 72 | |
| 73 | // ImagesToPrune returns the set of images that should be removed. |
| 74 | func (p *ImagePruner) ImagesToPrune(ctx context.Context, opts ImagePruneOptions) ([]string, error) { |
| 75 | if opts.Mode == ImagePruneNone { |
| 76 | return nil, nil |
| 77 | } else if opts.Mode != ImagePruneLocal && opts.Mode != ImagePruneAll { |
| 78 | return nil, fmt.Errorf("unsupported image prune mode: %s", opts.Mode) |
| 79 | } |
| 80 | var images []string |
| 81 | |
| 82 | if opts.Mode == ImagePruneAll { |
| 83 | namedImages, err := p.namedImages(ctx) |
| 84 | if err != nil { |
| 85 | return nil, err |
| 86 | } |
| 87 | images = append(images, namedImages...) |
| 88 | } |
| 89 | |
| 90 | projectImages, err := p.labeledLocalImages(ctx) |
| 91 | if err != nil { |
| 92 | return nil, err |
| 93 | } |
| 94 | for _, img := range projectImages { |
| 95 | if len(img.RepoTags) == 0 { |
| 96 | // currently, we're only pruning the tagged references, but |
| 97 | // if we start removing the dangling images and grouping by |
| 98 | // service, we can remove this (and should rely on `Image::ID`) |
| 99 | continue |
| 100 | } |
| 101 | |
| 102 | var shouldPrune bool |
| 103 | if opts.RemoveOrphans { |
| 104 | // indiscriminately prune all project images even if they're not |
| 105 | // referenced by the current Compose state (e.g. the service was |
| 106 | // removed from YAML) |
| 107 | shouldPrune = true |
| 108 | } else { |
| 109 | // only prune the image if it belongs to a known service for the project. |
| 110 | if _, err := p.project.GetService(img.Labels[api.ServiceLabel]); err == nil { |
| 111 | shouldPrune = true |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | if shouldPrune { |
| 116 | images = append(images, img.RepoTags[0]) |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | fallbackImages, err := p.unlabeledLocalImages(ctx) |
| 121 | if err != nil { |
| 122 | return nil, err |
| 123 | } |
| 124 | images = append(images, fallbackImages...) |
| 125 | |
| 126 | images = normalizeAndDedupeImages(images) |
| 127 | return images, nil |
| 128 | } |
| 129 | |
| 130 | // namedImages are those that are explicitly named in the service config. |
| 131 | // |
no test coverage detected