-
Notifications
You must be signed in to change notification settings - Fork 202
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
The following changes were not split into smaller commits since the entire package is still work in progress and I want to keep moving: * Various small fixes. * The internal image cache has been removed as it's a recipe for inconsistencies for longer running processes. This should make libimage easier to use for CRI-O and a Podman service. * LookupImage now returns storage.ErrUnknownImage rather than nil. This simplifies the callers and makes sure we have a consistent error. * LookupImage is now able to handle manifests lists. Unless the platform is explicitly ignored via the options, the matching image within the manifest list is now returned. This greatly simplifies the spec generation in Podman; no callers should have to worry about this kind of detail. * LookupImage has been refactored into smaller-sized and easier to read functions. * RemoveImages has been changed to assemble the data of removed or untagged images. This comes in handy for pruning images. I am heavily against having a dedicated API for pruning since the it's really just a combination of filtering and removing images which RemoveImages already supports. Hence these changes to satisfy the needs of `podman image prune`. Furthermore, it now returns an []error slice rather than a single error. Again to make Podman happy which needs to inspect *all* errors for setting the appropriate exit code. * A rather large refactoring of the removal code along with very verbose comments. Those were largely absent in the Podman code base but there many rules and contracts embedded that I partially could only reconstruct by manually tests and comparing to Docker. * Add a new `containers={true,false}` filter which allows filtering images whether they are used by containers (=true) or if no container is using them (=false). This filter is required for pruning images in Podman. * `libimage/types` has been merged into `libimage`. Podman has to do _a lot of_ massaging for the remote client already and the types are pretty much nailed down for the remote API. Hence, I prefer to do some translation between `libimage` types and what Podman needs rather than splitting `libimage` in half without an obvious reason. This way the package is self-contained allowing for an easier navigation and maintenance. * `libimage.PullPolicy` has been merged into `pkg/config.PullPolicy` to have _one_ central place to deal with pull policies. The type system in `pkg/config` sets "always" as the default unfortunately but I think consistency is more important at that point. * Added `CopyOptions.DirForceCompress` to enforce layer compression when copying to a `dir` destination. * We now use `github.com/disiqueira/gotree` for pretty printing image trees. That greatly simplifies the code and we don't have to worry about the logic of printing a tree. Note that trees are now always printed top down! * Added a new `libimage.ManifestList` type along with an API for local lookups and performing certain operations on it to wrap around `libimage/manifests` as previously done in `libpod/image` and other places in Podman. * Correct caching of `(*Image).Inspect`. * In addition to username, password and credentials, allow for speciying an identity token for copying images. That's needed for Podman's remote API. * Make image removal more tolerant toward corrupted images. * A new "until=timestamp" filter that can be used by all APIs supporting filtering. * An empty string now resolves to PullPolicyMissing. * `(*Runtime) systemContextCopy()` returns a deep copy of the runtime's system context. Golang's shallow copies are very dangerous for long running processes such as Podman's system service. Hence, we need to make sure that base data is not altered over time. That adds another external dependency but I do not see a way around that. Long term, I desire a `(*containers/image/types.SystemContext).Copy()` function. Signed-off-by: Valentin Rothberg <[email protected]>
- Loading branch information
Showing
38 changed files
with
2,484 additions
and
536 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
package libimage | ||
|
||
import ( | ||
"context" | ||
"time" | ||
) | ||
|
||
// ImageDiskUsage reports the total size of an image. That is the size | ||
type ImageDiskUsage struct { | ||
// Number of containers using the image. | ||
Containers int | ||
// ID of the image. | ||
ID string | ||
// Repository of the image. | ||
Repository string | ||
// Tag of the image. | ||
Tag string | ||
// Created time stamp. | ||
Created time.Time | ||
// The amount of space that an image shares with another one (i.e. their common data). | ||
SharedSize int64 | ||
// The the amount of space that is only used by a given image. | ||
UniqueSize int64 | ||
// Sum of shared an unique size. | ||
Size int64 | ||
} | ||
|
||
// DiskUsage calculates the disk usage for each image in the local containers | ||
// storage. Note that a single image may yield multiple usage reports, one for | ||
// each repository tag. | ||
func (r *Runtime) DiskUsage(ctx context.Context) ([]ImageDiskUsage, error) { | ||
layerTree, err := r.layerTree() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
images, err := r.ListImages(ctx, nil, nil) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
var allUsages []ImageDiskUsage | ||
for _, image := range images { | ||
usages, err := diskUsageForImage(ctx, image, layerTree) | ||
if err != nil { | ||
return nil, err | ||
} | ||
allUsages = append(allUsages, usages...) | ||
} | ||
return allUsages, err | ||
} | ||
|
||
// diskUsageForImage returns the disk-usage baseistics for the specified image. | ||
func diskUsageForImage(ctx context.Context, image *Image, tree *layerTree) ([]ImageDiskUsage, error) { | ||
base := ImageDiskUsage{ | ||
ID: image.ID(), | ||
Created: image.Created(), | ||
Repository: "<none>", | ||
Tag: "<none>", | ||
} | ||
|
||
// Shared, unique and total size. | ||
parent, err := tree.parent(ctx, image) | ||
if err != nil { | ||
return nil, err | ||
} | ||
childIDs, err := tree.children(ctx, image, false) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
// Optimistically set unique size to the full size of the image. | ||
size, err := image.Size() | ||
if err != nil { | ||
return nil, err | ||
} | ||
base.UniqueSize = size | ||
|
||
if len(childIDs) > 0 { | ||
// If we have children, we share everything. | ||
base.SharedSize = base.UniqueSize | ||
base.UniqueSize = 0 | ||
} else if parent != nil { | ||
// If we have no children but a parent, remove the parent | ||
// (shared) size from the unique one. | ||
size, err := parent.Size() | ||
if err != nil { | ||
return nil, err | ||
} | ||
base.UniqueSize -= size | ||
base.SharedSize = size | ||
} | ||
|
||
base.Size = base.SharedSize + base.UniqueSize | ||
|
||
// Number of containers using the image. | ||
containers, err := image.Containers() | ||
if err != nil { | ||
return nil, err | ||
} | ||
base.Containers = len(containers) | ||
|
||
repoTags, err := image.NamedRepoTags() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if len(repoTags) == 0 { | ||
return []ImageDiskUsage{base}, nil | ||
} | ||
|
||
pairs, err := ToNameTagPairs(repoTags) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
results := make([]ImageDiskUsage, len(pairs)) | ||
for i, pair := range pairs { | ||
res := base | ||
res.Repository = pair.Name | ||
res.Tag = pair.Tag | ||
results[i] = res | ||
} | ||
|
||
return results, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.