From e916d77c81b8a61d7a37276e310b4a150b04f4ab Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Wed, 13 Sep 2023 15:29:44 +0200 Subject: [PATCH] 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 f3b7436b61d2c3c69d7782d6bd817c2623be52e1, which has not yet been included in a release, so skipping deprecation and aliases for these. Signed-off-by: Sebastiaan van Stijn --- api/types/platform_helpers.go | 47 ++++++++++++++++++++++++++++++++ pkg/transfer/archive/exporter.go | 2 +- pkg/transfer/image/imagestore.go | 4 +-- platforms/platforms.go | 30 -------------------- services/introspection/local.go | 4 +-- 5 files changed, 52 insertions(+), 35 deletions(-) create mode 100644 api/types/platform_helpers.go diff --git a/api/types/platform_helpers.go b/api/types/platform_helpers.go new file mode 100644 index 000000000..7e662e9bb --- /dev/null +++ b/api/types/platform_helpers.go @@ -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 +} diff --git a/pkg/transfer/archive/exporter.go b/pkg/transfer/archive/exporter.go index d71f75c4a..59ee899db 100644 --- a/pkg/transfer/archive/exporter.go +++ b/pkg/transfer/archive/exporter.go @@ -156,7 +156,7 @@ func (iis *ImageExportStream) UnmarshalAny(ctx context.Context, sm streaming.Str return err } - specified := platforms.FromProto(s.Platforms) + specified := types.OCIPlatformFromProto(s.Platforms) iis.stream = tstreaming.WriteByteStream(ctx, stream) iis.mediaType = s.MediaType iis.platforms = specified diff --git a/pkg/transfer/image/imagestore.go b/pkg/transfer/image/imagestore.go index 975fc7f25..88d115d41 100644 --- a/pkg/transfer/image/imagestore.go +++ b/pkg/transfer/image/imagestore.go @@ -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: platforms.ToProto(is.platforms), + Platforms: types.OCIPlatformToProto(is.platforms), ExtraReferences: referencesToProto(is.extraReferences), Unpacks: unpackToProto(is.unpacks), } @@ -380,7 +380,7 @@ 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 = platforms.FromProto(s.Platforms) + is.platforms = types.OCIPlatformFromProto(s.Platforms) is.extraReferences = referencesFromProto(s.ExtraReferences) is.unpacks = unpackFromProto(s.Unpacks) diff --git a/platforms/platforms.go b/platforms/platforms.go index afcc867bb..43e4ad3d8 100644 --- a/platforms/platforms.go +++ b/platforms/platforms.go @@ -118,8 +118,6 @@ import ( "strings" specs "github.com/opencontainers/image-spec/specs-go/v1" - - "github.com/containerd/containerd/api/types" ) var ( @@ -290,31 +288,3 @@ 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 { - 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 -} diff --git a/services/introspection/local.go b/services/introspection/local.go index f1e1853cf..5ed328b40 100644 --- a/services/introspection/local.go +++ b/services/introspection/local.go @@ -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" @@ -222,7 +222,7 @@ func pluginsToPB(plugins []*plugin.Plugin) []*api.Plugin { Type: p.Registration.Type.String(), ID: p.Registration.ID, Requires: requires, - Platforms: platforms.ToProto(p.Meta.Platforms), + Platforms: types.OCIPlatformToProto(p.Meta.Platforms), Capabilities: p.Meta.Capabilities, Exports: p.Meta.Exports, InitErr: initErr,