mirror of
https://github.com/kubernetes-sigs/node-feature-discovery.git
synced 2024-12-14 11:57:51 +00:00
source/custom: refactor kconfig rule internal representation
Use a more well-defined form with separate key and value fields.
This commit is contained in:
parent
6e039818eb
commit
aca9e3efb8
1 changed files with 30 additions and 11 deletions
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
Copyright 2020 The Kubernetes Authors.
|
||||
Copyright 2020-2021 The Kubernetes Authors.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
|
@ -17,35 +17,54 @@ limitations under the License.
|
|||
package rules
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"encoding/json"
|
||||
"strings"
|
||||
|
||||
"sigs.k8s.io/node-feature-discovery/source/internal/kernelutils"
|
||||
)
|
||||
|
||||
// KconfigRule implements Rule
|
||||
type KconfigRule []string
|
||||
type KconfigRule []kconfig
|
||||
|
||||
var kConfigs map[string]struct{}
|
||||
type kconfig struct {
|
||||
Name string
|
||||
Value string
|
||||
}
|
||||
|
||||
var kConfigs map[string]string
|
||||
|
||||
func (kconfigs *KconfigRule) Match() (bool, error) {
|
||||
for _, f := range *kconfigs {
|
||||
if _, ok := kConfigs[f]; !ok {
|
||||
if v, ok := kConfigs[f.Name]; !ok || f.Value != v {
|
||||
return false, nil
|
||||
}
|
||||
}
|
||||
return true, nil
|
||||
}
|
||||
|
||||
func (c *kconfig) UnmarshalJSON(data []byte) error {
|
||||
var raw string
|
||||
if err := json.Unmarshal(data, &raw); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
split := strings.SplitN(raw, "=", 2)
|
||||
c.Name = split[0]
|
||||
if len(split) == 1 {
|
||||
c.Value = "true"
|
||||
} else {
|
||||
c.Value = split[1]
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func init() {
|
||||
kConfigs = make(map[string]struct{})
|
||||
kConfigs = make(map[string]string)
|
||||
|
||||
kconfig, err := kernelutils.ParseKconfig("")
|
||||
if err == nil {
|
||||
for k, v := range kconfig {
|
||||
if v != "true" {
|
||||
kConfigs[fmt.Sprintf("%s=%s", k, v)] = struct{}{}
|
||||
} else {
|
||||
kConfigs[k] = struct{}{}
|
||||
}
|
||||
kConfigs[k] = v
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue