1
0
Fork 0
mirror of https://github.com/kubernetes-sigs/node-feature-discovery.git synced 2024-12-14 11:57:51 +00:00

source/kernel: unmangled kconfig values for custom rules

Stop converting "=y" and "=m" to "true" for the raw feature values used
in "kernel.config" custom rule processing.

In practice, this means that to check if a kernel config flag has been
set to "y" or "m", one needs to explicitly check for both of the values:

  matchFeatures:
    - feature: kernel.config
      matchExpressions:
        FOO: {op: In, value: ["y", "m"]}

instead of (how it used to be):

  matchFeatures:
    - feature: kernel.config
      matchExpressions:
        FOO: {op: IsTrue}

The legacy kconfig custom rule is unchanged as are the
kernel-config.<flag> feature labels.
This commit is contained in:
Markus Lehtonen 2021-11-30 15:15:42 +02:00
parent 5de3cefa56
commit 6f881a4822
3 changed files with 32 additions and 17 deletions

View file

@ -20,7 +20,6 @@ import (
"fmt"
nfdv1alpha1 "sigs.k8s.io/node-feature-discovery/pkg/apis/nfd/v1alpha1"
"sigs.k8s.io/node-feature-discovery/source"
"sigs.k8s.io/node-feature-discovery/source/kernel"
)
@ -30,9 +29,9 @@ type KconfigRule struct {
}
func (r *KconfigRule) Match() (bool, error) {
options, ok := source.GetFeatureSource("kernel").GetFeatures().Values[kernel.ConfigFeature]
if !ok {
options := kernel.GetLegacyKconfig()
if options == nil {
return false, fmt.Errorf("kernel config options not available")
}
return r.MatchValues(options.Elements)
return r.MatchValues(options)
}

View file

@ -47,11 +47,16 @@ func readKconfigGzip(filename string) ([]byte, error) {
return ioutil.ReadAll(r)
}
// ParseKconfig reads kconfig and return a map
func parseKconfig(configPath string) (map[string]string, error) {
kconfig := map[string]string{}
// parseKconfig reads Linux kernel configuration and returns all set options
// and their values. It returns two copies of the parsed options: one with
// values exactly as they are presented in the kernel configuration file (with
// the exception that leading and trailing quotes are stripped) and one where
// '=y' and '=m' are converted to 'true'.
func parseKconfig(configPath string) (realKconfig, legacyKconfig map[string]string, err error) {
realKconfig = map[string]string{}
legacyKconfig = map[string]string{}
raw := []byte(nil)
var err error
var searchPaths []string
kVer, err := getVersion()
@ -91,7 +96,7 @@ func parseKconfig(configPath string) (map[string]string, error) {
}
if raw == nil {
return nil, fmt.Errorf("failed to read kernel config from %+v", append([]string{configPath}, searchPaths...))
return nil, nil, fmt.Errorf("failed to read kernel config from %+v", append([]string{configPath}, searchPaths...))
}
// Process data, line-by-line
@ -105,15 +110,18 @@ func parseKconfig(configPath string) (map[string]string, error) {
}
// Trim the "CONFIG_" prefix
name := split[0][7:]
value := strings.Trim(split[1], `"`)
realKconfig[name] = value
// Provide the "mangled" kconfig values for backwards compatibility
if split[1] == "y" || split[1] == "m" {
kconfig[name] = "true"
legacyKconfig[name] = "true"
} else {
value := strings.Trim(split[1], `"`)
kconfig[name] = value
legacyKconfig[name] = value
}
}
}
return kconfig, nil
return realKconfig, legacyKconfig, nil
}

View file

@ -58,6 +58,9 @@ func newDefaultConfig() *Config {
type kernelSource struct {
config *Config
features *feature.DomainFeatures
// legacyKconfig contains mangled kconfig values used for
// kernel.config-<flag> labels and legacy kConfig custom rules.
legacyKconfig map[string]string
}
// Singleton source instance
@ -98,9 +101,8 @@ func (s *kernelSource) GetLabels() (source.FeatureLabels, error) {
labels[VersionFeature+"."+k] = v
}
// Check flags
for _, opt := range s.config.ConfigOpts {
if val, ok := features.Values[ConfigFeature].Elements[opt]; ok {
if val, ok := s.legacyKconfig[opt]; ok {
labels[ConfigFeature+"."+opt] = val
}
}
@ -124,10 +126,12 @@ func (s *kernelSource) Discover() error {
}
// Read kconfig
if kconfig, err := parseKconfig(s.config.KconfigFile); err != nil {
if realKconfig, legacyKconfig, err := parseKconfig(s.config.KconfigFile); err != nil {
s.legacyKconfig = nil
klog.Errorf("failed to read kconfig: %s", err)
} else {
s.features.Values[ConfigFeature] = feature.NewValueFeatures(kconfig)
s.features.Values[ConfigFeature] = feature.NewValueFeatures(realKconfig)
s.legacyKconfig = legacyKconfig
}
if kmods, err := getLoadedModules(); err != nil {
@ -155,6 +159,10 @@ func (s *kernelSource) GetFeatures() *feature.DomainFeatures {
return s.features
}
func GetLegacyKconfig() map[string]string {
return src.legacyKconfig
}
func init() {
source.Register(&src)
}