2018-02-07 15:52:47 -05:00
|
|
|
package images // import "github.com/docker/docker/daemon/images"
|
2016-05-21 16:36:11 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2016-07-20 19:11:28 -04:00
|
|
|
"time"
|
2016-05-21 16:36:11 -04:00
|
|
|
|
2017-01-25 19:54:18 -05:00
|
|
|
"github.com/docker/distribution/reference"
|
2016-11-09 16:32:53 -05:00
|
|
|
"github.com/docker/docker/api/types/image"
|
2016-05-21 16:36:11 -04:00
|
|
|
"github.com/docker/docker/layer"
|
2018-02-05 17:41:45 -05:00
|
|
|
"github.com/docker/docker/pkg/system"
|
2016-05-21 16:36:11 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// ImageHistory returns a slice of ImageHistory structures for the specified image
|
|
|
|
// name by walking the image lineage.
|
2018-02-07 15:52:47 -05:00
|
|
|
func (i *ImageService) ImageHistory(name string) ([]*image.HistoryResponseItem, error) {
|
2016-07-20 19:11:28 -04:00
|
|
|
start := time.Now()
|
2020-03-19 16:54:48 -04:00
|
|
|
img, err := i.GetImage(name, nil)
|
2016-05-21 16:36:11 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2016-11-09 16:32:53 -05:00
|
|
|
history := []*image.HistoryResponseItem{}
|
2016-05-21 16:36:11 -04:00
|
|
|
|
|
|
|
layerCounter := 0
|
|
|
|
rootFS := *img.RootFS
|
|
|
|
rootFS.DiffIDs = nil
|
|
|
|
|
|
|
|
for _, h := range img.History {
|
|
|
|
var layerSize int64
|
|
|
|
|
|
|
|
if !h.EmptyLayer {
|
|
|
|
if len(img.RootFS.DiffIDs) <= layerCounter {
|
|
|
|
return nil, fmt.Errorf("too many non-empty layers in History section")
|
|
|
|
}
|
2018-02-05 17:41:45 -05:00
|
|
|
if !system.IsOSSupported(img.OperatingSystem()) {
|
|
|
|
return nil, system.ErrNotSupportedOperatingSystem
|
|
|
|
}
|
2016-05-21 16:36:11 -04:00
|
|
|
rootFS.Append(img.RootFS.DiffIDs[layerCounter])
|
2018-02-02 17:18:46 -05:00
|
|
|
l, err := i.layerStores[img.OperatingSystem()].Get(rootFS.ChainID())
|
2016-05-21 16:36:11 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
layerSize, err = l.DiffSize()
|
2018-02-02 17:18:46 -05:00
|
|
|
layer.ReleaseAndLog(i.layerStores[img.OperatingSystem()], l)
|
2016-05-21 16:36:11 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
layerCounter++
|
|
|
|
}
|
|
|
|
|
2016-11-09 16:32:53 -05:00
|
|
|
history = append([]*image.HistoryResponseItem{{
|
2016-05-21 16:36:11 -04:00
|
|
|
ID: "<missing>",
|
|
|
|
Created: h.Created.Unix(),
|
|
|
|
CreatedBy: h.CreatedBy,
|
|
|
|
Comment: h.Comment,
|
|
|
|
Size: layerSize,
|
|
|
|
}}, history...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fill in image IDs and tags
|
|
|
|
histImg := img
|
|
|
|
id := img.ID()
|
|
|
|
for _, h := range history {
|
|
|
|
h.ID = id.String()
|
|
|
|
|
|
|
|
var tags []string
|
2018-02-02 17:18:46 -05:00
|
|
|
for _, r := range i.referenceStore.References(id.Digest()) {
|
2016-05-21 16:36:11 -04:00
|
|
|
if _, ok := r.(reference.NamedTagged); ok {
|
2017-01-25 19:54:18 -05:00
|
|
|
tags = append(tags, reference.FamiliarString(r))
|
2016-05-21 16:36:11 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
h.Tags = tags
|
|
|
|
|
|
|
|
id = histImg.Parent
|
|
|
|
if id == "" {
|
|
|
|
break
|
|
|
|
}
|
2020-03-19 16:54:48 -04:00
|
|
|
histImg, err = i.GetImage(id.String(), nil)
|
2016-05-21 16:36:11 -04:00
|
|
|
if err != nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2016-07-20 19:11:28 -04:00
|
|
|
imageActions.WithValues("history").UpdateSince(start)
|
2016-05-21 16:36:11 -04:00
|
|
|
return history, nil
|
|
|
|
}
|