diff options
author | OpenShift Merge Robot <openshift-merge-robot@users.noreply.github.com> | 2019-03-14 09:53:49 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-03-14 09:53:49 -0700 |
commit | fc5951ad55b5d9c4e2cc9ca0188c1adf8a12a3bb (patch) | |
tree | 06c269da2e787b6ed77cb9be5e0d835d336109bb /libpod/image | |
parent | 38d2b952fb5e285183795e2b2533e83bfdf96615 (diff) | |
parent | a4b3b9ffbb9bf4cac1863ac8c3b5dbf7748f9fdd (diff) | |
download | podman-fc5951ad55b5d9c4e2cc9ca0188c1adf8a12a3bb.tar.gz podman-fc5951ad55b5d9c4e2cc9ca0188c1adf8a12a3bb.tar.bz2 podman-fc5951ad55b5d9c4e2cc9ca0188c1adf8a12a3bb.zip |
Merge pull request #1642 from kunalkushwaha/image-tree
Tree implementation for podman images
Diffstat (limited to 'libpod/image')
-rw-r--r-- | libpod/image/image.go | 67 |
1 files changed, 67 insertions, 0 deletions
diff --git a/libpod/image/image.go b/libpod/image/image.go index 72f07dad1..c5939e055 100644 --- a/libpod/image/image.go +++ b/libpod/image/image.go @@ -1212,3 +1212,70 @@ func (i *Image) newImageEvent(status events.Status) { logrus.Infof("unable to write event to %s", i.imageruntime.EventsLogFilePath) } } + +// LayerInfo keeps information of single layer +type LayerInfo struct { + // Layer ID + ID string + // Parent ID of current layer. + ParentID string + // ChildID of current layer. + // there can be multiple children in case of fork + ChildID []string + // RepoTag will have image repo names, if layer is top layer of image + RepoTags []string + // Size stores Uncompressed size of layer. + Size int64 +} + +// GetLayersMapWithImageInfo returns map of image-layers, with associated information like RepoTags, parent and list of child layers. +func GetLayersMapWithImageInfo(imageruntime *Runtime) (map[string]*LayerInfo, error) { + + // Memory allocated to store map of layers with key LayerID. + // Map will build dependency chain with ParentID and ChildID(s) + layerInfoMap := make(map[string]*LayerInfo) + + // scan all layers & fill size and parent id for each layer in layerInfoMap + layers, err := imageruntime.store.Layers() + if err != nil { + return nil, err + } + for _, layer := range layers { + _, ok := layerInfoMap[layer.ID] + if !ok { + layerInfoMap[layer.ID] = &LayerInfo{ + ID: layer.ID, + Size: layer.UncompressedSize, + ParentID: layer.Parent, + } + } else { + return nil, fmt.Errorf("detected multiple layers with the same ID %q", layer.ID) + } + } + + // scan all layers & add all childs for each layers to layerInfo + for _, layer := range layers { + _, ok := layerInfoMap[layer.ID] + if ok { + if layer.Parent != "" { + layerInfoMap[layer.Parent].ChildID = append(layerInfoMap[layer.Parent].ChildID, layer.ID) + } + } else { + return nil, fmt.Errorf("lookup error: layer-id %s, not found", layer.ID) + } + } + + // Add the Repo Tags to Top layer of each image. + imgs, err := imageruntime.store.Images() + if err != nil { + return nil, err + } + for _, img := range imgs { + e, ok := layerInfoMap[img.TopLayer] + if !ok { + return nil, fmt.Errorf("top-layer for image %s not found local store", img.ID) + } + e.RepoTags = append(e.RepoTags, img.Names...) + } + return layerInfoMap, nil +} |