diff --git a/go.mod b/go.mod index 952f578229..79d18c33ee 100644 --- a/go.mod +++ b/go.mod @@ -6,7 +6,7 @@ require ( github.com/alecthomas/kong v1.14.0 github.com/containerd/containerd/v2 v2.2.2 github.com/containerd/continuity v0.4.5 - github.com/containerd/platforms v1.0.0-rc.2 + github.com/containerd/platforms v1.0.0-rc.4 github.com/docker/cli v29.3.1+incompatible github.com/go-echarts/go-echarts/v2 v2.7.1 github.com/gofrs/flock v0.13.0 diff --git a/go.sum b/go.sum index 05c39d8f09..59c05966de 100644 --- a/go.sum +++ b/go.sum @@ -28,8 +28,8 @@ github.com/containerd/errdefs/pkg v0.3.0 h1:9IKJ06FvyNlexW690DXuQNx2KA2cUJXx151X github.com/containerd/errdefs/pkg v0.3.0/go.mod h1:NJw6s9HwNuRhnjJhM7pylWwMyAkmCQvQ4GpJHEqRLVk= 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/platforms v1.0.0-rc.2 h1:0SPgaNZPVWGEi4grZdV8VRYQn78y+nm6acgLGv/QzE4= -github.com/containerd/platforms v1.0.0-rc.2/go.mod h1:J71L7B+aiM5SdIEqmd9wp6THLVRzJGXfNuWCZCllLA4= +github.com/containerd/platforms v1.0.0-rc.4 h1:M42JrUT4zfZTqtkUwkr0GzmUWbfyO5VO0Q5b3op97T4= +github.com/containerd/platforms v1.0.0-rc.4/go.mod h1:lKlMXyLybmBedS/JJm11uDofzI8L2v0J2ZbYvNsbq1A= github.com/containerd/typeurl/v2 v2.2.3 h1:yNA/94zxWdvYACdYO8zofhrTVuQY73fFU1y++dYSw40= github.com/containerd/typeurl/v2 v2.2.3/go.mod h1:95ljDnPfD3bAbDJRugOiShd/DlAAsxGtUBhJxIn7SCk= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= diff --git a/vendor/github.com/containerd/platforms/.golangci.yml b/vendor/github.com/containerd/platforms/.golangci.yml index d574fe11d7..9320503c7b 100644 --- a/vendor/github.com/containerd/platforms/.golangci.yml +++ b/vendor/github.com/containerd/platforms/.golangci.yml @@ -1,32 +1,25 @@ +version: "2" linters: enable: - copyloopvar - - gofmt - - goimports + - dupword - gosec - - ineffassign - misspell - nolintlint - revive - - staticcheck - - tenv # Detects using os.Setenv instead of t.Setenv since Go 1.17 - unconvert - - unused - - govet - - dupword # Checks for duplicate words in the source code disable: - errcheck - -run: - timeout: 5m - -issues: - exclude-dirs: - - api - - cluster - - design - - docs - - docs/man - - releases - - reports - - test # e2e scripts + exclusions: + generated: lax + presets: + - comments + - common-false-positives + - legacy + - std-error-handling +formatters: + enable: + - gofmt + - goimports + exclusions: + generated: lax diff --git a/vendor/github.com/containerd/platforms/compare.go b/vendor/github.com/containerd/platforms/compare.go index 24403f3b3d..ea5e7aa14b 100644 --- a/vendor/github.com/containerd/platforms/compare.go +++ b/vendor/github.com/containerd/platforms/compare.go @@ -152,6 +152,88 @@ func Only(platform specs.Platform) MatchComparer { return Ordered(platformVector(Normalize(platform))...) } +// OnlyOS returns a match comparer that matches only platforms with the same +// OS, OS version, and OS features, regardless of architecture. When comparing, +// it always ranks the best architecture match highest using the default +// platform resolution logic. +func OnlyOS(platform specs.Platform) MatchComparer { + normalized := Normalize(platform) + return onlyOSComparer{ + platform: normalized, + osvM: newOSVersionMatcher(normalized), + archOrder: orderedPlatformComparer{ + matchers: []Matcher{NewMatcher(normalized)}, + }, + } +} + +func newOSVersionMatcher(platform specs.Platform) osVerMatcher { + if platform.OS == "windows" { + return &windowsVersionMatcher{ + windowsOSVersion: getWindowsOSVersion(platform.OSVersion), + } + } + return nil +} + +type onlyOSComparer struct { + platform specs.Platform + osvM osVerMatcher + archOrder orderedPlatformComparer +} + +func (c onlyOSComparer) matchOS(platform specs.Platform) bool { + normalized := Normalize(platform) + if c.platform.OS != normalized.OS { + return false + } + if c.osvM != nil { + if !c.osvM.Match(platform.OSVersion) { + return false + } + } + if len(normalized.OSFeatures) > 0 { + if len(c.platform.OSFeatures) < len(normalized.OSFeatures) { + return false + } + j := 0 + for _, feature := range normalized.OSFeatures { + found := false + for ; j < len(c.platform.OSFeatures); j++ { + if feature == c.platform.OSFeatures[j] { + found = true + j++ + break + } + if feature < c.platform.OSFeatures[j] { + return false + } + } + if !found { + return false + } + } + } + return true +} + +func (c onlyOSComparer) Match(platform specs.Platform) bool { + return c.matchOS(platform) +} + +func (c onlyOSComparer) Less(p1, p2 specs.Platform) bool { + p1m := c.matchOS(p1) + p2m := c.matchOS(p2) + if p1m && !p2m { + return true + } + if !p1m { + return false + } + // Both match — rank by architecture preference + return c.archOrder.Less(p1, p2) +} + // OnlyStrict returns a match comparer for a single platform. // // Unlike Only, OnlyStrict does not match sub platforms. @@ -213,9 +295,20 @@ func (c orderedPlatformComparer) Less(p1 specs.Platform, p2 specs.Platform) bool return true } if p1m || p2m { + if p1m && p2m { + // Prefer one with most matching features + if len(p1.OSFeatures) != len(p2.OSFeatures) { + return len(p1.OSFeatures) > len(p2.OSFeatures) + } + } return false } } + if len(p1.OSFeatures) > 0 || len(p2.OSFeatures) > 0 { + p1.OSFeatures = nil + p2.OSFeatures = nil + return c.Less(p1, p2) + } return false } @@ -242,9 +335,20 @@ func (c anyPlatformComparer) Less(p1, p2 specs.Platform) bool { p2m = true } if p1m && p2m { - return false + if len(p1.OSFeatures) != len(p2.OSFeatures) { + return len(p1.OSFeatures) > len(p2.OSFeatures) + } + break } } + + // If neither match and has features, strip features and compare + if !p1m && !p2m && (len(p1.OSFeatures) > 0 || len(p2.OSFeatures) > 0) { + p1.OSFeatures = nil + p2.OSFeatures = nil + return c.Less(p1, p2) + } + // If one matches, and the other does, sort match first return p1m && !p2m } diff --git a/vendor/github.com/containerd/platforms/cpuinfo_linux.go b/vendor/github.com/containerd/platforms/cpuinfo_linux.go index 98c7001f93..06da8b9612 100644 --- a/vendor/github.com/containerd/platforms/cpuinfo_linux.go +++ b/vendor/github.com/containerd/platforms/cpuinfo_linux.go @@ -45,7 +45,6 @@ func getMachineArch() (string, error) { // So we don't need to access the ARM registers to detect platform information // by ourselves. We can just parse these information from /proc/cpuinfo func getCPUInfo(pattern string) (info string, err error) { - cpuinfo, err := os.Open("/proc/cpuinfo") if err != nil { return "", err @@ -75,7 +74,6 @@ func getCPUInfo(pattern string) (info string, err error) { // getCPUVariantFromArch get CPU variant from arch through a system call func getCPUVariantFromArch(arch string) (string, error) { - var variant string arch = strings.ToLower(arch) diff --git a/vendor/github.com/containerd/platforms/cpuinfo_other.go b/vendor/github.com/containerd/platforms/cpuinfo_other.go index 97a1fe8a3e..b8c7a4b226 100644 --- a/vendor/github.com/containerd/platforms/cpuinfo_other.go +++ b/vendor/github.com/containerd/platforms/cpuinfo_other.go @@ -24,10 +24,10 @@ import ( ) func getCPUVariant() (string, error) { - var variant string - if runtime.GOOS == "windows" || runtime.GOOS == "darwin" { + switch runtime.GOOS { + case "windows", "darwin": // Windows/Darwin only supports v7 for ARM32 and v8 for ARM64 and so we can use // runtime.GOARCH to determine the variants switch runtime.GOARCH { @@ -38,7 +38,7 @@ func getCPUVariant() (string, error) { default: variant = "unknown" } - } else if runtime.GOOS == "freebsd" { + case "freebsd": // FreeBSD supports ARMv6 and ARMv7 as well as ARMv4 and ARMv5 (though deprecated) // detecting those variants is currently unimplemented switch runtime.GOARCH { @@ -47,7 +47,7 @@ func getCPUVariant() (string, error) { default: variant = "unknown" } - } else { + default: return "", fmt.Errorf("getCPUVariant for OS %s: %v", runtime.GOOS, errNotImplemented) } diff --git a/vendor/github.com/containerd/platforms/platform_windows_compat.go b/vendor/github.com/containerd/platforms/platform_windows_compat.go index f31ebe0c9e..ef21a29068 100644 --- a/vendor/github.com/containerd/platforms/platform_windows_compat.go +++ b/vendor/github.com/containerd/platforms/platform_windows_compat.go @@ -17,6 +17,7 @@ package platforms import ( + "slices" "strconv" "strings" @@ -162,3 +163,14 @@ func (c *windowsMatchComparer) Less(p1, p2 specs.Platform) bool { } return m1 && !m2 } + +type windowsStripFeaturesMatcher struct { + Matcher +} + +func (m windowsStripFeaturesMatcher) Match(p specs.Platform) bool { + if i := slices.Index(p.OSFeatures, "win32k"); i >= 0 { + p.OSFeatures = slices.Delete(slices.Clone(p.OSFeatures), i, i+1) + } + return m.Matcher.Match(p) +} diff --git a/vendor/github.com/containerd/platforms/platforms.go b/vendor/github.com/containerd/platforms/platforms.go index 14d65abd4f..81d7ee3869 100644 --- a/vendor/github.com/containerd/platforms/platforms.go +++ b/vendor/github.com/containerd/platforms/platforms.go @@ -111,9 +111,11 @@ package platforms import ( "fmt" + "net/url" "path" "regexp" "runtime" + "slices" "strconv" "strings" @@ -121,12 +123,10 @@ import ( ) var ( - specifierRe = regexp.MustCompile(`^[A-Za-z0-9_.-]+$`) - osAndVersionRe = regexp.MustCompile(`^([A-Za-z0-9_-]+)(?:\(([A-Za-z0-9_.-]*)\))?$`) + specifierRe = regexp.MustCompile(`^[A-Za-z0-9_.-]+$`) + osRe = regexp.MustCompile(`^([A-Za-z0-9_-]+)(?:\(([A-Za-z0-9_.%-]*)((?:\+[A-Za-z0-9_.%-]+)*)\))?$`) ) -const osAndVersionFormat = "%s(%s)" - // Platform is a type alias for convenience, so there is no need to import image-spec package everywhere. type Platform = specs.Platform @@ -143,6 +143,10 @@ type Matcher interface { // functionality. // // Applications should opt to use `Match` over directly parsing specifiers. +// +// For OSFeatures, this matcher will match if the platform to match has +// OSFeatures which are a subset of the OSFeatures of the platform +// provided to NewMatcher. func NewMatcher(platform specs.Platform) Matcher { m := &matcher{ Platform: Normalize(platform), @@ -152,6 +156,11 @@ func NewMatcher(platform specs.Platform) Matcher { m.osvM = &windowsVersionMatcher{ windowsOSVersion: getWindowsOSVersion(platform.OSVersion), } + + // In prior versions, the win32k os feature was not considered for matching, + // strip out the win32k feature for comparison + var stripped Matcher = windowsStripFeaturesMatcher{m} + // In prior versions, on windows, the returned matcher implements a // MatchComprarer interface. // This preserves that behavior for backwards compatibility. @@ -161,8 +170,9 @@ func NewMatcher(platform specs.Platform) Matcher { // It was likely intended to be used in `Ordered` but it is not since // `Less` that is implemented here ends up getting masked due to wrapping. if runtime.GOOS == "windows" { - return &windowsMatchComparer{m} + return &windowsMatchComparer{stripped} } + return stripped } return m } @@ -178,10 +188,39 @@ type matcher struct { func (m *matcher) Match(platform specs.Platform) bool { normalized := Normalize(platform) - return m.OS == normalized.OS && + if m.OS == normalized.OS && m.Architecture == normalized.Architecture && m.Variant == normalized.Variant && - m.matchOSVersion(platform) + m.matchOSVersion(platform) { + if len(normalized.OSFeatures) == 0 { + return true + } + if len(m.OSFeatures) >= len(normalized.OSFeatures) { + // Ensure that normalized.OSFeatures is a subset of + // m.OSFeatures + j := 0 + for _, feature := range normalized.OSFeatures { + found := false + for ; j < len(m.OSFeatures); j++ { + if feature == m.OSFeatures[j] { + found = true + j++ + break + } + // Since both lists are ordered, if the feature is less + // than what is seen, it is not in the list + if feature < m.OSFeatures[j] { + return false + } + } + if !found { + return false + } + } + return true + } + } + return false } func (m *matcher) matchOSVersion(platform specs.Platform) bool { @@ -210,11 +249,14 @@ func ParseAll(specifiers []string) ([]specs.Platform, error) { // Parse parses the platform specifier syntax into a platform declaration. // -// Platform specifiers are in the format `[()]||[()]/[/]`. +// Platform specifiers are in the format `[()]||[()]/[/]`. // The minimum required information for a platform specifier is the operating -// system or architecture. The OSVersion can be part of the OS like `windows(10.0.17763)` -// When an OSVersion is specified, then specs.Platform.OSVersion is populated with that value, -// and an empty string otherwise. +// system or architecture. The "os options" may be OSVersion which can be part of the OS +// like `windows(10.0.17763)`. When an OSVersion is specified, then specs.Platform.OSVersion is +// populated with that value, and an empty string otherwise. The "os options" may also include an +// array of OSFeatures, each feature prefixed with '+', without any other separator, and provided +// after the OSVersion when the OSVersion is specified. An "os options" with version and features +// is like `windows(10.0.17763+win32k)`. // 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 @@ -231,14 +273,24 @@ func Parse(specifier string) (specs.Platform, error) { var p specs.Platform for i, part := range parts { if i == 0 { - // First element is [()] - osVer := osAndVersionRe.FindStringSubmatch(part) - if osVer == nil { - return specs.Platform{}, fmt.Errorf("%q is an invalid OS component of %q: OSAndVersion specifier component must match %q: %w", part, specifier, osAndVersionRe.String(), errInvalidArgument) + // First element is [([+]*)] + osOptions := osRe.FindStringSubmatch(part) + if osOptions == nil { + return specs.Platform{}, fmt.Errorf("%q is an invalid OS component of %q: OSAndVersion specifier component must match %q: %w", part, specifier, osRe.String(), errInvalidArgument) } - p.OS = normalizeOS(osVer[1]) - p.OSVersion = osVer[2] + p.OS = normalizeOS(osOptions[1]) + osVersion, err := decodeOSOption(osOptions[2]) + if err != nil { + return specs.Platform{}, fmt.Errorf("%q has an invalid OS version %q: %w", specifier, osOptions[2], err) + } + p.OSVersion = osVersion + if osOptions[3] != "" { + p.OSFeatures, err = parseOSFeatures(osOptions[3][1:]) + if err != nil { + return specs.Platform{}, fmt.Errorf("%q has invalid OS features: %w", specifier, err) + } + } } else { 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) @@ -296,6 +348,30 @@ func Parse(specifier string) (specs.Platform, error) { return specs.Platform{}, fmt.Errorf("%q: cannot parse platform specifier: %w", specifier, errInvalidArgument) } +func parseOSFeatures(s string) ([]string, error) { + if s == "" { + return nil, nil + } + + var features []string + for raw := range strings.SplitSeq(s, "+") { + raw = strings.TrimSpace(raw) + if raw == "" { + return nil, fmt.Errorf("empty os feature: %w", errInvalidArgument) + } + feature, err := decodeOSOption(raw) + if err != nil { + return nil, fmt.Errorf("invalid os feature %q: %w", raw, err) + } + if feature == "" { + continue + } + features = append(features, feature) + } + + return features, nil +} + // MustParse is like Parses but panics if the specifier cannot be parsed. // Simplifies initialization of global variables. func MustParse(specifier string) specs.Platform { @@ -321,12 +397,77 @@ func FormatAll(platform specs.Platform) string { if platform.OS == "" { return "unknown" } + if platform.OSVersion == "" && len(platform.OSFeatures) == 0 { + return path.Join(platform.OS, platform.Architecture, platform.Variant) + } - if platform.OSVersion != "" { - OSAndVersion := fmt.Sprintf(osAndVersionFormat, platform.OS, platform.OSVersion) - return path.Join(OSAndVersion, platform.Architecture, platform.Variant) + var b strings.Builder + b.WriteString(platform.OS) + osv := encodeOSOption(platform.OSVersion) + formatted := formatOSFeatures(platform.OSFeatures) + if osv != "" || formatted != "" { + b.Grow(len(osv) + len(formatted) + 3) // parens + maybe '+' + b.WriteByte('(') + if osv != "" { + b.WriteString(osv) + } + if formatted != "" { + b.WriteByte('+') + b.WriteString(formatted) + } + b.WriteByte(')') } - return path.Join(platform.OS, platform.Architecture, platform.Variant) + + return path.Join(b.String(), platform.Architecture, platform.Variant) +} + +func formatOSFeatures(features []string) string { + if len(features) == 0 { + return "" + } + + if !slices.IsSorted(features) { + features = slices.Clone(features) + slices.Sort(features) + } + var b strings.Builder + var wrote bool + var prev string + for _, f := range features { + if f == "" || f == prev { + // skip empty and duplicate values + continue + } + prev = f + if wrote { + b.WriteByte('+') + } + b.WriteString(encodeOSOption(f)) + wrote = true + } + return b.String() +} + +// osOptionReplacer encodes characters in OS option values (version and +// features) that are ambiguous with the format syntax. The percent sign +// must be replaced first to avoid double-encoding. +var osOptionReplacer = strings.NewReplacer( + "%", "%25", + "+", "%2B", + "(", "%28", + ")", "%29", + "/", "%2F", +) + +func encodeOSOption(v string) string { + return osOptionReplacer.Replace(v) +} + +func decodeOSOption(v string) (string, error) { + if strings.Contains(v, "%") { + return url.PathUnescape(v) + } + return v, nil } // Normalize validates and translate the platform to the canonical value. @@ -336,6 +477,11 @@ func FormatAll(platform specs.Platform) string { func Normalize(platform specs.Platform) specs.Platform { platform.OS = normalizeOS(platform.OS) platform.Architecture, platform.Variant = normalizeArch(platform.Architecture, platform.Variant) + if len(platform.OSFeatures) > 0 { + platform.OSFeatures = slices.Clone(platform.OSFeatures) + slices.Sort(platform.OSFeatures) + platform.OSFeatures = slices.Compact(platform.OSFeatures) + } return platform } diff --git a/vendor/modules.txt b/vendor/modules.txt index 9f566ff83c..85a594e858 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -43,8 +43,8 @@ github.com/containerd/errdefs # github.com/containerd/log v0.1.0 ## explicit; go 1.20 github.com/containerd/log -# github.com/containerd/platforms v1.0.0-rc.2 -## explicit; go 1.20 +# github.com/containerd/platforms v1.0.0-rc.4 +## explicit; go 1.24 github.com/containerd/platforms # github.com/containerd/typeurl/v2 v2.2.3 ## explicit; go 1.21