|
| 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