Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: New host collector and analyzer for Kernel Configs #1546

Merged
merged 11 commits into from
May 26, 2024
54 changes: 54 additions & 0 deletions config/crds/troubleshoot.sh_analyzers.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2299,6 +2299,60 @@ spec:
required:
- outcomes
type: object
kernelConfigs:
properties:
annotations:
additionalProperties:
type: string
type: object
checkName:
type: string
collectorName:
type: string
exclude:
type: BoolString
outcomes:
items:
properties:
fail:
properties:
message:
type: string
uri:
type: string
when:
type: string
type: object
pass:
properties:
message:
type: string
uri:
type: string
when:
type: string
type: object
warn:
properties:
message:
type: string
uri:
type: string
when:
type: string
type: object
type: object
type: array
selectedConfigs:
items:
type: string
type: array
strict:
type: BoolString
required:
- outcomes
- selectedConfigs
type: object
kernelModules:
properties:
annotations:
Expand Down
61 changes: 61 additions & 0 deletions config/crds/troubleshoot.sh_hostcollectors.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -588,6 +588,60 @@ spec:
required:
- outcomes
type: object
kernelConfigs:
properties:
annotations:
additionalProperties:
type: string
type: object
checkName:
type: string
collectorName:
type: string
exclude:
type: BoolString
outcomes:
items:
properties:
fail:
properties:
message:
type: string
uri:
type: string
when:
type: string
type: object
pass:
properties:
message:
type: string
uri:
type: string
when:
type: string
type: object
warn:
properties:
message:
type: string
uri:
type: string
when:
type: string
type: object
type: object
type: array
selectedConfigs:
items:
type: string
type: array
strict:
type: BoolString
required:
- outcomes
- selectedConfigs
type: object
kernelModules:
properties:
annotations:
Expand Down Expand Up @@ -1338,6 +1392,13 @@ spec:
exclude:
type: BoolString
type: object
kernelConfigs:
properties:
collectorName:
type: string
exclude:
type: BoolString
type: object
kernelModules:
properties:
collectorName:
Expand Down
61 changes: 61 additions & 0 deletions config/crds/troubleshoot.sh_hostpreflights.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -588,6 +588,60 @@ spec:
required:
- outcomes
type: object
kernelConfigs:
properties:
annotations:
additionalProperties:
type: string
type: object
checkName:
type: string
collectorName:
type: string
exclude:
type: BoolString
outcomes:
items:
properties:
fail:
properties:
message:
type: string
uri:
type: string
when:
type: string
type: object
pass:
properties:
message:
type: string
uri:
type: string
when:
type: string
type: object
warn:
properties:
message:
type: string
uri:
type: string
when:
type: string
type: object
type: object
type: array
selectedConfigs:
items:
type: string
type: array
strict:
type: BoolString
required:
- outcomes
- selectedConfigs
type: object
kernelModules:
properties:
annotations:
Expand Down Expand Up @@ -1338,6 +1392,13 @@ spec:
exclude:
type: BoolString
type: object
kernelConfigs:
properties:
collectorName:
type: string
exclude:
type: BoolString
type: object
kernelModules:
properties:
collectorName:
Expand Down
61 changes: 61 additions & 0 deletions config/crds/troubleshoot.sh_supportbundles.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19138,6 +19138,60 @@ spec:
required:
- outcomes
type: object
kernelConfigs:
properties:
annotations:
additionalProperties:
type: string
type: object
checkName:
type: string
collectorName:
type: string
exclude:
type: BoolString
outcomes:
items:
properties:
fail:
properties:
message:
type: string
uri:
type: string
when:
type: string
type: object
pass:
properties:
message:
type: string
uri:
type: string
when:
type: string
type: object
warn:
properties:
message:
type: string
uri:
type: string
when:
type: string
type: object
type: object
type: array
selectedConfigs:
items:
type: string
type: array
strict:
type: BoolString
required:
- outcomes
- selectedConfigs
type: object
kernelModules:
properties:
annotations:
Expand Down Expand Up @@ -19888,6 +19942,13 @@ spec:
exclude:
type: BoolString
type: object
kernelConfigs:
properties:
collectorName:
type: string
exclude:
type: BoolString
type: object
kernelModules:
properties:
collectorName:
Expand Down
2 changes: 2 additions & 0 deletions pkg/analyze/host_analyzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ func GetHostAnalyzer(analyzer *troubleshootv1beta2.HostAnalyze) (HostAnalyzer, b
return &AnalyzeHostOS{analyzer.HostOS}, true
case analyzer.TextAnalyze != nil:
return &AnalyzeHostTextAnalyze{analyzer.TextAnalyze}, true
case analyzer.KernelConfigs != nil:
return &AnalyzeHostKernelConfigs{analyzer.KernelConfigs}, true
default:
return nil, false
}
Expand Down
97 changes: 97 additions & 0 deletions pkg/analyze/host_kernel_configs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
package analyzer

import (
"encoding/json"
"regexp"

"strings"

"github.com/pkg/errors"
troubleshootv1beta2 "github.com/replicatedhq/troubleshoot/pkg/apis/troubleshoot/v1beta2"
"github.com/replicatedhq/troubleshoot/pkg/collect"
"k8s.io/klog/v2"
)

type AnalyzeHostKernelConfigs struct {
hostAnalyzer *troubleshootv1beta2.KernelConfigsAnalyze
}

func (a *AnalyzeHostKernelConfigs) Title() string {
return hostAnalyzerTitleOrDefault(a.hostAnalyzer.AnalyzeMeta, "Kernel Configs")
}

func (a *AnalyzeHostKernelConfigs) IsExcluded() (bool, error) {
return isExcluded(a.hostAnalyzer.Exclude)
}

func (a *AnalyzeHostKernelConfigs) Analyze(
getCollectedFileContents func(string) ([]byte, error), findFiles getChildCollectedFileContents,
) ([]*AnalyzeResult, error) {
hostAnalyzer := a.hostAnalyzer

contents, err := getCollectedFileContents(collect.HostKernelConfigsPath)
if err != nil {
return nil, errors.Wrap(err, "failed to get collected file")
}

kConfigs := collect.KConfigs{}
if err := json.Unmarshal(contents, &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
if matches == nil || len(matches) < 3 {
return nil, errors.Errorf("invalid kernel config: %s", config)
}

key := matches[1]
value := matches[2]

// check if the kernel config exists
if _, ok := kConfigs[key]; !ok {
configsNotFound = append(configsNotFound, config)
continue
}
// check if the kernel config value matches
if kConfigs[key] != value {
klog.V(2).Infof("collected kernel config %s=%s does not match expected value %s", key, kConfigs[key], value)
configsNotFound = append(configsNotFound, config)
}
}

var results []*AnalyzeResult
for _, outcome := range hostAnalyzer.Outcomes {
result := &AnalyzeResult{
Title: a.Title(),
Strict: hostAnalyzer.Strict.BoolOrDefaultFalse(),
}

if outcome.Pass != nil && len(configsNotFound) == 0 {
result.IsPass = true
result.Message = outcome.Pass.Message
results = append(results, result)
break
}

if outcome.Fail != nil && len(configsNotFound) > 0 {
result.IsFail = true
result.Message = addMissingKernelConfigs(outcome.Fail.Message, configsNotFound)
results = append(results, result)
break
}

}

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, ", "))
}
Loading
Loading