2015-12-10 01:38:04 +01:00
|
|
|
package schema2
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/docker/distribution"
|
|
|
|
"github.com/docker/distribution/context"
|
2016-12-17 01:28:34 +01:00
|
|
|
"github.com/opencontainers/go-digest"
|
2015-12-10 01:38:04 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// builder is a type for constructing manifests.
|
|
|
|
type builder struct {
|
|
|
|
// bs is a BlobService used to publish the configuration blob.
|
|
|
|
bs distribution.BlobService
|
|
|
|
|
2016-12-15 01:17:20 +01:00
|
|
|
// configMediaType is media type used to describe configuration
|
|
|
|
configMediaType string
|
|
|
|
|
2015-12-10 01:38:04 +01:00
|
|
|
// configJSON references
|
|
|
|
configJSON []byte
|
|
|
|
|
2016-12-15 01:17:20 +01:00
|
|
|
// dependencies is a list of descriptors that gets built by successive
|
|
|
|
// calls to AppendReference. In case of image configuration these are layers.
|
|
|
|
dependencies []distribution.Descriptor
|
2015-12-10 01:38:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewManifestBuilder is used to build new manifests for the current schema
|
|
|
|
// version. It takes a BlobService so it can publish the configuration blob
|
|
|
|
// as part of the Build process.
|
2016-12-15 01:17:20 +01:00
|
|
|
func NewManifestBuilder(bs distribution.BlobService, configMediaType string, configJSON []byte) distribution.ManifestBuilder {
|
2015-12-10 01:38:04 +01:00
|
|
|
mb := &builder{
|
2016-12-15 01:17:20 +01:00
|
|
|
bs: bs,
|
|
|
|
configMediaType: configMediaType,
|
|
|
|
configJSON: make([]byte, len(configJSON)),
|
2015-12-10 01:38:04 +01:00
|
|
|
}
|
|
|
|
copy(mb.configJSON, configJSON)
|
|
|
|
|
|
|
|
return mb
|
|
|
|
}
|
|
|
|
|
|
|
|
// Build produces a final manifest from the given references.
|
|
|
|
func (mb *builder) Build(ctx context.Context) (distribution.Manifest, error) {
|
|
|
|
m := Manifest{
|
2016-01-06 23:15:14 +01:00
|
|
|
Versioned: SchemaVersion,
|
2016-12-15 01:17:20 +01:00
|
|
|
Layers: make([]distribution.Descriptor, len(mb.dependencies)),
|
2015-12-10 01:38:04 +01:00
|
|
|
}
|
2016-12-15 01:17:20 +01:00
|
|
|
copy(m.Layers, mb.dependencies)
|
2015-12-10 01:38:04 +01:00
|
|
|
|
|
|
|
configDigest := digest.FromBytes(mb.configJSON)
|
|
|
|
|
|
|
|
var err error
|
|
|
|
m.Config, err = mb.bs.Stat(ctx, configDigest)
|
|
|
|
switch err {
|
|
|
|
case nil:
|
2016-06-29 23:52:50 +02:00
|
|
|
// Override MediaType, since Put always replaces the specified media
|
|
|
|
// type with application/octet-stream in the descriptor it returns.
|
2016-12-15 01:17:20 +01:00
|
|
|
m.Config.MediaType = mb.configMediaType
|
2015-12-10 01:38:04 +01:00
|
|
|
return FromStruct(m)
|
|
|
|
case distribution.ErrBlobUnknown:
|
|
|
|
// nop
|
|
|
|
default:
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add config to the blob store
|
2016-12-15 01:17:20 +01:00
|
|
|
m.Config, err = mb.bs.Put(ctx, mb.configMediaType, mb.configJSON)
|
2016-04-12 02:05:41 +02:00
|
|
|
// Override MediaType, since Put always replaces the specified media
|
|
|
|
// type with application/octet-stream in the descriptor it returns.
|
2016-12-15 01:17:20 +01:00
|
|
|
m.Config.MediaType = mb.configMediaType
|
2015-12-10 01:38:04 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return FromStruct(m)
|
|
|
|
}
|
|
|
|
|
|
|
|
// AppendReference adds a reference to the current ManifestBuilder.
|
|
|
|
func (mb *builder) AppendReference(d distribution.Describable) error {
|
2016-12-15 01:17:20 +01:00
|
|
|
mb.dependencies = append(mb.dependencies, d.Descriptor())
|
2015-12-10 01:38:04 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// References returns the current references added to this builder.
|
|
|
|
func (mb *builder) References() []distribution.Descriptor {
|
2016-12-15 01:17:20 +01:00
|
|
|
return mb.dependencies
|
2015-12-10 01:38:04 +01:00
|
|
|
}
|