Skip to content

Commit f070f22

Browse files
committed
Windows: do not use 'uname' for doing OS validation
The idea of the OS validator is to check if the host OS is supported. e.g. 'uname' would return 'Linux' on Linux and something else on OSX, BSD. But 'uname' is a Unix tool, and only available on Windows if third party packages are installed like MSYS. - Split the os_validator*.go files for Unix and Windows. - Align the fetching of OS name and kernel version for Windows with the kubelet which uses the CurrentVersion reg key under HKLM. - In the Windows spec use 'Windows Server' as the required name and match it in 'CurrentVersion.ProductName' as a prefix. - Add OS specific unit tests.
1 parent 1e0d9c3 commit f070f22

File tree

5 files changed

+134
-5
lines changed

5 files changed

+134
-5
lines changed

validators/os_validator.go renamed to validators/os_validator_unix.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
//go:build !windows
2+
// +build !windows
3+
14
/*
25
Copyright 2016 The Kubernetes Authors.
36
@@ -38,7 +41,7 @@ func (o *OSValidator) Name() string {
3841
func (o *OSValidator) Validate(spec SysSpec) ([]error, []error) {
3942
os, err := exec.Command("uname").CombinedOutput()
4043
if err != nil {
41-
return nil, []error{fmt.Errorf("failed to get os name: %w", err)}
44+
return nil, []error{fmt.Errorf("failed to get OS name: %w", err)}
4245
}
4346
if err = o.validateOS(strings.TrimSpace(string(os)), spec.OS); err != nil {
4447
return nil, []error{err}

validators/os_validator_test.go renamed to validators/os_validator_unix_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
//go:build !windows
2+
// +build !windows
3+
14
/*
25
Copyright 2016 The Kubernetes Authors.
36

validators/os_validator_windows.go

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
//go:build windows
2+
// +build windows
3+
4+
/*
5+
Copyright 2024 The Kubernetes Authors.
6+
7+
Licensed under the Apache License, Version 2.0 (the "License");
8+
you may not use this file except in compliance with the License.
9+
You may obtain a copy of the License at
10+
11+
http://www.apache.org/licenses/LICENSE-2.0
12+
13+
Unless required by applicable law or agreed to in writing, software
14+
distributed under the License is distributed on an "AS IS" BASIS,
15+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
See the License for the specific language governing permissions and
17+
limitations under the License.
18+
*/
19+
20+
package system
21+
22+
import (
23+
"fmt"
24+
"os/exec"
25+
"strings"
26+
)
27+
28+
var _ Validator = &OSValidator{}
29+
30+
// OSValidator validates OS.
31+
type OSValidator struct {
32+
Reporter Reporter
33+
}
34+
35+
// Name is part of the system.Validator interface.
36+
func (o *OSValidator) Name() string {
37+
return "os"
38+
}
39+
40+
// Validate is part of the system.Validator interface.
41+
func (o *OSValidator) Validate(spec SysSpec) ([]error, []error) {
42+
args := []string{`(Get-ItemProperty -Path 'HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion').ProductName`}
43+
os, err := exec.Command("powershell", args...).Output()
44+
if err != nil {
45+
return nil, []error{fmt.Errorf("failed to get OS name: %w", err)}
46+
}
47+
if err = o.validateOS(strings.TrimSpace(string(os)), spec.OS); err != nil {
48+
return nil, []error{err}
49+
}
50+
return nil, nil
51+
}
52+
53+
// validateOS would check if the reported string such as 'Windows Server 2019' contains
54+
// the required OS prefix from the spec 'Windows Server'.
55+
func (o *OSValidator) validateOS(os, specOS string) error {
56+
if !strings.HasPrefix(os, specOS) {
57+
o.Reporter.Report("OS", os, bad)
58+
return fmt.Errorf("unsupported operating system: %s", os)
59+
}
60+
o.Reporter.Report("OS", os, good)
61+
return nil
62+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
//go:build windows
2+
// +build windows
3+
4+
/*
5+
Copyright 2016 The Kubernetes Authors.
6+
7+
Licensed under the Apache License, Version 2.0 (the "License");
8+
you may not use this file except in compliance with the License.
9+
You may obtain a copy of the License at
10+
11+
http://www.apache.org/licenses/LICENSE-2.0
12+
13+
Unless required by applicable law or agreed to in writing, software
14+
distributed under the License is distributed on an "AS IS" BASIS,
15+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
See the License for the specific language governing permissions and
17+
limitations under the License.
18+
*/
19+
20+
package system
21+
22+
import (
23+
"testing"
24+
25+
"github.com/stretchr/testify/assert"
26+
)
27+
28+
func TestValidateOS(t *testing.T) {
29+
v := &OSValidator{
30+
Reporter: DefaultReporter,
31+
}
32+
specOS := "Windows Server"
33+
for _, test := range []struct {
34+
os string
35+
err bool
36+
}{
37+
{
38+
os: "Windows Server",
39+
err: false,
40+
},
41+
{
42+
os: "Windows 10 Enterprise",
43+
err: true,
44+
},
45+
{
46+
os: "Windows",
47+
err: true,
48+
},
49+
} {
50+
t.Run(test.os, func(t *testing.T) {
51+
err := v.validateOS(test.os, specOS)
52+
if !test.err {
53+
assert.Nil(t, err, "Expect error not to occur with os %q", test.os)
54+
} else {
55+
assert.NotNil(t, err, "Expect error to occur with os %q", test.os)
56+
}
57+
})
58+
}
59+
}

validators/types_windows.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ import (
2424
"strings"
2525
)
2626

27-
// DefaultSysSpec is the default SysSpec for Windows
27+
// DefaultSysSpec is the default SysSpec for Windows.
2828
var DefaultSysSpec = SysSpec{
29-
OS: "Microsoft Windows Server 2016",
29+
OS: "Windows Server",
3030
KernelSpec: KernelSpec{
3131
Versions: []string{`10\.0\.1439[3-9]`, `10\.0\.14[4-9][0-9]{2}`, `10\.0\.1[5-9][0-9]{3}`, `10\.0\.[2-9][0-9]{4}`, `10\.[1-9]+\.[0-9]+`}, //requires >= '10.0.14393'
3232
VersionsNote: "The kernel version should be >= '10.0.14393'",
@@ -47,9 +47,11 @@ type KernelValidatorHelperImpl struct{}
4747

4848
var _ KernelValidatorHelper = &KernelValidatorHelperImpl{}
4949

50-
// GetKernelReleaseVersion returns the windows release version (ex. 10.0.14393) as a string
50+
// GetKernelReleaseVersion returns the Windows release version (e.g. 10.0.14393) as a string.
51+
// It does not include the UBR (revision)
5152
func (o *KernelValidatorHelperImpl) GetKernelReleaseVersion() (string, error) {
52-
args := []string{"(Get-CimInstance Win32_OperatingSystem).Version"}
53+
args := []string{`$props = Get-ItemProperty -Path 'HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion'; ` +
54+
`"$($props.CurrentMajorVersionNumber).$($props.CurrentMinorVersionNumber).$($props.CurrentBuildNumber)"`}
5355
releaseVersion, err := exec.Command("powershell", args...).Output()
5456
if err != nil {
5557
return "", err

0 commit comments

Comments
 (0)