-
Notifications
You must be signed in to change notification settings - Fork 95
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
- Loading branch information
Showing
16 changed files
with
924 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, ", ")) | ||
} |
Oops, something went wrong.