github.com/adityamillind98/moby@v23.0.0-rc.4+incompatible/distribution/config.go (about)

     1  package distribution // import "github.com/docker/docker/distribution"
     2  
     3  import (
     4  	"context"
     5  	"encoding/json"
     6  	"io"
     7  	"runtime"
     8  
     9  	"github.com/docker/distribution"
    10  	"github.com/docker/distribution/manifest/schema2"
    11  	"github.com/docker/docker/api/types"
    12  	"github.com/docker/docker/distribution/metadata"
    13  	"github.com/docker/docker/distribution/xfer"
    14  	"github.com/docker/docker/image"
    15  	"github.com/docker/docker/layer"
    16  	"github.com/docker/docker/pkg/progress"
    17  	"github.com/docker/docker/pkg/system"
    18  	refstore "github.com/docker/docker/reference"
    19  	"github.com/docker/docker/registry"
    20  	"github.com/opencontainers/go-digest"
    21  	specs "github.com/opencontainers/image-spec/specs-go/v1"
    22  	"github.com/pkg/errors"
    23  )
    24  
    25  // Config stores configuration for communicating
    26  // with a registry.
    27  type Config struct {
    28  	// MetaHeaders stores HTTP headers with metadata about the image
    29  	MetaHeaders map[string][]string
    30  	// AuthConfig holds authentication credentials for authenticating with
    31  	// the registry.
    32  	AuthConfig *types.AuthConfig
    33  	// ProgressOutput is the interface for showing the status of the pull
    34  	// operation.
    35  	ProgressOutput progress.Output
    36  	// RegistryService is the registry service to use for TLS configuration
    37  	// and endpoint lookup.
    38  	RegistryService registry.Service
    39  	// ImageEventLogger notifies events for a given image
    40  	ImageEventLogger func(id, name, action string)
    41  	// MetadataStore is the storage backend for distribution-specific
    42  	// metadata.
    43  	MetadataStore metadata.Store
    44  	// ImageStore manages images.
    45  	ImageStore ImageConfigStore
    46  	// ReferenceStore manages tags. This value is optional, when excluded
    47  	// content will not be tagged.
    48  	ReferenceStore refstore.Store
    49  }
    50  
    51  // ImagePullConfig stores pull configuration.
    52  type ImagePullConfig struct {
    53  	Config
    54  
    55  	// DownloadManager manages concurrent pulls.
    56  	DownloadManager *xfer.LayerDownloadManager
    57  	// Schema2Types is an optional list of valid schema2 configuration types
    58  	// allowed by the pull operation. If omitted, the default list of accepted
    59  	// types is used.
    60  	Schema2Types []string
    61  	// Platform is the requested platform of the image being pulled
    62  	Platform *specs.Platform
    63  }
    64  
    65  // ImagePushConfig stores push configuration.
    66  type ImagePushConfig struct {
    67  	Config
    68  
    69  	// ConfigMediaType is the configuration media type for
    70  	// schema2 manifests.
    71  	ConfigMediaType string
    72  	// LayerStores manages layers.
    73  	LayerStores PushLayerProvider
    74  	// UploadManager dispatches uploads.
    75  	UploadManager *xfer.LayerUploadManager
    76  }
    77  
    78  // ImageConfigStore handles storing and getting image configurations
    79  // by digest. Allows getting an image configurations rootfs from the
    80  // configuration.
    81  type ImageConfigStore interface {
    82  	Put(context.Context, []byte) (digest.Digest, error)
    83  	Get(context.Context, digest.Digest) ([]byte, error)
    84  }
    85  
    86  // PushLayerProvider provides layers to be pushed by ChainID.
    87  type PushLayerProvider interface {
    88  	Get(layer.ChainID) (PushLayer, error)
    89  }
    90  
    91  // PushLayer is a pushable layer with metadata about the layer
    92  // and access to the content of the layer.
    93  type PushLayer interface {
    94  	ChainID() layer.ChainID
    95  	DiffID() layer.DiffID
    96  	Parent() PushLayer
    97  	Open() (io.ReadCloser, error)
    98  	Size() int64
    99  	MediaType() string
   100  	Release()
   101  }
   102  
   103  type imageConfigStore struct {
   104  	image.Store
   105  }
   106  
   107  // NewImageConfigStoreFromStore returns an ImageConfigStore backed
   108  // by an image.Store for container images.
   109  func NewImageConfigStoreFromStore(is image.Store) ImageConfigStore {
   110  	return &imageConfigStore{
   111  		Store: is,
   112  	}
   113  }
   114  
   115  func (s *imageConfigStore) Put(_ context.Context, c []byte) (digest.Digest, error) {
   116  	id, err := s.Store.Create(c)
   117  	return digest.Digest(id), err
   118  }
   119  
   120  func (s *imageConfigStore) Get(_ context.Context, d digest.Digest) ([]byte, error) {
   121  	img, err := s.Store.Get(image.IDFromDigest(d))
   122  	if err != nil {
   123  		return nil, err
   124  	}
   125  	return img.RawJSON(), nil
   126  }
   127  
   128  func rootFSFromConfig(c []byte) (*image.RootFS, error) {
   129  	var unmarshalledConfig image.Image
   130  	if err := json.Unmarshal(c, &unmarshalledConfig); err != nil {
   131  		return nil, err
   132  	}
   133  	return unmarshalledConfig.RootFS, nil
   134  }
   135  
   136  func platformFromConfig(c []byte) (*specs.Platform, error) {
   137  	var unmarshalledConfig image.Image
   138  	if err := json.Unmarshal(c, &unmarshalledConfig); err != nil {
   139  		return nil, err
   140  	}
   141  
   142  	os := unmarshalledConfig.OS
   143  	if os == "" {
   144  		os = runtime.GOOS
   145  	}
   146  	if !system.IsOSSupported(os) {
   147  		return nil, errors.Wrapf(system.ErrNotSupportedOperatingSystem, "image operating system %q cannot be used on this platform", os)
   148  	}
   149  	return &specs.Platform{
   150  		OS:           os,
   151  		Architecture: unmarshalledConfig.Architecture,
   152  		Variant:      unmarshalledConfig.Variant,
   153  		OSVersion:    unmarshalledConfig.OSVersion,
   154  	}, nil
   155  }
   156  
   157  type storeLayerProvider struct {
   158  	ls layer.Store
   159  }
   160  
   161  // NewLayerProvidersFromStore returns layer providers backed by
   162  // an instance of LayerStore. Only getting layers as gzipped
   163  // tars is supported.
   164  func NewLayerProvidersFromStore(ls layer.Store) PushLayerProvider {
   165  	return &storeLayerProvider{ls: ls}
   166  }
   167  
   168  func (p *storeLayerProvider) Get(lid layer.ChainID) (PushLayer, error) {
   169  	if lid == "" {
   170  		return &storeLayer{
   171  			Layer: layer.EmptyLayer,
   172  		}, nil
   173  	}
   174  	l, err := p.ls.Get(lid)
   175  	if err != nil {
   176  		return nil, err
   177  	}
   178  
   179  	sl := storeLayer{
   180  		Layer: l,
   181  		ls:    p.ls,
   182  	}
   183  	if d, ok := l.(distribution.Describable); ok {
   184  		return &describableStoreLayer{
   185  			storeLayer:  sl,
   186  			describable: d,
   187  		}, nil
   188  	}
   189  
   190  	return &sl, nil
   191  }
   192  
   193  type storeLayer struct {
   194  	layer.Layer
   195  	ls layer.Store
   196  }
   197  
   198  func (l *storeLayer) Parent() PushLayer {
   199  	p := l.Layer.Parent()
   200  	if p == nil {
   201  		return nil
   202  	}
   203  	sl := storeLayer{
   204  		Layer: p,
   205  		ls:    l.ls,
   206  	}
   207  	if d, ok := p.(distribution.Describable); ok {
   208  		return &describableStoreLayer{
   209  			storeLayer:  sl,
   210  			describable: d,
   211  		}
   212  	}
   213  
   214  	return &sl
   215  }
   216  
   217  func (l *storeLayer) Open() (io.ReadCloser, error) {
   218  	return l.Layer.TarStream()
   219  }
   220  
   221  func (l *storeLayer) Size() int64 {
   222  	return l.Layer.DiffSize()
   223  }
   224  
   225  func (l *storeLayer) MediaType() string {
   226  	// layer store always returns uncompressed tars
   227  	return schema2.MediaTypeUncompressedLayer
   228  }
   229  
   230  func (l *storeLayer) Release() {
   231  	if l.ls != nil {
   232  		layer.ReleaseAndLog(l.ls, l.Layer)
   233  	}
   234  }
   235  
   236  type describableStoreLayer struct {
   237  	storeLayer
   238  	describable distribution.Describable
   239  }
   240  
   241  func (l *describableStoreLayer) Descriptor() distribution.Descriptor {
   242  	return l.describable.Descriptor()
   243  }