Merge pull request #13091 from erofs/os_features

Add `os.features` support for EROFS native container images
This commit is contained in:
Fu Wei
2026-04-01 23:14:34 +00:00
committed by GitHub
23 changed files with 374 additions and 276 deletions

View File

@@ -25187,6 +25187,13 @@ file: {
type: TYPE_STRING
json_name: "osVersion"
}
field: {
name: "os_features"
number: 5
label: LABEL_REPEATED
type: TYPE_STRING
json_name: "osFeatures"
}
}
options: {
go_package: "github.com/containerd/containerd/api/types;types"
@@ -25195,7 +25202,7 @@ file: {
location: {
span: 16
span: 0
span: 29
span: 30
span: 1
}
location: {
@@ -25229,7 +25236,7 @@ file: {
path: 0
span: 24
span: 0
span: 29
span: 30
span: 1
leading_comments: " Platform follows the structure of the OCI platform specification, from\n descriptors.\n"
}
@@ -25397,6 +25404,55 @@ file: {
span: 22
span: 23
}
location: {
path: 4
path: 0
path: 2
path: 4
span: 29
span: 2
span: 34
}
location: {
path: 4
path: 0
path: 2
path: 4
path: 4
span: 29
span: 2
span: 10
}
location: {
path: 4
path: 0
path: 2
path: 4
path: 5
span: 29
span: 11
span: 17
}
location: {
path: 4
path: 0
path: 2
path: 4
path: 1
span: 29
span: 18
span: 29
}
location: {
path: 4
path: 0
path: 2
path: 4
path: 3
span: 29
span: 32
span: 33
}
}
syntax: "proto3"
buf_extension: {

View File

@@ -42,10 +42,11 @@ type Platform struct {
sizeCache protoimpl.SizeCache
unknownFields protoimpl.UnknownFields
OS string `protobuf:"bytes,1,opt,name=os,proto3" json:"os,omitempty"`
Architecture string `protobuf:"bytes,2,opt,name=architecture,proto3" json:"architecture,omitempty"`
Variant string `protobuf:"bytes,3,opt,name=variant,proto3" json:"variant,omitempty"`
OSVersion string `protobuf:"bytes,4,opt,name=os_version,json=osVersion,proto3" json:"os_version,omitempty"`
OS string `protobuf:"bytes,1,opt,name=os,proto3" json:"os,omitempty"`
Architecture string `protobuf:"bytes,2,opt,name=architecture,proto3" json:"architecture,omitempty"`
Variant string `protobuf:"bytes,3,opt,name=variant,proto3" json:"variant,omitempty"`
OSVersion string `protobuf:"bytes,4,opt,name=os_version,json=osVersion,proto3" json:"os_version,omitempty"`
OSFeatures []string `protobuf:"bytes,5,rep,name=os_features,json=osFeatures,proto3" json:"os_features,omitempty"`
}
func (x *Platform) Reset() {
@@ -108,23 +109,32 @@ func (x *Platform) GetOsVersion() string {
return ""
}
func (x *Platform) GetOsFeatures() []string {
if x != nil {
return x.OSFeatures
}
return nil
}
var File_types_platform_proto protoreflect.FileDescriptor
var file_types_platform_proto_rawDesc = []byte{
0x0a, 0x14, 0x74, 0x79, 0x70, 0x65, 0x73, 0x2f, 0x70, 0x6c, 0x61, 0x74, 0x66, 0x6f, 0x72, 0x6d,
0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x12, 0x10, 0x63, 0x6f, 0x6e, 0x74, 0x61, 0x69, 0x6e, 0x65,
0x72, 0x64, 0x2e, 0x74, 0x79, 0x70, 0x65, 0x73, 0x22, 0x77, 0x0a, 0x08, 0x50, 0x6c, 0x61, 0x74,
0x66, 0x6f, 0x72, 0x6d, 0x12, 0x0e, 0x0a, 0x02, 0x6f, 0x73, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09,
0x52, 0x02, 0x6f, 0x73, 0x12, 0x22, 0x0a, 0x0c, 0x61, 0x72, 0x63, 0x68, 0x69, 0x74, 0x65, 0x63,
0x74, 0x75, 0x72, 0x65, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x0c, 0x61, 0x72, 0x63, 0x68,
0x69, 0x74, 0x65, 0x63, 0x74, 0x75, 0x72, 0x65, 0x12, 0x18, 0x0a, 0x07, 0x76, 0x61, 0x72, 0x69,
0x61, 0x6e, 0x74, 0x18, 0x03, 0x20, 0x01, 0x28, 0x09, 0x52, 0x07, 0x76, 0x61, 0x72, 0x69, 0x61,
0x6e, 0x74, 0x12, 0x1d, 0x0a, 0x0a, 0x6f, 0x73, 0x5f, 0x76, 0x65, 0x72, 0x73, 0x69, 0x6f, 0x6e,
0x18, 0x04, 0x20, 0x01, 0x28, 0x09, 0x52, 0x09, 0x6f, 0x73, 0x56, 0x65, 0x72, 0x73, 0x69, 0x6f,
0x6e, 0x42, 0x32, 0x5a, 0x30, 0x67, 0x69, 0x74, 0x68, 0x75, 0x62, 0x2e, 0x63, 0x6f, 0x6d, 0x2f,
0x63, 0x6f, 0x6e, 0x74, 0x61, 0x69, 0x6e, 0x65, 0x72, 0x64, 0x2f, 0x63, 0x6f, 0x6e, 0x74, 0x61,
0x69, 0x6e, 0x65, 0x72, 0x64, 0x2f, 0x61, 0x70, 0x69, 0x2f, 0x74, 0x79, 0x70, 0x65, 0x73, 0x3b,
0x74, 0x79, 0x70, 0x65, 0x73, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33,
0x72, 0x64, 0x2e, 0x74, 0x79, 0x70, 0x65, 0x73, 0x22, 0x98, 0x01, 0x0a, 0x08, 0x50, 0x6c, 0x61,
0x74, 0x66, 0x6f, 0x72, 0x6d, 0x12, 0x0e, 0x0a, 0x02, 0x6f, 0x73, 0x18, 0x01, 0x20, 0x01, 0x28,
0x09, 0x52, 0x02, 0x6f, 0x73, 0x12, 0x22, 0x0a, 0x0c, 0x61, 0x72, 0x63, 0x68, 0x69, 0x74, 0x65,
0x63, 0x74, 0x75, 0x72, 0x65, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x0c, 0x61, 0x72, 0x63,
0x68, 0x69, 0x74, 0x65, 0x63, 0x74, 0x75, 0x72, 0x65, 0x12, 0x18, 0x0a, 0x07, 0x76, 0x61, 0x72,
0x69, 0x61, 0x6e, 0x74, 0x18, 0x03, 0x20, 0x01, 0x28, 0x09, 0x52, 0x07, 0x76, 0x61, 0x72, 0x69,
0x61, 0x6e, 0x74, 0x12, 0x1d, 0x0a, 0x0a, 0x6f, 0x73, 0x5f, 0x76, 0x65, 0x72, 0x73, 0x69, 0x6f,
0x6e, 0x18, 0x04, 0x20, 0x01, 0x28, 0x09, 0x52, 0x09, 0x6f, 0x73, 0x56, 0x65, 0x72, 0x73, 0x69,
0x6f, 0x6e, 0x12, 0x1f, 0x0a, 0x0b, 0x6f, 0x73, 0x5f, 0x66, 0x65, 0x61, 0x74, 0x75, 0x72, 0x65,
0x73, 0x18, 0x05, 0x20, 0x03, 0x28, 0x09, 0x52, 0x0a, 0x6f, 0x73, 0x46, 0x65, 0x61, 0x74, 0x75,
0x72, 0x65, 0x73, 0x42, 0x32, 0x5a, 0x30, 0x67, 0x69, 0x74, 0x68, 0x75, 0x62, 0x2e, 0x63, 0x6f,
0x6d, 0x2f, 0x63, 0x6f, 0x6e, 0x74, 0x61, 0x69, 0x6e, 0x65, 0x72, 0x64, 0x2f, 0x63, 0x6f, 0x6e,
0x74, 0x61, 0x69, 0x6e, 0x65, 0x72, 0x64, 0x2f, 0x61, 0x70, 0x69, 0x2f, 0x74, 0x79, 0x70, 0x65,
0x73, 0x3b, 0x74, 0x79, 0x70, 0x65, 0x73, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33,
}
var (

View File

@@ -27,4 +27,5 @@ message Platform {
string architecture = 2;
string variant = 3;
string os_version = 4;
repeated string os_features = 5;
}

View File

@@ -28,6 +28,7 @@ func OCIPlatformToProto(platforms []oci.Platform) []*Platform {
OSVersion: platforms[i].OSVersion,
Architecture: platforms[i].Architecture,
Variant: platforms[i].Variant,
OSFeatures: platforms[i].OSFeatures,
}
}
return ap
@@ -43,6 +44,7 @@ func OCIPlatformFromProto(platforms []*Platform) []oci.Platform {
OSVersion: platforms[i].OSVersion,
Architecture: platforms[i].Architecture,
Variant: platforms[i].Variant,
OSFeatures: platforms[i].OSFeatures,
}
}
return op

View File

@@ -953,22 +953,10 @@ func (c *Client) GetSnapshotterSupportedPlatforms(ctx context.Context, snapshott
}
sn := resp.Plugins[0]
snPlatforms := toPlatforms(sn.Platforms)
snPlatforms := apitypes.OCIPlatformFromProto(sn.Platforms)
return platforms.Any(snPlatforms...), nil
}
func toPlatforms(pt []*apitypes.Platform) []ocispec.Platform {
platforms := make([]ocispec.Platform, len(pt))
for i, p := range pt {
platforms[i] = ocispec.Platform{
Architecture: p.Architecture,
OS: p.OS,
Variant: p.Variant,
}
}
return platforms
}
// GetSnapshotterCapabilities returns the capabilities of a snapshotter.
func (c *Client) GetSnapshotterCapabilities(ctx context.Context, snapshotterName string) ([]string, error) {
filters := []string{fmt.Sprintf("type==%s, id==%s", plugins.SnapshotPlugin, snapshotterName)}

View File

@@ -337,10 +337,10 @@ func (i *image) Unpack(ctx context.Context, snapshotterName string, opts ...Unpa
if err != nil {
return err
}
if config.CheckPlatformSupported {
if err := i.checkSnapshotterSupport(ctx, snapshotterName, manifest); err != nil {
return err
}
if err := i.checkSnapshotterSupport(ctx, snapshotterName, manifest,
config.CheckPlatformSupported); err != nil {
return err
}
for _, layer := range layers {
@@ -421,13 +421,18 @@ func (i *image) getLayers(ctx context.Context, manifest ocispec.Manifest) ([]roo
return layers, nil
}
func (i *image) checkSnapshotterSupport(ctx context.Context, snapshotterName string, manifest ocispec.Manifest) error {
snapshotterPlatformMatcher, err := i.client.GetSnapshotterSupportedPlatforms(ctx, snapshotterName)
func (i *image) checkSnapshotterSupport(ctx context.Context, snapshotterName string,
manifest ocispec.Manifest, checkSnapshotterSupport bool) error {
manifestPlatform, err := images.ConfigPlatform(ctx, i.ContentStore(), manifest.Config)
if err != nil {
return err
}
manifestPlatform, err := images.ConfigPlatform(ctx, i.ContentStore(), manifest.Config)
if len(manifestPlatform.OSFeatures) == 0 && !checkSnapshotterSupport {
return nil
}
snapshotterPlatformMatcher, err := i.client.GetSnapshotterSupportedPlatforms(ctx, snapshotterName)
if err != nil {
return err
}

View File

@@ -425,6 +425,7 @@ func unpackToProto(uc []transfer.UnpackConfiguration) []*transfertypes.UnpackCon
OS: uc[i].Platform.OS,
Architecture: uc[i].Platform.Architecture,
Variant: uc[i].Platform.Variant,
OSFeatures: uc[i].Platform.OSFeatures,
}
auc[i] = &transfertypes.UnpackConfiguration{
Platform: &p,
@@ -442,6 +443,7 @@ func unpackFromProto(auc []*transfertypes.UnpackConfiguration) []transfer.Unpack
uc[i].Platform.OS = auc[i].Platform.OS
uc[i].Platform.Architecture = auc[i].Platform.Architecture
uc[i].Platform.Variant = auc[i].Platform.Variant
uc[i].Platform.OSFeatures = auc[i].Platform.OSFeatures
}
}
return uc

4
go.mod
View File

@@ -24,7 +24,7 @@ require (
github.com/containerd/log v0.1.0
github.com/containerd/nri v0.11.0
github.com/containerd/otelttrpc v0.1.0
github.com/containerd/platforms v1.0.0-rc.3
github.com/containerd/platforms v1.0.0-rc.4
github.com/containerd/plugin v1.0.0
github.com/containerd/ttrpc v1.2.8
github.com/containerd/typeurl/v2 v2.2.3
@@ -162,3 +162,5 @@ require (
sigs.k8s.io/yaml v1.6.0 // indirect
tags.cncf.io/container-device-interface/specs-go v1.1.0 // indirect
)
replace github.com/containerd/containerd/api => ./api

6
go.sum
View File

@@ -43,8 +43,6 @@ github.com/containerd/cgroups/v3 v3.1.3 h1:eUNflyMddm18+yrDmZPn3jI7C5hJ9ahABE5q6
github.com/containerd/cgroups/v3 v3.1.3/go.mod h1:PKZ2AcWmSBsY/tJUVhtS/rluX0b1uq1GmPO1ElCmbOw=
github.com/containerd/console v1.0.5 h1:R0ymNeydRqH2DmakFNdmjR2k0t7UPuiOV/N/27/qqsc=
github.com/containerd/console v1.0.5/go.mod h1:YynlIjWYF8myEu6sdkwKIvGQq+cOckRm6So2avqoYAk=
github.com/containerd/containerd/api v1.11.0-beta.0 h1:qtnn2fNjzVl82CRfzt6VvDikwQnMD66eq06Djb+I1Lc=
github.com/containerd/containerd/api v1.11.0-beta.0/go.mod h1:NBm1OAk8ZL+LG8R0ceObGxT5hbUYj7CzTmR3xh0DlMM=
github.com/containerd/continuity v0.4.5 h1:ZRoN1sXq9u7V6QoHMcVWGhOwDFqZ4B9i5H6un1Wh0x4=
github.com/containerd/continuity v0.4.5/go.mod h1:/lNJvtJKUQStBzpVQ1+rasXO1LAWtUQssk28EZvJ3nE=
github.com/containerd/errdefs v1.0.0 h1:tg5yIfIlQIrxYtu9ajqY42W3lpS19XqdxRQeEwYG8PI=
@@ -67,8 +65,8 @@ github.com/containerd/nri v0.11.0 h1:26mcQwNG58AZn0YkOrlJQ0yxQVmyZooflnVWJTqQrqQ
github.com/containerd/nri v0.11.0/go.mod h1:bjGTLdUA58WgghKHg8azFMGXr05n1wDHrt3NSVBHiGI=
github.com/containerd/otelttrpc v0.1.0 h1:UOX68eVTE8H/T45JveIg+I22Ev2aFj4qPITCmXsskjw=
github.com/containerd/otelttrpc v0.1.0/go.mod h1:XhoA2VvaGPW1clB2ULwrBZfXVuEWuyOd2NUD1IM0yTg=
github.com/containerd/platforms v1.0.0-rc.3 h1:YdvwaHtrN6wHcGJ2mYRYP3Nso8OcysuqFe9Hxm1X/tI=
github.com/containerd/platforms v1.0.0-rc.3/go.mod h1:gw0R+alP3nFQPh1L4K9bv13fRWeeyokLGLu2fKuqI10=
github.com/containerd/platforms v1.0.0-rc.4 h1:M42JrUT4zfZTqtkUwkr0GzmUWbfyO5VO0Q5b3op97T4=
github.com/containerd/platforms v1.0.0-rc.4/go.mod h1:lKlMXyLybmBedS/JJm11uDofzI8L2v0J2ZbYvNsbq1A=
github.com/containerd/plugin v1.0.0 h1:c8Kf1TNl6+e2TtMHZt+39yAPDbouRH9WAToRjex483Y=
github.com/containerd/plugin v1.0.0/go.mod h1:hQfJe5nmWfImiqT1q8Si3jLv3ynMUIBB47bQ+KexvO8=
github.com/containerd/ttrpc v1.2.8 h1:xbVu6D4qF2jihdh9rDVOKqUMiFBQk6YctTdo1zk087Y=

View File

@@ -69,6 +69,9 @@ func init() {
p := platforms.DefaultSpec()
p.OS = "linux"
ic.Meta.Platforms = append(ic.Meta.Platforms, p)
// Select this differ for EROFS native images by default
p.OSFeatures = []string{"erofs"}
ic.Meta.Platforms = append(ic.Meta.Platforms, p)
cs := md.(*metadata.DB).ContentStore()
config := ic.Config.(*Config)

View File

@@ -102,6 +102,9 @@ func init() {
sn := ms.Snapshotter(uc.Snapshotter)
if sn == nil {
if uc.Optional {
continue
}
return nil, fmt.Errorf("snapshotter %q not found: %w", uc.Snapshotter, errdefs.ErrNotFound)
}
var (
@@ -165,12 +168,15 @@ func init() {
}
}
if applier == nil {
if uc.Optional {
continue
}
return nil, fmt.Errorf("no matching diff plugins: %w", errdefs.ErrNotFound)
}
// If CheckPlatformSupported is false, we will match all platforms
// If CheckPlatformSupported is false, platforms.OnlyOS() is applied
if !config.CheckPlatformSupported {
target = platforms.All
target = platforms.OnlyOS(p)
}
up := unpack.Platform{
@@ -235,6 +241,9 @@ type unpackConfiguration struct {
// LayerTypes are the allowed layer types for this unpack configuration
LayerTypes []string `toml:"layer_types"`
// Optional skips the configuration when initialization fails
Optional bool `toml:"optional"`
}
func defaultConfig() *transferConfig {

View File

@@ -0,0 +1,48 @@
/*
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 transfer
import (
"github.com/containerd/containerd/v2/defaults"
"github.com/containerd/platforms"
specs "github.com/opencontainers/image-spec/specs-go/v1"
)
// Use EROFS snapshotter to extract EROFS native images
func erofsPlatformSpec() specs.Platform {
p := platforms.DefaultSpec()
p.OSFeatures = []string{"erofs"}
return p
}
func defaultUnpackConfig() []unpackConfiguration {
return []unpackConfiguration{
{
Platform: platforms.Format(platforms.DefaultSpec()),
Snapshotter: defaults.DefaultSnapshotter,
Differ: defaults.DefaultDiffer,
},
{
Platform: platforms.FormatAll(erofsPlatformSpec()),
Snapshotter: "erofs",
Differ: "erofs",
Optional: true,
},
}
}

View File

@@ -1,4 +1,4 @@
//go:build !windows && !darwin
//go:build !windows && !darwin && !linux
/*
Copyright The containerd Authors.

View File

@@ -1,191 +0,0 @@
Apache License
Version 2.0, January 2004
https://www.apache.org/licenses/
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
1. Definitions.
"License" shall mean the terms and conditions for use, reproduction,
and distribution as defined by Sections 1 through 9 of this document.
"Licensor" shall mean the copyright owner or entity authorized by
the copyright owner that is granting the License.
"Legal Entity" shall mean the union of the acting entity and all
other entities that control, are controlled by, or are under common
control with that entity. For the purposes of this definition,
"control" means (i) the power, direct or indirect, to cause the
direction or management of such entity, whether by contract or
otherwise, or (ii) ownership of fifty percent (50%) or more of the
outstanding shares, or (iii) beneficial ownership of such entity.
"You" (or "Your") shall mean an individual or Legal Entity
exercising permissions granted by this License.
"Source" form shall mean the preferred form for making modifications,
including but not limited to software source code, documentation
source, and configuration files.
"Object" form shall mean any form resulting from mechanical
transformation or translation of a Source form, including but
not limited to compiled object code, generated documentation,
and conversions to other media types.
"Work" shall mean the work of authorship, whether in Source or
Object form, made available under the License, as indicated by a
copyright notice that is included in or attached to the work
(an example is provided in the Appendix below).
"Derivative Works" shall mean any work, whether in Source or Object
form, that is based on (or derived from) the Work and for which the
editorial revisions, annotations, elaborations, or other modifications
represent, as a whole, an original work of authorship. For the purposes
of this License, Derivative Works shall not include works that remain
separable from, or merely link (or bind by name) to the interfaces of,
the Work and Derivative Works thereof.
"Contribution" shall mean any work of authorship, including
the original version of the Work and any modifications or additions
to that Work or Derivative Works thereof, that is intentionally
submitted to Licensor for inclusion in the Work by the copyright owner
or by an individual or Legal Entity authorized to submit on behalf of
the copyright owner. For the purposes of this definition, "submitted"
means any form of electronic, verbal, or written communication sent
to the Licensor or its representatives, including but not limited to
communication on electronic mailing lists, source code control systems,
and issue tracking systems that are managed by, or on behalf of, the
Licensor for the purpose of discussing and improving the Work, but
excluding communication that is conspicuously marked or otherwise
designated in writing by the copyright owner as "Not a Contribution."
"Contributor" shall mean Licensor and any individual or Legal Entity
on behalf of whom a Contribution has been received by Licensor and
subsequently incorporated within the Work.
2. Grant of Copyright License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
copyright license to reproduce, prepare Derivative Works of,
publicly display, publicly perform, sublicense, and distribute the
Work and such Derivative Works in Source or Object form.
3. Grant of Patent License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
(except as stated in this section) patent license to make, have made,
use, offer to sell, sell, import, and otherwise transfer the Work,
where such license applies only to those patent claims licensable
by such Contributor that are necessarily infringed by their
Contribution(s) alone or by combination of their Contribution(s)
with the Work to which such Contribution(s) was submitted. If You
institute patent litigation against any entity (including a
cross-claim or counterclaim in a lawsuit) alleging that the Work
or a Contribution incorporated within the Work constitutes direct
or contributory patent infringement, then any patent licenses
granted to You under this License for that Work shall terminate
as of the date such litigation is filed.
4. Redistribution. You may reproduce and distribute copies of the
Work or Derivative Works thereof in any medium, with or without
modifications, and in Source or Object form, provided that You
meet the following conditions:
(a) You must give any other recipients of the Work or
Derivative Works a copy of this License; and
(b) You must cause any modified files to carry prominent notices
stating that You changed the files; and
(c) You must retain, in the Source form of any Derivative Works
that You distribute, all copyright, patent, trademark, and
attribution notices from the Source form of the Work,
excluding those notices that do not pertain to any part of
the Derivative Works; and
(d) If the Work includes a "NOTICE" text file as part of its
distribution, then any Derivative Works that You distribute must
include a readable copy of the attribution notices contained
within such NOTICE file, excluding those notices that do not
pertain to any part of the Derivative Works, in at least one
of the following places: within a NOTICE text file distributed
as part of the Derivative Works; within the Source form or
documentation, if provided along with the Derivative Works; or,
within a display generated by the Derivative Works, if and
wherever such third-party notices normally appear. The contents
of the NOTICE file are for informational purposes only and
do not modify the License. You may add Your own attribution
notices within Derivative Works that You distribute, alongside
or as an addendum to the NOTICE text from the Work, provided
that such additional attribution notices cannot be construed
as modifying the License.
You may add Your own copyright statement to Your modifications and
may provide additional or different license terms and conditions
for use, reproduction, or distribution of Your modifications, or
for any such Derivative Works as a whole, provided Your use,
reproduction, and distribution of the Work otherwise complies with
the conditions stated in this License.
5. Submission of Contributions. Unless You explicitly state otherwise,
any Contribution intentionally submitted for inclusion in the Work
by You to the Licensor shall be under the terms and conditions of
this License, without any additional terms or conditions.
Notwithstanding the above, nothing herein shall supersede or modify
the terms of any separate license agreement you may have executed
with Licensor regarding such Contributions.
6. Trademarks. This License does not grant permission to use the trade
names, trademarks, service marks, or product names of the Licensor,
except as required for reasonable and customary use in describing the
origin of the Work and reproducing the content of the NOTICE file.
7. Disclaimer of Warranty. Unless required by applicable law or
agreed to in writing, Licensor provides the Work (and each
Contributor provides its Contributions) on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
implied, including, without limitation, any warranties or conditions
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
PARTICULAR PURPOSE. You are solely responsible for determining the
appropriateness of using or redistributing the Work and assume any
risks associated with Your exercise of permissions under this License.
8. Limitation of Liability. In no event and under no legal theory,
whether in tort (including negligence), contract, or otherwise,
unless required by applicable law (such as deliberate and grossly
negligent acts) or agreed to in writing, shall any Contributor be
liable to You for damages, including any direct, indirect, special,
incidental, or consequential damages of any character arising as a
result of this License or out of the use or inability to use the
Work (including but not limited to damages for loss of goodwill,
work stoppage, computer failure or malfunction, or any and all
other commercial damages or losses), even if such Contributor
has been advised of the possibility of such damages.
9. Accepting Warranty or Additional Liability. While redistributing
the Work or Derivative Works thereof, You may choose to offer,
and charge a fee for, acceptance of support, warranty, indemnity,
or other liability obligations and/or rights consistent with this
License. However, in accepting such obligations, You may act only
on Your own behalf and on Your sole responsibility, not on behalf
of any other Contributor, and only if You agree to indemnify,
defend, and hold each Contributor harmless for any liability
incurred by, or claims asserted against, such Contributor by reason
of your accepting any such warranty or additional liability.
END OF TERMS AND CONDITIONS
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
https://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.

View File

@@ -42,10 +42,11 @@ type Platform struct {
sizeCache protoimpl.SizeCache
unknownFields protoimpl.UnknownFields
OS string `protobuf:"bytes,1,opt,name=os,proto3" json:"os,omitempty"`
Architecture string `protobuf:"bytes,2,opt,name=architecture,proto3" json:"architecture,omitempty"`
Variant string `protobuf:"bytes,3,opt,name=variant,proto3" json:"variant,omitempty"`
OSVersion string `protobuf:"bytes,4,opt,name=os_version,json=osVersion,proto3" json:"os_version,omitempty"`
OS string `protobuf:"bytes,1,opt,name=os,proto3" json:"os,omitempty"`
Architecture string `protobuf:"bytes,2,opt,name=architecture,proto3" json:"architecture,omitempty"`
Variant string `protobuf:"bytes,3,opt,name=variant,proto3" json:"variant,omitempty"`
OSVersion string `protobuf:"bytes,4,opt,name=os_version,json=osVersion,proto3" json:"os_version,omitempty"`
OSFeatures []string `protobuf:"bytes,5,rep,name=os_features,json=osFeatures,proto3" json:"os_features,omitempty"`
}
func (x *Platform) Reset() {
@@ -108,23 +109,32 @@ func (x *Platform) GetOsVersion() string {
return ""
}
func (x *Platform) GetOsFeatures() []string {
if x != nil {
return x.OSFeatures
}
return nil
}
var File_types_platform_proto protoreflect.FileDescriptor
var file_types_platform_proto_rawDesc = []byte{
0x0a, 0x14, 0x74, 0x79, 0x70, 0x65, 0x73, 0x2f, 0x70, 0x6c, 0x61, 0x74, 0x66, 0x6f, 0x72, 0x6d,
0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x12, 0x10, 0x63, 0x6f, 0x6e, 0x74, 0x61, 0x69, 0x6e, 0x65,
0x72, 0x64, 0x2e, 0x74, 0x79, 0x70, 0x65, 0x73, 0x22, 0x77, 0x0a, 0x08, 0x50, 0x6c, 0x61, 0x74,
0x66, 0x6f, 0x72, 0x6d, 0x12, 0x0e, 0x0a, 0x02, 0x6f, 0x73, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09,
0x52, 0x02, 0x6f, 0x73, 0x12, 0x22, 0x0a, 0x0c, 0x61, 0x72, 0x63, 0x68, 0x69, 0x74, 0x65, 0x63,
0x74, 0x75, 0x72, 0x65, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x0c, 0x61, 0x72, 0x63, 0x68,
0x69, 0x74, 0x65, 0x63, 0x74, 0x75, 0x72, 0x65, 0x12, 0x18, 0x0a, 0x07, 0x76, 0x61, 0x72, 0x69,
0x61, 0x6e, 0x74, 0x18, 0x03, 0x20, 0x01, 0x28, 0x09, 0x52, 0x07, 0x76, 0x61, 0x72, 0x69, 0x61,
0x6e, 0x74, 0x12, 0x1d, 0x0a, 0x0a, 0x6f, 0x73, 0x5f, 0x76, 0x65, 0x72, 0x73, 0x69, 0x6f, 0x6e,
0x18, 0x04, 0x20, 0x01, 0x28, 0x09, 0x52, 0x09, 0x6f, 0x73, 0x56, 0x65, 0x72, 0x73, 0x69, 0x6f,
0x6e, 0x42, 0x32, 0x5a, 0x30, 0x67, 0x69, 0x74, 0x68, 0x75, 0x62, 0x2e, 0x63, 0x6f, 0x6d, 0x2f,
0x63, 0x6f, 0x6e, 0x74, 0x61, 0x69, 0x6e, 0x65, 0x72, 0x64, 0x2f, 0x63, 0x6f, 0x6e, 0x74, 0x61,
0x69, 0x6e, 0x65, 0x72, 0x64, 0x2f, 0x61, 0x70, 0x69, 0x2f, 0x74, 0x79, 0x70, 0x65, 0x73, 0x3b,
0x74, 0x79, 0x70, 0x65, 0x73, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33,
0x72, 0x64, 0x2e, 0x74, 0x79, 0x70, 0x65, 0x73, 0x22, 0x98, 0x01, 0x0a, 0x08, 0x50, 0x6c, 0x61,
0x74, 0x66, 0x6f, 0x72, 0x6d, 0x12, 0x0e, 0x0a, 0x02, 0x6f, 0x73, 0x18, 0x01, 0x20, 0x01, 0x28,
0x09, 0x52, 0x02, 0x6f, 0x73, 0x12, 0x22, 0x0a, 0x0c, 0x61, 0x72, 0x63, 0x68, 0x69, 0x74, 0x65,
0x63, 0x74, 0x75, 0x72, 0x65, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x0c, 0x61, 0x72, 0x63,
0x68, 0x69, 0x74, 0x65, 0x63, 0x74, 0x75, 0x72, 0x65, 0x12, 0x18, 0x0a, 0x07, 0x76, 0x61, 0x72,
0x69, 0x61, 0x6e, 0x74, 0x18, 0x03, 0x20, 0x01, 0x28, 0x09, 0x52, 0x07, 0x76, 0x61, 0x72, 0x69,
0x61, 0x6e, 0x74, 0x12, 0x1d, 0x0a, 0x0a, 0x6f, 0x73, 0x5f, 0x76, 0x65, 0x72, 0x73, 0x69, 0x6f,
0x6e, 0x18, 0x04, 0x20, 0x01, 0x28, 0x09, 0x52, 0x09, 0x6f, 0x73, 0x56, 0x65, 0x72, 0x73, 0x69,
0x6f, 0x6e, 0x12, 0x1f, 0x0a, 0x0b, 0x6f, 0x73, 0x5f, 0x66, 0x65, 0x61, 0x74, 0x75, 0x72, 0x65,
0x73, 0x18, 0x05, 0x20, 0x03, 0x28, 0x09, 0x52, 0x0a, 0x6f, 0x73, 0x46, 0x65, 0x61, 0x74, 0x75,
0x72, 0x65, 0x73, 0x42, 0x32, 0x5a, 0x30, 0x67, 0x69, 0x74, 0x68, 0x75, 0x62, 0x2e, 0x63, 0x6f,
0x6d, 0x2f, 0x63, 0x6f, 0x6e, 0x74, 0x61, 0x69, 0x6e, 0x65, 0x72, 0x64, 0x2f, 0x63, 0x6f, 0x6e,
0x74, 0x61, 0x69, 0x6e, 0x65, 0x72, 0x64, 0x2f, 0x61, 0x70, 0x69, 0x2f, 0x74, 0x79, 0x70, 0x65,
0x73, 0x3b, 0x74, 0x79, 0x70, 0x65, 0x73, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33,
}
var (

View File

@@ -27,4 +27,5 @@ message Platform {
string architecture = 2;
string variant = 3;
string os_version = 4;
repeated string os_features = 5;
}

View File

@@ -28,6 +28,7 @@ func OCIPlatformToProto(platforms []oci.Platform) []*Platform {
OSVersion: platforms[i].OSVersion,
Architecture: platforms[i].Architecture,
Variant: platforms[i].Variant,
OSFeatures: platforms[i].OSFeatures,
}
}
return ap
@@ -43,6 +44,7 @@ func OCIPlatformFromProto(platforms []*Platform) []oci.Platform {
OSVersion: platforms[i].OSVersion,
Architecture: platforms[i].Architecture,
Variant: platforms[i].Variant,
OSFeatures: platforms[i].OSFeatures,
}
}
return op

View File

@@ -152,6 +152,88 @@ func Only(platform specs.Platform) MatchComparer {
return Ordered(platformVector(Normalize(platform))...)
}
// OnlyOS returns a match comparer that matches only platforms with the same
// OS, OS version, and OS features, regardless of architecture. When comparing,
// it always ranks the best architecture match highest using the default
// platform resolution logic.
func OnlyOS(platform specs.Platform) MatchComparer {
normalized := Normalize(platform)
return onlyOSComparer{
platform: normalized,
osvM: newOSVersionMatcher(normalized),
archOrder: orderedPlatformComparer{
matchers: []Matcher{NewMatcher(normalized)},
},
}
}
func newOSVersionMatcher(platform specs.Platform) osVerMatcher {
if platform.OS == "windows" {
return &windowsVersionMatcher{
windowsOSVersion: getWindowsOSVersion(platform.OSVersion),
}
}
return nil
}
type onlyOSComparer struct {
platform specs.Platform
osvM osVerMatcher
archOrder orderedPlatformComparer
}
func (c onlyOSComparer) matchOS(platform specs.Platform) bool {
normalized := Normalize(platform)
if c.platform.OS != normalized.OS {
return false
}
if c.osvM != nil {
if !c.osvM.Match(platform.OSVersion) {
return false
}
}
if len(normalized.OSFeatures) > 0 {
if len(c.platform.OSFeatures) < len(normalized.OSFeatures) {
return false
}
j := 0
for _, feature := range normalized.OSFeatures {
found := false
for ; j < len(c.platform.OSFeatures); j++ {
if feature == c.platform.OSFeatures[j] {
found = true
j++
break
}
if feature < c.platform.OSFeatures[j] {
return false
}
}
if !found {
return false
}
}
}
return true
}
func (c onlyOSComparer) Match(platform specs.Platform) bool {
return c.matchOS(platform)
}
func (c onlyOSComparer) Less(p1, p2 specs.Platform) bool {
p1m := c.matchOS(p1)
p2m := c.matchOS(p2)
if p1m && !p2m {
return true
}
if !p1m {
return false
}
// Both match — rank by architecture preference
return c.archOrder.Less(p1, p2)
}
// OnlyStrict returns a match comparer for a single platform.
//
// Unlike Only, OnlyStrict does not match sub platforms.

View File

@@ -45,7 +45,6 @@ func getMachineArch() (string, error) {
// So we don't need to access the ARM registers to detect platform information
// by ourselves. We can just parse these information from /proc/cpuinfo
func getCPUInfo(pattern string) (info string, err error) {
cpuinfo, err := os.Open("/proc/cpuinfo")
if err != nil {
return "", err
@@ -75,7 +74,6 @@ func getCPUInfo(pattern string) (info string, err error) {
// getCPUVariantFromArch get CPU variant from arch through a system call
func getCPUVariantFromArch(arch string) (string, error) {
var variant string
arch = strings.ToLower(arch)

View File

@@ -24,7 +24,6 @@ import (
)
func getCPUVariant() (string, error) {
var variant string
switch runtime.GOOS {

View File

@@ -17,6 +17,7 @@
package platforms
import (
"slices"
"strconv"
"strings"
@@ -162,3 +163,14 @@ func (c *windowsMatchComparer) Less(p1, p2 specs.Platform) bool {
}
return m1 && !m2
}
type windowsStripFeaturesMatcher struct {
Matcher
}
func (m windowsStripFeaturesMatcher) Match(p specs.Platform) bool {
if i := slices.Index(p.OSFeatures, "win32k"); i >= 0 {
p.OSFeatures = slices.Delete(slices.Clone(p.OSFeatures), i, i+1)
}
return m.Matcher.Match(p)
}

View File

@@ -156,6 +156,11 @@ func NewMatcher(platform specs.Platform) Matcher {
m.osvM = &windowsVersionMatcher{
windowsOSVersion: getWindowsOSVersion(platform.OSVersion),
}
// In prior versions, the win32k os feature was not considered for matching,
// strip out the win32k feature for comparison
var stripped Matcher = windowsStripFeaturesMatcher{m}
// In prior versions, on windows, the returned matcher implements a
// MatchComprarer interface.
// This preserves that behavior for backwards compatibility.
@@ -165,8 +170,9 @@ func NewMatcher(platform specs.Platform) Matcher {
// It was likely intended to be used in `Ordered` but it is not since
// `Less` that is implemented here ends up getting masked due to wrapping.
if runtime.GOOS == "windows" {
return &windowsMatchComparer{m}
return &windowsMatchComparer{stripped}
}
return stripped
}
return m
}
@@ -280,13 +286,9 @@ func Parse(specifier string) (specs.Platform, error) {
}
p.OSVersion = osVersion
if osOptions[3] != "" {
rawFeatures := strings.Split(osOptions[3][1:], "+")
p.OSFeatures = make([]string, len(rawFeatures))
for i, f := range rawFeatures {
p.OSFeatures[i], err = decodeOSOption(f)
if err != nil {
return specs.Platform{}, fmt.Errorf("%q has an invalid OS feature %q: %w", specifier, f, err)
}
p.OSFeatures, err = parseOSFeatures(osOptions[3][1:])
if err != nil {
return specs.Platform{}, fmt.Errorf("%q has invalid OS features: %w", specifier, err)
}
}
} else {
@@ -346,6 +348,30 @@ func Parse(specifier string) (specs.Platform, error) {
return specs.Platform{}, fmt.Errorf("%q: cannot parse platform specifier: %w", specifier, errInvalidArgument)
}
func parseOSFeatures(s string) ([]string, error) {
if s == "" {
return nil, nil
}
var features []string
for raw := range strings.SplitSeq(s, "+") {
raw = strings.TrimSpace(raw)
if raw == "" {
return nil, fmt.Errorf("empty os feature: %w", errInvalidArgument)
}
feature, err := decodeOSOption(raw)
if err != nil {
return nil, fmt.Errorf("invalid os feature %q: %w", raw, err)
}
if feature == "" {
continue
}
features = append(features, feature)
}
return features, nil
}
// MustParse is like Parses but panics if the specifier cannot be parsed.
// Simplifies initialization of global variables.
func MustParse(specifier string) specs.Platform {
@@ -371,21 +397,55 @@ func FormatAll(platform specs.Platform) string {
if platform.OS == "" {
return "unknown"
}
if platform.OSVersion == "" && len(platform.OSFeatures) == 0 {
return path.Join(platform.OS, platform.Architecture, platform.Variant)
}
var b strings.Builder
b.WriteString(platform.OS)
osv := encodeOSOption(platform.OSVersion)
formatted := formatOSFeatures(platform.OSFeatures)
if osv != "" || formatted != "" {
b.Grow(len(osv) + len(formatted) + 3) // parens + maybe '+'
b.WriteByte('(')
if osv != "" {
b.WriteString(osv)
}
if formatted != "" {
b.WriteByte('+')
b.WriteString(formatted)
}
b.WriteByte(')')
}
return path.Join(b.String(), platform.Architecture, platform.Variant)
}
func formatOSFeatures(features []string) string {
if len(features) == 0 {
return ""
}
osOptions := encodeOSOption(platform.OSVersion)
features := platform.OSFeatures
if !slices.IsSorted(features) {
features = slices.Clone(features)
slices.Sort(features)
}
var b strings.Builder
var wrote bool
var prev string
for _, f := range features {
osOptions += "+" + encodeOSOption(f)
if f == "" || f == prev {
// skip empty and duplicate values
continue
}
prev = f
if wrote {
b.WriteByte('+')
}
b.WriteString(encodeOSOption(f))
wrote = true
}
if osOptions != "" {
OSAndVersion := fmt.Sprintf("%s(%s)", platform.OS, osOptions)
return path.Join(OSAndVersion, platform.Architecture, platform.Variant)
}
return path.Join(platform.OS, platform.Architecture, platform.Variant)
return b.String()
}
// osOptionReplacer encodes characters in OS option values (version and

9
vendor/modules.txt vendored
View File

@@ -114,8 +114,8 @@ github.com/containerd/cgroups/v3/cgroup2/stats
# github.com/containerd/console v1.0.5
## explicit; go 1.13
github.com/containerd/console
# github.com/containerd/containerd/api v1.11.0-beta.0
## explicit; go 1.23.0
# github.com/containerd/containerd/api v1.11.0-beta.0 => ./api
## explicit; go 1.24.0
github.com/containerd/containerd/api/events
github.com/containerd/containerd/api/runtime/sandbox/v1
github.com/containerd/containerd/api/runtime/task/v2
@@ -203,8 +203,8 @@ github.com/containerd/nri/types/v1
## explicit; go 1.21
github.com/containerd/otelttrpc
github.com/containerd/otelttrpc/internal
# github.com/containerd/platforms v1.0.0-rc.3
## explicit; go 1.21
# github.com/containerd/platforms v1.0.0-rc.4
## explicit; go 1.24
github.com/containerd/platforms
# github.com/containerd/plugin v1.0.0
## explicit; go 1.20
@@ -990,3 +990,4 @@ tags.cncf.io/container-device-interface/pkg/parser
# tags.cncf.io/container-device-interface/specs-go v1.1.0
## explicit; go 1.19
tags.cncf.io/container-device-interface/specs-go
# github.com/containerd/containerd/api => ./api