From 3f620143fedbf9c30099b2fd7f4d0320fe033bb0 Mon Sep 17 00:00:00 2001 From: Derek McGowan Date: Fri, 19 Jan 2024 22:11:49 -0800 Subject: [PATCH 1/2] Use github.com/containerd/platforms package Signed-off-by: Derek McGowan --- go.mod | 1 + go.sum | 2 + platforms/compare.go | 158 +------ platforms/compare_test.go | 415 ------------------ platforms/cpuinfo_linux_test.go | 139 ------ platforms/defaults.go | 19 +- platforms/defaults_unix_test.go | 44 -- platforms/defaults_windows_test.go | 374 ---------------- platforms/platforms.go | 141 +----- platforms/platforms_test.go | 376 ---------------- .../containerd/platforms/.gitattributes | 1 + .../containerd/platforms/.golangci.yml | 30 ++ .../github.com/containerd/platforms/LICENSE | 191 ++++++++ .../github.com/containerd/platforms/README.md | 32 ++ .../containerd/platforms/compare.go | 203 +++++++++ .../containerd/platforms}/cpuinfo.go | 0 .../containerd/platforms}/cpuinfo_linux.go | 0 .../containerd/platforms}/cpuinfo_other.go | 0 .../containerd/platforms}/database.go | 0 .../containerd/platforms/defaults.go | 26 +- .../containerd/platforms}/defaults_darwin.go | 0 .../containerd/platforms}/defaults_freebsd.go | 0 .../containerd/platforms}/defaults_unix.go | 0 .../containerd/platforms}/defaults_windows.go | 0 .../containerd/platforms}/errors.go | 0 .../containerd/platforms/platforms.go | 290 ++++++++++++ .../containerd/platforms}/platforms_other.go | 0 .../platforms}/platforms_windows.go | 0 vendor/modules.txt | 3 + 29 files changed, 797 insertions(+), 1648 deletions(-) delete mode 100644 platforms/compare_test.go delete mode 100644 platforms/cpuinfo_linux_test.go delete mode 100644 platforms/defaults_unix_test.go delete mode 100644 platforms/defaults_windows_test.go delete mode 100644 platforms/platforms_test.go create mode 100644 vendor/github.com/containerd/platforms/.gitattributes create mode 100644 vendor/github.com/containerd/platforms/.golangci.yml create mode 100644 vendor/github.com/containerd/platforms/LICENSE create mode 100644 vendor/github.com/containerd/platforms/README.md create mode 100644 vendor/github.com/containerd/platforms/compare.go rename {platforms => vendor/github.com/containerd/platforms}/cpuinfo.go (100%) rename {platforms => vendor/github.com/containerd/platforms}/cpuinfo_linux.go (100%) rename {platforms => vendor/github.com/containerd/platforms}/cpuinfo_other.go (100%) rename {platforms => vendor/github.com/containerd/platforms}/database.go (100%) rename platforms/platforms_windows_test.go => vendor/github.com/containerd/platforms/defaults.go (55%) rename {platforms => vendor/github.com/containerd/platforms}/defaults_darwin.go (100%) rename {platforms => vendor/github.com/containerd/platforms}/defaults_freebsd.go (100%) rename {platforms => vendor/github.com/containerd/platforms}/defaults_unix.go (100%) rename {platforms => vendor/github.com/containerd/platforms}/defaults_windows.go (100%) rename {platforms => vendor/github.com/containerd/platforms}/errors.go (100%) create mode 100644 vendor/github.com/containerd/platforms/platforms.go rename {platforms => vendor/github.com/containerd/platforms}/platforms_other.go (100%) rename {platforms => vendor/github.com/containerd/platforms}/platforms_windows.go (100%) diff --git a/go.mod b/go.mod index 5606f735e137..8e97d1c30318 100644 --- a/go.mod +++ b/go.mod @@ -18,6 +18,7 @@ require ( github.com/containerd/go-runc v1.1.0 github.com/containerd/log v0.1.0 github.com/containerd/nri v0.5.0 + github.com/containerd/platforms v0.1.1 github.com/containerd/plugin v0.0.0-20231101173250-7ec69893e1e7 github.com/containerd/ttrpc v1.2.2 github.com/containerd/typeurl/v2 v2.1.1 diff --git a/go.sum b/go.sum index 42b4b47af9db..3a398986f2b1 100644 --- a/go.sum +++ b/go.sum @@ -60,6 +60,8 @@ github.com/containerd/log v0.1.0 h1:TCJt7ioM2cr/tfR8GPbGf9/VRAX8D2B4PjzCpfX540I= github.com/containerd/log v0.1.0/go.mod h1:VRRf09a7mHDIRezVKTRCrOq78v577GXq3bSa3EhrzVo= github.com/containerd/nri v0.5.0 h1:bwCtKpi8i5FCA8g8WjIZNod91CEfIloYpV0+TH2prnQ= github.com/containerd/nri v0.5.0/go.mod h1:qIu2NlP3r/qK4YGnNuQf0De4VPqQWP2i2CVBfAZbGzg= +github.com/containerd/platforms v0.1.1 h1:gp0xXBoY+1CjH54gJDon0kBjIbK2C4XSX1BGwP5ptG0= +github.com/containerd/platforms v0.1.1/go.mod h1:XOM2BS6kN6gXafPLg80V6y/QUib+xoLyC3qVmHzibko= github.com/containerd/plugin v0.0.0-20231101173250-7ec69893e1e7 h1:MUbtIMHEcMzj+8mPgHd5ett0WVbY/KYHa5tMvFs5Ejs= github.com/containerd/plugin v0.0.0-20231101173250-7ec69893e1e7/go.mod h1:j6HlpMtkiZMgT4UsfVNxPBUkwdw9KQGU6nCLfRxnq+w= github.com/containerd/ttrpc v1.2.2 h1:9vqZr0pxwOF5koz6N0N3kJ0zDHokrcPxIR/ZR2YFtOs= diff --git a/platforms/compare.go b/platforms/compare.go index 3913ef663731..e874201ca445 100644 --- a/platforms/compare.go +++ b/platforms/compare.go @@ -17,72 +17,11 @@ package platforms import ( - "strconv" - "strings" - + "github.com/containerd/platforms" specs "github.com/opencontainers/image-spec/specs-go/v1" ) -// MatchComparer is able to match and compare platforms to -// filter and sort platforms. -type MatchComparer interface { - Matcher - - Less(specs.Platform, specs.Platform) bool -} - -// platformVector returns an (ordered) vector of appropriate specs.Platform -// objects to try matching for the given platform object (see platforms.Only). -func platformVector(platform specs.Platform) []specs.Platform { - vector := []specs.Platform{platform} - - switch platform.Architecture { - case "amd64": - if amd64Version, err := strconv.Atoi(strings.TrimPrefix(platform.Variant, "v")); err == nil && amd64Version > 1 { - for amd64Version--; amd64Version >= 1; amd64Version-- { - vector = append(vector, specs.Platform{ - Architecture: platform.Architecture, - OS: platform.OS, - OSVersion: platform.OSVersion, - OSFeatures: platform.OSFeatures, - Variant: "v" + strconv.Itoa(amd64Version), - }) - } - } - vector = append(vector, specs.Platform{ - Architecture: "386", - OS: platform.OS, - OSVersion: platform.OSVersion, - OSFeatures: platform.OSFeatures, - }) - case "arm": - if armVersion, err := strconv.Atoi(strings.TrimPrefix(platform.Variant, "v")); err == nil && armVersion > 5 { - for armVersion--; armVersion >= 5; armVersion-- { - vector = append(vector, specs.Platform{ - Architecture: platform.Architecture, - OS: platform.OS, - OSVersion: platform.OSVersion, - OSFeatures: platform.OSFeatures, - Variant: "v" + strconv.Itoa(armVersion), - }) - } - } - case "arm64": - variant := platform.Variant - if variant == "" { - variant = "v8" - } - vector = append(vector, platformVector(specs.Platform{ - Architecture: "arm", - OS: platform.OS, - OSVersion: platform.OSVersion, - OSFeatures: platform.OSFeatures, - Variant: variant, - })...) - } - - return vector -} +type MatchComparer = platforms.MatchComparer // Only returns a match comparer for a single platform // using default resolution logic for the platform. @@ -92,7 +31,7 @@ func platformVector(platform specs.Platform) []specs.Platform { // For arm/v6, will also match arm/v5 // For amd64, will also match 386 func Only(platform specs.Platform) MatchComparer { - return Ordered(platformVector(Normalize(platform))...) + return platforms.Only(platform) } // OnlyStrict returns a match comparer for a single platform. @@ -104,100 +43,21 @@ func Only(platform specs.Platform) MatchComparer { // OnlyStrict matches non-canonical forms. // So, "arm64" matches "arm/64/v8". func OnlyStrict(platform specs.Platform) MatchComparer { - return Ordered(Normalize(platform)) + return platforms.OnlyStrict(platform) } // Ordered returns a platform MatchComparer which matches any of the platforms // but orders them in order they are provided. -func Ordered(platforms ...specs.Platform) MatchComparer { - matchers := make([]Matcher, len(platforms)) - for i := range platforms { - matchers[i] = NewMatcher(platforms[i]) - } - return orderedPlatformComparer{ - matchers: matchers, - } +func Ordered(ps ...specs.Platform) MatchComparer { + return platforms.Ordered(ps...) } // Any returns a platform MatchComparer which matches any of the platforms // with no preference for ordering. -func Any(platforms ...specs.Platform) MatchComparer { - matchers := make([]Matcher, len(platforms)) - for i := range platforms { - matchers[i] = NewMatcher(platforms[i]) - } - return anyPlatformComparer{ - matchers: matchers, - } +func Any(ps ...specs.Platform) MatchComparer { + return platforms.Any(ps...) } // All is a platform MatchComparer which matches all platforms // with preference for ordering. -var All MatchComparer = allPlatformComparer{} - -type orderedPlatformComparer struct { - matchers []Matcher -} - -func (c orderedPlatformComparer) Match(platform specs.Platform) bool { - for _, m := range c.matchers { - if m.Match(platform) { - return true - } - } - return false -} - -func (c orderedPlatformComparer) Less(p1 specs.Platform, p2 specs.Platform) bool { - for _, m := range c.matchers { - p1m := m.Match(p1) - p2m := m.Match(p2) - if p1m && !p2m { - return true - } - if p1m || p2m { - return false - } - } - return false -} - -type anyPlatformComparer struct { - matchers []Matcher -} - -func (c anyPlatformComparer) Match(platform specs.Platform) bool { - for _, m := range c.matchers { - if m.Match(platform) { - return true - } - } - return false -} - -func (c anyPlatformComparer) Less(p1, p2 specs.Platform) bool { - var p1m, p2m bool - for _, m := range c.matchers { - if !p1m && m.Match(p1) { - p1m = true - } - if !p2m && m.Match(p2) { - p2m = true - } - if p1m && p2m { - return false - } - } - // If one matches, and the other does, sort match first - return p1m && !p2m -} - -type allPlatformComparer struct{} - -func (allPlatformComparer) Match(specs.Platform) bool { - return true -} - -func (allPlatformComparer) Less(specs.Platform, specs.Platform) bool { - return false -} +var All = platforms.All diff --git a/platforms/compare_test.go b/platforms/compare_test.go deleted file mode 100644 index cbe90deead3e..000000000000 --- a/platforms/compare_test.go +++ /dev/null @@ -1,415 +0,0 @@ -/* - 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 platforms - -import ( - "testing" -) - -func TestOnly(t *testing.T) { - for _, tc := range []struct { - platform string - matches map[bool][]string - }{ - { - platform: "linux/amd64", - matches: map[bool][]string{ - true: { - "linux/amd64", - "linux/386", - }, - false: { - "linux/amd64/v2", - "linux/arm/v7", - "linux/arm64", - "windows/amd64", - "windows/arm", - }, - }, - }, - { - platform: "linux/amd64/v2", - matches: map[bool][]string{ - true: { - "linux/amd64", - "linux/amd64/v1", - "linux/amd64/v2", - "linux/386", - }, - false: { - "linux/amd64/v3", - "linux/amd64/v4", - "linux/arm/v7", - "linux/arm64", - "windows/amd64", - "windows/arm", - }, - }, - }, - { - platform: "linux/386", - matches: map[bool][]string{ - true: { - "linux/386", - }, - false: { - "linux/amd64", - "linux/arm/v7", - "linux/arm64", - "windows/amd64", - "windows/arm", - }, - }, - }, - { - platform: "windows/amd64", - matches: map[bool][]string{ - true: {"windows/amd64"}, - false: { - "linux/amd64", - "linux/arm/v7", - "linux/arm64", - "windows/arm", - }, - }, - }, - { - platform: "linux/arm/v8", - matches: map[bool][]string{ - true: { - "linux/arm", - "linux/arm/v5", - "linux/arm/v6", - "linux/arm/v7", - "linux/arm/v8", - }, - false: { - "linux/amd64", - "linux/arm/v4", - "linux/arm64", - "windows/amd64", - "windows/arm", - }, - }, - }, - { - platform: "linux/arm/v7", - matches: map[bool][]string{ - true: { - "linux/arm", - "linux/arm/v5", - "linux/arm/v6", - "linux/arm/v7", - }, - false: { - "linux/amd64", - "linux/arm/v4", - "linux/arm/v8", - "linux/arm64", - "windows/amd64", - "windows/arm", - }, - }, - }, - { - platform: "linux/arm/v6", - matches: map[bool][]string{ - true: { - "linux/arm/v5", - "linux/arm/v6", - }, - false: { - "linux/amd64", - "linux/arm", - "linux/arm/v4", - "linux/arm/v7", - "linux/arm/v8", - "linux/arm64", - "windows/amd64", - "windows/arm", - }, - }, - }, - { - platform: "linux/arm/v5", - matches: map[bool][]string{ - true: { - "linux/arm/v5", - }, - false: { - "linux/amd64", - "linux/arm", - "linux/arm/v4", - "linux/arm/v6", - "linux/arm/v7", - "linux/arm/v8", - "linux/arm64", - "windows/amd64", - "windows/arm", - }, - }, - }, - { - platform: "linux/arm/v4", - matches: map[bool][]string{ - true: { - "linux/arm/v4", - }, - false: { - "linux/amd64", - "linux/arm", - "linux/arm/v5", - "linux/arm/v6", - "linux/arm/v7", - "linux/arm/v8", - "linux/arm64", - "windows/amd64", - "windows/arm", - }, - }, - }, - { - platform: "linux/arm64", - matches: map[bool][]string{ - true: { - "linux/arm", - "linux/arm/v5", - "linux/arm/v6", - "linux/arm/v7", - "linux/arm/v8", - "linux/arm64", - "linux/arm64/v8", - }, - false: { - "linux/amd64", - "linux/arm/v4", - "linux/arm/v9", - "linux/arm64/v9", - "windows/amd64", - "windows/arm", - }, - }, - }, - } { - testcase := tc - t.Run(testcase.platform, func(t *testing.T) { - p, err := Parse(testcase.platform) - if err != nil { - t.Fatal(err) - } - m := Only(p) - for shouldMatch, platforms := range testcase.matches { - for _, matchPlatform := range platforms { - mp, err := Parse(matchPlatform) - if err != nil { - t.Fatal(err) - } - if match := m.Match(mp); shouldMatch != match { - t.Errorf("Only(%q).Match(%q) should return %v, but returns %v", testcase.platform, matchPlatform, shouldMatch, match) - } - } - } - }) - } -} - -func TestOnlyStrict(t *testing.T) { - for _, tc := range []struct { - platform string - matches map[bool][]string - }{ - { - platform: "linux/amd64", - matches: map[bool][]string{ - true: { - "linux/amd64", - }, - false: { - "linux/386", - "linux/arm/v7", - "linux/arm64", - "windows/amd64", - "windows/arm", - }, - }, - }, - { - platform: "linux/386", - matches: map[bool][]string{ - true: { - "linux/386", - }, - false: { - "linux/amd64", - "linux/arm/v7", - "linux/arm64", - "windows/amd64", - "windows/arm", - }, - }, - }, - { - platform: "windows/amd64", - matches: map[bool][]string{ - true: {"windows/amd64"}, - false: { - "linux/amd64", - "linux/arm/v7", - "linux/arm64", - "windows/arm", - }, - }, - }, - { - platform: "linux/arm/v8", - matches: map[bool][]string{ - true: { - "linux/arm/v8", - }, - false: { - "linux/arm", - "linux/arm/v5", - "linux/arm/v6", - "linux/arm/v7", - "linux/amd64", - "linux/arm/v4", - "linux/arm64", - "windows/amd64", - "windows/arm", - }, - }, - }, - { - platform: "linux/arm/v7", - matches: map[bool][]string{ - true: { - "linux/arm", - "linux/arm/v7", - }, - false: { - "linux/arm/v5", - "linux/arm/v6", - "linux/amd64", - "linux/arm/v4", - "linux/arm/v8", - "linux/arm64", - "windows/amd64", - "windows/arm", - }, - }, - }, - { - platform: "linux/arm/v6", - matches: map[bool][]string{ - true: { - "linux/arm/v6", - }, - false: { - "linux/arm/v5", - "linux/amd64", - "linux/arm", - "linux/arm/v4", - "linux/arm/v7", - "linux/arm/v8", - "linux/arm64", - "windows/amd64", - "windows/arm", - }, - }, - }, - { - platform: "linux/arm/v5", - matches: map[bool][]string{ - true: { - "linux/arm/v5", - }, - false: { - "linux/amd64", - "linux/arm", - "linux/arm/v4", - "linux/arm/v6", - "linux/arm/v7", - "linux/arm/v8", - "linux/arm64", - "windows/amd64", - "windows/arm", - }, - }, - }, - { - platform: "linux/arm/v4", - matches: map[bool][]string{ - true: { - "linux/arm/v4", - }, - false: { - "linux/amd64", - "linux/arm", - "linux/arm/v5", - "linux/arm/v6", - "linux/arm/v7", - "linux/arm/v8", - "linux/arm64", - "windows/amd64", - "windows/arm", - }, - }, - }, - { - platform: "linux/arm64", - matches: map[bool][]string{ - true: { - "linux/arm64", - "linux/arm64/v8", - }, - false: { - "linux/arm", - "linux/arm/v5", - "linux/arm/v6", - "linux/arm/v7", - "linux/arm/v8", - "linux/amd64", - "linux/arm/v4", - "linux/arm/v9", - "linux/arm64/v9", - "windows/amd64", - "windows/arm", - }, - }, - }, - } { - testcase := tc - t.Run(testcase.platform, func(t *testing.T) { - p, err := Parse(testcase.platform) - if err != nil { - t.Fatal(err) - } - m := OnlyStrict(p) - for shouldMatch, platforms := range testcase.matches { - for _, matchPlatform := range platforms { - mp, err := Parse(matchPlatform) - if err != nil { - t.Fatal(err) - } - if match := m.Match(mp); shouldMatch != match { - t.Errorf("OnlyStrict(%q).Match(%q) should return %v, but returns %v", testcase.platform, matchPlatform, shouldMatch, match) - } - } - } - }) - } -} diff --git a/platforms/cpuinfo_linux_test.go b/platforms/cpuinfo_linux_test.go deleted file mode 100644 index ecb11509d57c..000000000000 --- a/platforms/cpuinfo_linux_test.go +++ /dev/null @@ -1,139 +0,0 @@ -/* - 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 platforms - -import ( - "errors" - "runtime" - "testing" -) - -func TestCPUVariant(t *testing.T) { - if !isArmArch(runtime.GOARCH) { - t.Skip("only relevant on linux/arm") - } - - variants := []string{"v8", "v7", "v6", "v5", "v4", "v3"} - - p, err := getCPUVariant() - if err != nil { - t.Fatalf("Error getting CPU variant: %v", err) - return - } - - for _, variant := range variants { - if p == variant { - t.Logf("got valid variant as expected: %#v = %#v", p, variant) - return - } - } - - t.Fatalf("could not get valid variant as expected: %v", variants) -} - -func TestGetCPUVariantFromArch(t *testing.T) { - - for _, testcase := range []struct { - name string - input string - output string - expectedErr error - }{ - { - name: "Test aarch64", - input: "aarch64", - output: "8", - expectedErr: nil, - }, - { - name: "Test Armv8 with capital", - input: "Armv8", - output: "8", - expectedErr: nil, - }, - { - name: "Test armv7", - input: "armv7", - output: "7", - expectedErr: nil, - }, - { - name: "Test armv6", - input: "armv6", - output: "6", - expectedErr: nil, - }, - { - name: "Test armv5", - input: "armv5", - output: "5", - expectedErr: nil, - }, - { - name: "Test armv4", - input: "armv4", - output: "4", - expectedErr: nil, - }, - { - name: "Test armv3", - input: "armv3", - output: "3", - expectedErr: nil, - }, - { - name: "Test unknown input", - input: "armv9", - output: "unknown", - expectedErr: nil, - }, - { - name: "Test invalid input which doesn't start with armv", - input: "armxxxx", - output: "", - expectedErr: errInvalidArgument, - }, - { - name: "Test invalid input whose length is less than 5", - input: "armv", - output: "", - expectedErr: errInvalidArgument, - }, - } { - t.Run(testcase.name, func(t *testing.T) { - t.Logf("input: %v", testcase.input) - - variant, err := getCPUVariantFromArch(testcase.input) - - if err == nil { - if testcase.expectedErr != nil { - t.Fatalf("Expect to get error: %v, however no error got", testcase.expectedErr) - } else { - if variant != testcase.output { - t.Fatalf("Expect to get variant: %v, however %v returned", testcase.output, variant) - } - } - - } else { - if !errors.Is(err, testcase.expectedErr) { - t.Fatalf("Expect to get error: %v, however error %v returned", testcase.expectedErr, err) - } - } - }) - - } -} diff --git a/platforms/defaults.go b/platforms/defaults.go index cfa3ff34a19b..ab2a89990ddd 100644 --- a/platforms/defaults.go +++ b/platforms/defaults.go @@ -16,12 +16,27 @@ package platforms +import ( + "github.com/containerd/platforms" + specs "github.com/opencontainers/image-spec/specs-go/v1" +) + // DefaultString returns the default string specifier for the platform. func DefaultString() string { - return Format(DefaultSpec()) + return platforms.DefaultString() } // DefaultStrict returns strict form of Default. func DefaultStrict() MatchComparer { - return OnlyStrict(DefaultSpec()) + return platforms.DefaultStrict() +} + +// DefaultSpec returns the current platform's default platform specification. +func DefaultSpec() specs.Platform { + return platforms.DefaultSpec() +} + +// Default returns the default matcher for the platform. +func Default() MatchComparer { + return platforms.Default() } diff --git a/platforms/defaults_unix_test.go b/platforms/defaults_unix_test.go deleted file mode 100644 index 8aba958772f0..000000000000 --- a/platforms/defaults_unix_test.go +++ /dev/null @@ -1,44 +0,0 @@ -//go:build !windows - -/* - 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 platforms - -import ( - "reflect" - "runtime" - "testing" - - specs "github.com/opencontainers/image-spec/specs-go/v1" -) - -func TestDefault(t *testing.T) { - expected := specs.Platform{ - OS: runtime.GOOS, - Architecture: runtime.GOARCH, - Variant: cpuVariant(), - } - p := DefaultSpec() - if !reflect.DeepEqual(p, expected) { - t.Fatalf("default platform not as expected: %#v != %#v", p, expected) - } - - s := DefaultString() - if s != Format(p) { - t.Fatalf("default specifier should match formatted default spec: %v != %v", s, p) - } -} diff --git a/platforms/defaults_windows_test.go b/platforms/defaults_windows_test.go deleted file mode 100644 index ab73ddda078f..000000000000 --- a/platforms/defaults_windows_test.go +++ /dev/null @@ -1,374 +0,0 @@ -/* - 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 platforms - -import ( - "fmt" - "reflect" - "runtime" - "sort" - "testing" - - imagespec "github.com/opencontainers/image-spec/specs-go/v1" - "github.com/stretchr/testify/assert" - "golang.org/x/sys/windows" -) - -func TestDefault(t *testing.T) { - major, minor, build := windows.RtlGetNtVersionNumbers() - expected := imagespec.Platform{ - OS: runtime.GOOS, - Architecture: runtime.GOARCH, - OSVersion: fmt.Sprintf("%d.%d.%d", major, minor, build), - Variant: cpuVariant(), - } - p := DefaultSpec() - if !reflect.DeepEqual(p, expected) { - t.Fatalf("default platform not as expected: %#v != %#v", p, expected) - } - - s := DefaultString() - if s != Format(p) { - t.Fatalf("default specifier should match formatted default spec: %v != %v", s, p) - } -} - -func TestDefaultMatchComparer(t *testing.T) { - defaultMatcher := Default() - - for _, test := range []struct { - platform imagespec.Platform - match bool - }{ - { - platform: DefaultSpec(), - match: true, - }, - { - platform: imagespec.Platform{ - OS: "linux", - Architecture: runtime.GOARCH, - }, - match: false, - }, - } { - assert.Equal(t, test.match, defaultMatcher.Match(test.platform)) - } - -} - -func TestMatchComparerMatch_WCOW(t *testing.T) { - major, minor, build := windows.RtlGetNtVersionNumbers() - buildStr := fmt.Sprintf("%d.%d.%d", major, minor, build) - m := windowsmatcher{ - Platform: DefaultSpec(), - osVersionPrefix: buildStr, - defaultMatcher: &matcher{ - Platform: Normalize(DefaultSpec()), - }, - } - for _, test := range []struct { - platform imagespec.Platform - match bool - }{ - { - platform: DefaultSpec(), - match: true, - }, - { - platform: imagespec.Platform{ - Architecture: "amd64", - OS: "windows", - OSVersion: buildStr + ".1", - }, - match: true, - }, - { - platform: imagespec.Platform{ - Architecture: "amd64", - OS: "windows", - OSVersion: buildStr + ".2", - }, - match: true, - }, - { - platform: imagespec.Platform{ - Architecture: "amd64", - OS: "windows", - // Use an nonexistent Windows build so we don't get a match. Ws2019's build is 17763/ - OSVersion: "10.0.17762.1", - }, - match: false, - }, - { - platform: imagespec.Platform{ - Architecture: "amd64", - OS: "windows", - // Use an nonexistent Windows build so we don't get a match. Ws2019's build is 17763/ - OSVersion: "10.0.17764.1", - }, - match: false, - }, - { - platform: imagespec.Platform{ - Architecture: "amd64", - OS: "windows", - }, - match: true, - }, - { - platform: imagespec.Platform{ - Architecture: "amd64", - OS: "linux", - }, - match: false, - }, - } { - assert.Equal(t, test.match, m.Match(test.platform), "should match: %t, %s to %s", test.match, m.Platform, test.platform) - } -} - -// TestMatchComparerMatch_ABICheckWCOW checks windows platform matcher -// behavior for stable ABI and non-stable ABI compliant versions -func TestMatchComparerMatch_ABICheckWCOW(t *testing.T) { - platformWS2019 := imagespec.Platform{ - Architecture: "amd64", - OS: "windows", - OSVersion: "10.0.17763", - } - platformWS2022 := imagespec.Platform{ - Architecture: "amd64", - OS: "windows", - OSVersion: "10.0.20348", - } - platformWindows11 := imagespec.Platform{ - Architecture: "amd64", - OS: "windows", - OSVersion: "10.0.22621", - } - matcherWS2019 := windowsmatcher{ - Platform: platformWS2019, - osVersionPrefix: platformWS2019.OSVersion, - defaultMatcher: &matcher{ - Platform: Normalize(platformWS2019), - }, - } - matcherWS2022 := windowsmatcher{ - Platform: platformWS2022, - osVersionPrefix: platformWS2022.OSVersion, - defaultMatcher: &matcher{ - Platform: Normalize(platformWS2022), - }, - } - matcherWindows11 := windowsmatcher{ - Platform: platformWindows11, - osVersionPrefix: platformWindows11.OSVersion, - defaultMatcher: &matcher{ - Platform: Normalize(platformWindows11), - }, - } - - for _, test := range []struct { - hostPlatformMatcher windowsmatcher - testPlatform imagespec.Platform - match bool - }{ - { - hostPlatformMatcher: matcherWS2019, - testPlatform: imagespec.Platform{ - Architecture: "amd64", - OS: "windows", - OSVersion: "10.0.17763", - }, - match: true, - }, - { - hostPlatformMatcher: matcherWS2019, - testPlatform: imagespec.Platform{ - Architecture: "amd64", - OS: "windows", - OSVersion: "10.0.20348", - }, - match: false, - }, - { - hostPlatformMatcher: matcherWS2022, - testPlatform: imagespec.Platform{ - Architecture: "amd64", - OS: "windows", - OSVersion: "10.0.17763", - }, - match: false, - }, - { - hostPlatformMatcher: matcherWS2022, - testPlatform: imagespec.Platform{ - Architecture: "amd64", - OS: "windows", - OSVersion: "10.0.20348", - }, - match: true, - }, - { - hostPlatformMatcher: matcherWindows11, - testPlatform: imagespec.Platform{ - Architecture: "amd64", - OS: "windows", - OSVersion: "10.0.17763", - }, - match: false, - }, - { - hostPlatformMatcher: matcherWindows11, - testPlatform: imagespec.Platform{ - Architecture: "amd64", - OS: "windows", - OSVersion: "10.0.20348", - }, - match: true, - }, - } { - assert.Equal(t, test.match, test.hostPlatformMatcher.Match(test.testPlatform), "should match: %t, %s to %s", test.match, test.hostPlatformMatcher.Platform, test.testPlatform) - } -} - -func TestMatchComparerMatch_LCOW(t *testing.T) { - major, minor, build := windows.RtlGetNtVersionNumbers() - buildStr := fmt.Sprintf("%d.%d.%d", major, minor, build) - m := windowsmatcher{ - Platform: imagespec.Platform{ - OS: "linux", - Architecture: "amd64", - }, - osVersionPrefix: "", - defaultMatcher: &matcher{ - Platform: Normalize(imagespec.Platform{ - OS: "linux", - Architecture: "amd64", - }, - ), - }, - } - for _, test := range []struct { - platform imagespec.Platform - match bool - }{ - { - platform: DefaultSpec(), - match: false, - }, - { - platform: imagespec.Platform{ - Architecture: "amd64", - OS: "windows", - }, - match: false, - }, - { - platform: imagespec.Platform{ - Architecture: "amd64", - OS: "windows", - OSVersion: buildStr + ".2", - }, - match: false, - }, - { - platform: imagespec.Platform{ - Architecture: "amd64", - OS: "windows", - // Use an nonexistent Windows build so we don't get a match. Ws2019's build is 17763/ - OSVersion: "10.0.17762.1", - }, - match: false, - }, - { - platform: imagespec.Platform{ - Architecture: "amd64", - OS: "linux", - }, - match: true, - }, - } { - assert.Equal(t, test.match, m.Match(test.platform), "should match %b, %s to %s", test.match, m.Platform, test.platform) - } -} - -func TestMatchComparerLess(t *testing.T) { - m := windowsmatcher{ - Platform: DefaultSpec(), - osVersionPrefix: "10.0.17763", - defaultMatcher: &matcher{ - Platform: Normalize(DefaultSpec()), - }, - } - platforms := []imagespec.Platform{ - { - Architecture: "amd64", - OS: "windows", - OSVersion: "10.0.17764.1", - }, - { - Architecture: "amd64", - OS: "windows", - }, - { - Architecture: "amd64", - OS: "windows", - OSVersion: "10.0.17763.1", - }, - { - Architecture: "amd64", - OS: "windows", - OSVersion: "10.0.17763.2", - }, - { - Architecture: "amd64", - OS: "windows", - OSVersion: "10.0.17762.1", - }, - } - expected := []imagespec.Platform{ - { - Architecture: "amd64", - OS: "windows", - OSVersion: "10.0.17763.2", - }, - { - Architecture: "amd64", - OS: "windows", - OSVersion: "10.0.17763.1", - }, - { - Architecture: "amd64", - OS: "windows", - }, - { - Architecture: "amd64", - OS: "windows", - OSVersion: "10.0.17764.1", - }, - { - Architecture: "amd64", - OS: "windows", - OSVersion: "10.0.17762.1", - }, - } - sort.SliceStable(platforms, func(i, j int) bool { - return m.Less(platforms[i], platforms[j]) - }) - assert.Equal(t, expected, platforms) -} diff --git a/platforms/platforms.go b/platforms/platforms.go index 43e4ad3d83a1..55070a7784f1 100644 --- a/platforms/platforms.go +++ b/platforms/platforms.go @@ -110,27 +110,15 @@ package platforms import ( - "fmt" - "path" - "regexp" - "runtime" - "strconv" - "strings" - + "github.com/containerd/platforms" specs "github.com/opencontainers/image-spec/specs-go/v1" ) -var ( - specifierRe = regexp.MustCompile(`^[A-Za-z0-9_-]+$`) -) - // Platform is a type alias for convenience, so there is no need to import image-spec package everywhere. -type Platform = specs.Platform +type Platform = platforms.Platform // Matcher matches platforms specifications, provided by an image or runtime. -type Matcher interface { - Match(platform specs.Platform) bool -} +type Matcher = platforms.Matcher // NewMatcher returns a simple matcher based on the provided platform // specification. The returned matcher only looks for equality based on os, @@ -141,35 +129,12 @@ type Matcher interface { // // Applications should opt to use `Match` over directly parsing specifiers. func NewMatcher(platform specs.Platform) Matcher { - return newDefaultMatcher(platform) -} - -type matcher struct { - specs.Platform -} - -func (m *matcher) Match(platform specs.Platform) bool { - normalized := Normalize(platform) - return m.OS == normalized.OS && - m.Architecture == normalized.Architecture && - m.Variant == normalized.Variant -} - -func (m *matcher) String() string { - return Format(m.Platform) + return platforms.NewMatcher(platform) } // ParseAll parses a list of platform specifiers into a list of platform. func ParseAll(specifiers []string) ([]specs.Platform, error) { - platforms := make([]specs.Platform, len(specifiers)) - for i, s := range specifiers { - p, err := Parse(s) - if err != nil { - return nil, fmt.Errorf("invalid platform %s: %w", s, err) - } - platforms[i] = p - } - return platforms, nil + return platforms.ParseAll(specifiers) } // Parse parses the platform specifier syntax into a platform declaration. @@ -181,101 +146,18 @@ func ParseAll(specifiers []string) ([]specs.Platform, error) { // back to the known set of architectures. The missing component will be // inferred based on the local environment. func Parse(specifier string) (specs.Platform, error) { - if strings.Contains(specifier, "*") { - // TODO(stevvooe): need to work out exact wildcard handling - return specs.Platform{}, fmt.Errorf("%q: wildcards not yet supported: %w", specifier, errInvalidArgument) - } - - parts := strings.Split(specifier, "/") - - for _, part := range parts { - if !specifierRe.MatchString(part) { - return specs.Platform{}, fmt.Errorf("%q is an invalid component of %q: platform specifier component must match %q: %w", part, specifier, specifierRe.String(), errInvalidArgument) - } - } - - var p specs.Platform - switch len(parts) { - case 1: - // in this case, we will test that the value might be an OS, then look - // it up. If it is not known, we'll treat it as an architecture. Since - // we have very little information about the platform here, we are - // going to be a little more strict if we don't know about the argument - // value. - p.OS = normalizeOS(parts[0]) - if isKnownOS(p.OS) { - // picks a default architecture - p.Architecture = runtime.GOARCH - if p.Architecture == "arm" && cpuVariant() != "v7" { - p.Variant = cpuVariant() - } - - if p.OS == "windows" { - p.OSVersion = GetWindowsOsVersion() - } - - return p, nil - } - - p.Architecture, p.Variant = normalizeArch(parts[0], "") - if p.Architecture == "arm" && p.Variant == "v7" { - p.Variant = "" - } - if isKnownArch(p.Architecture) { - p.OS = runtime.GOOS - return p, nil - } - - return specs.Platform{}, fmt.Errorf("%q: unknown operating system or architecture: %w", specifier, errInvalidArgument) - case 2: - // In this case, we treat as a regular os/arch pair. We don't care - // about whether or not we know of the platform. - p.OS = normalizeOS(parts[0]) - p.Architecture, p.Variant = normalizeArch(parts[1], "") - if p.Architecture == "arm" && p.Variant == "v7" { - p.Variant = "" - } - - if p.OS == "windows" { - p.OSVersion = GetWindowsOsVersion() - } - - return p, nil - case 3: - // we have a fully specified variant, this is rare - p.OS = normalizeOS(parts[0]) - p.Architecture, p.Variant = normalizeArch(parts[1], parts[2]) - if p.Architecture == "arm64" && p.Variant == "" { - p.Variant = "v8" - } - - if p.OS == "windows" { - p.OSVersion = GetWindowsOsVersion() - } - - return p, nil - } - - return specs.Platform{}, fmt.Errorf("%q: cannot parse platform specifier: %w", specifier, errInvalidArgument) + return platforms.Parse(specifier) } // MustParse is like Parses but panics if the specifier cannot be parsed. // Simplifies initialization of global variables. func MustParse(specifier string) specs.Platform { - p, err := Parse(specifier) - if err != nil { - panic("platform: Parse(" + strconv.Quote(specifier) + "): " + err.Error()) - } - return p + return platforms.MustParse(specifier) } // Format returns a string specifier from the provided platform specification. func Format(platform specs.Platform) string { - if platform.OS == "" { - return "unknown" - } - - return path.Join(platform.OS, platform.Architecture, platform.Variant) + return platforms.Format(platform) } // Normalize validates and translate the platform to the canonical value. @@ -283,8 +165,9 @@ func Format(platform specs.Platform) string { // For example, if "Aarch64" is encountered, we change it to "arm64" or if // "x86_64" is encountered, it becomes "amd64". func Normalize(platform specs.Platform) specs.Platform { - platform.OS = normalizeOS(platform.OS) - platform.Architecture, platform.Variant = normalizeArch(platform.Architecture, platform.Variant) + return platforms.Normalize(platform) +} - return platform +func GetWindowsOsVersion() string { + return platforms.GetWindowsOsVersion() } diff --git a/platforms/platforms_test.go b/platforms/platforms_test.go deleted file mode 100644 index c2af02178beb..000000000000 --- a/platforms/platforms_test.go +++ /dev/null @@ -1,376 +0,0 @@ -/* - 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 platforms - -import ( - "path" - "reflect" - "runtime" - "testing" - - specs "github.com/opencontainers/image-spec/specs-go/v1" -) - -func TestParseSelector(t *testing.T) { - var ( - defaultOS = runtime.GOOS - defaultArch = runtime.GOARCH - defaultVariant = "" - ) - - if defaultArch == "arm" && cpuVariant() != "v7" { - defaultVariant = cpuVariant() - } - - for _, testcase := range []struct { - skip bool - input string - expected specs.Platform - matches []specs.Platform - formatted string - }{ - // While wildcards are a valid use case for platform selection, - // addressing these cases is outside the initial scope for this - // package. When we do add platform wildcards, we should add in these - // testcases to ensure that they are correctly represented. - { - skip: true, - input: "*", - expected: specs.Platform{ - OS: "*", - Architecture: "*", - }, - formatted: "*/*", - }, - { - skip: true, - input: "linux/*", - expected: specs.Platform{ - OS: "linux", - Architecture: "*", - }, - formatted: "linux/*", - }, - { - skip: true, - input: "*/arm64", - expected: specs.Platform{ - OS: "*", - Architecture: "arm64", - }, - matches: []specs.Platform{ - { - OS: "*", - Architecture: "aarch64", - }, - { - OS: "*", - Architecture: "aarch64", - Variant: "v8", - }, - { - OS: "*", - Architecture: "arm64", - Variant: "v8", - }, - }, - formatted: "*/arm64", - }, - { - input: "linux/arm64", - expected: specs.Platform{ - OS: "linux", - Architecture: "arm64", - }, - matches: []specs.Platform{ - { - OS: "linux", - Architecture: "aarch64", - }, - { - OS: "linux", - Architecture: "aarch64", - Variant: "v8", - }, - { - OS: "linux", - Architecture: "arm64", - Variant: "v8", - }, - }, - formatted: "linux/arm64", - }, - { - input: "linux/arm64/v8", - expected: specs.Platform{ - OS: "linux", - Architecture: "arm64", - Variant: "v8", - }, - matches: []specs.Platform{ - { - OS: "linux", - Architecture: "aarch64", - }, - { - OS: "linux", - Architecture: "aarch64", - Variant: "v8", - }, - { - OS: "linux", - Architecture: "arm64", - }, - }, - formatted: "linux/arm64/v8", - }, - { - // NOTE(stevvooe): In this case, the consumer can assume this is v7 - // but we leave the variant blank. This will represent the vast - // majority of arm images. - input: "linux/arm", - expected: specs.Platform{ - OS: "linux", - Architecture: "arm", - }, - matches: []specs.Platform{ - { - OS: "linux", - Architecture: "arm", - Variant: "v7", - }, - { - OS: "linux", - Architecture: "armhf", - }, - { - OS: "linux", - Architecture: "arm", - Variant: "7", - }, - }, - formatted: "linux/arm", - }, - { - input: "linux/arm/v6", - expected: specs.Platform{ - OS: "linux", - Architecture: "arm", - Variant: "v6", - }, - matches: []specs.Platform{ - { - OS: "linux", - Architecture: "armel", - }, - }, - formatted: "linux/arm/v6", - }, - { - input: "linux/arm/v7", - expected: specs.Platform{ - OS: "linux", - Architecture: "arm", - Variant: "v7", - }, - matches: []specs.Platform{ - { - OS: "linux", - Architecture: "arm", - }, - { - OS: "linux", - Architecture: "armhf", - }, - }, - formatted: "linux/arm/v7", - }, - { - input: "arm", - expected: specs.Platform{ - OS: defaultOS, - Architecture: "arm", - }, - formatted: path.Join(defaultOS, "arm"), - }, - { - input: "armel", - expected: specs.Platform{ - OS: defaultOS, - Architecture: "arm", - Variant: "v6", - }, - formatted: path.Join(defaultOS, "arm/v6"), - }, - { - input: "armhf", - expected: specs.Platform{ - OS: defaultOS, - Architecture: "arm", - }, - formatted: path.Join(defaultOS, "arm"), - }, - { - input: "Aarch64", - expected: specs.Platform{ - OS: defaultOS, - Architecture: "arm64", - }, - formatted: path.Join(defaultOS, "arm64"), - }, - { - input: "x86_64", - expected: specs.Platform{ - OS: defaultOS, - Architecture: "amd64", - }, - formatted: path.Join(defaultOS, "amd64"), - }, - { - input: "Linux/x86_64", - expected: specs.Platform{ - OS: "linux", - Architecture: "amd64", - }, - formatted: "linux/amd64", - }, - { - input: "i386", - expected: specs.Platform{ - OS: defaultOS, - Architecture: "386", - }, - formatted: path.Join(defaultOS, "386"), - }, - { - input: "linux", - expected: specs.Platform{ - OS: "linux", - Architecture: defaultArch, - Variant: defaultVariant, - }, - formatted: path.Join("linux", defaultArch, defaultVariant), - }, - { - input: "s390x", - expected: specs.Platform{ - OS: defaultOS, - Architecture: "s390x", - }, - formatted: path.Join(defaultOS, "s390x"), - }, - { - input: "linux/s390x", - expected: specs.Platform{ - OS: "linux", - Architecture: "s390x", - }, - formatted: "linux/s390x", - }, - { - input: "macOS", - expected: specs.Platform{ - OS: "darwin", - Architecture: defaultArch, - Variant: defaultVariant, - }, - formatted: path.Join("darwin", defaultArch, defaultVariant), - }, - } { - t.Run(testcase.input, func(t *testing.T) { - if testcase.skip { - t.Skip("this case is not yet supported") - } - p, err := Parse(testcase.input) - if err != nil { - t.Fatal(err) - } - - if !reflect.DeepEqual(p, testcase.expected) { - t.Fatalf("platform did not match expected: %#v != %#v", p, testcase.expected) - } - - m := NewMatcher(p) - - // ensure that match works on the input to the output. - if ok := m.Match(testcase.expected); !ok { - t.Fatalf("expected specifier %q matches %#v", testcase.input, testcase.expected) - } - for _, mc := range testcase.matches { - if ok := m.Match(mc); !ok { - t.Fatalf("expected specifier %q matches %#v", testcase.input, mc) - } - } - - formatted := Format(p) - if formatted != testcase.formatted { - t.Fatalf("unexpected format: %q != %q", formatted, testcase.formatted) - } - - // re-parse the formatted output and ensure we are stable - reparsed, err := Parse(formatted) - if err != nil { - t.Fatalf("error parsing formatted output: %v", err) - } - - if Format(reparsed) != formatted { - t.Fatalf("normalized output did not survive the round trip: %v != %v", Format(reparsed), formatted) - } - }) - } -} - -func TestParseSelectorInvalid(t *testing.T) { - for _, testcase := range []struct { - input string - }{ - { - input: "", // empty - }, - { - input: "/linux/arm", // leading slash - }, - { - input: "linux/arm/", // trailing slash - }, - { - input: "linux /arm", // spaces - }, - { - input: "linux/&arm", // invalid character - }, - { - input: "linux/arm/foo/bar", // too many components - }, - } { - t.Run(testcase.input, func(t *testing.T) { - if _, err := Parse(testcase.input); err == nil { - t.Fatalf("should have received an error") - } - }) - } -} - -func FuzzPlatformsParse(f *testing.F) { - f.Add("linux/amd64") - f.Fuzz(func(t *testing.T, s string) { - pf, err := Parse(s) - if err != nil && (pf.OS != "" || pf.Architecture != "") { - t.Errorf("either %+v or %+v must be nil", err, pf) - } - }) -} diff --git a/vendor/github.com/containerd/platforms/.gitattributes b/vendor/github.com/containerd/platforms/.gitattributes new file mode 100644 index 000000000000..a0717e4b3b90 --- /dev/null +++ b/vendor/github.com/containerd/platforms/.gitattributes @@ -0,0 +1 @@ +*.go text eol=lf \ No newline at end of file diff --git a/vendor/github.com/containerd/platforms/.golangci.yml b/vendor/github.com/containerd/platforms/.golangci.yml new file mode 100644 index 000000000000..a695775df49d --- /dev/null +++ b/vendor/github.com/containerd/platforms/.golangci.yml @@ -0,0 +1,30 @@ +linters: + enable: + - exportloopref # Checks for pointers to enclosing loop variables + - gofmt + - goimports + - gosec + - ineffassign + - misspell + - nolintlint + - revive + - staticcheck + - tenv # Detects using os.Setenv instead of t.Setenv since Go 1.17 + - unconvert + - unused + - vet + - dupword # Checks for duplicate words in the source code + disable: + - errcheck + +run: + timeout: 5m + skip-dirs: + - api + - cluster + - design + - docs + - docs/man + - releases + - reports + - test # e2e scripts diff --git a/vendor/github.com/containerd/platforms/LICENSE b/vendor/github.com/containerd/platforms/LICENSE new file mode 100644 index 000000000000..584149b6ee28 --- /dev/null +++ b/vendor/github.com/containerd/platforms/LICENSE @@ -0,0 +1,191 @@ + + 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. diff --git a/vendor/github.com/containerd/platforms/README.md b/vendor/github.com/containerd/platforms/README.md new file mode 100644 index 000000000000..2059de771c56 --- /dev/null +++ b/vendor/github.com/containerd/platforms/README.md @@ -0,0 +1,32 @@ +# platforms + +A Go package for formatting, normalizing and matching container platforms. + +This package is based on the Open Containers Image Spec definition of a [platform](https://github.com/opencontainers/image-spec/blob/main/specs-go/v1/descriptor.go#L52). + +## Platform Specifier + +While the OCI platform specifications provide a tool for components to +specify structured information, user input typically doesn't need the full +context and much can be inferred. To solve this problem, this package introduces +"specifiers". A specifier has the format +`||/[/]`. The user can provide either the +operating system or the architecture or both. + +An example of a common specifier is `linux/amd64`. If the host has a default +runtime that matches this, the user can simply provide the component that +matters. For example, if an image provides `amd64` and `arm64` support, the +operating system, `linux` can be inferred, so they only have to provide +`arm64` or `amd64`. Similar behavior is implemented for operating systems, +where the architecture may be known but a runtime may support images from +different operating systems. + +## Project details + +**platforms** is a containerd sub-project, licensed under the [Apache 2.0 license](./LICENSE). +As a containerd sub-project, you will find the: + * [Project governance](https://github.com/containerd/project/blob/main/GOVERNANCE.md), + * [Maintainers](https://github.com/containerd/project/blob/main/MAINTAINERS), + * and [Contributing guidelines](https://github.com/containerd/project/blob/main/CONTRIBUTING.md) + +information in our [`containerd/project`](https://github.com/containerd/project) repository. \ No newline at end of file diff --git a/vendor/github.com/containerd/platforms/compare.go b/vendor/github.com/containerd/platforms/compare.go new file mode 100644 index 000000000000..3913ef663731 --- /dev/null +++ b/vendor/github.com/containerd/platforms/compare.go @@ -0,0 +1,203 @@ +/* + 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 platforms + +import ( + "strconv" + "strings" + + specs "github.com/opencontainers/image-spec/specs-go/v1" +) + +// MatchComparer is able to match and compare platforms to +// filter and sort platforms. +type MatchComparer interface { + Matcher + + Less(specs.Platform, specs.Platform) bool +} + +// platformVector returns an (ordered) vector of appropriate specs.Platform +// objects to try matching for the given platform object (see platforms.Only). +func platformVector(platform specs.Platform) []specs.Platform { + vector := []specs.Platform{platform} + + switch platform.Architecture { + case "amd64": + if amd64Version, err := strconv.Atoi(strings.TrimPrefix(platform.Variant, "v")); err == nil && amd64Version > 1 { + for amd64Version--; amd64Version >= 1; amd64Version-- { + vector = append(vector, specs.Platform{ + Architecture: platform.Architecture, + OS: platform.OS, + OSVersion: platform.OSVersion, + OSFeatures: platform.OSFeatures, + Variant: "v" + strconv.Itoa(amd64Version), + }) + } + } + vector = append(vector, specs.Platform{ + Architecture: "386", + OS: platform.OS, + OSVersion: platform.OSVersion, + OSFeatures: platform.OSFeatures, + }) + case "arm": + if armVersion, err := strconv.Atoi(strings.TrimPrefix(platform.Variant, "v")); err == nil && armVersion > 5 { + for armVersion--; armVersion >= 5; armVersion-- { + vector = append(vector, specs.Platform{ + Architecture: platform.Architecture, + OS: platform.OS, + OSVersion: platform.OSVersion, + OSFeatures: platform.OSFeatures, + Variant: "v" + strconv.Itoa(armVersion), + }) + } + } + case "arm64": + variant := platform.Variant + if variant == "" { + variant = "v8" + } + vector = append(vector, platformVector(specs.Platform{ + Architecture: "arm", + OS: platform.OS, + OSVersion: platform.OSVersion, + OSFeatures: platform.OSFeatures, + Variant: variant, + })...) + } + + return vector +} + +// Only returns a match comparer for a single platform +// using default resolution logic for the platform. +// +// For arm/v8, will also match arm/v7, arm/v6 and arm/v5 +// For arm/v7, will also match arm/v6 and arm/v5 +// For arm/v6, will also match arm/v5 +// For amd64, will also match 386 +func Only(platform specs.Platform) MatchComparer { + return Ordered(platformVector(Normalize(platform))...) +} + +// OnlyStrict returns a match comparer for a single platform. +// +// Unlike Only, OnlyStrict does not match sub platforms. +// So, "arm/vN" will not match "arm/vM" where M < N, +// and "amd64" will not also match "386". +// +// OnlyStrict matches non-canonical forms. +// So, "arm64" matches "arm/64/v8". +func OnlyStrict(platform specs.Platform) MatchComparer { + return Ordered(Normalize(platform)) +} + +// Ordered returns a platform MatchComparer which matches any of the platforms +// but orders them in order they are provided. +func Ordered(platforms ...specs.Platform) MatchComparer { + matchers := make([]Matcher, len(platforms)) + for i := range platforms { + matchers[i] = NewMatcher(platforms[i]) + } + return orderedPlatformComparer{ + matchers: matchers, + } +} + +// Any returns a platform MatchComparer which matches any of the platforms +// with no preference for ordering. +func Any(platforms ...specs.Platform) MatchComparer { + matchers := make([]Matcher, len(platforms)) + for i := range platforms { + matchers[i] = NewMatcher(platforms[i]) + } + return anyPlatformComparer{ + matchers: matchers, + } +} + +// All is a platform MatchComparer which matches all platforms +// with preference for ordering. +var All MatchComparer = allPlatformComparer{} + +type orderedPlatformComparer struct { + matchers []Matcher +} + +func (c orderedPlatformComparer) Match(platform specs.Platform) bool { + for _, m := range c.matchers { + if m.Match(platform) { + return true + } + } + return false +} + +func (c orderedPlatformComparer) Less(p1 specs.Platform, p2 specs.Platform) bool { + for _, m := range c.matchers { + p1m := m.Match(p1) + p2m := m.Match(p2) + if p1m && !p2m { + return true + } + if p1m || p2m { + return false + } + } + return false +} + +type anyPlatformComparer struct { + matchers []Matcher +} + +func (c anyPlatformComparer) Match(platform specs.Platform) bool { + for _, m := range c.matchers { + if m.Match(platform) { + return true + } + } + return false +} + +func (c anyPlatformComparer) Less(p1, p2 specs.Platform) bool { + var p1m, p2m bool + for _, m := range c.matchers { + if !p1m && m.Match(p1) { + p1m = true + } + if !p2m && m.Match(p2) { + p2m = true + } + if p1m && p2m { + return false + } + } + // If one matches, and the other does, sort match first + return p1m && !p2m +} + +type allPlatformComparer struct{} + +func (allPlatformComparer) Match(specs.Platform) bool { + return true +} + +func (allPlatformComparer) Less(specs.Platform, specs.Platform) bool { + return false +} diff --git a/platforms/cpuinfo.go b/vendor/github.com/containerd/platforms/cpuinfo.go similarity index 100% rename from platforms/cpuinfo.go rename to vendor/github.com/containerd/platforms/cpuinfo.go diff --git a/platforms/cpuinfo_linux.go b/vendor/github.com/containerd/platforms/cpuinfo_linux.go similarity index 100% rename from platforms/cpuinfo_linux.go rename to vendor/github.com/containerd/platforms/cpuinfo_linux.go diff --git a/platforms/cpuinfo_other.go b/vendor/github.com/containerd/platforms/cpuinfo_other.go similarity index 100% rename from platforms/cpuinfo_other.go rename to vendor/github.com/containerd/platforms/cpuinfo_other.go diff --git a/platforms/database.go b/vendor/github.com/containerd/platforms/database.go similarity index 100% rename from platforms/database.go rename to vendor/github.com/containerd/platforms/database.go diff --git a/platforms/platforms_windows_test.go b/vendor/github.com/containerd/platforms/defaults.go similarity index 55% rename from platforms/platforms_windows_test.go rename to vendor/github.com/containerd/platforms/defaults.go index d5f3883f603a..cfa3ff34a19b 100644 --- a/platforms/platforms_windows_test.go +++ b/vendor/github.com/containerd/platforms/defaults.go @@ -16,26 +16,12 @@ package platforms -import ( - "testing" - - specs "github.com/opencontainers/image-spec/specs-go/v1" - "github.com/stretchr/testify/require" -) - -func TestNormalize(t *testing.T) { - require.Equal(t, DefaultSpec(), Normalize(DefaultSpec())) +// DefaultString returns the default string specifier for the platform. +func DefaultString() string { + return Format(DefaultSpec()) } -func TestFallbackOnOSVersion(t *testing.T) { - p := specs.Platform{ - OS: "windows", - Architecture: "amd64", - OSVersion: "99.99.99.99", - } - - other := specs.Platform{OS: p.OS, Architecture: p.Architecture} - - m := NewMatcher(p) - require.True(t, m.Match(other)) +// DefaultStrict returns strict form of Default. +func DefaultStrict() MatchComparer { + return OnlyStrict(DefaultSpec()) } diff --git a/platforms/defaults_darwin.go b/vendor/github.com/containerd/platforms/defaults_darwin.go similarity index 100% rename from platforms/defaults_darwin.go rename to vendor/github.com/containerd/platforms/defaults_darwin.go diff --git a/platforms/defaults_freebsd.go b/vendor/github.com/containerd/platforms/defaults_freebsd.go similarity index 100% rename from platforms/defaults_freebsd.go rename to vendor/github.com/containerd/platforms/defaults_freebsd.go diff --git a/platforms/defaults_unix.go b/vendor/github.com/containerd/platforms/defaults_unix.go similarity index 100% rename from platforms/defaults_unix.go rename to vendor/github.com/containerd/platforms/defaults_unix.go diff --git a/platforms/defaults_windows.go b/vendor/github.com/containerd/platforms/defaults_windows.go similarity index 100% rename from platforms/defaults_windows.go rename to vendor/github.com/containerd/platforms/defaults_windows.go diff --git a/platforms/errors.go b/vendor/github.com/containerd/platforms/errors.go similarity index 100% rename from platforms/errors.go rename to vendor/github.com/containerd/platforms/errors.go diff --git a/vendor/github.com/containerd/platforms/platforms.go b/vendor/github.com/containerd/platforms/platforms.go new file mode 100644 index 000000000000..43e4ad3d83a1 --- /dev/null +++ b/vendor/github.com/containerd/platforms/platforms.go @@ -0,0 +1,290 @@ +/* + 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 platforms provides a toolkit for normalizing, matching and +// specifying container platforms. +// +// Centered around OCI platform specifications, we define a string-based +// specifier syntax that can be used for user input. With a specifier, users +// only need to specify the parts of the platform that are relevant to their +// context, providing an operating system or architecture or both. +// +// How do I use this package? +// +// The vast majority of use cases should simply use the match function with +// user input. The first step is to parse a specifier into a matcher: +// +// m, err := Parse("linux") +// if err != nil { ... } +// +// Once you have a matcher, use it to match against the platform declared by a +// component, typically from an image or runtime. Since extracting an images +// platform is a little more involved, we'll use an example against the +// platform default: +// +// if ok := m.Match(Default()); !ok { /* doesn't match */ } +// +// This can be composed in loops for resolving runtimes or used as a filter for +// fetch and select images. +// +// More details of the specifier syntax and platform spec follow. +// +// # Declaring Platform Support +// +// Components that have strict platform requirements should use the OCI +// platform specification to declare their support. Typically, this will be +// images and runtimes that should make these declaring which platform they +// support specifically. This looks roughly as follows: +// +// type Platform struct { +// Architecture string +// OS string +// Variant string +// } +// +// Most images and runtimes should at least set Architecture and OS, according +// to their GOARCH and GOOS values, respectively (follow the OCI image +// specification when in doubt). ARM should set variant under certain +// discussions, which are outlined below. +// +// # Platform Specifiers +// +// While the OCI platform specifications provide a tool for components to +// specify structured information, user input typically doesn't need the full +// context and much can be inferred. To solve this problem, we introduced +// "specifiers". A specifier has the format +// `||/[/]`. The user can provide either the +// operating system or the architecture or both. +// +// An example of a common specifier is `linux/amd64`. If the host has a default +// of runtime that matches this, the user can simply provide the component that +// matters. For example, if a image provides amd64 and arm64 support, the +// operating system, `linux` can be inferred, so they only have to provide +// `arm64` or `amd64`. Similar behavior is implemented for operating systems, +// where the architecture may be known but a runtime may support images from +// different operating systems. +// +// # Normalization +// +// Because not all users are familiar with the way the Go runtime represents +// platforms, several normalizations have been provided to make this package +// easier to user. +// +// The following are performed for architectures: +// +// Value Normalized +// aarch64 arm64 +// armhf arm +// armel arm/v6 +// i386 386 +// x86_64 amd64 +// x86-64 amd64 +// +// We also normalize the operating system `macos` to `darwin`. +// +// # ARM Support +// +// To qualify ARM architecture, the Variant field is used to qualify the arm +// version. The most common arm version, v7, is represented without the variant +// unless it is explicitly provided. This is treated as equivalent to armhf. A +// previous architecture, armel, will be normalized to arm/v6. +// +// Similarly, the most common arm64 version v8, and most common amd64 version v1 +// are represented without the variant. +// +// While these normalizations are provided, their support on arm platforms has +// not yet been fully implemented and tested. +package platforms + +import ( + "fmt" + "path" + "regexp" + "runtime" + "strconv" + "strings" + + specs "github.com/opencontainers/image-spec/specs-go/v1" +) + +var ( + specifierRe = regexp.MustCompile(`^[A-Za-z0-9_-]+$`) +) + +// Platform is a type alias for convenience, so there is no need to import image-spec package everywhere. +type Platform = specs.Platform + +// Matcher matches platforms specifications, provided by an image or runtime. +type Matcher interface { + Match(platform specs.Platform) bool +} + +// NewMatcher returns a simple matcher based on the provided platform +// specification. The returned matcher only looks for equality based on os, +// architecture and variant. +// +// One may implement their own matcher if this doesn't provide the required +// functionality. +// +// Applications should opt to use `Match` over directly parsing specifiers. +func NewMatcher(platform specs.Platform) Matcher { + return newDefaultMatcher(platform) +} + +type matcher struct { + specs.Platform +} + +func (m *matcher) Match(platform specs.Platform) bool { + normalized := Normalize(platform) + return m.OS == normalized.OS && + m.Architecture == normalized.Architecture && + m.Variant == normalized.Variant +} + +func (m *matcher) String() string { + return Format(m.Platform) +} + +// ParseAll parses a list of platform specifiers into a list of platform. +func ParseAll(specifiers []string) ([]specs.Platform, error) { + platforms := make([]specs.Platform, len(specifiers)) + for i, s := range specifiers { + p, err := Parse(s) + if err != nil { + return nil, fmt.Errorf("invalid platform %s: %w", s, err) + } + platforms[i] = p + } + return platforms, nil +} + +// Parse parses the platform specifier syntax into a platform declaration. +// +// Platform specifiers are in the format `||/[/]`. +// The minimum required information for a platform specifier is the operating +// system or architecture. If there is only a single string (no slashes), the +// value will be matched against the known set of operating systems, then fall +// back to the known set of architectures. The missing component will be +// inferred based on the local environment. +func Parse(specifier string) (specs.Platform, error) { + if strings.Contains(specifier, "*") { + // TODO(stevvooe): need to work out exact wildcard handling + return specs.Platform{}, fmt.Errorf("%q: wildcards not yet supported: %w", specifier, errInvalidArgument) + } + + parts := strings.Split(specifier, "/") + + for _, part := range parts { + if !specifierRe.MatchString(part) { + return specs.Platform{}, fmt.Errorf("%q is an invalid component of %q: platform specifier component must match %q: %w", part, specifier, specifierRe.String(), errInvalidArgument) + } + } + + var p specs.Platform + switch len(parts) { + case 1: + // in this case, we will test that the value might be an OS, then look + // it up. If it is not known, we'll treat it as an architecture. Since + // we have very little information about the platform here, we are + // going to be a little more strict if we don't know about the argument + // value. + p.OS = normalizeOS(parts[0]) + if isKnownOS(p.OS) { + // picks a default architecture + p.Architecture = runtime.GOARCH + if p.Architecture == "arm" && cpuVariant() != "v7" { + p.Variant = cpuVariant() + } + + if p.OS == "windows" { + p.OSVersion = GetWindowsOsVersion() + } + + return p, nil + } + + p.Architecture, p.Variant = normalizeArch(parts[0], "") + if p.Architecture == "arm" && p.Variant == "v7" { + p.Variant = "" + } + if isKnownArch(p.Architecture) { + p.OS = runtime.GOOS + return p, nil + } + + return specs.Platform{}, fmt.Errorf("%q: unknown operating system or architecture: %w", specifier, errInvalidArgument) + case 2: + // In this case, we treat as a regular os/arch pair. We don't care + // about whether or not we know of the platform. + p.OS = normalizeOS(parts[0]) + p.Architecture, p.Variant = normalizeArch(parts[1], "") + if p.Architecture == "arm" && p.Variant == "v7" { + p.Variant = "" + } + + if p.OS == "windows" { + p.OSVersion = GetWindowsOsVersion() + } + + return p, nil + case 3: + // we have a fully specified variant, this is rare + p.OS = normalizeOS(parts[0]) + p.Architecture, p.Variant = normalizeArch(parts[1], parts[2]) + if p.Architecture == "arm64" && p.Variant == "" { + p.Variant = "v8" + } + + if p.OS == "windows" { + p.OSVersion = GetWindowsOsVersion() + } + + return p, nil + } + + return specs.Platform{}, fmt.Errorf("%q: cannot parse platform specifier: %w", specifier, errInvalidArgument) +} + +// MustParse is like Parses but panics if the specifier cannot be parsed. +// Simplifies initialization of global variables. +func MustParse(specifier string) specs.Platform { + p, err := Parse(specifier) + if err != nil { + panic("platform: Parse(" + strconv.Quote(specifier) + "): " + err.Error()) + } + return p +} + +// Format returns a string specifier from the provided platform specification. +func Format(platform specs.Platform) string { + if platform.OS == "" { + return "unknown" + } + + return path.Join(platform.OS, platform.Architecture, platform.Variant) +} + +// Normalize validates and translate the platform to the canonical value. +// +// For example, if "Aarch64" is encountered, we change it to "arm64" or if +// "x86_64" is encountered, it becomes "amd64". +func Normalize(platform specs.Platform) specs.Platform { + platform.OS = normalizeOS(platform.OS) + platform.Architecture, platform.Variant = normalizeArch(platform.Architecture, platform.Variant) + + return platform +} diff --git a/platforms/platforms_other.go b/vendor/github.com/containerd/platforms/platforms_other.go similarity index 100% rename from platforms/platforms_other.go rename to vendor/github.com/containerd/platforms/platforms_other.go diff --git a/platforms/platforms_windows.go b/vendor/github.com/containerd/platforms/platforms_windows.go similarity index 100% rename from platforms/platforms_windows.go rename to vendor/github.com/containerd/platforms/platforms_windows.go diff --git a/vendor/modules.txt b/vendor/modules.txt index 8112632fe4e1..cbb68c8be21b 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -147,6 +147,9 @@ github.com/containerd/nri/pkg/net/multiplex github.com/containerd/nri/pkg/runtime-tools/generate github.com/containerd/nri/pkg/stub github.com/containerd/nri/types/v1 +# github.com/containerd/platforms v0.1.1 +## explicit; go 1.20 +github.com/containerd/platforms # github.com/containerd/plugin v0.0.0-20231101173250-7ec69893e1e7 ## explicit; go 1.20 github.com/containerd/plugin From e79ec7a095b8131f92e86efadd7254d20a88f9eb Mon Sep 17 00:00:00 2001 From: Derek McGowan Date: Fri, 19 Jan 2024 22:56:11 -0800 Subject: [PATCH 2/2] Remove deprecated platforms package Signed-off-by: Derek McGowan --- client/client.go | 2 +- client/client_opts.go | 2 +- client/container_checkpoint_opts.go | 2 +- client/image.go | 2 +- client/import.go | 2 +- client/pull.go | 2 +- cmd/containerd/server/server.go | 2 +- cmd/ctr/commands/content/fetch.go | 2 +- cmd/ctr/commands/images/convert.go | 2 +- cmd/ctr/commands/images/export.go | 2 +- cmd/ctr/commands/images/images.go | 2 +- cmd/ctr/commands/images/import.go | 2 +- cmd/ctr/commands/images/mount.go | 2 +- cmd/ctr/commands/images/pull.go | 2 +- cmd/ctr/commands/images/push.go | 2 +- cmd/ctr/commands/oci/oci.go | 2 +- cmd/ctr/commands/plugins/plugins.go | 2 +- cmd/ctr/commands/run/run_unix.go | 2 +- contrib/fuzz/fuzz_images.go | 2 +- core/images/archive/exporter.go | 2 +- core/images/archive/importer.go | 2 +- core/images/converter/converter.go | 2 +- core/images/converter/default.go | 2 +- core/images/handlers.go | 2 +- core/images/image.go | 2 +- core/images/usage/calculator.go | 2 +- core/images/usage/calculator_test.go | 2 +- core/metrics/cgroups/cgroups.go | 2 +- core/remotes/handlers.go | 2 +- core/runtime/v2/manager.go | 2 +- core/runtime/v2/manager_unix.go | 2 +- core/runtime/v2/manager_windows.go | 2 +- core/sandbox/controller.go | 2 +- core/sandbox/proxy/controller.go | 2 +- .../build_local_containerd_helper_test.go | 2 +- integration/client/client_test.go | 2 +- integration/client/client_unix_test.go | 2 +- integration/client/container_test.go | 2 +- integration/client/convert_test.go | 2 +- integration/client/export_test.go | 2 +- integration/client/image_test.go | 2 +- integration/client/import_test.go | 2 +- pkg/cri/cri.go | 2 +- pkg/cri/server/base/cri_base.go | 2 +- pkg/cri/server/container_create.go | 2 +- pkg/cri/server/container_create_linux_test.go | 2 +- pkg/cri/server/container_create_test.go | 2 +- pkg/cri/server/images/check.go | 2 +- pkg/cri/server/images/image_pull_test.go | 2 +- pkg/cri/server/images/service.go | 2 +- pkg/cri/server/images/service_test.go | 2 +- pkg/cri/server/podsandbox/controller.go | 2 +- pkg/cri/server/service_test.go | 2 +- pkg/cri/store/image/fake_image.go | 2 +- pkg/cri/store/image/image.go | 2 +- pkg/oci/spec.go | 2 +- pkg/oci/spec_opts.go | 2 +- pkg/transfer/archive/exporter.go | 2 +- pkg/transfer/image/imagestore.go | 2 +- pkg/transfer/local/pull_test.go | 2 +- pkg/transfer/local/push.go | 2 +- pkg/unpack/unpacker.go | 2 +- platforms/compare.go | 63 ------- platforms/defaults.go | 42 ----- platforms/platforms.go | 173 ------------------ plugins/cri/images/plugin.go | 2 +- plugins/diff/walking/plugin/plugin.go | 2 +- plugins/diff/windows/cimfs.go | 2 +- plugins/diff/windows/windows.go | 2 +- plugins/sandbox/controller.go | 2 +- plugins/snapshots/blockfile/plugin/plugin.go | 2 +- plugins/snapshots/btrfs/plugin/plugin.go | 2 +- plugins/snapshots/devmapper/plugin/plugin.go | 2 +- plugins/snapshots/native/plugin/plugin.go | 2 +- plugins/snapshots/overlay/plugin/plugin.go | 2 +- plugins/snapshots/windows/cimfs.go | 2 +- plugins/snapshots/windows/windows.go | 2 +- plugins/transfer/plugin.go | 2 +- 78 files changed, 75 insertions(+), 353 deletions(-) delete mode 100644 platforms/compare.go delete mode 100644 platforms/defaults.go delete mode 100644 platforms/platforms.go diff --git a/client/client.go b/client/client.go index a88a5f97aaf7..173d5d207780 100644 --- a/client/client.go +++ b/client/client.go @@ -56,10 +56,10 @@ import ( "github.com/containerd/containerd/v2/pkg/errdefs" "github.com/containerd/containerd/v2/pkg/events" "github.com/containerd/containerd/v2/pkg/namespaces" - "github.com/containerd/containerd/v2/platforms" "github.com/containerd/containerd/v2/plugins" "github.com/containerd/containerd/v2/plugins/services/introspection" ptypes "github.com/containerd/containerd/v2/protobuf/types" + "github.com/containerd/platforms" "github.com/containerd/typeurl/v2" ocispec "github.com/opencontainers/image-spec/specs-go/v1" "github.com/opencontainers/runtime-spec/specs-go" diff --git a/client/client_opts.go b/client/client_opts.go index e3b262c6876e..54b37d4ca3a2 100644 --- a/client/client_opts.go +++ b/client/client_opts.go @@ -22,7 +22,7 @@ import ( "github.com/containerd/containerd/v2/core/images" "github.com/containerd/containerd/v2/core/remotes" "github.com/containerd/containerd/v2/core/snapshots" - "github.com/containerd/containerd/v2/platforms" + "github.com/containerd/platforms" ocispec "github.com/opencontainers/image-spec/specs-go/v1" "google.golang.org/grpc" diff --git a/client/container_checkpoint_opts.go b/client/container_checkpoint_opts.go index d04c365abfeb..9be0b45e42c1 100644 --- a/client/container_checkpoint_opts.go +++ b/client/container_checkpoint_opts.go @@ -29,9 +29,9 @@ import ( "github.com/containerd/containerd/v2/core/images" "github.com/containerd/containerd/v2/core/runtime/v2/runc/options" "github.com/containerd/containerd/v2/pkg/rootfs" - "github.com/containerd/containerd/v2/platforms" "github.com/containerd/containerd/v2/protobuf" "github.com/containerd/containerd/v2/protobuf/proto" + "github.com/containerd/platforms" "github.com/opencontainers/go-digest" imagespec "github.com/opencontainers/image-spec/specs-go/v1" ) diff --git a/client/image.go b/client/image.go index 9974dfb1db89..f0eef70acd24 100644 --- a/client/image.go +++ b/client/image.go @@ -32,7 +32,7 @@ import ( "github.com/containerd/containerd/v2/pkg/errdefs" "github.com/containerd/containerd/v2/pkg/labels" "github.com/containerd/containerd/v2/pkg/rootfs" - "github.com/containerd/containerd/v2/platforms" + "github.com/containerd/platforms" "github.com/opencontainers/go-digest" "github.com/opencontainers/image-spec/identity" ocispec "github.com/opencontainers/image-spec/specs-go/v1" diff --git a/client/import.go b/client/import.go index bc67d591d1d1..847d27c40861 100644 --- a/client/import.go +++ b/client/import.go @@ -23,7 +23,7 @@ import ( "github.com/containerd/containerd/v2/core/images" "github.com/containerd/containerd/v2/core/images/archive" "github.com/containerd/containerd/v2/pkg/errdefs" - "github.com/containerd/containerd/v2/platforms" + "github.com/containerd/platforms" digest "github.com/opencontainers/go-digest" ocispec "github.com/opencontainers/image-spec/specs-go/v1" ) diff --git a/client/pull.go b/client/pull.go index fd4c8bce66b3..9b817869428b 100644 --- a/client/pull.go +++ b/client/pull.go @@ -31,7 +31,7 @@ import ( "github.com/containerd/containerd/v2/pkg/errdefs" "github.com/containerd/containerd/v2/pkg/tracing" "github.com/containerd/containerd/v2/pkg/unpack" - "github.com/containerd/containerd/v2/platforms" + "github.com/containerd/platforms" ) const ( diff --git a/cmd/containerd/server/server.go b/cmd/containerd/server/server.go index 92d7a1f36a98..c3721f9b3952 100644 --- a/cmd/containerd/server/server.go +++ b/cmd/containerd/server/server.go @@ -61,10 +61,10 @@ import ( "github.com/containerd/containerd/v2/pkg/dialer" "github.com/containerd/containerd/v2/pkg/sys" "github.com/containerd/containerd/v2/pkg/timeout" - "github.com/containerd/containerd/v2/platforms" "github.com/containerd/containerd/v2/plugins" "github.com/containerd/containerd/v2/plugins/content/local" "github.com/containerd/containerd/v2/plugins/services/warning" + "github.com/containerd/platforms" "github.com/containerd/plugin" "github.com/containerd/plugin/dynamic" "github.com/containerd/plugin/registry" diff --git a/cmd/ctr/commands/content/fetch.go b/cmd/ctr/commands/content/fetch.go index 3c295fe72583..da6c70f5a70d 100644 --- a/cmd/ctr/commands/content/fetch.go +++ b/cmd/ctr/commands/content/fetch.go @@ -33,8 +33,8 @@ import ( "github.com/containerd/containerd/v2/core/remotes" "github.com/containerd/containerd/v2/pkg/errdefs" "github.com/containerd/containerd/v2/pkg/progress" - "github.com/containerd/containerd/v2/platforms" "github.com/containerd/log" + "github.com/containerd/platforms" "github.com/opencontainers/go-digest" ocispec "github.com/opencontainers/image-spec/specs-go/v1" "github.com/urfave/cli" diff --git a/cmd/ctr/commands/images/convert.go b/cmd/ctr/commands/images/convert.go index 5e91e0dfe3e2..29cb00dbf336 100644 --- a/cmd/ctr/commands/images/convert.go +++ b/cmd/ctr/commands/images/convert.go @@ -23,7 +23,7 @@ import ( "github.com/containerd/containerd/v2/cmd/ctr/commands" "github.com/containerd/containerd/v2/core/images/converter" "github.com/containerd/containerd/v2/core/images/converter/uncompress" - "github.com/containerd/containerd/v2/platforms" + "github.com/containerd/platforms" "github.com/urfave/cli" ) diff --git a/cmd/ctr/commands/images/export.go b/cmd/ctr/commands/images/export.go index 0f23f911e709..a5a35b381527 100644 --- a/cmd/ctr/commands/images/export.go +++ b/cmd/ctr/commands/images/export.go @@ -29,7 +29,7 @@ import ( "github.com/containerd/containerd/v2/pkg/transfer" tarchive "github.com/containerd/containerd/v2/pkg/transfer/archive" "github.com/containerd/containerd/v2/pkg/transfer/image" - "github.com/containerd/containerd/v2/platforms" + "github.com/containerd/platforms" ) var exportCommand = cli.Command{ diff --git a/cmd/ctr/commands/images/images.go b/cmd/ctr/commands/images/images.go index 28dda807f9f8..a58b4f028b28 100644 --- a/cmd/ctr/commands/images/images.go +++ b/cmd/ctr/commands/images/images.go @@ -28,8 +28,8 @@ import ( "github.com/containerd/containerd/v2/core/images" "github.com/containerd/containerd/v2/pkg/errdefs" "github.com/containerd/containerd/v2/pkg/progress" - "github.com/containerd/containerd/v2/platforms" "github.com/containerd/log" + "github.com/containerd/platforms" "github.com/urfave/cli" ) diff --git a/cmd/ctr/commands/images/import.go b/cmd/ctr/commands/images/import.go index 21971797aa9e..32cd72b84c2b 100644 --- a/cmd/ctr/commands/images/import.go +++ b/cmd/ctr/commands/images/import.go @@ -31,8 +31,8 @@ import ( "github.com/containerd/containerd/v2/pkg/transfer" tarchive "github.com/containerd/containerd/v2/pkg/transfer/archive" "github.com/containerd/containerd/v2/pkg/transfer/image" - "github.com/containerd/containerd/v2/platforms" "github.com/containerd/log" + "github.com/containerd/platforms" ) var importCommand = cli.Command{ diff --git a/cmd/ctr/commands/images/mount.go b/cmd/ctr/commands/images/mount.go index 3ad277bc607d..32be49cb6b99 100644 --- a/cmd/ctr/commands/images/mount.go +++ b/cmd/ctr/commands/images/mount.go @@ -26,7 +26,7 @@ import ( "github.com/containerd/containerd/v2/core/mount" "github.com/containerd/containerd/v2/defaults" "github.com/containerd/containerd/v2/pkg/errdefs" - "github.com/containerd/containerd/v2/platforms" + "github.com/containerd/platforms" "github.com/opencontainers/image-spec/identity" "github.com/urfave/cli" ) diff --git a/cmd/ctr/commands/images/pull.go b/cmd/ctr/commands/images/pull.go index 489f8172d05f..83ff1960432a 100644 --- a/cmd/ctr/commands/images/pull.go +++ b/cmd/ctr/commands/images/pull.go @@ -32,8 +32,8 @@ import ( "github.com/containerd/containerd/v2/pkg/transfer" "github.com/containerd/containerd/v2/pkg/transfer/image" "github.com/containerd/containerd/v2/pkg/transfer/registry" - "github.com/containerd/containerd/v2/platforms" "github.com/containerd/log" + "github.com/containerd/platforms" "github.com/opencontainers/image-spec/identity" ocispec "github.com/opencontainers/image-spec/specs-go/v1" "github.com/urfave/cli" diff --git a/cmd/ctr/commands/images/push.go b/cmd/ctr/commands/images/push.go index 9b0e5573193c..62d4aa2d833e 100644 --- a/cmd/ctr/commands/images/push.go +++ b/cmd/ctr/commands/images/push.go @@ -36,8 +36,8 @@ import ( "github.com/containerd/containerd/v2/pkg/transfer" "github.com/containerd/containerd/v2/pkg/transfer/image" "github.com/containerd/containerd/v2/pkg/transfer/registry" - "github.com/containerd/containerd/v2/platforms" "github.com/containerd/log" + "github.com/containerd/platforms" digest "github.com/opencontainers/go-digest" ocispec "github.com/opencontainers/image-spec/specs-go/v1" "github.com/urfave/cli" diff --git a/cmd/ctr/commands/oci/oci.go b/cmd/ctr/commands/oci/oci.go index 8d8368579096..fce3756e2a49 100644 --- a/cmd/ctr/commands/oci/oci.go +++ b/cmd/ctr/commands/oci/oci.go @@ -24,7 +24,7 @@ import ( "github.com/containerd/containerd/v2/cmd/ctr/commands" "github.com/containerd/containerd/v2/core/containers" "github.com/containerd/containerd/v2/pkg/oci" - "github.com/containerd/containerd/v2/platforms" + "github.com/containerd/platforms" ) // Command is the parent for all OCI related tools under 'oci' diff --git a/cmd/ctr/commands/plugins/plugins.go b/cmd/ctr/commands/plugins/plugins.go index 7702518960df..ec60a8244054 100644 --- a/cmd/ctr/commands/plugins/plugins.go +++ b/cmd/ctr/commands/plugins/plugins.go @@ -25,7 +25,7 @@ import ( "github.com/containerd/containerd/v2/api/types" "github.com/containerd/containerd/v2/cmd/ctr/commands" - "github.com/containerd/containerd/v2/platforms" + "github.com/containerd/platforms" pluginutils "github.com/containerd/plugin" v1 "github.com/opencontainers/image-spec/specs-go/v1" "github.com/urfave/cli" diff --git a/cmd/ctr/commands/run/run_unix.go b/cmd/ctr/commands/run/run_unix.go index bd10b693e43b..87118a2a056e 100644 --- a/cmd/ctr/commands/run/run_unix.go +++ b/cmd/ctr/commands/run/run_unix.go @@ -40,8 +40,8 @@ import ( "github.com/containerd/containerd/v2/core/snapshots" "github.com/containerd/containerd/v2/pkg/oci" runtimeoptions "github.com/containerd/containerd/v2/pkg/runtimeoptions/v1" - "github.com/containerd/containerd/v2/platforms" "github.com/containerd/log" + "github.com/containerd/platforms" "github.com/intel/goresctrl/pkg/blockio" "github.com/opencontainers/runtime-spec/specs-go" "github.com/urfave/cli" diff --git a/contrib/fuzz/fuzz_images.go b/contrib/fuzz/fuzz_images.go index 1032266f9ec6..9418c72ca968 100644 --- a/contrib/fuzz/fuzz_images.go +++ b/contrib/fuzz/fuzz_images.go @@ -21,8 +21,8 @@ import ( fuzz "github.com/AdaLogics/go-fuzz-headers" "github.com/containerd/containerd/v2/core/images" - "github.com/containerd/containerd/v2/platforms" "github.com/containerd/containerd/v2/plugins/content/local" + "github.com/containerd/platforms" ocispec "github.com/opencontainers/image-spec/specs-go/v1" ) diff --git a/core/images/archive/exporter.go b/core/images/archive/exporter.go index 2034009c5a20..bd136a4b8e5a 100644 --- a/core/images/archive/exporter.go +++ b/core/images/archive/exporter.go @@ -30,8 +30,8 @@ import ( "github.com/containerd/containerd/v2/core/images" "github.com/containerd/containerd/v2/pkg/errdefs" "github.com/containerd/containerd/v2/pkg/labels" - "github.com/containerd/containerd/v2/platforms" "github.com/containerd/log" + "github.com/containerd/platforms" digest "github.com/opencontainers/go-digest" ocispecs "github.com/opencontainers/image-spec/specs-go" ocispec "github.com/opencontainers/image-spec/specs-go/v1" diff --git a/core/images/archive/importer.go b/core/images/archive/importer.go index f5c804128d34..314c22ab826f 100644 --- a/core/images/archive/importer.go +++ b/core/images/archive/importer.go @@ -32,8 +32,8 @@ import ( "github.com/containerd/containerd/v2/pkg/archive/compression" "github.com/containerd/containerd/v2/pkg/errdefs" "github.com/containerd/containerd/v2/pkg/labels" - "github.com/containerd/containerd/v2/platforms" "github.com/containerd/log" + "github.com/containerd/platforms" digest "github.com/opencontainers/go-digest" specs "github.com/opencontainers/image-spec/specs-go" ocispec "github.com/opencontainers/image-spec/specs-go/v1" diff --git a/core/images/converter/converter.go b/core/images/converter/converter.go index e88d702ec083..20308cb55a84 100644 --- a/core/images/converter/converter.go +++ b/core/images/converter/converter.go @@ -23,7 +23,7 @@ import ( "github.com/containerd/containerd/v2/core/content" "github.com/containerd/containerd/v2/core/images" "github.com/containerd/containerd/v2/core/leases" - "github.com/containerd/containerd/v2/platforms" + "github.com/containerd/platforms" ) type convertOpts struct { diff --git a/core/images/converter/default.go b/core/images/converter/default.go index 538055d95d57..98f7965367cc 100644 --- a/core/images/converter/default.go +++ b/core/images/converter/default.go @@ -26,8 +26,8 @@ import ( "github.com/containerd/containerd/v2/core/content" "github.com/containerd/containerd/v2/core/images" - "github.com/containerd/containerd/v2/platforms" "github.com/containerd/log" + "github.com/containerd/platforms" "github.com/opencontainers/go-digest" ocispec "github.com/opencontainers/image-spec/specs-go/v1" "golang.org/x/sync/errgroup" diff --git a/core/images/handlers.go b/core/images/handlers.go index 0a4397583dc7..407030ab3cd4 100644 --- a/core/images/handlers.go +++ b/core/images/handlers.go @@ -24,7 +24,7 @@ import ( "github.com/containerd/containerd/v2/core/content" "github.com/containerd/containerd/v2/pkg/errdefs" - "github.com/containerd/containerd/v2/platforms" + "github.com/containerd/platforms" ocispec "github.com/opencontainers/image-spec/specs-go/v1" "golang.org/x/sync/errgroup" "golang.org/x/sync/semaphore" diff --git a/core/images/image.go b/core/images/image.go index 90b1669a188a..16bc9f74ed1c 100644 --- a/core/images/image.go +++ b/core/images/image.go @@ -25,8 +25,8 @@ import ( "github.com/containerd/containerd/v2/core/content" "github.com/containerd/containerd/v2/pkg/errdefs" - "github.com/containerd/containerd/v2/platforms" "github.com/containerd/log" + "github.com/containerd/platforms" digest "github.com/opencontainers/go-digest" ocispec "github.com/opencontainers/image-spec/specs-go/v1" ) diff --git a/core/images/usage/calculator.go b/core/images/usage/calculator.go index 0090d38060d5..738a459b8818 100644 --- a/core/images/usage/calculator.go +++ b/core/images/usage/calculator.go @@ -25,7 +25,7 @@ import ( "github.com/containerd/containerd/v2/core/images" "github.com/containerd/containerd/v2/core/snapshots" "github.com/containerd/containerd/v2/pkg/errdefs" - "github.com/containerd/containerd/v2/platforms" + "github.com/containerd/platforms" ocispec "github.com/opencontainers/image-spec/specs-go/v1" "golang.org/x/sync/semaphore" diff --git a/core/images/usage/calculator_test.go b/core/images/usage/calculator_test.go index bbbde885fa4e..84ed167e41e3 100644 --- a/core/images/usage/calculator_test.go +++ b/core/images/usage/calculator_test.go @@ -22,8 +22,8 @@ import ( "github.com/containerd/containerd/v2/core/images" "github.com/containerd/containerd/v2/core/images/imagetest" - "github.com/containerd/containerd/v2/platforms" "github.com/containerd/log/logtest" + "github.com/containerd/platforms" ocispec "github.com/opencontainers/image-spec/specs-go/v1" ) diff --git a/core/metrics/cgroups/cgroups.go b/core/metrics/cgroups/cgroups.go index 7fcbc448f5df..604977cf0992 100644 --- a/core/metrics/cgroups/cgroups.go +++ b/core/metrics/cgroups/cgroups.go @@ -24,8 +24,8 @@ import ( v2 "github.com/containerd/containerd/v2/core/metrics/cgroups/v2" "github.com/containerd/containerd/v2/core/runtime" "github.com/containerd/containerd/v2/pkg/events" - "github.com/containerd/containerd/v2/platforms" "github.com/containerd/containerd/v2/plugins" + "github.com/containerd/platforms" "github.com/containerd/plugin" "github.com/containerd/plugin/registry" metrics "github.com/docker/go-metrics" diff --git a/core/remotes/handlers.go b/core/remotes/handlers.go index 3afa22daa6ec..8e9cbebcc41f 100644 --- a/core/remotes/handlers.go +++ b/core/remotes/handlers.go @@ -29,8 +29,8 @@ import ( "github.com/containerd/containerd/v2/core/images" "github.com/containerd/containerd/v2/pkg/errdefs" "github.com/containerd/containerd/v2/pkg/labels" - "github.com/containerd/containerd/v2/platforms" "github.com/containerd/log" + "github.com/containerd/platforms" ocispec "github.com/opencontainers/image-spec/specs-go/v1" "golang.org/x/sync/semaphore" ) diff --git a/core/runtime/v2/manager.go b/core/runtime/v2/manager.go index a14a980b5e42..c5cc52f15bee 100644 --- a/core/runtime/v2/manager.go +++ b/core/runtime/v2/manager.go @@ -36,10 +36,10 @@ import ( "github.com/containerd/containerd/v2/pkg/events/exchange" "github.com/containerd/containerd/v2/pkg/namespaces" "github.com/containerd/containerd/v2/pkg/timeout" - "github.com/containerd/containerd/v2/platforms" "github.com/containerd/containerd/v2/plugins" "github.com/containerd/containerd/v2/protobuf" "github.com/containerd/log" + "github.com/containerd/platforms" "github.com/containerd/plugin" "github.com/containerd/plugin/registry" ) diff --git a/core/runtime/v2/manager_unix.go b/core/runtime/v2/manager_unix.go index d4c147dc452a..be6ce6e1128a 100644 --- a/core/runtime/v2/manager_unix.go +++ b/core/runtime/v2/manager_unix.go @@ -19,7 +19,7 @@ package v2 import ( - "github.com/containerd/containerd/v2/platforms" + "github.com/containerd/platforms" ) func defaultPlatforms() []string { diff --git a/core/runtime/v2/manager_windows.go b/core/runtime/v2/manager_windows.go index 544e0afa9b02..b1bfbaa48867 100644 --- a/core/runtime/v2/manager_windows.go +++ b/core/runtime/v2/manager_windows.go @@ -17,7 +17,7 @@ package v2 import ( - "github.com/containerd/containerd/v2/platforms" + "github.com/containerd/platforms" ) func defaultPlatforms() []string { diff --git a/core/sandbox/controller.go b/core/sandbox/controller.go index b55f9b2fda56..d30bb9f7131b 100644 --- a/core/sandbox/controller.go +++ b/core/sandbox/controller.go @@ -23,7 +23,7 @@ import ( "github.com/containerd/containerd/v2/api/types" "github.com/containerd/containerd/v2/core/mount" - "github.com/containerd/containerd/v2/platforms" + "github.com/containerd/platforms" "github.com/containerd/typeurl/v2" ) diff --git a/core/sandbox/proxy/controller.go b/core/sandbox/proxy/controller.go index 9b06be186988..6992c4531c15 100644 --- a/core/sandbox/proxy/controller.go +++ b/core/sandbox/proxy/controller.go @@ -24,7 +24,7 @@ import ( "github.com/containerd/containerd/v2/core/mount" "github.com/containerd/containerd/v2/core/sandbox" "github.com/containerd/containerd/v2/pkg/errdefs" - "github.com/containerd/containerd/v2/platforms" + "github.com/containerd/platforms" "google.golang.org/protobuf/types/known/anypb" ) diff --git a/integration/build_local_containerd_helper_test.go b/integration/build_local_containerd_helper_test.go index f8355def1d34..f9577485eaf5 100644 --- a/integration/build_local_containerd_helper_test.go +++ b/integration/build_local_containerd_helper_test.go @@ -28,9 +28,9 @@ import ( srvconfig "github.com/containerd/containerd/v2/cmd/containerd/server/config" "github.com/containerd/containerd/v2/core/content" "github.com/containerd/containerd/v2/pkg/cri/constants" - "github.com/containerd/containerd/v2/platforms" "github.com/containerd/containerd/v2/plugins" "github.com/containerd/log/logtest" + "github.com/containerd/platforms" "github.com/containerd/plugin" "github.com/opencontainers/go-digest" diff --git a/integration/client/client_test.go b/integration/client/client_test.go index 5b29a0a3ecaa..d3b2bf3748a4 100644 --- a/integration/client/client_test.go +++ b/integration/client/client_test.go @@ -40,8 +40,8 @@ import ( "github.com/containerd/containerd/v2/internal/testutil" "github.com/containerd/containerd/v2/pkg/errdefs" "github.com/containerd/containerd/v2/pkg/namespaces" - "github.com/containerd/containerd/v2/platforms" "github.com/containerd/log" + "github.com/containerd/platforms" ) var ( diff --git a/integration/client/client_unix_test.go b/integration/client/client_unix_test.go index ef7a9afac699..59bea46a255f 100644 --- a/integration/client/client_unix_test.go +++ b/integration/client/client_unix_test.go @@ -23,7 +23,7 @@ import ( . "github.com/containerd/containerd/v2/client" "github.com/containerd/containerd/v2/integration/images" - "github.com/containerd/containerd/v2/platforms" + "github.com/containerd/platforms" ) var ( diff --git a/integration/client/container_test.go b/integration/client/container_test.go index 939215d85c5a..e5f5545191d4 100644 --- a/integration/client/container_test.go +++ b/integration/client/container_test.go @@ -41,12 +41,12 @@ import ( "github.com/containerd/containerd/v2/pkg/errdefs" "github.com/containerd/containerd/v2/pkg/namespaces" "github.com/containerd/containerd/v2/pkg/oci" - "github.com/containerd/containerd/v2/platforms" "github.com/containerd/containerd/v2/plugins" gogotypes "github.com/containerd/containerd/v2/protobuf/types" "github.com/containerd/continuity/fs" "github.com/containerd/go-runc" "github.com/containerd/log/logtest" + "github.com/containerd/platforms" "github.com/containerd/typeurl/v2" specs "github.com/opencontainers/runtime-spec/specs-go" "github.com/stretchr/testify/require" diff --git a/integration/client/convert_test.go b/integration/client/convert_test.go index 4f5f19c80e23..85f0f0c41014 100644 --- a/integration/client/convert_test.go +++ b/integration/client/convert_test.go @@ -23,7 +23,7 @@ import ( "github.com/containerd/containerd/v2/core/images" "github.com/containerd/containerd/v2/core/images/converter" "github.com/containerd/containerd/v2/core/images/converter/uncompress" - "github.com/containerd/containerd/v2/platforms" + "github.com/containerd/platforms" ocispec "github.com/opencontainers/image-spec/specs-go/v1" "github.com/stretchr/testify/assert" ) diff --git a/integration/client/export_test.go b/integration/client/export_test.go index c1c9e3346b13..bf727f308d32 100644 --- a/integration/client/export_test.go +++ b/integration/client/export_test.go @@ -31,7 +31,7 @@ import ( "github.com/containerd/containerd/v2/core/images/archive" "github.com/containerd/containerd/v2/pkg/errdefs" "github.com/containerd/containerd/v2/pkg/namespaces" - "github.com/containerd/containerd/v2/platforms" + "github.com/containerd/platforms" "github.com/google/uuid" ocispec "github.com/opencontainers/image-spec/specs-go/v1" ) diff --git a/integration/client/image_test.go b/integration/client/image_test.go index 105e41288624..3d907e0bb047 100644 --- a/integration/client/image_test.go +++ b/integration/client/image_test.go @@ -29,7 +29,7 @@ import ( imagelist "github.com/containerd/containerd/v2/integration/images" "github.com/containerd/containerd/v2/pkg/errdefs" "github.com/containerd/containerd/v2/pkg/labels" - "github.com/containerd/containerd/v2/platforms" + "github.com/containerd/platforms" ocispec "github.com/opencontainers/image-spec/specs-go/v1" ) diff --git a/integration/client/import_test.go b/integration/client/import_test.go index 4c7a03bb9124..ced7ea1f6bed 100644 --- a/integration/client/import_test.go +++ b/integration/client/import_test.go @@ -44,7 +44,7 @@ import ( "github.com/containerd/containerd/v2/pkg/transfer" tarchive "github.com/containerd/containerd/v2/pkg/transfer/archive" "github.com/containerd/containerd/v2/pkg/transfer/image" - "github.com/containerd/containerd/v2/platforms" + "github.com/containerd/platforms" "github.com/google/uuid" digest "github.com/opencontainers/go-digest" diff --git a/pkg/cri/cri.go b/pkg/cri/cri.go index 1760457159d5..3488b9915d24 100644 --- a/pkg/cri/cri.go +++ b/pkg/cri/cri.go @@ -33,8 +33,8 @@ import ( "github.com/containerd/containerd/v2/pkg/cri/server" "github.com/containerd/containerd/v2/pkg/cri/server/base" nriservice "github.com/containerd/containerd/v2/pkg/nri" - "github.com/containerd/containerd/v2/platforms" "github.com/containerd/containerd/v2/plugins" + "github.com/containerd/platforms" "google.golang.org/grpc" diff --git a/pkg/cri/server/base/cri_base.go b/pkg/cri/server/base/cri_base.go index a719e21e4867..77ba766353b6 100644 --- a/pkg/cri/server/base/cri_base.go +++ b/pkg/cri/server/base/cri_base.go @@ -34,9 +34,9 @@ import ( criconfig "github.com/containerd/containerd/v2/pkg/cri/config" "github.com/containerd/containerd/v2/pkg/cri/constants" "github.com/containerd/containerd/v2/pkg/oci" - "github.com/containerd/containerd/v2/platforms" "github.com/containerd/containerd/v2/plugins" "github.com/containerd/containerd/v2/plugins/services/warning" + "github.com/containerd/platforms" ) // CRIBase contains common dependencies for CRI's runtime, image, and podsandbox services. diff --git a/pkg/cri/server/container_create.go b/pkg/cri/server/container_create.go index 510e8d0e8bf0..272096aafe0e 100644 --- a/pkg/cri/server/container_create.go +++ b/pkg/cri/server/container_create.go @@ -45,7 +45,7 @@ import ( containerstore "github.com/containerd/containerd/v2/pkg/cri/store/container" "github.com/containerd/containerd/v2/pkg/cri/util" "github.com/containerd/containerd/v2/pkg/oci" - "github.com/containerd/containerd/v2/platforms" + "github.com/containerd/platforms" ) func init() { diff --git a/pkg/cri/server/container_create_linux_test.go b/pkg/cri/server/container_create_linux_test.go index b0f9576af525..6bb4d63a304d 100644 --- a/pkg/cri/server/container_create_linux_test.go +++ b/pkg/cri/server/container_create_linux_test.go @@ -31,7 +31,7 @@ import ( "github.com/containerd/containerd/v2/core/containers" "github.com/containerd/containerd/v2/core/mount" "github.com/containerd/containerd/v2/pkg/oci" - "github.com/containerd/containerd/v2/platforms" + "github.com/containerd/platforms" imagespec "github.com/opencontainers/image-spec/specs-go/v1" runtimespec "github.com/opencontainers/runtime-spec/specs-go" "github.com/opencontainers/selinux/go-selinux" diff --git a/pkg/cri/server/container_create_test.go b/pkg/cri/server/container_create_test.go index 2279f047f2d1..f0f93fcbbac4 100644 --- a/pkg/cri/server/container_create_test.go +++ b/pkg/cri/server/container_create_test.go @@ -25,7 +25,7 @@ import ( "testing" ostesting "github.com/containerd/containerd/v2/pkg/os/testing" - "github.com/containerd/containerd/v2/platforms" + "github.com/containerd/platforms" imagespec "github.com/opencontainers/image-spec/specs-go/v1" runtimespec "github.com/opencontainers/runtime-spec/specs-go" diff --git a/pkg/cri/server/images/check.go b/pkg/cri/server/images/check.go index bd343008474c..89610df950d9 100644 --- a/pkg/cri/server/images/check.go +++ b/pkg/cri/server/images/check.go @@ -22,8 +22,8 @@ import ( "sync" "github.com/containerd/containerd/v2/core/images" - "github.com/containerd/containerd/v2/platforms" "github.com/containerd/log" + "github.com/containerd/platforms" ) // LoadImages checks all existing images to ensure they are ready to diff --git a/pkg/cri/server/images/image_pull_test.go b/pkg/cri/server/images/image_pull_test.go index 8ffece4e545c..a3994e689013 100644 --- a/pkg/cri/server/images/image_pull_test.go +++ b/pkg/cri/server/images/image_pull_test.go @@ -29,7 +29,7 @@ import ( "github.com/containerd/containerd/v2/pkg/cri/annotations" criconfig "github.com/containerd/containerd/v2/pkg/cri/config" "github.com/containerd/containerd/v2/pkg/cri/labels" - "github.com/containerd/containerd/v2/platforms" + "github.com/containerd/platforms" ) func TestParseAuth(t *testing.T) { diff --git a/pkg/cri/server/images/service.go b/pkg/cri/server/images/service.go index 8c2f8a35d0eb..a73f988b16f5 100644 --- a/pkg/cri/server/images/service.go +++ b/pkg/cri/server/images/service.go @@ -29,8 +29,8 @@ import ( imagestore "github.com/containerd/containerd/v2/pkg/cri/store/image" snapshotstore "github.com/containerd/containerd/v2/pkg/cri/store/snapshot" "github.com/containerd/containerd/v2/pkg/events" - "github.com/containerd/containerd/v2/platforms" "github.com/containerd/log" + "github.com/containerd/platforms" docker "github.com/distribution/reference" imagedigest "github.com/opencontainers/go-digest" diff --git a/pkg/cri/server/images/service_test.go b/pkg/cri/server/images/service_test.go index 1c49e771fe3c..17799205126c 100644 --- a/pkg/cri/server/images/service_test.go +++ b/pkg/cri/server/images/service_test.go @@ -24,7 +24,7 @@ import ( imagestore "github.com/containerd/containerd/v2/pkg/cri/store/image" snapshotstore "github.com/containerd/containerd/v2/pkg/cri/store/snapshot" "github.com/containerd/containerd/v2/pkg/errdefs" - "github.com/containerd/containerd/v2/platforms" + "github.com/containerd/platforms" "github.com/stretchr/testify/assert" ) diff --git a/pkg/cri/server/podsandbox/controller.go b/pkg/cri/server/podsandbox/controller.go index 3953c7bbf089..96a94f5b14fb 100644 --- a/pkg/cri/server/podsandbox/controller.go +++ b/pkg/cri/server/podsandbox/controller.go @@ -38,9 +38,9 @@ import ( "github.com/containerd/containerd/v2/pkg/errdefs" "github.com/containerd/containerd/v2/pkg/oci" osinterface "github.com/containerd/containerd/v2/pkg/os" - "github.com/containerd/containerd/v2/platforms" "github.com/containerd/containerd/v2/plugins" "github.com/containerd/containerd/v2/protobuf" + "github.com/containerd/platforms" ) func init() { diff --git a/pkg/cri/server/service_test.go b/pkg/cri/server/service_test.go index f22f29a1b945..4b1e4aaa4899 100644 --- a/pkg/cri/server/service_test.go +++ b/pkg/cri/server/service_test.go @@ -31,7 +31,7 @@ import ( servertesting "github.com/containerd/containerd/v2/pkg/cri/testing" "github.com/containerd/containerd/v2/pkg/errdefs" ostesting "github.com/containerd/containerd/v2/pkg/os/testing" - "github.com/containerd/containerd/v2/platforms" + "github.com/containerd/platforms" ) type fakeSandboxService struct{} diff --git a/pkg/cri/store/image/fake_image.go b/pkg/cri/store/image/fake_image.go index 041443f99e48..469c56f7f0e8 100644 --- a/pkg/cri/store/image/fake_image.go +++ b/pkg/cri/store/image/fake_image.go @@ -19,7 +19,7 @@ package image import ( "fmt" - "github.com/containerd/containerd/v2/platforms" + "github.com/containerd/platforms" ) // NewFakeStore returns an image store with predefined images. diff --git a/pkg/cri/store/image/image.go b/pkg/cri/store/image/image.go index 8fd2ad23223d..34ecc1d04fb8 100644 --- a/pkg/cri/store/image/image.go +++ b/pkg/cri/store/image/image.go @@ -28,7 +28,7 @@ import ( "github.com/containerd/containerd/v2/pkg/cri/labels" "github.com/containerd/containerd/v2/pkg/cri/util" "github.com/containerd/containerd/v2/pkg/errdefs" - "github.com/containerd/containerd/v2/platforms" + "github.com/containerd/platforms" docker "github.com/distribution/reference" "k8s.io/apimachinery/pkg/util/sets" diff --git a/pkg/oci/spec.go b/pkg/oci/spec.go index a09b57c91ec3..d4808cf683f9 100644 --- a/pkg/oci/spec.go +++ b/pkg/oci/spec.go @@ -30,7 +30,7 @@ import ( "github.com/containerd/containerd/v2/api/types" "github.com/containerd/containerd/v2/core/containers" "github.com/containerd/containerd/v2/pkg/namespaces" - "github.com/containerd/containerd/v2/platforms" + "github.com/containerd/platforms" ) const ( diff --git a/pkg/oci/spec_opts.go b/pkg/oci/spec_opts.go index 2cbb48f6d1e3..aedf26711b91 100644 --- a/pkg/oci/spec_opts.go +++ b/pkg/oci/spec_opts.go @@ -34,9 +34,9 @@ import ( "github.com/containerd/containerd/v2/core/images" "github.com/containerd/containerd/v2/core/mount" "github.com/containerd/containerd/v2/pkg/namespaces" - "github.com/containerd/containerd/v2/platforms" "github.com/containerd/continuity/fs" "github.com/containerd/log" + "github.com/containerd/platforms" "github.com/moby/sys/user" v1 "github.com/opencontainers/image-spec/specs-go/v1" "github.com/opencontainers/runtime-spec/specs-go" diff --git a/pkg/transfer/archive/exporter.go b/pkg/transfer/archive/exporter.go index 98a0a9119bb4..d89fa0a5a6c8 100644 --- a/pkg/transfer/archive/exporter.go +++ b/pkg/transfer/archive/exporter.go @@ -31,8 +31,8 @@ import ( "github.com/containerd/containerd/v2/pkg/streaming" "github.com/containerd/containerd/v2/pkg/transfer/plugins" tstreaming "github.com/containerd/containerd/v2/pkg/transfer/streaming" - "github.com/containerd/containerd/v2/platforms" "github.com/containerd/log" + "github.com/containerd/platforms" ) func init() { diff --git a/pkg/transfer/image/imagestore.go b/pkg/transfer/image/imagestore.go index 075d68d676d0..16cb86631b45 100644 --- a/pkg/transfer/image/imagestore.go +++ b/pkg/transfer/image/imagestore.go @@ -33,7 +33,7 @@ import ( "github.com/containerd/containerd/v2/pkg/streaming" "github.com/containerd/containerd/v2/pkg/transfer" "github.com/containerd/containerd/v2/pkg/transfer/plugins" - "github.com/containerd/containerd/v2/platforms" + "github.com/containerd/platforms" ) func init() { diff --git a/pkg/transfer/local/pull_test.go b/pkg/transfer/local/pull_test.go index 908d030ab016..c46cf07e9bc2 100644 --- a/pkg/transfer/local/pull_test.go +++ b/pkg/transfer/local/pull_test.go @@ -22,7 +22,7 @@ import ( "github.com/containerd/containerd/v2/defaults" "github.com/containerd/containerd/v2/pkg/transfer" "github.com/containerd/containerd/v2/pkg/unpack" - "github.com/containerd/containerd/v2/platforms" + "github.com/containerd/platforms" ) func TestGetSupportedPlatform(t *testing.T) { diff --git a/pkg/transfer/local/push.go b/pkg/transfer/local/push.go index 098dde6381ee..120ae168839c 100644 --- a/pkg/transfer/local/push.go +++ b/pkg/transfer/local/push.go @@ -27,7 +27,7 @@ import ( "github.com/containerd/containerd/v2/core/remotes" "github.com/containerd/containerd/v2/pkg/errdefs" "github.com/containerd/containerd/v2/pkg/transfer" - "github.com/containerd/containerd/v2/platforms" + "github.com/containerd/platforms" "github.com/opencontainers/go-digest" ocispec "github.com/opencontainers/image-spec/specs-go/v1" ) diff --git a/pkg/unpack/unpacker.go b/pkg/unpack/unpacker.go index ea41629bf6df..07bcc769b387 100644 --- a/pkg/unpack/unpacker.go +++ b/pkg/unpack/unpacker.go @@ -38,8 +38,8 @@ import ( "github.com/containerd/containerd/v2/pkg/errdefs" "github.com/containerd/containerd/v2/pkg/labels" "github.com/containerd/containerd/v2/pkg/tracing" - "github.com/containerd/containerd/v2/platforms" "github.com/containerd/log" + "github.com/containerd/platforms" "github.com/opencontainers/go-digest" "github.com/opencontainers/image-spec/identity" ocispec "github.com/opencontainers/image-spec/specs-go/v1" diff --git a/platforms/compare.go b/platforms/compare.go deleted file mode 100644 index e874201ca445..000000000000 --- a/platforms/compare.go +++ /dev/null @@ -1,63 +0,0 @@ -/* - 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 platforms - -import ( - "github.com/containerd/platforms" - specs "github.com/opencontainers/image-spec/specs-go/v1" -) - -type MatchComparer = platforms.MatchComparer - -// Only returns a match comparer for a single platform -// using default resolution logic for the platform. -// -// For arm/v8, will also match arm/v7, arm/v6 and arm/v5 -// For arm/v7, will also match arm/v6 and arm/v5 -// For arm/v6, will also match arm/v5 -// For amd64, will also match 386 -func Only(platform specs.Platform) MatchComparer { - return platforms.Only(platform) -} - -// OnlyStrict returns a match comparer for a single platform. -// -// Unlike Only, OnlyStrict does not match sub platforms. -// So, "arm/vN" will not match "arm/vM" where M < N, -// and "amd64" will not also match "386". -// -// OnlyStrict matches non-canonical forms. -// So, "arm64" matches "arm/64/v8". -func OnlyStrict(platform specs.Platform) MatchComparer { - return platforms.OnlyStrict(platform) -} - -// Ordered returns a platform MatchComparer which matches any of the platforms -// but orders them in order they are provided. -func Ordered(ps ...specs.Platform) MatchComparer { - return platforms.Ordered(ps...) -} - -// Any returns a platform MatchComparer which matches any of the platforms -// with no preference for ordering. -func Any(ps ...specs.Platform) MatchComparer { - return platforms.Any(ps...) -} - -// All is a platform MatchComparer which matches all platforms -// with preference for ordering. -var All = platforms.All diff --git a/platforms/defaults.go b/platforms/defaults.go deleted file mode 100644 index ab2a89990ddd..000000000000 --- a/platforms/defaults.go +++ /dev/null @@ -1,42 +0,0 @@ -/* - 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 platforms - -import ( - "github.com/containerd/platforms" - specs "github.com/opencontainers/image-spec/specs-go/v1" -) - -// DefaultString returns the default string specifier for the platform. -func DefaultString() string { - return platforms.DefaultString() -} - -// DefaultStrict returns strict form of Default. -func DefaultStrict() MatchComparer { - return platforms.DefaultStrict() -} - -// DefaultSpec returns the current platform's default platform specification. -func DefaultSpec() specs.Platform { - return platforms.DefaultSpec() -} - -// Default returns the default matcher for the platform. -func Default() MatchComparer { - return platforms.Default() -} diff --git a/platforms/platforms.go b/platforms/platforms.go deleted file mode 100644 index 55070a7784f1..000000000000 --- a/platforms/platforms.go +++ /dev/null @@ -1,173 +0,0 @@ -/* - 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 platforms provides a toolkit for normalizing, matching and -// specifying container platforms. -// -// Centered around OCI platform specifications, we define a string-based -// specifier syntax that can be used for user input. With a specifier, users -// only need to specify the parts of the platform that are relevant to their -// context, providing an operating system or architecture or both. -// -// How do I use this package? -// -// The vast majority of use cases should simply use the match function with -// user input. The first step is to parse a specifier into a matcher: -// -// m, err := Parse("linux") -// if err != nil { ... } -// -// Once you have a matcher, use it to match against the platform declared by a -// component, typically from an image or runtime. Since extracting an images -// platform is a little more involved, we'll use an example against the -// platform default: -// -// if ok := m.Match(Default()); !ok { /* doesn't match */ } -// -// This can be composed in loops for resolving runtimes or used as a filter for -// fetch and select images. -// -// More details of the specifier syntax and platform spec follow. -// -// # Declaring Platform Support -// -// Components that have strict platform requirements should use the OCI -// platform specification to declare their support. Typically, this will be -// images and runtimes that should make these declaring which platform they -// support specifically. This looks roughly as follows: -// -// type Platform struct { -// Architecture string -// OS string -// Variant string -// } -// -// Most images and runtimes should at least set Architecture and OS, according -// to their GOARCH and GOOS values, respectively (follow the OCI image -// specification when in doubt). ARM should set variant under certain -// discussions, which are outlined below. -// -// # Platform Specifiers -// -// While the OCI platform specifications provide a tool for components to -// specify structured information, user input typically doesn't need the full -// context and much can be inferred. To solve this problem, we introduced -// "specifiers". A specifier has the format -// `||/[/]`. The user can provide either the -// operating system or the architecture or both. -// -// An example of a common specifier is `linux/amd64`. If the host has a default -// of runtime that matches this, the user can simply provide the component that -// matters. For example, if a image provides amd64 and arm64 support, the -// operating system, `linux` can be inferred, so they only have to provide -// `arm64` or `amd64`. Similar behavior is implemented for operating systems, -// where the architecture may be known but a runtime may support images from -// different operating systems. -// -// # Normalization -// -// Because not all users are familiar with the way the Go runtime represents -// platforms, several normalizations have been provided to make this package -// easier to user. -// -// The following are performed for architectures: -// -// Value Normalized -// aarch64 arm64 -// armhf arm -// armel arm/v6 -// i386 386 -// x86_64 amd64 -// x86-64 amd64 -// -// We also normalize the operating system `macos` to `darwin`. -// -// # ARM Support -// -// To qualify ARM architecture, the Variant field is used to qualify the arm -// version. The most common arm version, v7, is represented without the variant -// unless it is explicitly provided. This is treated as equivalent to armhf. A -// previous architecture, armel, will be normalized to arm/v6. -// -// Similarly, the most common arm64 version v8, and most common amd64 version v1 -// are represented without the variant. -// -// While these normalizations are provided, their support on arm platforms has -// not yet been fully implemented and tested. -package platforms - -import ( - "github.com/containerd/platforms" - specs "github.com/opencontainers/image-spec/specs-go/v1" -) - -// Platform is a type alias for convenience, so there is no need to import image-spec package everywhere. -type Platform = platforms.Platform - -// Matcher matches platforms specifications, provided by an image or runtime. -type Matcher = platforms.Matcher - -// NewMatcher returns a simple matcher based on the provided platform -// specification. The returned matcher only looks for equality based on os, -// architecture and variant. -// -// One may implement their own matcher if this doesn't provide the required -// functionality. -// -// Applications should opt to use `Match` over directly parsing specifiers. -func NewMatcher(platform specs.Platform) Matcher { - return platforms.NewMatcher(platform) -} - -// ParseAll parses a list of platform specifiers into a list of platform. -func ParseAll(specifiers []string) ([]specs.Platform, error) { - return platforms.ParseAll(specifiers) -} - -// Parse parses the platform specifier syntax into a platform declaration. -// -// Platform specifiers are in the format `||/[/]`. -// The minimum required information for a platform specifier is the operating -// system or architecture. If there is only a single string (no slashes), the -// value will be matched against the known set of operating systems, then fall -// back to the known set of architectures. The missing component will be -// inferred based on the local environment. -func Parse(specifier string) (specs.Platform, error) { - return platforms.Parse(specifier) -} - -// MustParse is like Parses but panics if the specifier cannot be parsed. -// Simplifies initialization of global variables. -func MustParse(specifier string) specs.Platform { - return platforms.MustParse(specifier) -} - -// Format returns a string specifier from the provided platform specification. -func Format(platform specs.Platform) string { - return platforms.Format(platform) -} - -// Normalize validates and translate the platform to the canonical value. -// -// For example, if "Aarch64" is encountered, we change it to "arm64" or if -// "x86_64" is encountered, it becomes "amd64". -func Normalize(platform specs.Platform) specs.Platform { - return platforms.Normalize(platform) -} - -func GetWindowsOsVersion() string { - return platforms.GetWindowsOsVersion() -} diff --git a/plugins/cri/images/plugin.go b/plugins/cri/images/plugin.go index eb0c98d9d1f5..c297a28a5af4 100644 --- a/plugins/cri/images/plugin.go +++ b/plugins/cri/images/plugin.go @@ -29,9 +29,9 @@ import ( "github.com/containerd/containerd/v2/pkg/cri/constants" "github.com/containerd/containerd/v2/pkg/cri/server/images" "github.com/containerd/containerd/v2/pkg/events" - "github.com/containerd/containerd/v2/platforms" "github.com/containerd/containerd/v2/plugins" "github.com/containerd/log" + "github.com/containerd/platforms" "github.com/containerd/plugin" "github.com/containerd/plugin/registry" ) diff --git a/plugins/diff/walking/plugin/plugin.go b/plugins/diff/walking/plugin/plugin.go index cc2f27ccd077..0c5ce67d0b1d 100644 --- a/plugins/diff/walking/plugin/plugin.go +++ b/plugins/diff/walking/plugin/plugin.go @@ -20,9 +20,9 @@ import ( "github.com/containerd/containerd/v2/core/diff" "github.com/containerd/containerd/v2/core/diff/apply" "github.com/containerd/containerd/v2/core/metadata" - "github.com/containerd/containerd/v2/platforms" "github.com/containerd/containerd/v2/plugins" "github.com/containerd/containerd/v2/plugins/diff/walking" + "github.com/containerd/platforms" "github.com/containerd/plugin" "github.com/containerd/plugin/registry" ) diff --git a/plugins/diff/windows/cimfs.go b/plugins/diff/windows/cimfs.go index 5a14abe06cb9..d4985ce0d7b5 100644 --- a/plugins/diff/windows/cimfs.go +++ b/plugins/diff/windows/cimfs.go @@ -30,8 +30,8 @@ import ( "github.com/containerd/containerd/v2/core/mount" "github.com/containerd/containerd/v2/pkg/archive" "github.com/containerd/containerd/v2/pkg/errdefs" - "github.com/containerd/containerd/v2/platforms" "github.com/containerd/containerd/v2/plugins" + "github.com/containerd/platforms" "github.com/containerd/plugin" "github.com/containerd/plugin/registry" ocispec "github.com/opencontainers/image-spec/specs-go/v1" diff --git a/plugins/diff/windows/windows.go b/plugins/diff/windows/windows.go index bdb68c8593d9..d9fec7e1207f 100644 --- a/plugins/diff/windows/windows.go +++ b/plugins/diff/windows/windows.go @@ -37,9 +37,9 @@ import ( "github.com/containerd/containerd/v2/pkg/epoch" "github.com/containerd/containerd/v2/pkg/errdefs" "github.com/containerd/containerd/v2/pkg/labels" - "github.com/containerd/containerd/v2/platforms" "github.com/containerd/containerd/v2/plugins" "github.com/containerd/log" + "github.com/containerd/platforms" "github.com/containerd/plugin" "github.com/containerd/plugin/registry" "github.com/opencontainers/go-digest" diff --git a/plugins/sandbox/controller.go b/plugins/sandbox/controller.go index 3b909490f444..1020bff73a2d 100644 --- a/plugins/sandbox/controller.go +++ b/plugins/sandbox/controller.go @@ -30,9 +30,9 @@ import ( "github.com/containerd/containerd/v2/pkg/errdefs" "github.com/containerd/containerd/v2/pkg/events" "github.com/containerd/containerd/v2/pkg/events/exchange" - "github.com/containerd/containerd/v2/platforms" "github.com/containerd/containerd/v2/plugins" "github.com/containerd/log" + "github.com/containerd/platforms" "github.com/containerd/plugin" "github.com/containerd/plugin/registry" diff --git a/plugins/snapshots/blockfile/plugin/plugin.go b/plugins/snapshots/blockfile/plugin/plugin.go index a855efd18993..69ee9ca6fc69 100644 --- a/plugins/snapshots/blockfile/plugin/plugin.go +++ b/plugins/snapshots/blockfile/plugin/plugin.go @@ -19,9 +19,9 @@ package plugin import ( "errors" - "github.com/containerd/containerd/v2/platforms" "github.com/containerd/containerd/v2/plugins" "github.com/containerd/containerd/v2/plugins/snapshots/blockfile" + "github.com/containerd/platforms" "github.com/containerd/plugin" "github.com/containerd/plugin/registry" ) diff --git a/plugins/snapshots/btrfs/plugin/plugin.go b/plugins/snapshots/btrfs/plugin/plugin.go index bf3ed588671d..3dfea38c5683 100644 --- a/plugins/snapshots/btrfs/plugin/plugin.go +++ b/plugins/snapshots/btrfs/plugin/plugin.go @@ -23,9 +23,9 @@ import ( ocispec "github.com/opencontainers/image-spec/specs-go/v1" - "github.com/containerd/containerd/v2/platforms" "github.com/containerd/containerd/v2/plugins" "github.com/containerd/containerd/v2/plugins/snapshots/btrfs" + "github.com/containerd/platforms" "github.com/containerd/plugin" "github.com/containerd/plugin/registry" ) diff --git a/plugins/snapshots/devmapper/plugin/plugin.go b/plugins/snapshots/devmapper/plugin/plugin.go index fd4d5f70b438..143916c080a1 100644 --- a/plugins/snapshots/devmapper/plugin/plugin.go +++ b/plugins/snapshots/devmapper/plugin/plugin.go @@ -22,9 +22,9 @@ import ( "errors" "fmt" - "github.com/containerd/containerd/v2/platforms" "github.com/containerd/containerd/v2/plugins" "github.com/containerd/containerd/v2/plugins/snapshots/devmapper" + "github.com/containerd/platforms" "github.com/containerd/plugin" "github.com/containerd/plugin/registry" ) diff --git a/plugins/snapshots/native/plugin/plugin.go b/plugins/snapshots/native/plugin/plugin.go index e68367b8d4e8..816735912ca7 100644 --- a/plugins/snapshots/native/plugin/plugin.go +++ b/plugins/snapshots/native/plugin/plugin.go @@ -19,9 +19,9 @@ package plugin import ( "errors" - "github.com/containerd/containerd/v2/platforms" "github.com/containerd/containerd/v2/plugins" "github.com/containerd/containerd/v2/plugins/snapshots/native" + "github.com/containerd/platforms" "github.com/containerd/plugin" "github.com/containerd/plugin/registry" ) diff --git a/plugins/snapshots/overlay/plugin/plugin.go b/plugins/snapshots/overlay/plugin/plugin.go index 7cffb971cb2a..1ea5d54133b1 100644 --- a/plugins/snapshots/overlay/plugin/plugin.go +++ b/plugins/snapshots/overlay/plugin/plugin.go @@ -21,10 +21,10 @@ package overlay import ( "errors" - "github.com/containerd/containerd/v2/platforms" "github.com/containerd/containerd/v2/plugins" "github.com/containerd/containerd/v2/plugins/snapshots/overlay" "github.com/containerd/containerd/v2/plugins/snapshots/overlay/overlayutils" + "github.com/containerd/platforms" "github.com/containerd/plugin" "github.com/containerd/plugin/registry" ) diff --git a/plugins/snapshots/windows/cimfs.go b/plugins/snapshots/windows/cimfs.go index a4e2bbe9cf6c..f60214267a5b 100644 --- a/plugins/snapshots/windows/cimfs.go +++ b/plugins/snapshots/windows/cimfs.go @@ -33,9 +33,9 @@ import ( "github.com/containerd/containerd/v2/core/snapshots" "github.com/containerd/containerd/v2/core/snapshots/storage" "github.com/containerd/containerd/v2/pkg/errdefs" - "github.com/containerd/containerd/v2/platforms" "github.com/containerd/containerd/v2/plugins" "github.com/containerd/log" + "github.com/containerd/platforms" "github.com/containerd/plugin" "github.com/containerd/plugin/registry" ocispec "github.com/opencontainers/image-spec/specs-go/v1" diff --git a/plugins/snapshots/windows/windows.go b/plugins/snapshots/windows/windows.go index d59496c34069..87588eac23f1 100644 --- a/plugins/snapshots/windows/windows.go +++ b/plugins/snapshots/windows/windows.go @@ -33,10 +33,10 @@ import ( "github.com/containerd/containerd/v2/core/snapshots" "github.com/containerd/containerd/v2/core/snapshots/storage" "github.com/containerd/containerd/v2/pkg/errdefs" - "github.com/containerd/containerd/v2/platforms" "github.com/containerd/containerd/v2/plugins" "github.com/containerd/continuity/fs" "github.com/containerd/log" + "github.com/containerd/platforms" "github.com/containerd/plugin" "github.com/containerd/plugin/registry" ocispec "github.com/opencontainers/image-spec/specs-go/v1" diff --git a/plugins/transfer/plugin.go b/plugins/transfer/plugin.go index 38972b29a0f5..ea78f8613316 100644 --- a/plugins/transfer/plugin.go +++ b/plugins/transfer/plugin.go @@ -27,9 +27,9 @@ import ( "github.com/containerd/containerd/v2/pkg/imageverifier" "github.com/containerd/containerd/v2/pkg/transfer/local" "github.com/containerd/containerd/v2/pkg/unpack" - "github.com/containerd/containerd/v2/platforms" "github.com/containerd/containerd/v2/plugins" "github.com/containerd/log" + "github.com/containerd/platforms" "github.com/containerd/plugin" "github.com/containerd/plugin/registry"