2018-07-09 16:27:24 +03:00
|
|
|
/*
|
2021-02-23 10:05:13 +02:00
|
|
|
Copyright 2018-2021 The Kubernetes Authors.
|
2018-07-09 16:27:24 +03: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 (
|
2021-03-02 10:33:45 +02:00
|
|
|
"strconv"
|
2018-07-09 16:27:24 +03:00
|
|
|
|
2021-02-23 10:05:13 +02:00
|
|
|
"k8s.io/klog/v2"
|
|
|
|
|
2021-03-02 10:33:45 +02:00
|
|
|
"sigs.k8s.io/node-feature-discovery/pkg/api/feature"
|
|
|
|
"sigs.k8s.io/node-feature-discovery/pkg/utils"
|
2018-11-28 14:30:03 +02:00
|
|
|
"sigs.k8s.io/node-feature-discovery/source"
|
2018-07-09 16:27:24 +03:00
|
|
|
)
|
|
|
|
|
2022-01-19 12:42:06 +05:30
|
|
|
// Name of this feature source
|
2021-05-12 16:27:29 +03:00
|
|
|
const Name = "kernel"
|
|
|
|
|
2021-03-02 10:33:45 +02:00
|
|
|
const (
|
2021-03-03 21:02:06 +02:00
|
|
|
ConfigFeature = "config"
|
|
|
|
LoadedModuleFeature = "loadedmodule"
|
|
|
|
SelinuxFeature = "selinux"
|
|
|
|
VersionFeature = "version"
|
2021-03-02 10:33:45 +02:00
|
|
|
)
|
|
|
|
|
2018-07-06 14:11:07 +03:00
|
|
|
// Configuration file options
|
2020-04-21 22:03:37 +03:00
|
|
|
type Config struct {
|
2018-07-06 14:11:07 +03:00
|
|
|
KconfigFile string
|
|
|
|
ConfigOpts []string `json:"configOpts,omitempty"`
|
2018-07-05 16:29:38 +03:00
|
|
|
}
|
|
|
|
|
2020-04-21 22:03:37 +03: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 14:11:07 +03:00
|
|
|
}
|
|
|
|
|
2021-03-02 10:33:45 +02:00
|
|
|
// kernelSource implements the FeatureSource, LabelSource and ConfigurableSource interfaces.
|
2021-03-01 09:02:22 +02:00
|
|
|
type kernelSource struct {
|
2021-03-02 10:33:45 +02:00
|
|
|
config *Config
|
|
|
|
features *feature.DomainFeatures
|
2021-11-30 15:15:42 +02:00
|
|
|
// legacyKconfig contains mangled kconfig values used for
|
|
|
|
// kernel.config-<flag> labels and legacy kConfig custom rules.
|
|
|
|
legacyKconfig map[string]string
|
2020-04-21 22:03:37 +03:00
|
|
|
}
|
|
|
|
|
2021-03-01 09:02:22 +02:00
|
|
|
// Singleton source instance
|
|
|
|
var (
|
2021-03-02 10:33:45 +02:00
|
|
|
src = kernelSource{config: newDefaultConfig()}
|
|
|
|
_ source.FeatureSource = &src
|
2021-03-01 09:02:22 +02:00
|
|
|
_ source.LabelSource = &src
|
|
|
|
_ source.ConfigurableSource = &src
|
|
|
|
)
|
|
|
|
|
|
|
|
func (s *kernelSource) Name() string { return Name }
|
2020-04-21 22:03:37 +03:00
|
|
|
|
2021-03-01 07:45:32 +02:00
|
|
|
// NewConfig method of the LabelSource interface
|
2021-03-01 09:02:22 +02:00
|
|
|
func (s *kernelSource) NewConfig() source.Config { return newDefaultConfig() }
|
2018-07-09 16:27:24 +03:00
|
|
|
|
2021-03-01 07:45:32 +02:00
|
|
|
// GetConfig method of the LabelSource interface
|
2021-03-01 09:02:22 +02:00
|
|
|
func (s *kernelSource) GetConfig() source.Config { return s.config }
|
2020-04-21 22:03:37 +03:00
|
|
|
|
2021-03-01 07:45:32 +02:00
|
|
|
// SetConfig method of the LabelSource interface
|
2021-03-01 09:02:22 +02:00
|
|
|
func (s *kernelSource) SetConfig(conf source.Config) {
|
2020-04-21 22:03:37 +03:00
|
|
|
switch v := conf.(type) {
|
|
|
|
case *Config:
|
|
|
|
s.config = v
|
|
|
|
default:
|
2021-02-23 10:05:13 +02:00
|
|
|
klog.Fatalf("invalid config type: %T", conf)
|
2020-04-21 22:03:37 +03:00
|
|
|
}
|
|
|
|
}
|
2018-07-09 16:27:24 +03:00
|
|
|
|
2021-03-01 09:02:22 +02:00
|
|
|
// Priority method of the LabelSource interface
|
|
|
|
func (s *kernelSource) Priority() int { return 0 }
|
|
|
|
|
2021-03-01 18:39:49 +02:00
|
|
|
// GetLabels method of the LabelSource interface
|
|
|
|
func (s *kernelSource) GetLabels() (source.FeatureLabels, error) {
|
2021-03-02 10:33:45 +02:00
|
|
|
labels := source.FeatureLabels{}
|
|
|
|
features := s.GetFeatures()
|
2018-07-09 16:27:24 +03:00
|
|
|
|
2021-03-02 10:33:45 +02:00
|
|
|
for k, v := range features.Values[VersionFeature].Elements {
|
|
|
|
labels[VersionFeature+"."+k] = v
|
2018-07-05 16:29:38 +03:00
|
|
|
}
|
|
|
|
|
2020-04-21 22:03:37 +03:00
|
|
|
for _, opt := range s.config.ConfigOpts {
|
2021-11-30 15:15:42 +02:00
|
|
|
if val, ok := s.legacyKconfig[opt]; ok {
|
2021-03-02 10:33:45 +02:00
|
|
|
labels[ConfigFeature+"."+opt] = val
|
2018-07-05 16:29:38 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-23 20:12:20 +02:00
|
|
|
if enabled, ok := features.Values[SelinuxFeature].Elements["enabled"]; ok && enabled == "true" {
|
|
|
|
labels["selinux.enabled"] = "true"
|
2018-12-20 13:32:31 +02:00
|
|
|
}
|
|
|
|
|
2021-03-02 10:33:45 +02:00
|
|
|
return labels, nil
|
2018-07-09 16:27:24 +03:00
|
|
|
}
|
|
|
|
|
2021-03-02 10:33:45 +02:00
|
|
|
// 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)
|
|
|
|
}
|
2018-07-09 16:27:24 +03:00
|
|
|
|
2021-03-02 10:33:45 +02:00
|
|
|
// Read kconfig
|
2021-11-30 15:15:42 +02:00
|
|
|
if realKconfig, legacyKconfig, err := parseKconfig(s.config.KconfigFile); err != nil {
|
|
|
|
s.legacyKconfig = nil
|
2021-03-02 10:33:45 +02:00
|
|
|
klog.Errorf("failed to read kconfig: %s", err)
|
|
|
|
} else {
|
2021-11-30 15:15:42 +02:00
|
|
|
s.features.Values[ConfigFeature] = feature.NewValueFeatures(realKconfig)
|
|
|
|
s.legacyKconfig = legacyKconfig
|
2018-07-09 16:27:24 +03:00
|
|
|
}
|
|
|
|
|
2021-03-03 21:02:06 +02:00
|
|
|
if kmods, err := getLoadedModules(); err != nil {
|
|
|
|
klog.Errorf("failed to get loaded kernel modules: %v", err)
|
|
|
|
} else {
|
|
|
|
s.features.Keys[LoadedModuleFeature] = feature.NewKeyFeatures(kmods...)
|
|
|
|
}
|
|
|
|
|
2021-03-02 10:33:45 +02:00
|
|
|
if selinux, err := SelinuxEnabled(); err != nil {
|
|
|
|
klog.Warning(err)
|
|
|
|
} else {
|
|
|
|
s.features.Values[SelinuxFeature] = feature.NewValueFeatures(nil)
|
|
|
|
s.features.Values[SelinuxFeature].Elements["enabled"] = strconv.FormatBool(selinux)
|
2018-07-09 16:27:24 +03:00
|
|
|
}
|
|
|
|
|
2021-03-02 10:33:45 +02:00
|
|
|
utils.KlogDump(3, "discovered kernel features:", " ", s.features)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *kernelSource) GetFeatures() *feature.DomainFeatures {
|
|
|
|
if s.features == nil {
|
|
|
|
s.features = feature.NewDomainFeatures()
|
|
|
|
}
|
|
|
|
return s.features
|
2018-07-09 16:27:24 +03:00
|
|
|
}
|
2021-03-01 09:02:22 +02:00
|
|
|
|
2021-11-30 15:15:42 +02:00
|
|
|
func GetLegacyKconfig() map[string]string {
|
|
|
|
return src.legacyKconfig
|
|
|
|
}
|
|
|
|
|
2021-03-01 09:02:22 +02:00
|
|
|
func init() {
|
|
|
|
source.Register(&src)
|
|
|
|
}
|