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:
parent
381442945b
commit
e916d77c81
47
api/types/platform_helpers.go
Normal file
47
api/types/platform_helpers.go
Normal 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
|
||||
}
|
@ -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
|
||||
|
@ -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)
|
||||
|
||||
|
@ -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
|
||||
}
|
||||
|
@ -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,
|
||||
|
Loading…
Reference in New Issue
Block a user