From 0020c1129e1bb3372aa41cb89077d4df6d22c975 Mon Sep 17 00:00:00 2001 From: Evans Mungai Date: Wed, 24 Jul 2024 07:04:59 +0100 Subject: [PATCH] feat: Allow checking kernel versions only in host os analyzer (#1585) * Allow checking kernel versions only in host os analyzer Signed-off-by: Evans Mungai * Minor fix in logic Signed-off-by: Evans Mungai * Fix formatting Signed-off-by: Evans Mungai --------- Signed-off-by: Evans Mungai --- pkg/analyze/host_os_info.go | 27 +++++++++++++++++++++++---- pkg/analyze/host_os_info_test.go | 28 ++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+), 4 deletions(-) diff --git a/pkg/analyze/host_os_info.go b/pkg/analyze/host_os_info.go index fbde82729..5ad6f8c10 100644 --- a/pkg/analyze/host_os_info.go +++ b/pkg/analyze/host_os_info.go @@ -86,19 +86,36 @@ func analyzeOSVersionResult(osInfo collect.HostOSInfo, outcomes []*troubleshootv } parts := strings.Split(when, " ") - platform := parts[0] + if len(parts) < 3 { + return []*AnalyzeResult{&result}, errors.New("when condition must have at least 3 parts") + } expectedVer := fixVersion(parts[2]) toleratedVer, err := semver.ParseTolerant(expectedVer) if err != nil { - return []*AnalyzeResult{&result}, errors.Wrapf(err, "failed to parse tolerant: %s", expectedVer) + return []*AnalyzeResult{&result}, errors.Wrapf(err, "failed to parse version: %s", expectedVer) } - when = fmt.Sprintf("%s %v", parts[1], toleratedVer) whenRange, err := semver.ParseRange(when) if err != nil { - return []*AnalyzeResult{&result}, errors.Wrapf(err, "failed to parse range: %s", when) + return []*AnalyzeResult{&result}, errors.Wrapf(err, "failed to parse version range: %s", when) + } + + // Match the kernel version regardless of the platform + // e.g "kernelVersion == 4.15" + if parts[0] == "kernelVersion" { + fixedKernelVer := fixVersion(osInfo.KernelVersion) + toleratedKernelVer, err := semver.ParseTolerant(fixedKernelVer) + if err != nil { + return []*AnalyzeResult{}, errors.Wrapf(err, "failed to parse tolerant: %v", fixedKernelVer) + } + if whenRange(toleratedKernelVer) { + return []*AnalyzeResult{&result}, nil + } } + // Match the platform version and and kernel version passed in as + // "--kernel" e.g "centos-8.2-kernel == 8.2" + platform := parts[0] kernelInfo := fmt.Sprintf("%s-%s-kernel", osInfo.Platform, osInfo.PlatformVersion) if len(strings.Split(platform, "-")) == 3 && strings.Split(platform, "-")[2] == "kernel" { if platform == kernelInfo { @@ -111,6 +128,8 @@ func analyzeOSVersionResult(osInfo collect.HostOSInfo, outcomes []*troubleshootv return []*AnalyzeResult{&result}, nil } } + // Match the platform version + // e.g "centos == 8.2" } else if platform == osInfo.Platform { fixedDistVer := fixVersion(osInfo.PlatformVersion) toleratedDistVer, err := semver.ParseTolerant(fixedDistVer) diff --git a/pkg/analyze/host_os_info_test.go b/pkg/analyze/host_os_info_test.go index 485400c76..d13fb05f2 100644 --- a/pkg/analyze/host_os_info_test.go +++ b/pkg/analyze/host_os_info_test.go @@ -319,6 +319,34 @@ func TestAnalyzeHostOS(t *testing.T) { }, }, }, + + { + name: "test kernelVersion >= 6.4.9-abc", + hostInfo: collect.HostOSInfo{ + Name: "my-host", + KernelVersion: "6.5.0-1024-gcp", + PlatformVersion: "22.04", + Platform: "ubuntu", + }, + hostAnalyzer: &troubleshootv1beta2.HostOSAnalyze{ + Outcomes: []*troubleshootv1beta2.Outcome{ + { + Pass: &troubleshootv1beta2.SingleOutcome{ + When: "kernelVersion >= 6.4.9-abc", + Message: "supported kernel version >= 6.4.9-abc", + }, + }, + }, + }, + + result: []*AnalyzeResult{ + { + Title: "Host OS Info", + IsPass: true, + Message: "supported kernel version >= 6.4.9-abc", + }, + }, + }, } for _, test := range tests { t.Run(test.name, func(t *testing.T) {