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 (
|
2023-05-16 17:26:18 +00:00
|
|
|
"fmt"
|
2021-03-02 08:33:45 +00:00
|
|
|
"strconv"
|
2018-07-09 13:27:24 +00:00
|
|
|
|
2021-02-23 08:05:13 +00:00
|
|
|
"k8s.io/klog/v2"
|
|
|
|
|
2022-07-04 07:56:35 +00:00
|
|
|
nfdv1alpha1 "sigs.k8s.io/node-feature-discovery/pkg/apis/nfd/v1alpha1"
|
2021-03-02 08:33:45 +00:00
|
|
|
"sigs.k8s.io/node-feature-discovery/pkg/utils"
|
2018-11-28 12:30:03 +00:00
|
|
|
"sigs.k8s.io/node-feature-discovery/source"
|
2018-07-09 13:27:24 +00:00
|
|
|
)
|
|
|
|
|
2022-01-19 07:12:06 +00:00
|
|
|
// Name of this feature source
|
2021-05-12 13:27:29 +00:00
|
|
|
const Name = "kernel"
|
|
|
|
|
2021-03-02 08:33:45 +00:00
|
|
|
const (
|
2023-03-13 21:26:13 +00:00
|
|
|
ConfigFeature = "config"
|
|
|
|
LoadedModuleFeature = "loadedmodule"
|
|
|
|
SelinuxFeature = "selinux"
|
|
|
|
VersionFeature = "version"
|
|
|
|
EnabledModuleFeature = "enabledmodule"
|
2021-03-02 08:33:45 +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
|
|
|
}
|
|
|
|
|
2021-03-02 08:33:45 +00:00
|
|
|
// kernelSource implements the FeatureSource, LabelSource and ConfigurableSource interfaces.
|
2021-03-01 07:02:22 +00:00
|
|
|
type kernelSource struct {
|
2021-03-02 08:33:45 +00:00
|
|
|
config *Config
|
2022-07-04 11:05:58 +00:00
|
|
|
features *nfdv1alpha1.Features
|
2021-11-30 13:15:42 +00:00
|
|
|
// legacyKconfig contains mangled kconfig values used for
|
|
|
|
// kernel.config-<flag> labels and legacy kConfig custom rules.
|
|
|
|
legacyKconfig map[string]string
|
2020-04-21 19:03:37 +00:00
|
|
|
}
|
|
|
|
|
2021-03-01 07:02:22 +00:00
|
|
|
// Singleton source instance
|
|
|
|
var (
|
2021-03-02 08:33:45 +00:00
|
|
|
src = kernelSource{config: newDefaultConfig()}
|
|
|
|
_ source.FeatureSource = &src
|
2021-03-01 07:02:22 +00:00
|
|
|
_ source.LabelSource = &src
|
|
|
|
_ source.ConfigurableSource = &src
|
|
|
|
)
|
|
|
|
|
|
|
|
func (s *kernelSource) Name() string { return Name }
|
2020-04-21 19:03:37 +00:00
|
|
|
|
2021-03-01 05:45:32 +00:00
|
|
|
// NewConfig method of the LabelSource interface
|
2021-03-01 07:02:22 +00:00
|
|
|
func (s *kernelSource) NewConfig() source.Config { return newDefaultConfig() }
|
2018-07-09 13:27:24 +00:00
|
|
|
|
2021-03-01 05:45:32 +00:00
|
|
|
// GetConfig method of the LabelSource interface
|
2021-03-01 07:02:22 +00:00
|
|
|
func (s *kernelSource) GetConfig() source.Config { return s.config }
|
2020-04-21 19:03:37 +00:00
|
|
|
|
2021-03-01 05:45:32 +00:00
|
|
|
// SetConfig method of the LabelSource interface
|
2021-03-01 07:02:22 +00:00
|
|
|
func (s *kernelSource) SetConfig(conf source.Config) {
|
2020-04-21 19:03:37 +00:00
|
|
|
switch v := conf.(type) {
|
|
|
|
case *Config:
|
|
|
|
s.config = v
|
|
|
|
default:
|
2023-05-16 17:26:18 +00:00
|
|
|
panic(fmt.Sprintf("invalid config type: %T", conf))
|
2020-04-21 19:03:37 +00:00
|
|
|
}
|
|
|
|
}
|
2018-07-09 13:27:24 +00:00
|
|
|
|
2021-03-01 07:02:22 +00:00
|
|
|
// Priority method of the LabelSource interface
|
|
|
|
func (s *kernelSource) Priority() int { return 0 }
|
|
|
|
|
2021-03-01 16:39:49 +00:00
|
|
|
// GetLabels method of the LabelSource interface
|
|
|
|
func (s *kernelSource) GetLabels() (source.FeatureLabels, error) {
|
2021-03-02 08:33:45 +00:00
|
|
|
labels := source.FeatureLabels{}
|
|
|
|
features := s.GetFeatures()
|
2018-07-09 13:27:24 +00:00
|
|
|
|
2022-06-14 14:02:49 +00:00
|
|
|
for k, v := range features.Attributes[VersionFeature].Elements {
|
2021-03-02 08:33:45 +00:00
|
|
|
labels[VersionFeature+"."+k] = v
|
2018-07-05 13:29:38 +00:00
|
|
|
}
|
|
|
|
|
2020-04-21 19:03:37 +00:00
|
|
|
for _, opt := range s.config.ConfigOpts {
|
2021-11-30 13:15:42 +00:00
|
|
|
if val, ok := s.legacyKconfig[opt]; ok {
|
2021-03-02 08:33:45 +00:00
|
|
|
labels[ConfigFeature+"."+opt] = val
|
2018-07-05 13:29:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-14 14:02:49 +00:00
|
|
|
if enabled, ok := features.Attributes[SelinuxFeature].Elements["enabled"]; ok && enabled == "true" {
|
2021-11-23 18:12:20 +00:00
|
|
|
labels["selinux.enabled"] = "true"
|
2018-12-20 11:32:31 +00:00
|
|
|
}
|
|
|
|
|
2021-03-02 08:33:45 +00:00
|
|
|
return labels, nil
|
2018-07-09 13:27:24 +00:00
|
|
|
}
|
|
|
|
|
2021-03-02 08:33:45 +00:00
|
|
|
// Discover method of the FeatureSource interface
|
|
|
|
func (s *kernelSource) Discover() error {
|
2022-07-04 11:05:58 +00:00
|
|
|
s.features = nfdv1alpha1.NewFeatures()
|
2021-03-02 08:33:45 +00:00
|
|
|
|
|
|
|
// Read kernel version
|
|
|
|
if version, err := parseVersion(); err != nil {
|
2023-05-16 17:26:18 +00:00
|
|
|
klog.ErrorS(err, "failed to get kernel version")
|
2021-03-02 08:33:45 +00:00
|
|
|
} else {
|
2022-07-04 07:56:35 +00:00
|
|
|
s.features.Attributes[VersionFeature] = nfdv1alpha1.NewAttributeFeatures(version)
|
2021-03-02 08:33:45 +00:00
|
|
|
}
|
2018-07-09 13:27:24 +00:00
|
|
|
|
2021-03-02 08:33:45 +00:00
|
|
|
// Read kconfig
|
2021-11-30 13:15:42 +00:00
|
|
|
if realKconfig, legacyKconfig, err := parseKconfig(s.config.KconfigFile); err != nil {
|
|
|
|
s.legacyKconfig = nil
|
2023-05-16 17:26:18 +00:00
|
|
|
klog.ErrorS(err, "failed to read kconfig")
|
2021-03-02 08:33:45 +00:00
|
|
|
} else {
|
2022-07-04 07:56:35 +00:00
|
|
|
s.features.Attributes[ConfigFeature] = nfdv1alpha1.NewAttributeFeatures(realKconfig)
|
2021-11-30 13:15:42 +00:00
|
|
|
s.legacyKconfig = legacyKconfig
|
2018-07-09 13:27:24 +00:00
|
|
|
}
|
|
|
|
|
2023-03-13 21:26:13 +00:00
|
|
|
var enabledModules []string
|
2021-03-03 19:02:06 +00:00
|
|
|
if kmods, err := getLoadedModules(); err != nil {
|
2023-05-16 17:26:18 +00:00
|
|
|
klog.ErrorS(err, "failed to get loaded kernel modules")
|
2021-03-03 19:02:06 +00:00
|
|
|
} else {
|
2023-03-13 21:26:13 +00:00
|
|
|
enabledModules = append(enabledModules, kmods...)
|
2022-07-04 07:56:35 +00:00
|
|
|
s.features.Flags[LoadedModuleFeature] = nfdv1alpha1.NewFlagFeatures(kmods...)
|
2021-03-03 19:02:06 +00:00
|
|
|
}
|
|
|
|
|
2023-03-13 21:26:13 +00:00
|
|
|
if builtinMods, err := getBuiltinModules(); err != nil {
|
2023-05-16 17:26:18 +00:00
|
|
|
klog.ErrorS(err, "failed to get builtin kernel modules")
|
2023-03-13 21:26:13 +00:00
|
|
|
} else {
|
|
|
|
enabledModules = append(enabledModules, builtinMods...)
|
|
|
|
s.features.Flags[EnabledModuleFeature] = nfdv1alpha1.NewFlagFeatures(enabledModules...)
|
|
|
|
}
|
|
|
|
|
2021-03-02 08:33:45 +00:00
|
|
|
if selinux, err := SelinuxEnabled(); err != nil {
|
2023-05-16 17:26:18 +00:00
|
|
|
klog.ErrorS(err, "failed to detect selinux status")
|
2021-03-02 08:33:45 +00:00
|
|
|
} else {
|
2022-07-04 07:56:35 +00:00
|
|
|
s.features.Attributes[SelinuxFeature] = nfdv1alpha1.NewAttributeFeatures(nil)
|
2022-06-14 14:02:49 +00:00
|
|
|
s.features.Attributes[SelinuxFeature].Elements["enabled"] = strconv.FormatBool(selinux)
|
2018-07-09 13:27:24 +00:00
|
|
|
}
|
|
|
|
|
2021-03-02 08:33:45 +00:00
|
|
|
utils.KlogDump(3, "discovered kernel features:", " ", s.features)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-07-04 11:05:58 +00:00
|
|
|
func (s *kernelSource) GetFeatures() *nfdv1alpha1.Features {
|
2021-03-02 08:33:45 +00:00
|
|
|
if s.features == nil {
|
2022-07-04 11:05:58 +00:00
|
|
|
s.features = nfdv1alpha1.NewFeatures()
|
2021-03-02 08:33:45 +00:00
|
|
|
}
|
|
|
|
return s.features
|
2018-07-09 13:27:24 +00:00
|
|
|
}
|
2021-03-01 07:02:22 +00:00
|
|
|
|
2021-11-30 13:15:42 +00:00
|
|
|
func GetLegacyKconfig() map[string]string {
|
|
|
|
return src.legacyKconfig
|
|
|
|
}
|
|
|
|
|
2021-03-01 07:02:22 +00:00
|
|
|
func init() {
|
|
|
|
source.Register(&src)
|
|
|
|
}
|