Skip to content

Commit

Permalink
feat: Allow checking kernel versions only in host os analyzer (#1585)
Browse files Browse the repository at this point in the history
* Allow checking kernel versions only in host os analyzer

Signed-off-by: Evans Mungai <evans@replicated.com>

* Minor fix in logic

Signed-off-by: Evans Mungai <evans@replicated.com>

* Fix formatting

Signed-off-by: Evans Mungai <evans@replicated.com>

---------

Signed-off-by: Evans Mungai <evans@replicated.com>
  • Loading branch information
banjoh authored Jul 24, 2024
1 parent 1788376 commit 0020c11
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 4 deletions.
27 changes: 23 additions & 4 deletions pkg/analyze/host_os_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
// "<platform>-<kernelVersion>-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 {
Expand All @@ -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)
Expand Down
28 changes: 28 additions & 0 deletions pkg/analyze/host_os_info_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 0020c11

Please sign in to comment.