Skip to content

Commit

Permalink
implement kernel config
Browse files Browse the repository at this point in the history
  • Loading branch information
nvanthao committed Oct 30, 2024
1 parent 9592b37 commit c3ba8ba
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 18 deletions.
4 changes: 1 addition & 3 deletions pkg/analyze/collected_contents.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,9 @@ import (

type collectedContent struct {
NodeName string
Data collectorData
Data []byte
}

type collectorData interface{}

type nodeNames struct {
Nodes []string `json:"nodes"`
}
Expand Down
56 changes: 41 additions & 15 deletions pkg/analyze/host_kernel_configs.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package analyzer

import (
"encoding/json"
"fmt"
"regexp"

"strings"
Expand All @@ -16,6 +17,8 @@ type AnalyzeHostKernelConfigs struct {
hostAnalyzer *troubleshootv1beta2.KernelConfigsAnalyze
}

var kConfigRegex = regexp.MustCompile("^(CONFIG_[A-Z0-9_]+)=([ymn]+)$")

func (a *AnalyzeHostKernelConfigs) Title() string {
return hostAnalyzerTitleOrDefault(a.hostAnalyzer.AnalyzeMeta, "Kernel Configs")
}
Expand All @@ -27,20 +30,50 @@ func (a *AnalyzeHostKernelConfigs) IsExcluded() (bool, error) {
func (a *AnalyzeHostKernelConfigs) Analyze(
getCollectedFileContents func(string) ([]byte, error), findFiles getChildCollectedFileContents,
) ([]*AnalyzeResult, error) {
hostAnalyzer := a.hostAnalyzer

contents, err := getCollectedFileContents(collect.HostKernelConfigsPath)
collectedContents, err := retrieveCollectedContents(
getCollectedFileContents,
collect.HostKernelConfigsPath,
collect.NodeInfoBaseDir,
collect.HostKernelConfigsFileName,
)
if err != nil {
return nil, errors.Wrap(err, "failed to get collected file")
return []*AnalyzeResult{{Title: a.Title()}}, err
}

var results []*AnalyzeResult

for _, content := range collectedContents {
currentTitle := a.Title()
if content.NodeName != "" {
currentTitle = fmt.Sprintf("%s - Node %s", a.Title(), content.NodeName)
}
result, err := a.analyzeSingleNode(content, currentTitle)
if err != nil {
return nil, errors.Wrapf(err, "failed to analyze kernel configs for %s", currentTitle)
}
if result != nil {
results = append(results, result...)
}
}

return results, nil
}

func addMissingKernelConfigs(message string, missingConfigs []string) string {
if message == "" && len(missingConfigs) == 0 {
return message
}
return strings.ReplaceAll(message, "{{ .ConfigsNotFound }}", strings.Join(missingConfigs, ", "))
}

func (a *AnalyzeHostKernelConfigs) analyzeSingleNode(content collectedContent, currentTitle string) ([]*AnalyzeResult, error) {
hostAnalyzer := a.hostAnalyzer
kConfigs := collect.KConfigs{}
if err := json.Unmarshal(contents, &kConfigs); err != nil {
if err := json.Unmarshal(content.Data, &kConfigs); err != nil {
return nil, errors.Wrap(err, "failed to read kernel configs")
}

var configsNotFound []string
kConfigRegex := regexp.MustCompile("^(CONFIG_[A-Z0-9_]+)=([ymn]+)$")
for _, config := range hostAnalyzer.SelectedConfigs {
matches := kConfigRegex.FindStringSubmatch(config)
// zero tolerance for invalid kernel config
Expand All @@ -66,8 +99,8 @@ func (a *AnalyzeHostKernelConfigs) Analyze(
var results []*AnalyzeResult
for _, outcome := range hostAnalyzer.Outcomes {
result := &AnalyzeResult{
Title: a.Title(),
Strict: hostAnalyzer.Strict.BoolOrDefaultFalse(),
Title: currentTitle,
Strict: a.hostAnalyzer.Strict.BoolOrDefaultFalse(),
}

if outcome.Pass != nil && len(configsNotFound) == 0 {
Expand All @@ -88,10 +121,3 @@ func (a *AnalyzeHostKernelConfigs) Analyze(

return results, nil
}

func addMissingKernelConfigs(message string, missingConfigs []string) string {
if message == "" && len(missingConfigs) == 0 {
return message
}
return strings.ReplaceAll(message, "{{ .ConfigsNotFound }}", strings.Join(missingConfigs, ", "))
}
1 change: 1 addition & 0 deletions pkg/collect/host_kernel_configs.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ type CollectHostKernelConfigs struct {
type KConfigs map[string]string

const HostKernelConfigsPath = `host-collectors/system/kernel-configs.json`
const HostKernelConfigsFileName = `kernel-configs.json`

const (
kConfigBuiltIn string = "y"
Expand Down

0 comments on commit c3ba8ba

Please sign in to comment.