OCI: Add From/ToProto helpers for Descriptor
Helpers to convert from the OCI image specs [Descriptor] to its protobuf structure for Descriptor and vice-versa appear three times. It seems sane to just expose this facility in /oci. Signed-off-by: Danny Canter <danny@dcantah.dev>
This commit is contained in:
parent
55a8102ec1
commit
0a6b8f0ee0
@ -20,12 +20,11 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
|
|
||||||
diffapi "github.com/containerd/containerd/api/services/diff/v1"
|
diffapi "github.com/containerd/containerd/api/services/diff/v1"
|
||||||
"github.com/containerd/containerd/api/types"
|
|
||||||
"github.com/containerd/containerd/diff"
|
"github.com/containerd/containerd/diff"
|
||||||
"github.com/containerd/containerd/errdefs"
|
"github.com/containerd/containerd/errdefs"
|
||||||
"github.com/containerd/containerd/mount"
|
"github.com/containerd/containerd/mount"
|
||||||
|
"github.com/containerd/containerd/oci"
|
||||||
"github.com/containerd/typeurl/v2"
|
"github.com/containerd/typeurl/v2"
|
||||||
"github.com/opencontainers/go-digest"
|
|
||||||
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
|
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -49,7 +48,7 @@ func (s *service) Apply(ctx context.Context, er *diffapi.ApplyRequest) (*diffapi
|
|||||||
var (
|
var (
|
||||||
ocidesc ocispec.Descriptor
|
ocidesc ocispec.Descriptor
|
||||||
err error
|
err error
|
||||||
desc = toDescriptor(er.Diff)
|
desc = oci.DescriptorFromProto(er.Diff)
|
||||||
mounts = mount.FromProto(er.Mounts)
|
mounts = mount.FromProto(er.Mounts)
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -68,7 +67,7 @@ func (s *service) Apply(ctx context.Context, er *diffapi.ApplyRequest) (*diffapi
|
|||||||
}
|
}
|
||||||
|
|
||||||
return &diffapi.ApplyResponse{
|
return &diffapi.ApplyResponse{
|
||||||
Applied: fromDescriptor(ocidesc),
|
Applied: oci.DescriptorToProto(ocidesc),
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -104,24 +103,6 @@ func (s *service) Diff(ctx context.Context, dr *diffapi.DiffRequest) (*diffapi.D
|
|||||||
}
|
}
|
||||||
|
|
||||||
return &diffapi.DiffResponse{
|
return &diffapi.DiffResponse{
|
||||||
Diff: fromDescriptor(ocidesc),
|
Diff: oci.DescriptorToProto(ocidesc),
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func toDescriptor(d *types.Descriptor) ocispec.Descriptor {
|
|
||||||
return ocispec.Descriptor{
|
|
||||||
MediaType: d.MediaType,
|
|
||||||
Digest: digest.Digest(d.Digest),
|
|
||||||
Size: d.Size,
|
|
||||||
Annotations: d.Annotations,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func fromDescriptor(d ocispec.Descriptor) *types.Descriptor {
|
|
||||||
return &types.Descriptor{
|
|
||||||
MediaType: d.MediaType,
|
|
||||||
Digest: d.Digest.String(),
|
|
||||||
Size: d.Size,
|
|
||||||
Annotations: d.Annotations,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
@ -20,14 +20,13 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
|
|
||||||
diffapi "github.com/containerd/containerd/api/services/diff/v1"
|
diffapi "github.com/containerd/containerd/api/services/diff/v1"
|
||||||
"github.com/containerd/containerd/api/types"
|
|
||||||
"github.com/containerd/containerd/diff"
|
"github.com/containerd/containerd/diff"
|
||||||
"github.com/containerd/containerd/errdefs"
|
"github.com/containerd/containerd/errdefs"
|
||||||
"github.com/containerd/containerd/mount"
|
"github.com/containerd/containerd/mount"
|
||||||
|
"github.com/containerd/containerd/oci"
|
||||||
"github.com/containerd/containerd/pkg/epoch"
|
"github.com/containerd/containerd/pkg/epoch"
|
||||||
"github.com/containerd/containerd/protobuf"
|
"github.com/containerd/containerd/protobuf"
|
||||||
ptypes "github.com/containerd/containerd/protobuf/types"
|
ptypes "github.com/containerd/containerd/protobuf/types"
|
||||||
"github.com/opencontainers/go-digest"
|
|
||||||
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
|
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
|
||||||
|
|
||||||
"google.golang.org/protobuf/types/known/timestamppb"
|
"google.golang.org/protobuf/types/known/timestamppb"
|
||||||
@ -59,7 +58,7 @@ func (r *diffRemote) Apply(ctx context.Context, desc ocispec.Descriptor, mounts
|
|||||||
}
|
}
|
||||||
|
|
||||||
req := &diffapi.ApplyRequest{
|
req := &diffapi.ApplyRequest{
|
||||||
Diff: fromDescriptor(desc),
|
Diff: oci.DescriptorToProto(desc),
|
||||||
Mounts: mount.ToProto(mounts),
|
Mounts: mount.ToProto(mounts),
|
||||||
Payloads: payloads,
|
Payloads: payloads,
|
||||||
}
|
}
|
||||||
@ -67,7 +66,7 @@ func (r *diffRemote) Apply(ctx context.Context, desc ocispec.Descriptor, mounts
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return ocispec.Descriptor{}, errdefs.FromGRPC(err)
|
return ocispec.Descriptor{}, errdefs.FromGRPC(err)
|
||||||
}
|
}
|
||||||
return toDescriptor(resp.Applied), nil
|
return oci.DescriptorFromProto(resp.Applied), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *diffRemote) Compare(ctx context.Context, a, b []mount.Mount, opts ...diff.Opt) (ocispec.Descriptor, error) {
|
func (r *diffRemote) Compare(ctx context.Context, a, b []mount.Mount, opts ...diff.Opt) (ocispec.Descriptor, error) {
|
||||||
@ -96,26 +95,5 @@ func (r *diffRemote) Compare(ctx context.Context, a, b []mount.Mount, opts ...di
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return ocispec.Descriptor{}, errdefs.FromGRPC(err)
|
return ocispec.Descriptor{}, errdefs.FromGRPC(err)
|
||||||
}
|
}
|
||||||
return toDescriptor(resp.Diff), nil
|
return oci.DescriptorFromProto(resp.Diff), nil
|
||||||
}
|
|
||||||
|
|
||||||
func toDescriptor(d *types.Descriptor) ocispec.Descriptor {
|
|
||||||
if d == nil {
|
|
||||||
return ocispec.Descriptor{}
|
|
||||||
}
|
|
||||||
return ocispec.Descriptor{
|
|
||||||
MediaType: d.MediaType,
|
|
||||||
Digest: digest.Digest(d.Digest),
|
|
||||||
Size: d.Size,
|
|
||||||
Annotations: d.Annotations,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func fromDescriptor(d ocispec.Descriptor) *types.Descriptor {
|
|
||||||
return &types.Descriptor{
|
|
||||||
MediaType: d.MediaType,
|
|
||||||
Digest: d.Digest.String(),
|
|
||||||
Size: d.Size,
|
|
||||||
Annotations: d.Annotations,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
25
oci/spec.go
25
oci/spec.go
@ -21,8 +21,11 @@ import (
|
|||||||
"path/filepath"
|
"path/filepath"
|
||||||
"runtime"
|
"runtime"
|
||||||
|
|
||||||
|
"github.com/opencontainers/go-digest"
|
||||||
|
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
|
||||||
"github.com/opencontainers/runtime-spec/specs-go"
|
"github.com/opencontainers/runtime-spec/specs-go"
|
||||||
|
|
||||||
|
"github.com/containerd/containerd/api/types"
|
||||||
"github.com/containerd/containerd/containers"
|
"github.com/containerd/containerd/containers"
|
||||||
"github.com/containerd/containerd/namespaces"
|
"github.com/containerd/containerd/namespaces"
|
||||||
"github.com/containerd/containerd/platforms"
|
"github.com/containerd/containerd/platforms"
|
||||||
@ -220,3 +223,25 @@ func populateDefaultDarwinSpec(s *Spec) error {
|
|||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// DescriptorFromProto converts containerds protobuf [types.Descriptor]
|
||||||
|
// to the OCI image specs [ocispec.Descriptor].
|
||||||
|
func DescriptorFromProto(d *types.Descriptor) ocispec.Descriptor {
|
||||||
|
return ocispec.Descriptor{
|
||||||
|
MediaType: d.MediaType,
|
||||||
|
Digest: digest.Digest(d.Digest),
|
||||||
|
Size: d.Size,
|
||||||
|
Annotations: d.Annotations,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// DescriptorToProto converts the OCI image specs [ocispec.Descriptor]
|
||||||
|
// to containerds protobuf [types.Descriptor].
|
||||||
|
func DescriptorToProto(d ocispec.Descriptor) *types.Descriptor {
|
||||||
|
return &types.Descriptor{
|
||||||
|
MediaType: d.MediaType,
|
||||||
|
Digest: d.Digest.String(),
|
||||||
|
Size: d.Size,
|
||||||
|
Annotations: d.Annotations,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -21,14 +21,13 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
diffapi "github.com/containerd/containerd/api/services/diff/v1"
|
diffapi "github.com/containerd/containerd/api/services/diff/v1"
|
||||||
"github.com/containerd/containerd/api/types"
|
|
||||||
"github.com/containerd/containerd/diff"
|
"github.com/containerd/containerd/diff"
|
||||||
"github.com/containerd/containerd/errdefs"
|
"github.com/containerd/containerd/errdefs"
|
||||||
"github.com/containerd/containerd/mount"
|
"github.com/containerd/containerd/mount"
|
||||||
|
"github.com/containerd/containerd/oci"
|
||||||
"github.com/containerd/containerd/plugin"
|
"github.com/containerd/containerd/plugin"
|
||||||
"github.com/containerd/containerd/services"
|
"github.com/containerd/containerd/services"
|
||||||
"github.com/containerd/typeurl/v2"
|
"github.com/containerd/typeurl/v2"
|
||||||
"github.com/opencontainers/go-digest"
|
|
||||||
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
|
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
|
||||||
"google.golang.org/grpc"
|
"google.golang.org/grpc"
|
||||||
)
|
)
|
||||||
@ -97,7 +96,7 @@ func (l *local) Apply(ctx context.Context, er *diffapi.ApplyRequest, _ ...grpc.C
|
|||||||
var (
|
var (
|
||||||
ocidesc ocispec.Descriptor
|
ocidesc ocispec.Descriptor
|
||||||
err error
|
err error
|
||||||
desc = toDescriptor(er.Diff)
|
desc = oci.DescriptorFromProto(er.Diff)
|
||||||
mounts = mount.FromProto(er.Mounts)
|
mounts = mount.FromProto(er.Mounts)
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -122,7 +121,7 @@ func (l *local) Apply(ctx context.Context, er *diffapi.ApplyRequest, _ ...grpc.C
|
|||||||
}
|
}
|
||||||
|
|
||||||
return &diffapi.ApplyResponse{
|
return &diffapi.ApplyResponse{
|
||||||
Applied: fromDescriptor(ocidesc),
|
Applied: oci.DescriptorToProto(ocidesc),
|
||||||
}, nil
|
}, nil
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -161,24 +160,6 @@ func (l *local) Diff(ctx context.Context, dr *diffapi.DiffRequest, _ ...grpc.Cal
|
|||||||
}
|
}
|
||||||
|
|
||||||
return &diffapi.DiffResponse{
|
return &diffapi.DiffResponse{
|
||||||
Diff: fromDescriptor(ocidesc),
|
Diff: oci.DescriptorToProto(ocidesc),
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func toDescriptor(d *types.Descriptor) ocispec.Descriptor {
|
|
||||||
return ocispec.Descriptor{
|
|
||||||
MediaType: d.MediaType,
|
|
||||||
Digest: digest.Digest(d.Digest),
|
|
||||||
Size: d.Size,
|
|
||||||
Annotations: d.Annotations,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func fromDescriptor(d ocispec.Descriptor) *types.Descriptor {
|
|
||||||
return &types.Descriptor{
|
|
||||||
MediaType: d.MediaType,
|
|
||||||
Digest: d.Digest.String(),
|
|
||||||
Size: d.Size,
|
|
||||||
Annotations: d.Annotations,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
Loading…
Reference in New Issue
Block a user