From 5a36c05403b6d3f9b068919a8fa445558114ee60 Mon Sep 17 00:00:00 2001 From: Kirtana Ashok Date: Thu, 30 May 2024 11:01:35 -0700 Subject: [PATCH] Remove hcsshim import from repo Move windows CheckHostAndContainerCompat() function from hcsshim locally to platform repo itself Signed-off-by: Kirtana Ashok --- defaults_windows.go | 13 +++-- go.mod | 1 - go.sum | 2 - platform_compat_windows.go | 81 +++++++++++++++++++++++++++++++ platform_compat_windows_test.go | 84 +++++++++++++++++++++++++++++++++ 5 files changed, 171 insertions(+), 10 deletions(-) create mode 100644 platform_compat_windows.go create mode 100644 platform_compat_windows_test.go diff --git a/defaults_windows.go b/defaults_windows.go index d10fa90..974890e 100644 --- a/defaults_windows.go +++ b/defaults_windows.go @@ -22,7 +22,6 @@ import ( "strconv" "strings" - "github.com/Microsoft/hcsshim/osversion" specs "github.com/opencontainers/image-spec/specs-go/v1" "golang.org/x/sys/windows" ) @@ -56,25 +55,25 @@ func (m windowsmatcher) Match(p specs.Platform) bool { return true } - hostOsVersion := GetOsVersion(m.osVersionPrefix) - ctrOsVersion := GetOsVersion(p.OSVersion) - return osversion.CheckHostAndContainerCompat(hostOsVersion, ctrOsVersion) + hostOsVersion := getOsVersion(m.osVersionPrefix) + ctrOsVersion := getOsVersion(p.OSVersion) + return checkHostAndContainerCompat(hostOsVersion, ctrOsVersion) } return match } -func GetOsVersion(osVersionPrefix string) osversion.OSVersion { +func getOsVersion(osVersionPrefix string) osVersion { parts := strings.Split(osVersionPrefix, ".") if len(parts) < 3 { - return osversion.OSVersion{} + return osVersion{} } majorVersion, _ := strconv.Atoi(parts[0]) minorVersion, _ := strconv.Atoi(parts[1]) buildNumber, _ := strconv.Atoi(parts[2]) - return osversion.OSVersion{ + return osVersion{ MajorVersion: uint8(majorVersion), MinorVersion: uint8(minorVersion), Build: uint16(buildNumber), diff --git a/go.mod b/go.mod index 81aa215..caa21a7 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,6 @@ module github.com/containerd/platforms go 1.20 require ( - github.com/Microsoft/hcsshim v0.10.0 github.com/containerd/log v0.1.0 github.com/opencontainers/image-spec v1.1.0-rc5 github.com/stretchr/testify v1.8.4 diff --git a/go.sum b/go.sum index a28ca24..350c09d 100644 --- a/go.sum +++ b/go.sum @@ -1,5 +1,3 @@ -github.com/Microsoft/hcsshim v0.10.0 h1:PbvoxdUGgXxyirmN5Oncp3POLkxEG5LbWCEBfWmHTGA= -github.com/Microsoft/hcsshim v0.10.0/go.mod h1:3j1trOamcUdi86J5Tr5+1BpqMjSv/QeRWkX2whBF6dY= github.com/containerd/log v0.1.0 h1:TCJt7ioM2cr/tfR8GPbGf9/VRAX8D2B4PjzCpfX540I= github.com/containerd/log v0.1.0/go.mod h1:VRRf09a7mHDIRezVKTRCrOq78v577GXq3bSa3EhrzVo= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= diff --git a/platform_compat_windows.go b/platform_compat_windows.go new file mode 100644 index 0000000..ac85b0c --- /dev/null +++ b/platform_compat_windows.go @@ -0,0 +1,81 @@ +/* + 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 + +// osVersion is a wrapper for Windows version information +// https://msdn.microsoft.com/en-us/library/windows/desktop/ms724439(v=vs.85).aspx +type osVersion struct { + Version uint32 + MajorVersion uint8 + MinorVersion uint8 + Build uint16 +} + +// Windows Client and Server build numbers. +// +// See: +// https://learn.microsoft.com/en-us/windows/release-health/release-information +// https://learn.microsoft.com/en-us/windows/release-health/windows-server-release-info +// https://learn.microsoft.com/en-us/windows/release-health/windows11-release-information +const ( + // RS5 (version 1809, codename "Redstone 5") corresponds to Windows Server + // 2019 (ltsc2019), and Windows 10 (October 2018 Update). + RS5 = 17763 + + // V21H2Server corresponds to Windows Server 2022 (ltsc2022). + V21H2Server = 20348 + + // LTSC2022 (Windows Server 2022) is an alias for [V21H2Server] + LTSC2022 = V21H2Server + + // V22H2Win11 corresponds to Windows 11 (2022 Update). + V22H2Win11 = 22621 +) + +// List of stable ABI compliant ltsc releases +// Note: List must be sorted in ascending order +var compatLTSCReleases = []uint16{ + V21H2Server, +} + +// CheckHostAndContainerCompat checks if given host and container +// OS versions are compatible. +// It includes support for stable ABI compliant versions as well. +// Every release after WS 2022 will support the previous ltsc +// container image. Stable ABI is in preview mode for windows 11 client. +// Refer: https://learn.microsoft.com/en-us/virtualization/windowscontainers/deploy-containers/version-compatibility?tabs=windows-server-2022%2Cwindows-10#windows-server-host-os-compatibility +func checkHostAndContainerCompat(host, ctr osVersion) bool { + // check major minor versions of host and guest + if host.MajorVersion != ctr.MajorVersion || + host.MinorVersion != ctr.MinorVersion { + return false + } + + // If host is < WS 2022, exact version match is required + if host.Build < V21H2Server { + return host.Build == ctr.Build + } + + var supportedLtscRelease uint16 + for i := len(compatLTSCReleases) - 1; i >= 0; i-- { + if host.Build >= compatLTSCReleases[i] { + supportedLtscRelease = compatLTSCReleases[i] + break + } + } + return ctr.Build >= supportedLtscRelease && ctr.Build <= host.Build +} diff --git a/platform_compat_windows_test.go b/platform_compat_windows_test.go new file mode 100644 index 0000000..4e9acb5 --- /dev/null +++ b/platform_compat_windows_test.go @@ -0,0 +1,84 @@ +/* + 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" +) + +// Test the platform compatibility of the different +// OS Versions considering two ltsc container image +// versions (ltsc2019, ltsc2022) +func Test_PlatformCompat(t *testing.T) { + for testName, tc := range map[string]struct { + hostOs uint16 + ctrOs uint16 + shouldRun bool + }{ + "RS5Host_ltsc2019": { + hostOs: RS5, + ctrOs: RS5, + shouldRun: true, + }, + "RS5Host_ltsc2022": { + hostOs: RS5, + ctrOs: V21H2Server, + shouldRun: false, + }, + "WS2022Host_ltsc2019": { + hostOs: V21H2Server, + ctrOs: RS5, + shouldRun: false, + }, + "WS2022Host_ltsc2022": { + hostOs: V21H2Server, + ctrOs: V21H2Server, + shouldRun: true, + }, + "Wind11Host_ltsc2019": { + hostOs: V22H2Win11, + ctrOs: RS5, + shouldRun: false, + }, + "Wind11Host_ltsc2022": { + hostOs: V22H2Win11, + ctrOs: V21H2Server, + shouldRun: true, + }, + } { + // Check if ltsc2019/ltsc2022 guest images are compatible on + // the given host OS versions + // + hostOSVersion := osVersion{ + MajorVersion: 10, + MinorVersion: 0, + Build: tc.hostOs, + } + ctrOSVersion := osVersion{ + MajorVersion: 10, + MinorVersion: 0, + Build: tc.ctrOs, + } + if checkHostAndContainerCompat(hostOSVersion, ctrOSVersion) != tc.shouldRun { + var expectedResultStr string + if !tc.shouldRun { + expectedResultStr = " NOT" + } + t.Fatalf("Failed %v: host %v should%s be able to run guest %v", testName, tc.hostOs, expectedResultStr, tc.ctrOs) + } + } +}