platforms: move ToProto, FromProto to api/types

These utilities resulted in the platforms package to have the containerd
API as dependency. As this package is used in many parts of the code, as
well as external consumers, we should try to keep it light on dependencies,
with the potential to make it a standalone module.

These utilities were added in f3b7436b61,
which has not yet been included in a release, so skipping deprecation
and aliases for these.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
Sebastiaan van Stijn 2023-09-13 15:29:44 +02:00
parent 381442945b
commit e916d77c81
No known key found for this signature in database
GPG Key ID: 76698F39D527CE8C
5 changed files with 52 additions and 35 deletions

View File

@ -0,0 +1,47 @@
/*
Copyright The containerd Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package types
import oci "github.com/opencontainers/image-spec/specs-go/v1"
// OCIPlatformToProto converts from a slice of OCI [specs.Platform] to a
// slice of the protobuf definition [Platform].
func OCIPlatformToProto(platforms []oci.Platform) []*Platform {
ap := make([]*Platform, len(platforms))
for i := range platforms {
ap[i] = &Platform{
OS: platforms[i].OS,
Architecture: platforms[i].Architecture,
Variant: platforms[i].Variant,
}
}
return ap
}
// OCIPlatformFromProto converts a slice of the protobuf definition [Platform]
// to a slice of OCI [specs.Platform].
func OCIPlatformFromProto(platforms []*Platform) []oci.Platform {
op := make([]oci.Platform, len(platforms))
for i := range platforms {
op[i] = oci.Platform{
OS: platforms[i].OS,
Architecture: platforms[i].Architecture,
Variant: platforms[i].Variant,
}
}
return op
}

View File

@ -156,7 +156,7 @@ func (iis *ImageExportStream) UnmarshalAny(ctx context.Context, sm streaming.Str
return err return err
} }
specified := platforms.FromProto(s.Platforms) specified := types.OCIPlatformFromProto(s.Platforms)
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

View File

@ -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: platforms.ToProto(is.platforms), Platforms: types.OCIPlatformToProto(is.platforms),
ExtraReferences: referencesToProto(is.extraReferences), ExtraReferences: referencesToProto(is.extraReferences),
Unpacks: unpackToProto(is.unpacks), Unpacks: unpackToProto(is.unpacks),
} }
@ -380,7 +380,7 @@ 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 = platforms.FromProto(s.Platforms) is.platforms = types.OCIPlatformFromProto(s.Platforms)
is.extraReferences = referencesFromProto(s.ExtraReferences) is.extraReferences = referencesFromProto(s.ExtraReferences)
is.unpacks = unpackFromProto(s.Unpacks) is.unpacks = unpackFromProto(s.Unpacks)

View File

@ -118,8 +118,6 @@ import (
"strings" "strings"
specs "github.com/opencontainers/image-spec/specs-go/v1" specs "github.com/opencontainers/image-spec/specs-go/v1"
"github.com/containerd/containerd/api/types"
) )
var ( var (
@ -290,31 +288,3 @@ 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 {
ap[i] = &types.Platform{
OS: platforms[i].OS,
Architecture: platforms[i].Architecture,
Variant: platforms[i].Variant,
}
}
return ap
}
// FromProto converts a slice of the protobuf definition [types.Platform]
// to a slice of [Platform].
func FromProto(platforms []*types.Platform) []specs.Platform {
op := make([]specs.Platform, len(platforms))
for i := range platforms {
op[i] = specs.Platform{
OS: platforms[i].OS,
Architecture: platforms[i].Architecture,
Variant: platforms[i].Variant,
}
}
return op
}

View File

@ -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"
@ -222,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.ToProto(p.Meta.Platforms), Platforms: types.OCIPlatformToProto(p.Meta.Platforms),
Capabilities: p.Meta.Capabilities, Capabilities: p.Meta.Capabilities,
Exports: p.Meta.Exports, Exports: p.Meta.Exports,
InitErr: initErr, InitErr: initErr,