Skip to content

Commit

Permalink
cgroup v2: add warning message for kernel < 5.8; add error message if…
Browse files Browse the repository at this point in the history
… kernel < 4.15
  • Loading branch information
pacoxu committed Jul 24, 2024
1 parent a0cb0d1 commit 8aaafa2
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 19 deletions.
40 changes: 31 additions & 9 deletions validators/kernel_validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,30 +67,52 @@ func (k *KernelValidator) Validate(spec SysSpec) ([]error, []error) {
}
k.kernelRelease = release
var errs []error
if err = k.validateKernelVersion(spec.KernelSpec); err != nil {
var warns []error
warn, err := k.validateKernelVersion(spec.KernelSpec)
if err != nil {
errs = append(errs, err)
}
if warn != nil {
warns = append(warns, warn)
}
// only validate kernel config when necessary (currently no kernel config for windows)
if len(spec.KernelSpec.Required) > 0 || len(spec.KernelSpec.Forbidden) > 0 || len(spec.KernelSpec.Optional) > 0 {
if err = k.validateKernelConfig(spec.KernelSpec); err != nil {
errs = append(errs, err)
}
}
return nil, errs
return warns, errs
}

// validateKernelVersion validates the kernel version.
func (k *KernelValidator) validateKernelVersion(kSpec KernelSpec) error {
versionRegexps := kSpec.Versions
for _, versionRegexp := range versionRegexps {
func (k *KernelValidator) validateKernelVersion(kSpec KernelSpec) (error, error) {
var warn, err error
var matched bool
for _, versionRegexp := range kSpec.Versions {
r := regexp.MustCompile(versionRegexp)
if r.MatchString(k.kernelRelease) {
k.Reporter.Report("KERNEL_VERSION", k.kernelRelease, good)
return nil
matched = true
break
}
}
k.Reporter.Report("KERNEL_VERSION", k.kernelRelease, bad)
return fmt.Errorf("unsupported kernel release: %s", k.kernelRelease)
if !matched {
err = fmt.Errorf("unsupported kernel release: %s", k.kernelRelease)
k.Reporter.Report("KERNEL_VERSION", k.kernelRelease, good)
}

matched = false
for _, versionRegexp := range kSpec.RecommendedVersions {
r := regexp.MustCompile(versionRegexp)
if r.MatchString(k.kernelRelease) {
matched = true
break
}
}
if !matched {
warn = fmt.Errorf("kernel release %s is not recommended, and %s", k.kernelRelease, kSpec.RecommendedNote)
k.Reporter.Report("KERNEL_VERSION", k.kernelRelease, good)
}
return warn, err
}

// validateKernelConfig validates the kernel configurations.
Expand Down
48 changes: 43 additions & 5 deletions validators/kernel_validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,54 +32,92 @@ func TestValidateKernelVersion(t *testing.T) {
// This is fine, because the test mainly tests the kernel version validation logic,
// not the DefaultSysSpec. The DefaultSysSpec should be tested with node e2e.
testRegex := []string{`^3\.[1-9][0-9].*$`, `^([4-9]|[1-9][0-9]+)\.([0-9]+)\.([0-9]+).*$`}
testRecommendedRegex := []string{`^5\.[8-9].*$`, `^5\.[1-9][0-9].*$`, `^([6-9]|[1-9][0-9]+)\.([0-9]+)\.([0-9]+).*$`}
for _, test := range []struct {
name string
version string
err bool
warn bool
}{
{
name: "3.19.9-99-test first version regex matches",
name: "3.19.9-99-test no version regex matches",
version: "3.19.9-99-test",
err: false,
warn: true,
},
{
name: "4.4.14+ one of version regexes matches",
name: "4.4.14+ no version regex matches",
version: "4.4.14+",
err: false,
warn: true,
},
{
name: "5.7.3 no version regex matches",
version: "5.7.3",
err: false,
warn: true,
},
{
name: "5.8.3-99-test matches",
version: "5.8.3",
err: false,
warn: false,
},
{
name: "5.9.3+ matches",
version: "5.9.3",
err: false,
warn: false,
},
{
name: "5.12.3 matches",
version: "5.12.3",
err: false,
warn: false,
},
{
name: "2.0.0 no version regex matches",
version: "2.0.0",
err: true,
warn: true,
},
{
name: "5.0.0 one of version regexes matches",
version: "5.0.0",
err: false,
warn: true,
},
{
name: "10.21.1 one of version regexes matches",
version: "10.21.1",
err: false,
warn: false,
},
{
name: "99.12.12 one of version regexes matches",
version: "99.12.12",
err: false,
warn: false,
},
{
name: "3.9.0 no version regex matches",
version: "3.9.0",
err: true,
warn: true,
},
} {
t.Run(test.name, func(t *testing.T) {
v.kernelRelease = test.version
err := v.validateKernelVersion(KernelSpec{Versions: testRegex})
warn, err := v.validateKernelVersion(KernelSpec{Versions: testRegex, RecommendedVersions: testRecommendedRegex})
if !test.err {
assert.Nil(t, err, "Expect error not to occur with kernel version %q", test.version)
} else {
assert.NotNil(t, err, "Expect error to occur with kenrel version %q", test.version)
assert.NotNil(t, err, "Expect error to occur with kernel version %q", test.version)
}
if !test.warn {
assert.Nil(t, warn, "Expect warn not to occur with kernel version %q", test.version)
} else {
assert.NotNil(t, warn, "Expect warn to occur with kernel version %q", test.version)
}
})
}
Expand Down Expand Up @@ -189,7 +227,7 @@ func TestValidateCachedKernelConfig(t *testing.T) {
if !test.err {
assert.Nil(t, err, "Expect error not to occur with kernel config %q", test.config)
} else {
assert.NotNil(t, err, "Expect error to occur with kenrel config %q", test.config)
assert.NotNil(t, err, "Expect error to occur with kernel config %q", test.config)
}
})
}
Expand Down
4 changes: 4 additions & 0 deletions validators/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ type KernelConfig struct {
type KernelSpec struct {
// Versions define supported kernel version. It is a group of regexps.
Versions []string `json:"versions,omitempty"`
// RecommendedVersions define recommended kernel versions. It is a group of regexps.
RecommendedVersions []string `json:"recommendedVersions,omitempty"`
// RecommendedNote provides a prompt message for user if RecommendedVersions do not match.
RecommendedNote string `json:"recommendedNote,omitempty"`
// Required contains all kernel configurations required to be enabled
// (built in or as module).
Required []KernelConfig `json:"required,omitempty"`
Expand Down
7 changes: 6 additions & 1 deletion validators/types_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,12 @@ import (
var DefaultSysSpec = SysSpec{
OS: "Linux",
KernelSpec: KernelSpec{
Versions: []string{`^3\.[1-9][0-9].*$`, `^([4-9]|[1-9][0-9]+)\.([0-9]+)\.([0-9]+).*$`}, // Requires 3.10+, or newer
// runc cgroup v2 https://github.com/opencontainers/runc/blob/3778ae603c706494fd1e2c2faf83b406e38d687d/docs/cgroup-v2.md?plain=1#L24
Versions: []string{`^3\.[1-9][0-9].*$`, `^([4-9]|[1-9][0-9]+)\.([0-9]+)\.([0-9]+).*$`}, // Requires 4.15+, or newer
// cgroup v2: Kubernetes recommended kernel version
// https://kubernetes.io/docs/concepts/architecture/cgroups/
RecommendedVersions: []string{`^5\.[8-9].*$`, `^5\.[1-9][0-9].*$`, `^([6-9]|[1-9][0-9]+)\.([0-9]+)\.([0-9]+).*$`}, // Requires 5.8+, or newer for cgroup v2
RecommendedNote: "kernel version should >= '4.15', and 5.8+ is recommended.",
// TODO(random-liu): Add more config
// TODO(random-liu): Add description for each kernel configuration:
Required: []KernelConfig{
Expand Down
10 changes: 6 additions & 4 deletions validators/types_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,12 @@ import (
var DefaultSysSpec = SysSpec{
OS: "Microsoft Windows Server 2016",
KernelSpec: KernelSpec{
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'
Required: []KernelConfig{},
Optional: []KernelConfig{},
Forbidden: []KernelConfig{},
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'
RecommendedVersions: []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'
RecommendedNote: "kernel version should>= '10.0.14393'",
Required: []KernelConfig{},
Optional: []KernelConfig{},
Forbidden: []KernelConfig{},
},
RuntimeSpec: RuntimeSpec{
DockerSpec: &DockerSpec{
Expand Down

0 comments on commit 8aaafa2

Please sign in to comment.