2015-12-10 00:38:04 +00:00
|
|
|
package schema2
|
|
|
|
|
|
|
|
import (
|
2017-08-11 22:31:16 +00:00
|
|
|
"context"
|
|
|
|
|
2020-08-24 11:18:39 +00:00
|
|
|
"github.com/distribution/distribution/v3"
|
2015-12-10 00:38:04 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// builder is a type for constructing manifests.
|
|
|
|
type builder struct {
|
2023-05-03 16:54:34 +00:00
|
|
|
// configDescriptor is used to describe configuration
|
|
|
|
configDescriptor distribution.Descriptor
|
2016-12-15 00:17:20 +00:00
|
|
|
|
2015-12-10 00:38:04 +00:00
|
|
|
// configJSON references
|
|
|
|
configJSON []byte
|
|
|
|
|
2016-12-15 00:17:20 +00: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 00:38:04 +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
|
|
|
|
// as part of the Build process.
|
2023-05-03 16:54:34 +00:00
|
|
|
func NewManifestBuilder(configDescriptor distribution.Descriptor, configJSON []byte) distribution.ManifestBuilder {
|
2015-12-10 00:38:04 +00:00
|
|
|
mb := &builder{
|
2023-05-03 16:54:34 +00:00
|
|
|
configDescriptor: configDescriptor,
|
|
|
|
configJSON: make([]byte, len(configJSON)),
|
2015-12-10 00:38:04 +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{
|
2016-01-06 22:15:14 +00:00
|
|
|
Versioned: SchemaVersion,
|
2016-12-15 00:17:20 +00:00
|
|
|
Layers: make([]distribution.Descriptor, len(mb.dependencies)),
|
2015-12-10 00:38:04 +00:00
|
|
|
}
|
2016-12-15 00:17:20 +00:00
|
|
|
copy(m.Layers, mb.dependencies)
|
2015-12-10 00:38:04 +00:00
|
|
|
|
2023-05-03 16:54:34 +00:00
|
|
|
m.Config = mb.configDescriptor
|
2015-12-10 00:38:04 +00:00
|
|
|
|
|
|
|
return FromStruct(m)
|
|
|
|
}
|
|
|
|
|
|
|
|
// AppendReference adds a reference to the current ManifestBuilder.
|
|
|
|
func (mb *builder) AppendReference(d distribution.Describable) error {
|
2016-12-15 00:17:20 +00:00
|
|
|
mb.dependencies = append(mb.dependencies, d.Descriptor())
|
2015-12-10 00:38:04 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// References returns the current references added to this builder.
|
|
|
|
func (mb *builder) References() []distribution.Descriptor {
|
2016-12-15 00:17:20 +00:00
|
|
|
return mb.dependencies
|
2015-12-10 00:38:04 +00:00
|
|
|
}
|