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"
|
2023-04-30 16:16:51 +00:00
|
|
|
"github.com/opencontainers/image-spec/specs-go"
|
2015-12-10 00:38:04 +00:00
|
|
|
)
|
|
|
|
|
2023-05-02 16:09:48 +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-02 16:09:48 +00:00
|
|
|
func NewManifestBuilder(configDescriptor distribution.Descriptor, configJSON []byte) *Builder {
|
|
|
|
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.
|
2023-05-02 16:09:48 +00:00
|
|
|
func (mb *Builder) Build(ctx context.Context) (distribution.Manifest, error) {
|
2015-12-10 00:38:04 +00:00
|
|
|
m := Manifest{
|
2023-04-30 16:16:51 +00:00
|
|
|
Versioned: specs.Versioned{SchemaVersion: defaultSchemaVersion},
|
|
|
|
MediaType: defaultMediaType,
|
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.
|
Descriptor: do not implement Describable interface
Commit cb6f0023500c3d2afb8c9f3ee4a0097526192156 implemented a generic
Manifest interface to represent manifests in the registry and remove
references to schema specific manifests.
As part of this refactor, the Describable interface was introduced,
which allowed for a single ManifestBuilder interface to handle both
schema1 and schema2 manifests. Implementations of Describable are
generally objects which can be described, not simply descriptors, but
for convenience, this interface was also implemented on Descriptor in
2ff77c00bad887928be04367f0dd58f6aed5b756.
This interface served its purpose, but no longer needed for most cases;
schema2 (and OCI) descriptors do not need this method, making it only
needed for `schema1.Reference`, which is now deprecated.
Requiring this interface to be implemented limits interoperability
between distribution's Descriptor and the OCI Descriptor types, which
are identical in every other way, except for the presence of the
Describable interface.
This patch:
- Removes the `Descriptor.Descriptor()` method (no longer implementing
the `Describable` interface).
- Updates ManifestBuilder interface and implementations to accept either
- Updates ManifestBuilder interface and implementations to accept a
`Descriptor`.
After this patch, the caller is responsible for changing a describable
type into a descriptor;
builder.AppendReference(describable.Descriptor())
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2023-04-30 15:53:17 +00:00
|
|
|
func (mb *Builder) AppendReference(ref distribution.Descriptor) error {
|
|
|
|
mb.dependencies = append(mb.dependencies, ref)
|
2015-12-10 00:38:04 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// References returns the current references added to this builder.
|
2023-05-02 16:09:48 +00:00
|
|
|
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
|
|
|
}
|