2016-11-17 19:28:05 +01:00
|
|
|
package ocischema
|
|
|
|
|
|
|
|
import (
|
2017-09-01 04:14:40 +02:00
|
|
|
"context"
|
2018-07-19 23:07:26 +02:00
|
|
|
"errors"
|
2017-09-01 04:14:40 +02:00
|
|
|
|
2016-11-17 19:28:05 +01:00
|
|
|
"github.com/docker/distribution"
|
2018-03-15 21:04:58 +01:00
|
|
|
"github.com/docker/distribution/manifest"
|
2016-11-17 19:28:05 +01:00
|
|
|
"github.com/opencontainers/go-digest"
|
2019-10-09 14:02:21 +02:00
|
|
|
v1 "github.com/opencontainers/image-spec/specs-go/v1"
|
2016-11-17 19:28:05 +01:00
|
|
|
)
|
|
|
|
|
2018-06-19 20:48:42 +02:00
|
|
|
// Builder is a type for constructing manifests.
|
2018-03-15 21:04:58 +01:00
|
|
|
type Builder struct {
|
2016-11-17 19:28:05 +01:00
|
|
|
// 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-08-01 01:34:11 +02:00
|
|
|
|
|
|
|
// Annotations contains arbitrary metadata relating to the targeted content.
|
|
|
|
annotations map[string]string
|
2018-03-15 21:04:58 +01:00
|
|
|
|
|
|
|
// For testing purposes
|
|
|
|
mediaType string
|
2016-11-17 19:28:05 +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
|
2017-08-01 01:34:11 +02:00
|
|
|
// as part of the Build process, and annotations.
|
|
|
|
func NewManifestBuilder(bs distribution.BlobService, configJSON []byte, annotations map[string]string) distribution.ManifestBuilder {
|
2018-03-15 21:04:58 +01:00
|
|
|
mb := &Builder{
|
2017-08-01 01:34:11 +02:00
|
|
|
bs: bs,
|
|
|
|
configJSON: make([]byte, len(configJSON)),
|
|
|
|
annotations: annotations,
|
2018-03-15 21:04:58 +01:00
|
|
|
mediaType: v1.MediaTypeImageManifest,
|
2016-11-17 19:28:05 +01:00
|
|
|
}
|
|
|
|
copy(mb.configJSON, configJSON)
|
|
|
|
|
|
|
|
return mb
|
|
|
|
}
|
|
|
|
|
2018-06-19 20:48:42 +02:00
|
|
|
// SetMediaType assigns the passed mediatype or error if the mediatype is not a
|
|
|
|
// valid media type for oci image manifests currently: "" or "application/vnd.oci.image.manifest.v1+json"
|
2018-07-19 23:07:26 +02:00
|
|
|
func (mb *Builder) SetMediaType(mediaType string) error {
|
2018-03-15 21:04:58 +01:00
|
|
|
if mediaType != "" && mediaType != v1.MediaTypeImageManifest {
|
2019-02-05 01:01:04 +01:00
|
|
|
return errors.New("invalid media type for OCI image manifest")
|
2018-03-15 21:04:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
mb.mediaType = mediaType
|
2018-07-19 23:07:26 +02:00
|
|
|
return nil
|
2018-03-15 21:04:58 +01:00
|
|
|
}
|
|
|
|
|
2016-11-17 19:28:05 +01:00
|
|
|
// Build produces a final manifest from the given references.
|
2018-03-15 21:04:58 +01:00
|
|
|
func (mb *Builder) Build(ctx context.Context) (distribution.Manifest, error) {
|
2016-11-17 19:28:05 +01:00
|
|
|
m := Manifest{
|
2018-03-15 21:04:58 +01:00
|
|
|
Versioned: manifest.Versioned{
|
|
|
|
SchemaVersion: 2,
|
|
|
|
MediaType: mb.mediaType,
|
|
|
|
},
|
2017-08-01 01:34:11 +02:00
|
|
|
Layers: make([]distribution.Descriptor, len(mb.layers)),
|
|
|
|
Annotations: mb.annotations,
|
2016-11-17 19:28:05 +01: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 21:19:47 +02:00
|
|
|
m.Config.MediaType = v1.MediaTypeImageConfig
|
2016-11-17 19:28:05 +01:00
|
|
|
return FromStruct(m)
|
|
|
|
case distribution.ErrBlobUnknown:
|
|
|
|
// nop
|
|
|
|
default:
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add config to the blob store
|
2017-07-11 21:19:47 +02:00
|
|
|
m.Config, err = mb.bs.Put(ctx, v1.MediaTypeImageConfig, mb.configJSON)
|
2016-11-17 19:28:05 +01:00
|
|
|
// Override MediaType, since Put always replaces the specified media
|
|
|
|
// type with application/octet-stream in the descriptor it returns.
|
2017-07-11 21:19:47 +02:00
|
|
|
m.Config.MediaType = v1.MediaTypeImageConfig
|
2016-11-17 19:28:05 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return FromStruct(m)
|
|
|
|
}
|
|
|
|
|
|
|
|
// AppendReference adds a reference to the current ManifestBuilder.
|
2018-03-15 21:04:58 +01:00
|
|
|
func (mb *Builder) AppendReference(d distribution.Describable) error {
|
2016-11-17 19:28:05 +01:00
|
|
|
mb.layers = append(mb.layers, d.Descriptor())
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// References returns the current references added to this builder.
|
2018-03-15 21:04:58 +01:00
|
|
|
func (mb *Builder) References() []distribution.Descriptor {
|
2016-11-17 19:28:05 +01:00
|
|
|
return mb.layers
|
|
|
|
}
|