2018-03-19 18:57:30 -04:00
|
|
|
/*
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
2018-09-13 12:32:19 -04:00
|
|
|
package platforms
|
2018-03-19 18:57:30 -04:00
|
|
|
|
|
|
|
import (
|
2021-06-21 11:08:47 -04:00
|
|
|
"fmt"
|
|
|
|
"runtime"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
|
2018-09-13 12:32:19 -04:00
|
|
|
specs "github.com/opencontainers/image-spec/specs-go/v1"
|
2021-06-21 11:08:47 -04:00
|
|
|
"golang.org/x/sys/windows"
|
2018-03-19 18:57:30 -04:00
|
|
|
)
|
|
|
|
|
2022-03-24 08:26:26 -04:00
|
|
|
// DefaultSpec returns the current platform's default platform specification.
|
|
|
|
func DefaultSpec() specs.Platform {
|
|
|
|
major, minor, build := windows.RtlGetNtVersionNumbers()
|
|
|
|
return specs.Platform{
|
|
|
|
OS: runtime.GOOS,
|
|
|
|
Architecture: runtime.GOARCH,
|
|
|
|
OSVersion: fmt.Sprintf("%d.%d.%d", major, minor, build),
|
|
|
|
// The Variant field will be empty if arch != ARM.
|
|
|
|
Variant: cpuVariant(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-13 15:39:50 -04:00
|
|
|
type windowsmatcher struct {
|
|
|
|
specs.Platform
|
2021-06-21 11:08:47 -04:00
|
|
|
osVersionPrefix string
|
2023-10-13 15:39:50 -04:00
|
|
|
defaultMatcher Matcher
|
2021-06-21 11:08:47 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Match matches platform with the same windows major, minor
|
|
|
|
// and build version.
|
2023-10-13 15:39:50 -04:00
|
|
|
func (m windowsmatcher) Match(p specs.Platform) bool {
|
|
|
|
match := m.defaultMatcher.Match(p)
|
2023-03-14 19:31:48 -04:00
|
|
|
|
2023-10-13 15:39:50 -04:00
|
|
|
if match && m.OS == "windows" {
|
2023-09-19 10:42:32 -04:00
|
|
|
// HPC containers do not have OS version filled
|
2024-06-11 18:57:47 -04:00
|
|
|
if m.OSVersion == "" || p.OSVersion == "" {
|
2023-03-14 19:31:48 -04:00
|
|
|
return true
|
|
|
|
}
|
2023-09-19 10:42:32 -04:00
|
|
|
|
2024-06-11 18:57:47 -04:00
|
|
|
hostOsVersion := getOSVersion(m.osVersionPrefix)
|
|
|
|
ctrOsVersion := getOSVersion(p.OSVersion)
|
|
|
|
return checkHostAndContainerCompat(hostOsVersion, ctrOsVersion)
|
2021-06-21 11:08:47 -04:00
|
|
|
}
|
2023-10-13 15:39:50 -04:00
|
|
|
|
|
|
|
return match
|
2021-06-21 11:08:47 -04:00
|
|
|
}
|
|
|
|
|
2024-06-11 18:57:47 -04:00
|
|
|
func getOSVersion(osVersionPrefix string) osVersion {
|
2023-09-19 10:42:32 -04:00
|
|
|
parts := strings.Split(osVersionPrefix, ".")
|
|
|
|
if len(parts) < 3 {
|
2024-06-11 18:57:47 -04:00
|
|
|
return osVersion{}
|
2023-09-19 10:42:32 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
majorVersion, _ := strconv.Atoi(parts[0])
|
|
|
|
minorVersion, _ := strconv.Atoi(parts[1])
|
|
|
|
buildNumber, _ := strconv.Atoi(parts[2])
|
|
|
|
|
2024-06-11 18:57:47 -04:00
|
|
|
return osVersion{
|
2023-09-19 10:42:32 -04:00
|
|
|
MajorVersion: uint8(majorVersion),
|
|
|
|
MinorVersion: uint8(minorVersion),
|
|
|
|
Build: uint16(buildNumber),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-21 11:08:47 -04:00
|
|
|
// Less sorts matched platforms in front of other platforms.
|
|
|
|
// For matched platforms, it puts platforms with larger revision
|
|
|
|
// number in front.
|
2023-10-13 15:39:50 -04:00
|
|
|
func (m windowsmatcher) Less(p1, p2 specs.Platform) bool {
|
2021-06-21 11:08:47 -04:00
|
|
|
m1, m2 := m.Match(p1), m.Match(p2)
|
|
|
|
if m1 && m2 {
|
|
|
|
r1, r2 := revision(p1.OSVersion), revision(p2.OSVersion)
|
|
|
|
return r1 > r2
|
|
|
|
}
|
|
|
|
return m1 && !m2
|
|
|
|
}
|
|
|
|
|
|
|
|
func revision(v string) int {
|
|
|
|
parts := strings.Split(v, ".")
|
|
|
|
if len(parts) < 4 {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
r, err := strconv.Atoi(parts[3])
|
|
|
|
if err != nil {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
2023-10-13 15:39:50 -04:00
|
|
|
func prefix(v string) string {
|
|
|
|
parts := strings.Split(v, ".")
|
|
|
|
if len(parts) < 4 {
|
|
|
|
return v
|
|
|
|
}
|
|
|
|
return strings.Join(parts[0:3], ".")
|
|
|
|
}
|
|
|
|
|
2021-06-21 11:08:47 -04:00
|
|
|
// Default returns the current platform's default platform specification.
|
2018-09-13 12:32:19 -04:00
|
|
|
func Default() MatchComparer {
|
2023-10-13 15:39:50 -04:00
|
|
|
return Only(DefaultSpec())
|
2018-03-19 18:57:30 -04:00
|
|
|
}
|