2016-11-17 18:28:05 +00:00
|
|
|
package ocischema
|
|
|
|
|
|
|
|
import (
|
2017-09-01 02:14:40 +00:00
|
|
|
"context"
|
|
|
|
|
2016-11-17 18:28:05 +00:00
|
|
|
"github.com/docker/distribution"
|
|
|
|
"github.com/opencontainers/go-digest"
|
2017-07-11 19:19:47 +00:00
|
|
|
"github.com/opencontainers/image-spec/specs-go/v1"
|
2016-11-17 18:28:05 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// builder is a type for constructing manifests.
|
|
|
|
type builder struct {
|
|
|
|
// bs is a BlobService used to publish the configuration blob.
|
|
|
|
bs distribution.BlobService
|
|
|
|
|
|
|
|
// configJSON references
|
|
|
|
configJSON []byte
|
|
|
|
|
|
|
|
// layers is a list of layer descriptors that gets built by successive
|
|
|
|
// calls to AppendReference.
|
|
|
|
layers []distribution.Descriptor
|
2017-07-31 23:34:11 +00:00
|
|
|
|
|
|
|
// Annotations contains arbitrary metadata relating to the targeted content.
|
|
|
|
annotations map[string]string
|
2016-11-17 18:28:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewManifestBuilder is used to build new manifests for the current schema
|
|
|
|
// version. It takes a BlobService so it can publish the configuration blob
|
2017-07-31 23:34:11 +00:00
|
|
|
// as part of the Build process, and annotations.
|
|
|
|
func NewManifestBuilder(bs distribution.BlobService, configJSON []byte, annotations map[string]string) distribution.ManifestBuilder {
|
2016-11-17 18:28:05 +00:00
|
|
|
mb := &builder{
|
2017-07-31 23:34:11 +00:00
|
|
|
bs: bs,
|
|
|
|
configJSON: make([]byte, len(configJSON)),
|
|
|
|
annotations: annotations,
|
2016-11-17 18:28:05 +00: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{
|
2017-07-31 23:34:11 +00:00
|
|
|
Versioned: SchemaVersion,
|
|
|
|
Layers: make([]distribution.Descriptor, len(mb.layers)),
|
|
|
|
Annotations: mb.annotations,
|
2016-11-17 18:28:05 +00:00
|
|
|
}
|
|
|
|
copy(m.Layers, mb.layers)
|
|
|
|
|
|
|
|
configDigest := digest.FromBytes(mb.configJSON)
|
|
|
|
|
|
|
|
var err error
|
|
|
|
m.Config, err = mb.bs.Stat(ctx, configDigest)
|
|
|
|
switch err {
|
|
|
|
case nil:
|
|
|
|
// Override MediaType, since Put always replaces the specified media
|
|
|
|
// type with application/octet-stream in the descriptor it returns.
|
2017-07-11 19:19:47 +00:00
|
|
|
m.Config.MediaType = v1.MediaTypeImageConfig
|
2016-11-17 18:28:05 +00:00
|
|
|
return FromStruct(m)
|
|
|
|
case distribution.ErrBlobUnknown:
|
|
|
|
// nop
|
|
|
|
default:
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add config to the blob store
|
2017-07-11 19:19:47 +00:00
|
|
|
m.Config, err = mb.bs.Put(ctx, v1.MediaTypeImageConfig, mb.configJSON)
|
2016-11-17 18:28:05 +00:00
|
|
|
// Override MediaType, since Put always replaces the specified media
|
|
|
|
// type with application/octet-stream in the descriptor it returns.
|
2017-07-11 19:19:47 +00:00
|
|
|
m.Config.MediaType = v1.MediaTypeImageConfig
|
2016-11-17 18:28:05 +00: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 {
|
|
|
|
mb.layers = append(mb.layers, d.Descriptor())
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// References returns the current references added to this builder.
|
|
|
|
func (mb *builder) References() []distribution.Descriptor {
|
|
|
|
return mb.layers
|
|
|
|
}
|