2018-02-05 21:05:59 +00:00
|
|
|
package image // import "github.com/docker/docker/image"
|
2015-11-18 22:18:07 +00:00
|
|
|
|
|
|
|
import (
|
2023-06-23 00:33:17 +00:00
|
|
|
"context"
|
2015-11-18 22:18:07 +00:00
|
|
|
"fmt"
|
|
|
|
"sync"
|
2017-03-02 20:47:02 +00:00
|
|
|
"time"
|
2015-11-18 22:18:07 +00:00
|
|
|
|
2023-09-13 15:41:45 +00:00
|
|
|
"github.com/containerd/log"
|
2022-12-06 11:27:39 +00:00
|
|
|
"github.com/docker/docker/errdefs"
|
2015-11-18 22:18:07 +00:00
|
|
|
"github.com/docker/docker/layer"
|
2022-03-04 13:49:42 +00:00
|
|
|
"github.com/opencontainers/go-digest"
|
2020-05-12 11:22:19 +00:00
|
|
|
"github.com/opencontainers/go-digest/digestset"
|
2017-05-25 21:03:29 +00:00
|
|
|
"github.com/pkg/errors"
|
2015-11-18 22:18:07 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Store is an interface for creating and accessing images
|
|
|
|
type Store interface {
|
|
|
|
Create(config []byte) (ID, error)
|
|
|
|
Get(id ID) (*Image, error)
|
|
|
|
Delete(id ID) ([]layer.Metadata, error)
|
|
|
|
Search(partialID string) (ID, error)
|
|
|
|
SetParent(id ID, parent ID) error
|
|
|
|
GetParent(id ID) (ID, error)
|
2017-03-02 20:47:02 +00:00
|
|
|
SetLastUpdated(id ID) error
|
|
|
|
GetLastUpdated(id ID) (time.Time, error)
|
2015-11-18 22:18:07 +00:00
|
|
|
Children(id ID) []ID
|
|
|
|
Map() map[ID]*Image
|
|
|
|
Heads() map[ID]*Image
|
2018-03-13 15:58:05 +00:00
|
|
|
Len() int
|
2015-11-18 22:18:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// LayerGetReleaser is a minimal interface for getting and releasing images.
|
|
|
|
type LayerGetReleaser interface {
|
|
|
|
Get(layer.ChainID) (layer.Layer, error)
|
|
|
|
Release(layer.Layer) ([]layer.Metadata, error)
|
|
|
|
}
|
|
|
|
|
|
|
|
type imageMeta struct {
|
|
|
|
layer layer.Layer
|
|
|
|
children map[ID]struct{}
|
|
|
|
}
|
|
|
|
|
|
|
|
type store struct {
|
2017-06-21 01:31:29 +00:00
|
|
|
sync.RWMutex
|
2021-03-19 14:34:08 +00:00
|
|
|
lss LayerGetReleaser
|
2015-11-18 22:18:07 +00:00
|
|
|
images map[ID]*imageMeta
|
|
|
|
fs StoreBackend
|
2017-01-07 01:23:18 +00:00
|
|
|
digestSet *digestset.Set
|
2015-11-18 22:18:07 +00:00
|
|
|
}
|
|
|
|
|
2017-09-19 19:14:46 +00:00
|
|
|
// NewImageStore returns new store object for given set of layer stores
|
2021-03-19 14:34:08 +00:00
|
|
|
func NewImageStore(fs StoreBackend, lss LayerGetReleaser) (Store, error) {
|
2015-11-18 22:18:07 +00:00
|
|
|
is := &store{
|
2017-09-19 19:14:46 +00:00
|
|
|
lss: lss,
|
2015-11-18 22:18:07 +00:00
|
|
|
images: make(map[ID]*imageMeta),
|
|
|
|
fs: fs,
|
2017-01-07 01:23:18 +00:00
|
|
|
digestSet: digestset.NewSet(),
|
2015-11-18 22:18:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// load all current images and retain layers
|
|
|
|
if err := is.restore(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return is, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (is *store) restore() error {
|
2022-11-08 11:29:38 +00:00
|
|
|
// As the code below is run when restoring all images (which can be "many"),
|
2023-06-23 00:33:17 +00:00
|
|
|
// constructing the "log.G(ctx).WithFields" is deliberately not "DRY", as the
|
2022-11-08 11:29:38 +00:00
|
|
|
// logger is only used for error-cases, and we don't want to do allocations
|
|
|
|
// if we don't need it. The "f" type alias is here is just for convenience,
|
|
|
|
// and to make the code _slightly_ more DRY. See the discussion on GitHub;
|
|
|
|
// https://github.com/moby/moby/pull/44426#discussion_r1059519071
|
2023-07-30 15:18:56 +00:00
|
|
|
type f = log.Fields
|
2016-09-15 23:37:32 +00:00
|
|
|
err := is.fs.Walk(func(dgst digest.Digest) error {
|
2022-11-08 11:03:20 +00:00
|
|
|
img, err := is.Get(ID(dgst))
|
2015-11-18 22:18:07 +00:00
|
|
|
if err != nil {
|
2023-06-23 00:33:17 +00:00
|
|
|
log.G(context.TODO()).WithFields(f{"digest": dgst, "err": err}).Error("invalid image")
|
2015-11-18 22:18:07 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
var l layer.Layer
|
|
|
|
if chainID := img.RootFS.ChainID(); chainID != "" {
|
2023-09-06 10:35:50 +00:00
|
|
|
if err := CheckOS(img.OperatingSystem()); err != nil {
|
2023-06-23 00:33:17 +00:00
|
|
|
log.G(context.TODO()).WithFields(f{"chainID": chainID, "os": img.OperatingSystem()}).Error("not restoring image with unsupported operating system")
|
2018-02-23 23:29:26 +00:00
|
|
|
return nil
|
2017-11-20 16:33:20 +00:00
|
|
|
}
|
2021-03-19 14:34:08 +00:00
|
|
|
l, err = is.lss.Get(chainID)
|
2015-11-18 22:18:07 +00:00
|
|
|
if err != nil {
|
2022-11-25 12:26:46 +00:00
|
|
|
if errors.Is(err, layer.ErrLayerDoesNotExist) {
|
2023-06-23 00:33:17 +00:00
|
|
|
log.G(context.TODO()).WithFields(f{"chainID": chainID, "os": img.OperatingSystem(), "err": err}).Error("not restoring image")
|
2018-02-13 19:39:58 +00:00
|
|
|
return nil
|
|
|
|
}
|
2015-11-18 22:18:07 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2016-09-15 23:37:32 +00:00
|
|
|
if err := is.digestSet.Add(dgst); err != nil {
|
2015-11-18 22:18:07 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-11-08 11:03:20 +00:00
|
|
|
is.images[ID(dgst)] = &imageMeta{
|
2015-11-18 22:18:07 +00:00
|
|
|
layer: l,
|
|
|
|
children: make(map[ID]struct{}),
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Second pass to fill in children maps
|
|
|
|
for id := range is.images {
|
|
|
|
if parent, err := is.GetParent(id); err == nil {
|
|
|
|
if parentMeta := is.images[parent]; parentMeta != nil {
|
|
|
|
parentMeta.children[id] = struct{}{}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (is *store) Create(config []byte) (ID, error) {
|
2021-01-11 14:59:35 +00:00
|
|
|
var img *Image
|
|
|
|
img, err := NewFromJSON(config)
|
2015-11-18 22:18:07 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Must reject any config that references diffIDs from the history
|
|
|
|
// which aren't among the rootfs layers.
|
|
|
|
rootFSLayers := make(map[layer.DiffID]struct{})
|
|
|
|
for _, diffID := range img.RootFS.DiffIDs {
|
|
|
|
rootFSLayers[diffID] = struct{}{}
|
|
|
|
}
|
|
|
|
|
|
|
|
layerCounter := 0
|
|
|
|
for _, h := range img.History {
|
|
|
|
if !h.EmptyLayer {
|
|
|
|
layerCounter++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if layerCounter > len(img.RootFS.DiffIDs) {
|
2022-12-06 11:27:39 +00:00
|
|
|
return "", errdefs.InvalidParameter(errors.New("too many non-empty layers in History section"))
|
2015-11-18 22:18:07 +00:00
|
|
|
}
|
|
|
|
|
2022-11-08 11:03:20 +00:00
|
|
|
imageDigest, err := is.fs.Set(config)
|
2015-11-18 22:18:07 +00:00
|
|
|
if err != nil {
|
2022-12-06 11:27:39 +00:00
|
|
|
return "", errdefs.InvalidParameter(err)
|
2015-11-18 22:18:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
is.Lock()
|
|
|
|
defer is.Unlock()
|
|
|
|
|
2022-11-08 11:03:20 +00:00
|
|
|
imageID := ID(imageDigest)
|
2015-11-18 22:18:07 +00:00
|
|
|
if _, exists := is.images[imageID]; exists {
|
|
|
|
return imageID, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
layerID := img.RootFS.ChainID()
|
|
|
|
|
|
|
|
var l layer.Layer
|
|
|
|
if layerID != "" {
|
2023-09-06 10:35:50 +00:00
|
|
|
if err := CheckOS(img.OperatingSystem()); err != nil {
|
|
|
|
return "", err
|
2017-11-20 16:33:20 +00:00
|
|
|
}
|
2021-03-19 14:34:08 +00:00
|
|
|
l, err = is.lss.Get(layerID)
|
2015-11-18 22:18:07 +00:00
|
|
|
if err != nil {
|
2022-12-06 11:27:39 +00:00
|
|
|
return "", errdefs.InvalidParameter(errors.Wrapf(err, "failed to get layer %s", layerID))
|
2015-11-18 22:18:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-08 11:03:20 +00:00
|
|
|
is.images[imageID] = &imageMeta{
|
2015-11-18 22:18:07 +00:00
|
|
|
layer: l,
|
|
|
|
children: make(map[ID]struct{}),
|
|
|
|
}
|
|
|
|
|
2022-11-08 11:03:20 +00:00
|
|
|
if err = is.digestSet.Add(imageDigest); err != nil {
|
2015-11-18 22:18:07 +00:00
|
|
|
delete(is.images, imageID)
|
2022-12-06 11:27:39 +00:00
|
|
|
return "", errdefs.InvalidParameter(err)
|
2015-11-18 22:18:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return imageID, nil
|
|
|
|
}
|
|
|
|
|
2017-07-19 14:20:13 +00:00
|
|
|
type imageNotFoundError string
|
|
|
|
|
|
|
|
func (e imageNotFoundError) Error() string {
|
|
|
|
return "No such image: " + string(e)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (imageNotFoundError) NotFound() {}
|
|
|
|
|
2015-11-18 22:18:07 +00:00
|
|
|
func (is *store) Search(term string) (ID, error) {
|
|
|
|
dgst, err := is.digestSet.Lookup(term)
|
|
|
|
if err != nil {
|
2017-01-07 01:23:18 +00:00
|
|
|
if err == digestset.ErrDigestNotFound {
|
2017-07-19 14:20:13 +00:00
|
|
|
err = imageNotFoundError(term)
|
2016-03-04 10:05:34 +00:00
|
|
|
}
|
2017-07-19 14:20:13 +00:00
|
|
|
return "", errors.WithStack(err)
|
2015-11-18 22:18:07 +00:00
|
|
|
}
|
2022-11-08 11:03:20 +00:00
|
|
|
return ID(dgst), nil
|
2015-11-18 22:18:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (is *store) Get(id ID) (*Image, error) {
|
|
|
|
// todo: Check if image is in images
|
|
|
|
// todo: Detect manual insertions and start using them
|
2016-09-15 23:37:32 +00:00
|
|
|
config, err := is.fs.Get(id.Digest())
|
2015-11-18 22:18:07 +00:00
|
|
|
if err != nil {
|
2022-12-06 11:27:39 +00:00
|
|
|
return nil, errdefs.NotFound(err)
|
2015-11-18 22:18:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
img, err := NewFromJSON(config)
|
|
|
|
if err != nil {
|
2022-12-06 11:27:39 +00:00
|
|
|
return nil, errdefs.InvalidParameter(err)
|
2015-11-18 22:18:07 +00:00
|
|
|
}
|
|
|
|
img.computedID = id
|
|
|
|
|
|
|
|
img.Parent, err = is.GetParent(id)
|
|
|
|
if err != nil {
|
|
|
|
img.Parent = ""
|
|
|
|
}
|
|
|
|
|
|
|
|
return img, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (is *store) Delete(id ID) ([]layer.Metadata, error) {
|
|
|
|
is.Lock()
|
|
|
|
defer is.Unlock()
|
|
|
|
|
2022-11-08 11:48:36 +00:00
|
|
|
imgMeta := is.images[id]
|
|
|
|
if imgMeta == nil {
|
2022-12-06 11:27:39 +00:00
|
|
|
return nil, errdefs.NotFound(fmt.Errorf("unrecognized image ID %s", id.String()))
|
2015-11-18 22:18:07 +00:00
|
|
|
}
|
2022-01-25 11:18:44 +00:00
|
|
|
_, err := is.Get(id)
|
2018-01-18 20:56:28 +00:00
|
|
|
if err != nil {
|
2022-12-06 11:27:39 +00:00
|
|
|
return nil, errdefs.NotFound(fmt.Errorf("unrecognized image %s, %v", id.String(), err))
|
2018-01-18 20:56:28 +00:00
|
|
|
}
|
2022-11-08 11:48:36 +00:00
|
|
|
for cID := range imgMeta.children {
|
|
|
|
is.fs.DeleteMetadata(cID.Digest(), "parent")
|
2015-11-18 22:18:07 +00:00
|
|
|
}
|
|
|
|
if parent, err := is.GetParent(id); err == nil && is.images[parent] != nil {
|
|
|
|
delete(is.images[parent].children, id)
|
|
|
|
}
|
|
|
|
|
2016-09-15 23:37:32 +00:00
|
|
|
if err := is.digestSet.Remove(id.Digest()); err != nil {
|
2023-06-23 00:33:17 +00:00
|
|
|
log.G(context.TODO()).Errorf("error removing %s from digest set: %q", id, err)
|
2015-12-04 21:15:54 +00:00
|
|
|
}
|
2015-11-18 22:18:07 +00:00
|
|
|
delete(is.images, id)
|
2016-09-15 23:37:32 +00:00
|
|
|
is.fs.Delete(id.Digest())
|
2015-11-18 22:18:07 +00:00
|
|
|
|
2022-11-08 11:48:36 +00:00
|
|
|
if imgMeta.layer != nil {
|
|
|
|
return is.lss.Release(imgMeta.layer)
|
2015-11-18 22:18:07 +00:00
|
|
|
}
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2022-11-08 11:48:36 +00:00
|
|
|
func (is *store) SetParent(id, parentID ID) error {
|
2015-11-18 22:18:07 +00:00
|
|
|
is.Lock()
|
|
|
|
defer is.Unlock()
|
2022-11-08 11:48:36 +00:00
|
|
|
parentMeta := is.images[parentID]
|
2015-11-18 22:18:07 +00:00
|
|
|
if parentMeta == nil {
|
2022-11-08 11:48:36 +00:00
|
|
|
return errdefs.NotFound(fmt.Errorf("unknown parent image ID %s", parentID.String()))
|
2015-11-18 22:18:07 +00:00
|
|
|
}
|
2016-02-06 01:04:44 +00:00
|
|
|
if parent, err := is.GetParent(id); err == nil && is.images[parent] != nil {
|
|
|
|
delete(is.images[parent].children, id)
|
|
|
|
}
|
2015-11-18 22:18:07 +00:00
|
|
|
parentMeta.children[id] = struct{}{}
|
2022-11-08 11:48:36 +00:00
|
|
|
return is.fs.SetMetadata(id.Digest(), "parent", []byte(parentID))
|
2015-11-18 22:18:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (is *store) GetParent(id ID) (ID, error) {
|
2016-09-15 23:37:32 +00:00
|
|
|
d, err := is.fs.GetMetadata(id.Digest(), "parent")
|
2015-11-18 22:18:07 +00:00
|
|
|
if err != nil {
|
2022-12-06 11:27:39 +00:00
|
|
|
return "", errdefs.NotFound(err)
|
2015-11-18 22:18:07 +00:00
|
|
|
}
|
|
|
|
return ID(d), nil // todo: validate?
|
|
|
|
}
|
|
|
|
|
2017-03-02 20:47:02 +00:00
|
|
|
// SetLastUpdated time for the image ID to the current time
|
|
|
|
func (is *store) SetLastUpdated(id ID) error {
|
|
|
|
lastUpdated := []byte(time.Now().Format(time.RFC3339Nano))
|
|
|
|
return is.fs.SetMetadata(id.Digest(), "lastUpdated", lastUpdated)
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetLastUpdated time for the image ID
|
|
|
|
func (is *store) GetLastUpdated(id ID) (time.Time, error) {
|
|
|
|
bytes, err := is.fs.GetMetadata(id.Digest(), "lastUpdated")
|
|
|
|
if err != nil || len(bytes) == 0 {
|
|
|
|
// No lastUpdated time
|
|
|
|
return time.Time{}, nil
|
|
|
|
}
|
|
|
|
return time.Parse(time.RFC3339Nano, string(bytes))
|
|
|
|
}
|
|
|
|
|
2015-11-18 22:18:07 +00:00
|
|
|
func (is *store) Children(id ID) []ID {
|
2017-06-21 01:31:29 +00:00
|
|
|
is.RLock()
|
|
|
|
defer is.RUnlock()
|
2015-11-18 22:18:07 +00:00
|
|
|
|
|
|
|
return is.children(id)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (is *store) children(id ID) []ID {
|
|
|
|
var ids []ID
|
|
|
|
if is.images[id] != nil {
|
|
|
|
for id := range is.images[id].children {
|
|
|
|
ids = append(ids, id)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ids
|
|
|
|
}
|
|
|
|
|
|
|
|
func (is *store) Heads() map[ID]*Image {
|
|
|
|
return is.imagesMap(false)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (is *store) Map() map[ID]*Image {
|
|
|
|
return is.imagesMap(true)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (is *store) imagesMap(all bool) map[ID]*Image {
|
2017-06-21 01:31:29 +00:00
|
|
|
is.RLock()
|
|
|
|
defer is.RUnlock()
|
2015-11-18 22:18:07 +00:00
|
|
|
|
|
|
|
images := make(map[ID]*Image)
|
|
|
|
|
|
|
|
for id := range is.images {
|
|
|
|
if !all && len(is.children(id)) > 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
img, err := is.Get(id)
|
|
|
|
if err != nil {
|
2023-06-23 00:33:17 +00:00
|
|
|
log.G(context.TODO()).Errorf("invalid image access: %q, error: %q", id, err)
|
2015-11-18 22:18:07 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
images[id] = img
|
|
|
|
}
|
|
|
|
return images
|
|
|
|
}
|
2018-03-13 15:58:05 +00:00
|
|
|
|
|
|
|
func (is *store) Len() int {
|
|
|
|
is.RLock()
|
|
|
|
defer is.RUnlock()
|
|
|
|
return len(is.images)
|
|
|
|
}
|