2018-07-09 13:27:24 +00:00
|
|
|
/*
|
2021-02-23 08:05:13 +00:00
|
|
|
Copyright 2018-2021 The Kubernetes Authors.
|
2018-07-09 13:27:24 +00:00
|
|
|
|
|
|
|
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 (
|
|
|
|
"regexp"
|
2020-11-23 13:51:11 +00:00
|
|
|
"strings"
|
2018-07-09 13:27:24 +00:00
|
|
|
|
2021-02-23 08:05:13 +00:00
|
|
|
"k8s.io/klog/v2"
|
|
|
|
|
2018-11-28 12:30:03 +00:00
|
|
|
"sigs.k8s.io/node-feature-discovery/source"
|
2020-08-24 16:39:17 +00:00
|
|
|
"sigs.k8s.io/node-feature-discovery/source/internal/kernelutils"
|
2018-07-09 13:27:24 +00:00
|
|
|
)
|
|
|
|
|
2018-07-06 11:11:07 +00:00
|
|
|
// Configuration file options
|
2020-04-21 19:03:37 +00:00
|
|
|
type Config struct {
|
2018-07-06 11:11:07 +00:00
|
|
|
KconfigFile string
|
|
|
|
ConfigOpts []string `json:"configOpts,omitempty"`
|
2018-07-05 13:29:38 +00:00
|
|
|
}
|
|
|
|
|
2020-04-21 19:03:37 +00:00
|
|
|
// newDefaultConfig returns a new config with pre-populated defaults
|
|
|
|
func newDefaultConfig() *Config {
|
|
|
|
return &Config{
|
|
|
|
KconfigFile: "",
|
|
|
|
ConfigOpts: []string{
|
|
|
|
"NO_HZ",
|
|
|
|
"NO_HZ_IDLE",
|
|
|
|
"NO_HZ_FULL",
|
|
|
|
"PREEMPT",
|
|
|
|
},
|
|
|
|
}
|
2018-07-06 11:11:07 +00:00
|
|
|
}
|
|
|
|
|
2018-07-09 13:27:24 +00:00
|
|
|
// Implement FeatureSource interface
|
2020-04-21 19:03:37 +00:00
|
|
|
type Source struct {
|
|
|
|
config *Config
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Source) Name() string { return "kernel" }
|
|
|
|
|
|
|
|
// NewConfig method of the FeatureSource interface
|
|
|
|
func (s *Source) NewConfig() source.Config { return newDefaultConfig() }
|
2018-07-09 13:27:24 +00:00
|
|
|
|
2020-04-21 19:03:37 +00:00
|
|
|
// GetConfig method of the FeatureSource interface
|
|
|
|
func (s *Source) GetConfig() source.Config { return s.config }
|
|
|
|
|
|
|
|
// SetConfig method of the FeatureSource interface
|
|
|
|
func (s *Source) SetConfig(conf source.Config) {
|
|
|
|
switch v := conf.(type) {
|
|
|
|
case *Config:
|
|
|
|
s.config = v
|
|
|
|
default:
|
2021-02-23 08:05:13 +00:00
|
|
|
klog.Fatalf("invalid config type: %T", conf)
|
2020-04-21 19:03:37 +00:00
|
|
|
}
|
|
|
|
}
|
2018-07-09 13:27:24 +00:00
|
|
|
|
2020-04-21 19:03:37 +00:00
|
|
|
func (s *Source) Discover() (source.Features, error) {
|
2018-07-09 13:27:24 +00:00
|
|
|
features := source.Features{}
|
|
|
|
|
|
|
|
// Read kernel version
|
|
|
|
version, err := parseVersion()
|
|
|
|
if err != nil {
|
2021-02-25 17:12:06 +00:00
|
|
|
klog.Errorf("failed to get kernel version: %s", err)
|
2018-07-09 13:27:24 +00:00
|
|
|
} else {
|
|
|
|
for key := range version {
|
|
|
|
features["version."+key] = version[key]
|
|
|
|
}
|
|
|
|
}
|
2018-07-05 13:29:38 +00:00
|
|
|
|
|
|
|
// Read kconfig
|
2020-08-24 16:39:17 +00:00
|
|
|
kconfig, err := kernelutils.ParseKconfig(s.config.KconfigFile)
|
2018-07-05 13:29:38 +00:00
|
|
|
if err != nil {
|
2021-02-25 17:12:06 +00:00
|
|
|
klog.Errorf("failed to read kconfig: %s", err)
|
2018-07-05 13:29:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check flags
|
2020-04-21 19:03:37 +00:00
|
|
|
for _, opt := range s.config.ConfigOpts {
|
2018-07-17 07:38:25 +00:00
|
|
|
if val, ok := kconfig[opt]; ok {
|
|
|
|
features["config."+opt] = val
|
2018-07-05 13:29:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-20 11:32:31 +00:00
|
|
|
selinux, err := SelinuxEnabled()
|
|
|
|
if err != nil {
|
2021-02-23 08:05:13 +00:00
|
|
|
klog.Warning(err)
|
2018-12-20 11:32:31 +00:00
|
|
|
} else if selinux {
|
|
|
|
features["selinux.enabled"] = true
|
|
|
|
}
|
|
|
|
|
2018-07-09 13:27:24 +00:00
|
|
|
return features, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read and parse kernel version
|
|
|
|
func parseVersion() (map[string]string, error) {
|
|
|
|
version := map[string]string{}
|
|
|
|
|
2020-08-24 18:21:35 +00:00
|
|
|
full, err := kernelutils.GetKernelVersion()
|
2018-07-09 13:27:24 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-05-14 16:17:04 +00:00
|
|
|
// Replace forbidden symbols
|
|
|
|
fullRegex := regexp.MustCompile("[^-A-Za-z0-9_.]")
|
|
|
|
full = fullRegex.ReplaceAllString(full, "_")
|
2020-11-23 13:51:11 +00:00
|
|
|
// Label values must start and end with an alphanumeric
|
|
|
|
full = strings.Trim(full, "-_.")
|
2020-05-14 16:17:04 +00:00
|
|
|
|
2018-07-09 13:27:24 +00:00
|
|
|
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
|
|
|
|
}
|