Platforms: Add From/ToProto helpers for types
Helpers to convert from a slice of platforms to our protobuf representation and vice-versa appear a couple times. It seems sane to just expose this facility in the platforms pkg. Signed-off-by: Danny Canter <danny@dcantah.dev>
This commit is contained in:
parent
b3ab1f26c4
commit
f3b7436b61
@ -156,15 +156,7 @@ func (iis *ImageExportStream) UnmarshalAny(ctx context.Context, sm streaming.Str
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
var specified []v1.Platform
|
specified := platforms.FromProto(s.Platforms)
|
||||||
for _, p := range s.Platforms {
|
|
||||||
specified = append(specified, v1.Platform{
|
|
||||||
OS: p.OS,
|
|
||||||
Architecture: p.Architecture,
|
|
||||||
Variant: p.Variant,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
iis.stream = tstreaming.WriteByteStream(ctx, stream)
|
iis.stream = tstreaming.WriteByteStream(ctx, stream)
|
||||||
iis.mediaType = s.MediaType
|
iis.mediaType = s.MediaType
|
||||||
iis.platforms = specified
|
iis.platforms = specified
|
||||||
|
@ -363,7 +363,7 @@ func (is *Store) MarshalAny(context.Context, streaming.StreamCreator) (typeurl.A
|
|||||||
Labels: is.imageLabels,
|
Labels: is.imageLabels,
|
||||||
ManifestLimit: uint32(is.manifestLimit),
|
ManifestLimit: uint32(is.manifestLimit),
|
||||||
AllMetadata: is.allMetadata,
|
AllMetadata: is.allMetadata,
|
||||||
Platforms: platformsToProto(is.platforms),
|
Platforms: platforms.ToProto(is.platforms),
|
||||||
ExtraReferences: referencesToProto(is.extraReferences),
|
ExtraReferences: referencesToProto(is.extraReferences),
|
||||||
Unpacks: unpackToProto(is.unpacks),
|
Unpacks: unpackToProto(is.unpacks),
|
||||||
}
|
}
|
||||||
@ -380,37 +380,13 @@ func (is *Store) UnmarshalAny(ctx context.Context, sm streaming.StreamGetter, a
|
|||||||
is.imageLabels = s.Labels
|
is.imageLabels = s.Labels
|
||||||
is.manifestLimit = int(s.ManifestLimit)
|
is.manifestLimit = int(s.ManifestLimit)
|
||||||
is.allMetadata = s.AllMetadata
|
is.allMetadata = s.AllMetadata
|
||||||
is.platforms = platformFromProto(s.Platforms)
|
is.platforms = platforms.FromProto(s.Platforms)
|
||||||
is.extraReferences = referencesFromProto(s.ExtraReferences)
|
is.extraReferences = referencesFromProto(s.ExtraReferences)
|
||||||
is.unpacks = unpackFromProto(s.Unpacks)
|
is.unpacks = unpackFromProto(s.Unpacks)
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func platformsToProto(platforms []ocispec.Platform) []*types.Platform {
|
|
||||||
ap := make([]*types.Platform, len(platforms))
|
|
||||||
for i := range platforms {
|
|
||||||
p := types.Platform{
|
|
||||||
OS: platforms[i].OS,
|
|
||||||
Architecture: platforms[i].Architecture,
|
|
||||||
Variant: platforms[i].Variant,
|
|
||||||
}
|
|
||||||
|
|
||||||
ap[i] = &p
|
|
||||||
}
|
|
||||||
return ap
|
|
||||||
}
|
|
||||||
|
|
||||||
func platformFromProto(platforms []*types.Platform) []ocispec.Platform {
|
|
||||||
op := make([]ocispec.Platform, len(platforms))
|
|
||||||
for i := range platforms {
|
|
||||||
op[i].OS = platforms[i].OS
|
|
||||||
op[i].Architecture = platforms[i].Architecture
|
|
||||||
op[i].Variant = platforms[i].Variant
|
|
||||||
}
|
|
||||||
return op
|
|
||||||
}
|
|
||||||
|
|
||||||
func referencesToProto(references []Reference) []*transfertypes.ImageReference {
|
func referencesToProto(references []Reference) []*transfertypes.ImageReference {
|
||||||
ir := make([]*transfertypes.ImageReference, len(references))
|
ir := make([]*transfertypes.ImageReference, len(references))
|
||||||
for i := range references {
|
for i := range references {
|
||||||
|
@ -116,6 +116,7 @@ import (
|
|||||||
|
|
||||||
specs "github.com/opencontainers/image-spec/specs-go/v1"
|
specs "github.com/opencontainers/image-spec/specs-go/v1"
|
||||||
|
|
||||||
|
"github.com/containerd/containerd/api/types"
|
||||||
"github.com/containerd/containerd/errdefs"
|
"github.com/containerd/containerd/errdefs"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -262,3 +263,30 @@ func Normalize(platform specs.Platform) specs.Platform {
|
|||||||
|
|
||||||
return platform
|
return platform
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ToProto converts from a slice of [Platform] to a slice of
|
||||||
|
// the protobuf definition [types.Platform].
|
||||||
|
func ToProto(platforms []Platform) []*types.Platform {
|
||||||
|
ap := make([]*types.Platform, len(platforms))
|
||||||
|
for i := range platforms {
|
||||||
|
p := types.Platform{
|
||||||
|
OS: platforms[i].OS,
|
||||||
|
Architecture: platforms[i].Architecture,
|
||||||
|
Variant: platforms[i].Variant,
|
||||||
|
}
|
||||||
|
ap[i] = &p
|
||||||
|
}
|
||||||
|
return ap
|
||||||
|
}
|
||||||
|
|
||||||
|
// FromProto converts a slice of the protobuf definition [types.Platform]
|
||||||
|
// to a slice of [Platform].
|
||||||
|
func FromProto(platforms []*types.Platform) []Platform {
|
||||||
|
op := make([]Platform, len(platforms))
|
||||||
|
for i := range platforms {
|
||||||
|
op[i].OS = platforms[i].OS
|
||||||
|
op[i].Architecture = platforms[i].Architecture
|
||||||
|
op[i].Variant = platforms[i].Variant
|
||||||
|
}
|
||||||
|
return op
|
||||||
|
}
|
||||||
|
@ -24,9 +24,9 @@ import (
|
|||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
api "github.com/containerd/containerd/api/services/introspection/v1"
|
api "github.com/containerd/containerd/api/services/introspection/v1"
|
||||||
"github.com/containerd/containerd/api/types"
|
|
||||||
"github.com/containerd/containerd/errdefs"
|
"github.com/containerd/containerd/errdefs"
|
||||||
"github.com/containerd/containerd/filters"
|
"github.com/containerd/containerd/filters"
|
||||||
|
"github.com/containerd/containerd/platforms"
|
||||||
"github.com/containerd/containerd/plugin"
|
"github.com/containerd/containerd/plugin"
|
||||||
ptypes "github.com/containerd/containerd/protobuf/types"
|
ptypes "github.com/containerd/containerd/protobuf/types"
|
||||||
"github.com/containerd/containerd/services"
|
"github.com/containerd/containerd/services"
|
||||||
@ -189,15 +189,6 @@ func adaptPlugin(o interface{}) filters.Adaptor {
|
|||||||
func pluginsToPB(plugins []*plugin.Plugin) []*api.Plugin {
|
func pluginsToPB(plugins []*plugin.Plugin) []*api.Plugin {
|
||||||
var pluginsPB []*api.Plugin
|
var pluginsPB []*api.Plugin
|
||||||
for _, p := range plugins {
|
for _, p := range plugins {
|
||||||
var platforms []*types.Platform
|
|
||||||
for _, p := range p.Meta.Platforms {
|
|
||||||
platforms = append(platforms, &types.Platform{
|
|
||||||
OS: p.OS,
|
|
||||||
Architecture: p.Architecture,
|
|
||||||
Variant: p.Variant,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
var requires []string
|
var requires []string
|
||||||
for _, r := range p.Registration.Requires {
|
for _, r := range p.Registration.Requires {
|
||||||
requires = append(requires, r.String())
|
requires = append(requires, r.String())
|
||||||
@ -231,7 +222,7 @@ func pluginsToPB(plugins []*plugin.Plugin) []*api.Plugin {
|
|||||||
Type: p.Registration.Type.String(),
|
Type: p.Registration.Type.String(),
|
||||||
ID: p.Registration.ID,
|
ID: p.Registration.ID,
|
||||||
Requires: requires,
|
Requires: requires,
|
||||||
Platforms: platforms,
|
Platforms: platforms.ToProto(p.Meta.Platforms),
|
||||||
Capabilities: p.Meta.Capabilities,
|
Capabilities: p.Meta.Capabilities,
|
||||||
Exports: p.Meta.Exports,
|
Exports: p.Meta.Exports,
|
||||||
InitErr: initErr,
|
InitErr: initErr,
|
||||||
|
Loading…
Reference in New Issue
Block a user