mirror of
https://github.com/kubernetes-sigs/node-feature-discovery.git
synced 2025-03-19 06:43:10 +00:00
source/kernel: implement FeatureSource
Separate feature discovery and creation of feature labels in the kernel source. Move kernelutils from source/internal back to the source/kernel package. Change the kconfig custom rule to rely on the FeatureSource interface (GetFeatures()) of the kernel source. Also, add minimalist unit test.
This commit is contained in:
parent
dd92c9a9ce
commit
0945019161
6 changed files with 144 additions and 81 deletions
|
@ -18,9 +18,11 @@ package rules
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"sigs.k8s.io/node-feature-discovery/source/internal/kernelutils"
|
"sigs.k8s.io/node-feature-discovery/source"
|
||||||
|
"sigs.k8s.io/node-feature-discovery/source/kernel"
|
||||||
)
|
)
|
||||||
|
|
||||||
// KconfigRule implements Rule
|
// KconfigRule implements Rule
|
||||||
|
@ -31,11 +33,14 @@ type kconfig struct {
|
||||||
Value string
|
Value string
|
||||||
}
|
}
|
||||||
|
|
||||||
var kConfigs map[string]string
|
|
||||||
|
|
||||||
func (kconfigs *KconfigRule) Match() (bool, error) {
|
func (kconfigs *KconfigRule) Match() (bool, error) {
|
||||||
|
options, ok := source.GetFeatureSource("kernel").GetFeatures().Values[kernel.ConfigFeature]
|
||||||
|
if !ok {
|
||||||
|
return false, fmt.Errorf("kernel config options not available")
|
||||||
|
}
|
||||||
|
|
||||||
for _, f := range *kconfigs {
|
for _, f := range *kconfigs {
|
||||||
if v, ok := kConfigs[f.Name]; !ok || f.Value != v {
|
if v, ok := options.Elements[f.Name]; !ok || f.Value != v {
|
||||||
return false, nil
|
return false, nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -57,14 +62,3 @@ func (c *kconfig) UnmarshalJSON(data []byte) error {
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func init() {
|
|
||||||
kConfigs = make(map[string]string)
|
|
||||||
|
|
||||||
kconfig, err := kernelutils.ParseKconfig("")
|
|
||||||
if err == nil {
|
|
||||||
for k, v := range kconfig {
|
|
||||||
kConfigs[k] = v
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
|
@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
|
||||||
limitations under the License.
|
limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package kernelutils
|
package kernel
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
@ -26,9 +26,9 @@ import (
|
||||||
"regexp"
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"k8s.io/apimachinery/pkg/util/validation"
|
||||||
"k8s.io/klog/v2"
|
"k8s.io/klog/v2"
|
||||||
|
|
||||||
"k8s.io/apimachinery/pkg/util/validation"
|
|
||||||
"sigs.k8s.io/node-feature-discovery/source"
|
"sigs.k8s.io/node-feature-discovery/source"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -52,13 +52,13 @@ func readKconfigGzip(filename string) ([]byte, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// ParseKconfig reads kconfig and return a map
|
// ParseKconfig reads kconfig and return a map
|
||||||
func ParseKconfig(configPath string) (map[string]string, error) {
|
func parseKconfig(configPath string) (map[string]string, error) {
|
||||||
kconfig := map[string]string{}
|
kconfig := map[string]string{}
|
||||||
raw := []byte(nil)
|
raw := []byte(nil)
|
||||||
var err error
|
var err error
|
||||||
var searchPaths []string
|
var searchPaths []string
|
||||||
|
|
||||||
kVer, err := GetKernelVersion()
|
kVer, err := getVersion()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
searchPaths = []string{
|
searchPaths = []string{
|
||||||
"/proc/config.gz",
|
"/proc/config.gz",
|
|
@ -17,17 +17,23 @@ limitations under the License.
|
||||||
package kernel
|
package kernel
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"regexp"
|
"strconv"
|
||||||
"strings"
|
|
||||||
|
|
||||||
"k8s.io/klog/v2"
|
"k8s.io/klog/v2"
|
||||||
|
|
||||||
|
"sigs.k8s.io/node-feature-discovery/pkg/api/feature"
|
||||||
|
"sigs.k8s.io/node-feature-discovery/pkg/utils"
|
||||||
"sigs.k8s.io/node-feature-discovery/source"
|
"sigs.k8s.io/node-feature-discovery/source"
|
||||||
"sigs.k8s.io/node-feature-discovery/source/internal/kernelutils"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
const Name = "kernel"
|
const Name = "kernel"
|
||||||
|
|
||||||
|
const (
|
||||||
|
ConfigFeature = "config"
|
||||||
|
SelinuxFeature = "selinux"
|
||||||
|
VersionFeature = "version"
|
||||||
|
)
|
||||||
|
|
||||||
// Configuration file options
|
// Configuration file options
|
||||||
type Config struct {
|
type Config struct {
|
||||||
KconfigFile string
|
KconfigFile string
|
||||||
|
@ -47,14 +53,16 @@ func newDefaultConfig() *Config {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// kernelSource implements the LabelSource and ConfigurableSource interfaces.
|
// kernelSource implements the FeatureSource, LabelSource and ConfigurableSource interfaces.
|
||||||
type kernelSource struct {
|
type kernelSource struct {
|
||||||
config *Config
|
config *Config
|
||||||
|
features *feature.DomainFeatures
|
||||||
}
|
}
|
||||||
|
|
||||||
// Singleton source instance
|
// Singleton source instance
|
||||||
var (
|
var (
|
||||||
src kernelSource
|
src = kernelSource{config: newDefaultConfig()}
|
||||||
|
_ source.FeatureSource = &src
|
||||||
_ source.LabelSource = &src
|
_ source.LabelSource = &src
|
||||||
_ source.ConfigurableSource = &src
|
_ source.ConfigurableSource = &src
|
||||||
)
|
)
|
||||||
|
@ -82,69 +90,62 @@ func (s *kernelSource) Priority() int { return 0 }
|
||||||
|
|
||||||
// GetLabels method of the LabelSource interface
|
// GetLabels method of the LabelSource interface
|
||||||
func (s *kernelSource) GetLabels() (source.FeatureLabels, error) {
|
func (s *kernelSource) GetLabels() (source.FeatureLabels, error) {
|
||||||
features := source.FeatureLabels{}
|
labels := source.FeatureLabels{}
|
||||||
|
features := s.GetFeatures()
|
||||||
|
|
||||||
// Read kernel version
|
for k, v := range features.Values[VersionFeature].Elements {
|
||||||
version, err := parseVersion()
|
labels[VersionFeature+"."+k] = v
|
||||||
if err != nil {
|
|
||||||
klog.Errorf("failed to get kernel version: %s", err)
|
|
||||||
} else {
|
|
||||||
for key := range version {
|
|
||||||
features["version."+key] = version[key]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Read kconfig
|
|
||||||
kconfig, err := kernelutils.ParseKconfig(s.config.KconfigFile)
|
|
||||||
if err != nil {
|
|
||||||
klog.Errorf("failed to read kconfig: %s", err)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check flags
|
// Check flags
|
||||||
for _, opt := range s.config.ConfigOpts {
|
for _, opt := range s.config.ConfigOpts {
|
||||||
if val, ok := kconfig[opt]; ok {
|
if val, ok := features.Values[ConfigFeature].Elements[opt]; ok {
|
||||||
features["config."+opt] = val
|
labels[ConfigFeature+"."+opt] = val
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
selinux, err := SelinuxEnabled()
|
for k, v := range features.Values[SelinuxFeature].Elements {
|
||||||
if err != nil {
|
labels[SelinuxFeature+"."+k] = v
|
||||||
|
}
|
||||||
|
|
||||||
|
return labels, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Discover method of the FeatureSource interface
|
||||||
|
func (s *kernelSource) Discover() error {
|
||||||
|
s.features = feature.NewDomainFeatures()
|
||||||
|
|
||||||
|
// Read kernel version
|
||||||
|
if version, err := parseVersion(); err != nil {
|
||||||
|
klog.Errorf("failed to get kernel version: %s", err)
|
||||||
|
} else {
|
||||||
|
s.features.Values[VersionFeature] = feature.NewValueFeatures(version)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Read kconfig
|
||||||
|
if kconfig, err := parseKconfig(s.config.KconfigFile); err != nil {
|
||||||
|
klog.Errorf("failed to read kconfig: %s", err)
|
||||||
|
} else {
|
||||||
|
s.features.Values[ConfigFeature] = feature.NewValueFeatures(kconfig)
|
||||||
|
}
|
||||||
|
|
||||||
|
if selinux, err := SelinuxEnabled(); err != nil {
|
||||||
klog.Warning(err)
|
klog.Warning(err)
|
||||||
} else if selinux {
|
} else {
|
||||||
features["selinux.enabled"] = true
|
s.features.Values[SelinuxFeature] = feature.NewValueFeatures(nil)
|
||||||
|
s.features.Values[SelinuxFeature].Elements["enabled"] = strconv.FormatBool(selinux)
|
||||||
}
|
}
|
||||||
|
|
||||||
return features, nil
|
utils.KlogDump(3, "discovered kernel features:", " ", s.features)
|
||||||
|
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Read and parse kernel version
|
func (s *kernelSource) GetFeatures() *feature.DomainFeatures {
|
||||||
func parseVersion() (map[string]string, error) {
|
if s.features == nil {
|
||||||
version := map[string]string{}
|
s.features = feature.NewDomainFeatures()
|
||||||
|
|
||||||
full, err := kernelutils.GetKernelVersion()
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
}
|
||||||
|
return s.features
|
||||||
// Replace forbidden symbols
|
|
||||||
fullRegex := regexp.MustCompile("[^-A-Za-z0-9_.]")
|
|
||||||
full = fullRegex.ReplaceAllString(full, "_")
|
|
||||||
// Label values must start and end with an alphanumeric
|
|
||||||
full = strings.Trim(full, "-_.")
|
|
||||||
|
|
||||||
version["full"] = full
|
|
||||||
|
|
||||||
// Regexp for parsing version components
|
|
||||||
re := regexp.MustCompile(`^(?P<major>\d+)(\.(?P<minor>\d+))?(\.(?P<revision>\d+))?(-.*)?$`)
|
|
||||||
if m := re.FindStringSubmatch(full); m != nil {
|
|
||||||
for i, name := range re.SubexpNames() {
|
|
||||||
if i != 0 && name != "" {
|
|
||||||
version[name] = m[i]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return version, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
Copyright 2018-2020 The Kubernetes Authors.
|
Copyright 2021 The Kubernetes Authors.
|
||||||
|
|
||||||
Licensed under the Apache License, Version 2.0 (the "License");
|
Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
you may not use this file except in compliance with the License.
|
you may not use this file except in compliance with the License.
|
||||||
|
@ -14,17 +14,23 @@ See the License for the specific language governing permissions and
|
||||||
limitations under the License.
|
limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package kernelutils
|
package kernel
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"io/ioutil"
|
"testing"
|
||||||
"strings"
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
"sigs.k8s.io/node-feature-discovery/pkg/api/feature"
|
||||||
)
|
)
|
||||||
|
|
||||||
func GetKernelVersion() (string, error) {
|
func TestKernelSource(t *testing.T) {
|
||||||
unameRaw, err := ioutil.ReadFile("/proc/sys/kernel/osrelease")
|
assert.Equal(t, src.Name(), Name)
|
||||||
if err != nil {
|
|
||||||
return "", err
|
// Check that GetLabels works with empty features
|
||||||
}
|
src.features = feature.NewDomainFeatures()
|
||||||
return strings.TrimSpace(string(unameRaw)), nil
|
l, err := src.GetLabels()
|
||||||
|
|
||||||
|
assert.Nil(t, err, err)
|
||||||
|
assert.Empty(t, l)
|
||||||
|
|
||||||
}
|
}
|
61
source/kernel/version.go
Normal file
61
source/kernel/version.go
Normal file
|
@ -0,0 +1,61 @@
|
||||||
|
/*
|
||||||
|
Copyright 2018-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.
|
||||||
|
You may obtain a copy of the License at
|
||||||
|
|
||||||
|
http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
|
||||||
|
Unless required by applicable law or agreed to in writing, software
|
||||||
|
distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
See the License for the specific language governing permissions and
|
||||||
|
limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package kernel
|
||||||
|
|
||||||
|
import (
|
||||||
|
"io/ioutil"
|
||||||
|
"regexp"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Read and parse kernel version
|
||||||
|
func parseVersion() (map[string]string, error) {
|
||||||
|
version := map[string]string{}
|
||||||
|
|
||||||
|
full, err := getVersion()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Replace forbidden symbols
|
||||||
|
fullRegex := regexp.MustCompile("[^-A-Za-z0-9_.]")
|
||||||
|
full = fullRegex.ReplaceAllString(full, "_")
|
||||||
|
// Label values must start and end with an alphanumeric
|
||||||
|
full = strings.Trim(full, "-_.")
|
||||||
|
|
||||||
|
version["full"] = full
|
||||||
|
|
||||||
|
// Regexp for parsing version components
|
||||||
|
re := regexp.MustCompile(`^(?P<major>\d+)(\.(?P<minor>\d+))?(\.(?P<revision>\d+))?(-.*)?$`)
|
||||||
|
if m := re.FindStringSubmatch(full); m != nil {
|
||||||
|
for i, name := range re.SubexpNames() {
|
||||||
|
if i != 0 && name != "" {
|
||||||
|
version[name] = m[i]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return version, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func getVersion() (string, error) {
|
||||||
|
unameRaw, err := ioutil.ReadFile("/proc/sys/kernel/osrelease")
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
return strings.TrimSpace(string(unameRaw)), nil
|
||||||
|
}
|
|
@ -65,6 +65,7 @@ func TestConfigurableSources(t *testing.T) {
|
||||||
|
|
||||||
func TestFeatureSources(t *testing.T) {
|
func TestFeatureSources(t *testing.T) {
|
||||||
sources := source.GetAllFeatureSources()
|
sources := source.GetAllFeatureSources()
|
||||||
|
assert.NotZero(t, len(sources))
|
||||||
|
|
||||||
for n, s := range sources {
|
for n, s := range sources {
|
||||||
msg := fmt.Sprintf("testing FeatureSource %q failed", n)
|
msg := fmt.Sprintf("testing FeatureSource %q failed", n)
|
||||||
|
|
Loading…
Add table
Reference in a new issue