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:
		@@ -156,15 +156,7 @@ func (iis *ImageExportStream) UnmarshalAny(ctx context.Context, sm streaming.Str
 | 
			
		||||
		return err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	var specified []v1.Platform
 | 
			
		||||
	for _, p := range s.Platforms {
 | 
			
		||||
		specified = append(specified, v1.Platform{
 | 
			
		||||
			OS:           p.OS,
 | 
			
		||||
			Architecture: p.Architecture,
 | 
			
		||||
			Variant:      p.Variant,
 | 
			
		||||
		})
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	specified := platforms.FromProto(s.Platforms)
 | 
			
		||||
	iis.stream = tstreaming.WriteByteStream(ctx, stream)
 | 
			
		||||
	iis.mediaType = s.MediaType
 | 
			
		||||
	iis.platforms = specified
 | 
			
		||||
 
 | 
			
		||||
@@ -363,7 +363,7 @@ func (is *Store) MarshalAny(context.Context, streaming.StreamCreator) (typeurl.A
 | 
			
		||||
		Labels:          is.imageLabels,
 | 
			
		||||
		ManifestLimit:   uint32(is.manifestLimit),
 | 
			
		||||
		AllMetadata:     is.allMetadata,
 | 
			
		||||
		Platforms:       platformsToProto(is.platforms),
 | 
			
		||||
		Platforms:       platforms.ToProto(is.platforms),
 | 
			
		||||
		ExtraReferences: referencesToProto(is.extraReferences),
 | 
			
		||||
		Unpacks:         unpackToProto(is.unpacks),
 | 
			
		||||
	}
 | 
			
		||||
@@ -380,37 +380,13 @@ func (is *Store) UnmarshalAny(ctx context.Context, sm streaming.StreamGetter, a
 | 
			
		||||
	is.imageLabels = s.Labels
 | 
			
		||||
	is.manifestLimit = int(s.ManifestLimit)
 | 
			
		||||
	is.allMetadata = s.AllMetadata
 | 
			
		||||
	is.platforms = platformFromProto(s.Platforms)
 | 
			
		||||
	is.platforms = platforms.FromProto(s.Platforms)
 | 
			
		||||
	is.extraReferences = referencesFromProto(s.ExtraReferences)
 | 
			
		||||
	is.unpacks = unpackFromProto(s.Unpacks)
 | 
			
		||||
 | 
			
		||||
	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 {
 | 
			
		||||
	ir := make([]*transfertypes.ImageReference, len(references))
 | 
			
		||||
	for i := range references {
 | 
			
		||||
 
 | 
			
		||||
@@ -116,6 +116,7 @@ import (
 | 
			
		||||
 | 
			
		||||
	specs "github.com/opencontainers/image-spec/specs-go/v1"
 | 
			
		||||
 | 
			
		||||
	"github.com/containerd/containerd/api/types"
 | 
			
		||||
	"github.com/containerd/containerd/errdefs"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
@@ -262,3 +263,30 @@ func Normalize(platform specs.Platform) specs.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"
 | 
			
		||||
 | 
			
		||||
	api "github.com/containerd/containerd/api/services/introspection/v1"
 | 
			
		||||
	"github.com/containerd/containerd/api/types"
 | 
			
		||||
	"github.com/containerd/containerd/errdefs"
 | 
			
		||||
	"github.com/containerd/containerd/filters"
 | 
			
		||||
	"github.com/containerd/containerd/platforms"
 | 
			
		||||
	"github.com/containerd/containerd/plugin"
 | 
			
		||||
	ptypes "github.com/containerd/containerd/protobuf/types"
 | 
			
		||||
	"github.com/containerd/containerd/services"
 | 
			
		||||
@@ -189,15 +189,6 @@ func adaptPlugin(o interface{}) filters.Adaptor {
 | 
			
		||||
func pluginsToPB(plugins []*plugin.Plugin) []*api.Plugin {
 | 
			
		||||
	var pluginsPB []*api.Plugin
 | 
			
		||||
	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
 | 
			
		||||
		for _, r := range p.Registration.Requires {
 | 
			
		||||
			requires = append(requires, r.String())
 | 
			
		||||
@@ -231,7 +222,7 @@ func pluginsToPB(plugins []*plugin.Plugin) []*api.Plugin {
 | 
			
		||||
			Type:         p.Registration.Type.String(),
 | 
			
		||||
			ID:           p.Registration.ID,
 | 
			
		||||
			Requires:     requires,
 | 
			
		||||
			Platforms:    platforms,
 | 
			
		||||
			Platforms:    platforms.ToProto(p.Meta.Platforms),
 | 
			
		||||
			Capabilities: p.Meta.Capabilities,
 | 
			
		||||
			Exports:      p.Meta.Exports,
 | 
			
		||||
			InitErr:      initErr,
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user