Skip to content

Commit 80e5fac

Browse files
authored
feat: New host collector and analyzer for Kernel Configs (#1546)
* new struct and update schemas * implement Collect function * add kernel config to collector struct * generate kernel config analyzer schema * implement kernel config analyzer * fail on no match in pass outcome * run make check-schemas * fix failed unit test * update from code review * add selectedConfigs field * run make check-schemas
1 parent 51c07b4 commit 80e5fac

16 files changed

+924
-0
lines changed

config/crds/troubleshoot.sh_analyzers.yaml

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2299,6 +2299,60 @@ spec:
22992299
required:
23002300
- outcomes
23012301
type: object
2302+
kernelConfigs:
2303+
properties:
2304+
annotations:
2305+
additionalProperties:
2306+
type: string
2307+
type: object
2308+
checkName:
2309+
type: string
2310+
collectorName:
2311+
type: string
2312+
exclude:
2313+
type: BoolString
2314+
outcomes:
2315+
items:
2316+
properties:
2317+
fail:
2318+
properties:
2319+
message:
2320+
type: string
2321+
uri:
2322+
type: string
2323+
when:
2324+
type: string
2325+
type: object
2326+
pass:
2327+
properties:
2328+
message:
2329+
type: string
2330+
uri:
2331+
type: string
2332+
when:
2333+
type: string
2334+
type: object
2335+
warn:
2336+
properties:
2337+
message:
2338+
type: string
2339+
uri:
2340+
type: string
2341+
when:
2342+
type: string
2343+
type: object
2344+
type: object
2345+
type: array
2346+
selectedConfigs:
2347+
items:
2348+
type: string
2349+
type: array
2350+
strict:
2351+
type: BoolString
2352+
required:
2353+
- outcomes
2354+
- selectedConfigs
2355+
type: object
23022356
kernelModules:
23032357
properties:
23042358
annotations:

config/crds/troubleshoot.sh_hostcollectors.yaml

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -588,6 +588,60 @@ spec:
588588
required:
589589
- outcomes
590590
type: object
591+
kernelConfigs:
592+
properties:
593+
annotations:
594+
additionalProperties:
595+
type: string
596+
type: object
597+
checkName:
598+
type: string
599+
collectorName:
600+
type: string
601+
exclude:
602+
type: BoolString
603+
outcomes:
604+
items:
605+
properties:
606+
fail:
607+
properties:
608+
message:
609+
type: string
610+
uri:
611+
type: string
612+
when:
613+
type: string
614+
type: object
615+
pass:
616+
properties:
617+
message:
618+
type: string
619+
uri:
620+
type: string
621+
when:
622+
type: string
623+
type: object
624+
warn:
625+
properties:
626+
message:
627+
type: string
628+
uri:
629+
type: string
630+
when:
631+
type: string
632+
type: object
633+
type: object
634+
type: array
635+
selectedConfigs:
636+
items:
637+
type: string
638+
type: array
639+
strict:
640+
type: BoolString
641+
required:
642+
- outcomes
643+
- selectedConfigs
644+
type: object
591645
kernelModules:
592646
properties:
593647
annotations:
@@ -1338,6 +1392,13 @@ spec:
13381392
exclude:
13391393
type: BoolString
13401394
type: object
1395+
kernelConfigs:
1396+
properties:
1397+
collectorName:
1398+
type: string
1399+
exclude:
1400+
type: BoolString
1401+
type: object
13411402
kernelModules:
13421403
properties:
13431404
collectorName:

config/crds/troubleshoot.sh_hostpreflights.yaml

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -588,6 +588,60 @@ spec:
588588
required:
589589
- outcomes
590590
type: object
591+
kernelConfigs:
592+
properties:
593+
annotations:
594+
additionalProperties:
595+
type: string
596+
type: object
597+
checkName:
598+
type: string
599+
collectorName:
600+
type: string
601+
exclude:
602+
type: BoolString
603+
outcomes:
604+
items:
605+
properties:
606+
fail:
607+
properties:
608+
message:
609+
type: string
610+
uri:
611+
type: string
612+
when:
613+
type: string
614+
type: object
615+
pass:
616+
properties:
617+
message:
618+
type: string
619+
uri:
620+
type: string
621+
when:
622+
type: string
623+
type: object
624+
warn:
625+
properties:
626+
message:
627+
type: string
628+
uri:
629+
type: string
630+
when:
631+
type: string
632+
type: object
633+
type: object
634+
type: array
635+
selectedConfigs:
636+
items:
637+
type: string
638+
type: array
639+
strict:
640+
type: BoolString
641+
required:
642+
- outcomes
643+
- selectedConfigs
644+
type: object
591645
kernelModules:
592646
properties:
593647
annotations:
@@ -1338,6 +1392,13 @@ spec:
13381392
exclude:
13391393
type: BoolString
13401394
type: object
1395+
kernelConfigs:
1396+
properties:
1397+
collectorName:
1398+
type: string
1399+
exclude:
1400+
type: BoolString
1401+
type: object
13411402
kernelModules:
13421403
properties:
13431404
collectorName:

config/crds/troubleshoot.sh_supportbundles.yaml

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19138,6 +19138,60 @@ spec:
1913819138
required:
1913919139
- outcomes
1914019140
type: object
19141+
kernelConfigs:
19142+
properties:
19143+
annotations:
19144+
additionalProperties:
19145+
type: string
19146+
type: object
19147+
checkName:
19148+
type: string
19149+
collectorName:
19150+
type: string
19151+
exclude:
19152+
type: BoolString
19153+
outcomes:
19154+
items:
19155+
properties:
19156+
fail:
19157+
properties:
19158+
message:
19159+
type: string
19160+
uri:
19161+
type: string
19162+
when:
19163+
type: string
19164+
type: object
19165+
pass:
19166+
properties:
19167+
message:
19168+
type: string
19169+
uri:
19170+
type: string
19171+
when:
19172+
type: string
19173+
type: object
19174+
warn:
19175+
properties:
19176+
message:
19177+
type: string
19178+
uri:
19179+
type: string
19180+
when:
19181+
type: string
19182+
type: object
19183+
type: object
19184+
type: array
19185+
selectedConfigs:
19186+
items:
19187+
type: string
19188+
type: array
19189+
strict:
19190+
type: BoolString
19191+
required:
19192+
- outcomes
19193+
- selectedConfigs
19194+
type: object
1914119195
kernelModules:
1914219196
properties:
1914319197
annotations:
@@ -19888,6 +19942,13 @@ spec:
1988819942
exclude:
1988919943
type: BoolString
1989019944
type: object
19945+
kernelConfigs:
19946+
properties:
19947+
collectorName:
19948+
type: string
19949+
exclude:
19950+
type: BoolString
19951+
type: object
1989119952
kernelModules:
1989219953
properties:
1989319954
collectorName:

pkg/analyze/host_analyzer.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ func GetHostAnalyzer(analyzer *troubleshootv1beta2.HostAnalyze) (HostAnalyzer, b
5252
return &AnalyzeHostOS{analyzer.HostOS}, true
5353
case analyzer.TextAnalyze != nil:
5454
return &AnalyzeHostTextAnalyze{analyzer.TextAnalyze}, true
55+
case analyzer.KernelConfigs != nil:
56+
return &AnalyzeHostKernelConfigs{analyzer.KernelConfigs}, true
5557
default:
5658
return nil, false
5759
}

pkg/analyze/host_kernel_configs.go

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
package analyzer
2+
3+
import (
4+
"encoding/json"
5+
"regexp"
6+
7+
"strings"
8+
9+
"github.com/pkg/errors"
10+
troubleshootv1beta2 "github.com/replicatedhq/troubleshoot/pkg/apis/troubleshoot/v1beta2"
11+
"github.com/replicatedhq/troubleshoot/pkg/collect"
12+
"k8s.io/klog/v2"
13+
)
14+
15+
type AnalyzeHostKernelConfigs struct {
16+
hostAnalyzer *troubleshootv1beta2.KernelConfigsAnalyze
17+
}
18+
19+
func (a *AnalyzeHostKernelConfigs) Title() string {
20+
return hostAnalyzerTitleOrDefault(a.hostAnalyzer.AnalyzeMeta, "Kernel Configs")
21+
}
22+
23+
func (a *AnalyzeHostKernelConfigs) IsExcluded() (bool, error) {
24+
return isExcluded(a.hostAnalyzer.Exclude)
25+
}
26+
27+
func (a *AnalyzeHostKernelConfigs) Analyze(
28+
getCollectedFileContents func(string) ([]byte, error), findFiles getChildCollectedFileContents,
29+
) ([]*AnalyzeResult, error) {
30+
hostAnalyzer := a.hostAnalyzer
31+
32+
contents, err := getCollectedFileContents(collect.HostKernelConfigsPath)
33+
if err != nil {
34+
return nil, errors.Wrap(err, "failed to get collected file")
35+
}
36+
37+
kConfigs := collect.KConfigs{}
38+
if err := json.Unmarshal(contents, &kConfigs); err != nil {
39+
return nil, errors.Wrap(err, "failed to read kernel configs")
40+
}
41+
42+
var configsNotFound []string
43+
kConfigRegex := regexp.MustCompile("^(CONFIG_[A-Z0-9_]+)=([ymn])$")
44+
for _, config := range hostAnalyzer.SelectedConfigs {
45+
matches := kConfigRegex.FindStringSubmatch(config)
46+
// zero tolerance for invalid kernel config
47+
if matches == nil || len(matches) < 3 {
48+
return nil, errors.Errorf("invalid kernel config: %s", config)
49+
}
50+
51+
key := matches[1]
52+
value := matches[2]
53+
54+
// check if the kernel config exists
55+
if _, ok := kConfigs[key]; !ok {
56+
configsNotFound = append(configsNotFound, config)
57+
continue
58+
}
59+
// check if the kernel config value matches
60+
if kConfigs[key] != value {
61+
klog.V(2).Infof("collected kernel config %s=%s does not match expected value %s", key, kConfigs[key], value)
62+
configsNotFound = append(configsNotFound, config)
63+
}
64+
}
65+
66+
var results []*AnalyzeResult
67+
for _, outcome := range hostAnalyzer.Outcomes {
68+
result := &AnalyzeResult{
69+
Title: a.Title(),
70+
Strict: hostAnalyzer.Strict.BoolOrDefaultFalse(),
71+
}
72+
73+
if outcome.Pass != nil && len(configsNotFound) == 0 {
74+
result.IsPass = true
75+
result.Message = outcome.Pass.Message
76+
results = append(results, result)
77+
break
78+
}
79+
80+
if outcome.Fail != nil && len(configsNotFound) > 0 {
81+
result.IsFail = true
82+
result.Message = addMissingKernelConfigs(outcome.Fail.Message, configsNotFound)
83+
results = append(results, result)
84+
break
85+
}
86+
87+
}
88+
89+
return results, nil
90+
}
91+
92+
func addMissingKernelConfigs(message string, missingConfigs []string) string {
93+
if message == "" && len(missingConfigs) == 0 {
94+
return message
95+
}
96+
return strings.ReplaceAll(message, "{{ .ConfigsNotFound }}", strings.Join(missingConfigs, ", "))
97+
}

0 commit comments

Comments
 (0)