2018-07-13 12:55:28 +00:00
|
|
|
/*
|
2021-02-23 08:05:13 +00:00
|
|
|
Copyright 2018-2021 The Kubernetes Authors.
|
2018-07-13 12:55:28 +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 local
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"path/filepath"
|
2018-09-21 12:16:41 +00:00
|
|
|
"strings"
|
2023-07-28 15:40:04 +00:00
|
|
|
"time"
|
2018-07-13 12:55:28 +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-03 20:03:11 +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-13 12:55:28 +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 = "local"
|
|
|
|
|
2022-03-03 01:12:46 +00:00
|
|
|
// LabelFeature of this feature source
|
2021-03-03 20:03:11 +00:00
|
|
|
const LabelFeature = "label"
|
|
|
|
|
2023-07-28 15:40:04 +00:00
|
|
|
// ExpiryDateKey is the key of this feature source indicating
|
|
|
|
// when features should be removed.
|
|
|
|
const ExpiryDateKey = "# +expiry-time"
|
|
|
|
|
2018-07-13 12:55:28 +00:00
|
|
|
// Config
|
|
|
|
var (
|
2019-03-28 00:16:54 +00:00
|
|
|
featureFilesDir = "/etc/kubernetes/node-feature-discovery/features.d/"
|
|
|
|
hookDir = "/etc/kubernetes/node-feature-discovery/source.d/"
|
2018-07-13 12:55:28 +00:00
|
|
|
)
|
|
|
|
|
2021-03-03 20:03:11 +00:00
|
|
|
// localSource implements the FeatureSource and LabelSource interfaces.
|
|
|
|
type localSource struct {
|
2022-07-04 11:05:58 +00:00
|
|
|
features *nfdv1alpha1.Features
|
2022-08-22 10:46:14 +00:00
|
|
|
config *Config
|
|
|
|
}
|
|
|
|
|
|
|
|
type Config struct {
|
|
|
|
HooksEnabled bool `json:"hooksEnabled,omitempty"`
|
2021-03-03 20:03:11 +00:00
|
|
|
}
|
2018-07-13 12:55:28 +00:00
|
|
|
|
2021-03-01 07:02:22 +00:00
|
|
|
// Singleton source instance
|
|
|
|
var (
|
2022-08-22 10:46:14 +00:00
|
|
|
src = localSource{config: newDefaultConfig()}
|
|
|
|
_ source.FeatureSource = &src
|
|
|
|
_ source.LabelSource = &src
|
|
|
|
_ source.ConfigurableSource = &src
|
2021-03-01 07:02:22 +00:00
|
|
|
)
|
2020-04-21 19:03:37 +00:00
|
|
|
|
2021-03-01 07:02:22 +00:00
|
|
|
// Name method of the LabelSource interface
|
|
|
|
func (s *localSource) Name() string { return Name }
|
2020-04-21 19:03:37 +00:00
|
|
|
|
2022-08-22 10:46:14 +00:00
|
|
|
// NewConfig method of the LabelSource interface
|
|
|
|
func (s *localSource) NewConfig() source.Config { return newDefaultConfig() }
|
|
|
|
|
|
|
|
// GetConfig method of the LabelSource interface
|
|
|
|
func (s *localSource) GetConfig() source.Config { return s.config }
|
|
|
|
|
|
|
|
// SetConfig method of the LabelSource interface
|
|
|
|
func (s *localSource) SetConfig(conf source.Config) {
|
|
|
|
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))
|
2022-08-22 10:46:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-01 07:02:22 +00:00
|
|
|
// Priority method of the LabelSource interface
|
|
|
|
func (s *localSource) Priority() int { return 20 }
|
2020-04-21 19:03:37 +00:00
|
|
|
|
2021-03-01 16:39:49 +00:00
|
|
|
// GetLabels method of the LabelSource interface
|
|
|
|
func (s *localSource) GetLabels() (source.FeatureLabels, error) {
|
2021-03-03 20:03:11 +00:00
|
|
|
labels := make(source.FeatureLabels)
|
|
|
|
features := s.GetFeatures()
|
|
|
|
|
2022-06-14 14:02:49 +00:00
|
|
|
for k, v := range features.Attributes[LabelFeature].Elements {
|
2021-03-03 20:03:11 +00:00
|
|
|
labels[k] = v
|
|
|
|
}
|
|
|
|
return labels, nil
|
|
|
|
}
|
|
|
|
|
2022-08-22 10:46:14 +00:00
|
|
|
// newDefaultConfig returns a new config with pre-populated defaults
|
|
|
|
func newDefaultConfig() *Config {
|
|
|
|
return &Config{
|
2023-04-24 11:35:26 +00:00
|
|
|
HooksEnabled: false,
|
2022-08-22 10:46:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-03 20:03:11 +00:00
|
|
|
// Discover method of the FeatureSource interface
|
|
|
|
func (s *localSource) Discover() error {
|
2022-07-04 11:05:58 +00:00
|
|
|
s.features = nfdv1alpha1.NewFeatures()
|
2021-03-03 20:03:11 +00:00
|
|
|
|
2019-03-28 00:16:54 +00:00
|
|
|
featuresFromFiles, err := getFeaturesFromFiles()
|
|
|
|
if err != nil {
|
2023-05-16 17:26:18 +00:00
|
|
|
klog.ErrorS(err, "failed to read feature files")
|
2019-03-28 00:16:54 +00:00
|
|
|
}
|
|
|
|
|
2022-08-22 10:46:14 +00:00
|
|
|
if s.config.HooksEnabled {
|
|
|
|
|
2023-05-16 17:26:18 +00:00
|
|
|
klog.InfoS("starting hooks...")
|
2023-04-24 11:35:26 +00:00
|
|
|
klog.InfoS("NOTE: hooks are deprecated and will be completely removed in a future release.")
|
2022-08-22 10:46:14 +00:00
|
|
|
|
|
|
|
featuresFromHooks, err := getFeaturesFromHooks()
|
|
|
|
if err != nil {
|
2023-05-16 17:26:18 +00:00
|
|
|
klog.ErrorS(err, "failed to run hooks")
|
2022-08-22 10:46:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Merge features from hooks and files
|
|
|
|
for k, v := range featuresFromHooks {
|
|
|
|
if old, ok := featuresFromFiles[k]; ok {
|
2023-05-16 17:26:18 +00:00
|
|
|
klog.InfoS("overriding label value", "labelKey", k, "oldValue", old, "newValue", v)
|
2022-08-22 10:46:14 +00:00
|
|
|
}
|
|
|
|
featuresFromFiles[k] = v
|
2018-12-04 14:30:30 +00:00
|
|
|
}
|
2019-03-28 00:16:54 +00:00
|
|
|
}
|
2022-08-22 10:46:14 +00:00
|
|
|
|
2022-07-04 07:56:35 +00:00
|
|
|
s.features.Attributes[LabelFeature] = nfdv1alpha1.NewAttributeFeatures(featuresFromFiles)
|
2021-03-03 20:03:11 +00:00
|
|
|
|
2023-05-17 13:42:32 +00:00
|
|
|
klog.V(3).InfoS("discovered features", "featureSource", s.Name(), "features", utils.DelayedDumper(s.features))
|
2019-03-28 00:16:54 +00:00
|
|
|
|
2021-03-03 20:03:11 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetFeatures method of the FeatureSource Interface
|
2022-07-04 11:05:58 +00:00
|
|
|
func (s *localSource) GetFeatures() *nfdv1alpha1.Features {
|
2021-03-03 20:03:11 +00:00
|
|
|
if s.features == nil {
|
2022-07-04 11:05:58 +00:00
|
|
|
s.features = nfdv1alpha1.NewFeatures()
|
2021-03-03 20:03:11 +00:00
|
|
|
}
|
|
|
|
return s.features
|
2019-03-28 00:16:54 +00:00
|
|
|
}
|
|
|
|
|
2021-12-09 16:53:14 +00:00
|
|
|
func parseFeatures(lines [][]byte) map[string]string {
|
2021-03-03 20:03:11 +00:00
|
|
|
features := make(map[string]string)
|
2019-03-28 00:16:54 +00:00
|
|
|
|
2023-08-04 12:24:46 +00:00
|
|
|
for _, l := range lines {
|
|
|
|
line := strings.TrimSpace(string(l))
|
2019-03-28 00:16:54 +00:00
|
|
|
if len(line) > 0 {
|
2023-08-04 12:34:13 +00:00
|
|
|
// Skip comment lines
|
|
|
|
if strings.HasPrefix(line, "#") {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2023-08-04 12:24:46 +00:00
|
|
|
lineSplit := strings.SplitN(line, "=", 2)
|
2019-03-28 00:16:54 +00:00
|
|
|
|
2021-12-09 16:53:14 +00:00
|
|
|
key := lineSplit[0]
|
2019-03-28 00:16:54 +00:00
|
|
|
|
|
|
|
// Check if it's a boolean value
|
|
|
|
if len(lineSplit) == 1 {
|
|
|
|
features[key] = "true"
|
|
|
|
} else {
|
|
|
|
features[key] = lineSplit[1]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return features
|
|
|
|
}
|
|
|
|
|
|
|
|
// Run all hooks and get features
|
2021-03-03 20:03:11 +00:00
|
|
|
func getFeaturesFromHooks() (map[string]string, error) {
|
2022-08-22 10:46:14 +00:00
|
|
|
|
2021-03-03 20:03:11 +00:00
|
|
|
features := make(map[string]string)
|
2018-07-13 12:55:28 +00:00
|
|
|
|
2022-09-08 10:23:49 +00:00
|
|
|
files, err := os.ReadDir(hookDir)
|
2018-07-13 12:55:28 +00:00
|
|
|
if err != nil {
|
|
|
|
if os.IsNotExist(err) {
|
2023-05-16 17:26:18 +00:00
|
|
|
klog.InfoS("hook directory does not exist", "path", hookDir)
|
2018-07-13 12:55:28 +00:00
|
|
|
return features, nil
|
|
|
|
}
|
2021-02-25 17:12:06 +00:00
|
|
|
return features, fmt.Errorf("unable to access %v: %v", hookDir, err)
|
2018-07-13 12:55:28 +00:00
|
|
|
}
|
2022-08-22 10:46:14 +00:00
|
|
|
if len(files) > 0 {
|
2023-05-16 17:26:18 +00:00
|
|
|
klog.InfoS("hooks are DEPRECATED since v0.12.0 and support will be removed in a future release; use feature files instead")
|
2022-08-22 10:46:14 +00:00
|
|
|
}
|
2018-07-13 12:55:28 +00:00
|
|
|
|
|
|
|
for _, file := range files {
|
2019-03-28 00:16:54 +00:00
|
|
|
fileName := file.Name()
|
|
|
|
lines, err := runHook(fileName)
|
2018-07-13 12:55:28 +00:00
|
|
|
if err != nil {
|
2023-05-16 17:26:18 +00:00
|
|
|
klog.ErrorS(err, "failed to run hook", "fileName", fileName)
|
2018-07-13 12:55:28 +00:00
|
|
|
continue
|
|
|
|
}
|
2019-03-28 00:16:54 +00:00
|
|
|
|
|
|
|
// Append features
|
2021-12-09 16:53:14 +00:00
|
|
|
fileFeatures := parseFeatures(lines)
|
2023-05-17 13:42:32 +00:00
|
|
|
klog.V(4).InfoS("hook executed", "fileName", fileName, "features", utils.DelayedDumper(fileFeatures))
|
2021-12-09 17:02:07 +00:00
|
|
|
for k, v := range fileFeatures {
|
2019-03-28 00:16:54 +00:00
|
|
|
if old, ok := features[k]; ok {
|
2023-05-16 17:26:18 +00:00
|
|
|
klog.InfoS("overriding label value from another hook", "labelKey", k, "oldValue", old, "newValue", v, "fileName", fileName)
|
2018-11-26 12:18:05 +00:00
|
|
|
}
|
2019-03-28 00:16:54 +00:00
|
|
|
features[k] = v
|
2018-07-13 12:55:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return features, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Run one hook
|
2019-03-28 00:16:54 +00:00
|
|
|
func runHook(file string) ([][]byte, error) {
|
|
|
|
var lines [][]byte
|
2018-07-13 12:55:28 +00:00
|
|
|
|
|
|
|
path := filepath.Join(hookDir, file)
|
|
|
|
filestat, err := os.Stat(path)
|
|
|
|
if err != nil {
|
2023-05-16 17:26:18 +00:00
|
|
|
klog.ErrorS(err, "failed to get filestat, skipping hook", "path", path)
|
2019-03-28 00:16:54 +00:00
|
|
|
return lines, err
|
2018-07-13 12:55:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if filestat.Mode().IsRegular() {
|
|
|
|
cmd := exec.Command(path)
|
|
|
|
var stdout bytes.Buffer
|
|
|
|
var stderr bytes.Buffer
|
|
|
|
cmd.Stdout = &stdout
|
|
|
|
cmd.Stderr = &stderr
|
|
|
|
|
|
|
|
// Run hook
|
|
|
|
err = cmd.Run()
|
|
|
|
|
|
|
|
// Forward stderr to our logger
|
2019-03-28 00:16:54 +00:00
|
|
|
errLines := bytes.Split(stderr.Bytes(), []byte("\n"))
|
|
|
|
for i, line := range errLines {
|
|
|
|
if i == len(errLines)-1 && len(line) == 0 {
|
2018-07-13 12:55:28 +00:00
|
|
|
// Don't print the last empty string
|
|
|
|
break
|
|
|
|
}
|
2023-05-16 17:26:18 +00:00
|
|
|
klog.InfoS(fmt.Sprintf("%s: %s", file, line))
|
2018-07-13 12:55:28 +00:00
|
|
|
}
|
|
|
|
|
2019-03-28 00:16:54 +00:00
|
|
|
// Do not return any lines if an error occurred
|
2018-07-13 12:55:28 +00:00
|
|
|
if err != nil {
|
2019-03-28 00:16:54 +00:00
|
|
|
return lines, err
|
2018-07-13 12:55:28 +00:00
|
|
|
}
|
|
|
|
lines = bytes.Split(stdout.Bytes(), []byte("\n"))
|
2019-03-28 00:16:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return lines, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read all files to get features
|
2021-03-03 20:03:11 +00:00
|
|
|
func getFeaturesFromFiles() (map[string]string, error) {
|
|
|
|
features := make(map[string]string)
|
2019-03-28 00:16:54 +00:00
|
|
|
|
2022-09-08 10:23:49 +00:00
|
|
|
files, err := os.ReadDir(featureFilesDir)
|
2019-03-28 00:16:54 +00:00
|
|
|
if err != nil {
|
|
|
|
if os.IsNotExist(err) {
|
2023-05-16 17:26:18 +00:00
|
|
|
klog.InfoS("features directory does not exist", "path", featureFilesDir)
|
2019-03-28 00:16:54 +00:00
|
|
|
return features, nil
|
|
|
|
}
|
2021-02-25 17:12:06 +00:00
|
|
|
return features, fmt.Errorf("unable to access %v: %v", featureFilesDir, err)
|
2019-03-28 00:16:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, file := range files {
|
|
|
|
fileName := file.Name()
|
|
|
|
lines, err := getFileContent(fileName)
|
|
|
|
if err != nil {
|
2023-05-16 17:26:18 +00:00
|
|
|
klog.ErrorS(err, "failed to read file", "fileName", fileName)
|
2019-03-28 00:16:54 +00:00
|
|
|
continue
|
|
|
|
}
|
2023-07-28 15:46:17 +00:00
|
|
|
|
2019-03-28 00:16:54 +00:00
|
|
|
// Append features
|
2021-12-09 16:53:14 +00:00
|
|
|
fileFeatures := parseFeatures(lines)
|
2023-07-28 15:40:04 +00:00
|
|
|
|
|
|
|
// Check expiration of file features
|
2023-07-28 15:46:17 +00:00
|
|
|
expiryDate, err := getExpirationDate(lines)
|
|
|
|
if err != nil {
|
|
|
|
klog.ErrorS(err, "failed to parse feature file expiry date", "fileName", fileName)
|
|
|
|
continue
|
|
|
|
}
|
2023-07-28 15:40:04 +00:00
|
|
|
|
2023-07-28 15:46:17 +00:00
|
|
|
if expiryDate.Before(time.Now()) {
|
|
|
|
continue
|
2023-07-28 15:40:04 +00:00
|
|
|
}
|
|
|
|
|
2023-05-17 13:42:32 +00:00
|
|
|
klog.V(4).InfoS("feature file read", "fileName", fileName, "features", utils.DelayedDumper(fileFeatures))
|
2021-12-09 17:02:07 +00:00
|
|
|
for k, v := range fileFeatures {
|
2019-03-28 00:16:54 +00:00
|
|
|
if old, ok := features[k]; ok {
|
2023-05-16 17:26:18 +00:00
|
|
|
klog.InfoS("overriding label value from another feature file", "labelKey", k, "oldValue", old, "newValue", v, "fileName", fileName)
|
2018-07-13 12:55:28 +00:00
|
|
|
}
|
2019-03-28 00:16:54 +00:00
|
|
|
features[k] = v
|
2018-07-13 12:55:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return features, nil
|
|
|
|
}
|
2019-03-28 00:16:54 +00:00
|
|
|
|
2023-07-28 15:46:17 +00:00
|
|
|
// Return the expiration date of a feature file
|
|
|
|
func getExpirationDate(lines [][]byte) (time.Time, error) {
|
|
|
|
for _, line := range lines {
|
|
|
|
if len(line) > 0 {
|
|
|
|
lineSplit := strings.SplitN(string(line), ":", 2)
|
|
|
|
|
|
|
|
key := lineSplit[0]
|
|
|
|
|
|
|
|
if key == ExpiryDateKey {
|
|
|
|
expiryDate, err := time.Parse(time.RFC3339, lineSplit[1])
|
|
|
|
if err != nil {
|
|
|
|
return time.Now(), err
|
|
|
|
}
|
|
|
|
return expiryDate, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return time.Now(), nil
|
|
|
|
}
|
|
|
|
|
2019-03-28 00:16:54 +00:00
|
|
|
// Read one file
|
|
|
|
func getFileContent(fileName string) ([][]byte, error) {
|
|
|
|
var lines [][]byte
|
|
|
|
|
|
|
|
path := filepath.Join(featureFilesDir, fileName)
|
|
|
|
filestat, err := os.Stat(path)
|
|
|
|
if err != nil {
|
2023-05-16 17:26:18 +00:00
|
|
|
klog.ErrorS(err, "failed to get filestat, skipping features file", "path", path)
|
2019-03-28 00:16:54 +00:00
|
|
|
return lines, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if filestat.Mode().IsRegular() {
|
2022-09-08 10:23:49 +00:00
|
|
|
fileContent, err := os.ReadFile(path)
|
2019-03-28 00:16:54 +00:00
|
|
|
|
|
|
|
// Do not return any lines if an error occurred
|
|
|
|
if err != nil {
|
|
|
|
return lines, err
|
|
|
|
}
|
|
|
|
lines = bytes.Split(fileContent, []byte("\n"))
|
|
|
|
}
|
|
|
|
|
|
|
|
return lines, nil
|
|
|
|
}
|
2021-03-01 07:02:22 +00:00
|
|
|
|
|
|
|
func init() {
|
|
|
|
source.Register(&src)
|
|
|
|
}
|