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"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"path/filepath"
|
2018-09-21 12:16:41 +00:00
|
|
|
"strings"
|
2018-07-13 12:55:28 +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"
|
2018-07-13 12:55:28 +00:00
|
|
|
)
|
|
|
|
|
2021-05-12 13:27:29 +00:00
|
|
|
const Name = "local"
|
|
|
|
|
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-01 05:45:32 +00:00
|
|
|
// Source implements LabelSource.
|
2018-07-13 12:55:28 +00:00
|
|
|
type Source struct{}
|
|
|
|
|
2021-03-01 05:45:32 +00:00
|
|
|
// Name method of the LabelSource interface
|
2021-07-07 11:32:11 +00:00
|
|
|
func (s Source) Name() string { return Name }
|
2018-07-13 12:55:28 +00:00
|
|
|
|
2021-03-01 05:45:32 +00:00
|
|
|
// NewConfig method of the LabelSource interface
|
2020-04-21 19:03:37 +00:00
|
|
|
func (s *Source) NewConfig() source.Config { return nil }
|
|
|
|
|
2021-03-01 05:45:32 +00:00
|
|
|
// GetConfig method of the LabelSource interface
|
2020-04-21 19:03:37 +00:00
|
|
|
func (s *Source) GetConfig() source.Config { return nil }
|
|
|
|
|
2021-03-01 05:45:32 +00:00
|
|
|
// SetConfig method of the LabelSource interface
|
2020-04-21 19:03:37 +00:00
|
|
|
func (s *Source) SetConfig(source.Config) {}
|
|
|
|
|
2021-03-01 05:45:32 +00:00
|
|
|
// Discover method of the LabelSource interface
|
|
|
|
func (s Source) Discover() (source.FeatureLabels, error) {
|
2019-03-28 00:16:54 +00:00
|
|
|
featuresFromHooks, err := getFeaturesFromHooks()
|
|
|
|
if err != nil {
|
2021-02-23 08:05:13 +00:00
|
|
|
klog.Error(err)
|
2019-03-28 00:16:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
featuresFromFiles, err := getFeaturesFromFiles()
|
|
|
|
if err != nil {
|
2021-02-23 08:05:13 +00:00
|
|
|
klog.Error(err)
|
2019-03-28 00:16:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Merge features from hooks and files
|
|
|
|
for k, v := range featuresFromHooks {
|
2018-12-04 14:30:30 +00:00
|
|
|
if old, ok := featuresFromFiles[k]; ok {
|
2021-02-23 08:05:13 +00:00
|
|
|
klog.Warningf("overriding label '%s': value changed from '%s' to '%s'",
|
2018-12-04 14:30:30 +00:00
|
|
|
k, old, v)
|
|
|
|
}
|
2019-03-28 00:16:54 +00:00
|
|
|
featuresFromFiles[k] = v
|
|
|
|
}
|
|
|
|
|
|
|
|
return featuresFromFiles, nil
|
|
|
|
}
|
|
|
|
|
2021-03-01 05:45:32 +00:00
|
|
|
func parseFeatures(lines [][]byte, prefix string) source.FeatureLabels {
|
|
|
|
features := source.FeatureLabels{}
|
2019-03-28 00:16:54 +00:00
|
|
|
|
|
|
|
for _, line := range lines {
|
|
|
|
if len(line) > 0 {
|
|
|
|
lineSplit := strings.SplitN(string(line), "=", 2)
|
|
|
|
|
|
|
|
// Check if we need to add prefix
|
2019-04-05 22:31:40 +00:00
|
|
|
var key string
|
|
|
|
if strings.Contains(lineSplit[0], "/") {
|
|
|
|
if lineSplit[0][0] == '/' {
|
|
|
|
key = lineSplit[0][1:]
|
|
|
|
} else {
|
|
|
|
key = lineSplit[0]
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
key = prefix + "-" + 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-01 05:45:32 +00:00
|
|
|
func getFeaturesFromHooks() (source.FeatureLabels, error) {
|
|
|
|
features := source.FeatureLabels{}
|
2018-07-13 12:55:28 +00:00
|
|
|
|
|
|
|
files, err := ioutil.ReadDir(hookDir)
|
|
|
|
if err != nil {
|
|
|
|
if os.IsNotExist(err) {
|
2021-02-23 08:05:13 +00:00
|
|
|
klog.Infof("hook directory %v does not exist", 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
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
2021-02-23 08:05:13 +00:00
|
|
|
klog.Errorf("source local failed running hook '%v': %v", fileName, err)
|
2018-07-13 12:55:28 +00:00
|
|
|
continue
|
|
|
|
}
|
2019-03-28 00:16:54 +00:00
|
|
|
|
|
|
|
// Append features
|
|
|
|
for k, v := range parseFeatures(lines, fileName) {
|
|
|
|
if old, ok := features[k]; ok {
|
2021-02-23 08:05:13 +00:00
|
|
|
klog.Warningf("overriding label '%s' from another hook (%s): value changed from '%s' to '%s'",
|
2019-03-28 00:16:54 +00:00
|
|
|
k, fileName, old, v)
|
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 {
|
2021-02-23 08:05:13 +00:00
|
|
|
klog.Errorf("skipping %v, failed to get stat: %v", path, err)
|
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
|
|
|
|
}
|
2021-02-23 08:05:13 +00:00
|
|
|
klog.Errorf("%v: %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-01 05:45:32 +00:00
|
|
|
func getFeaturesFromFiles() (source.FeatureLabels, error) {
|
|
|
|
features := source.FeatureLabels{}
|
2019-03-28 00:16:54 +00:00
|
|
|
|
|
|
|
files, err := ioutil.ReadDir(featureFilesDir)
|
|
|
|
if err != nil {
|
|
|
|
if os.IsNotExist(err) {
|
2021-02-23 08:05:13 +00:00
|
|
|
klog.Infof("features directory %v does not exist", 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 {
|
2021-02-23 08:05:13 +00:00
|
|
|
klog.Errorf("source local failed reading file '%v': %v", fileName, err)
|
2019-03-28 00:16:54 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Append features
|
|
|
|
for k, v := range parseFeatures(lines, fileName) {
|
|
|
|
if old, ok := features[k]; ok {
|
2021-02-23 08:05:13 +00:00
|
|
|
klog.Warningf("overriding label '%s' from another features.d file (%s): value changed from '%s' to '%s'",
|
2019-03-28 00:16:54 +00:00
|
|
|
k, fileName, old, v)
|
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
|
|
|
|
|
|
|
// 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 {
|
2021-02-23 08:05:13 +00:00
|
|
|
klog.Errorf("skipping %v, failed to get stat: %v", path, err)
|
2019-03-28 00:16:54 +00:00
|
|
|
return lines, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if filestat.Mode().IsRegular() {
|
|
|
|
fileContent, err := ioutil.ReadFile(path)
|
|
|
|
|
|
|
|
// Do not return any lines if an error occurred
|
|
|
|
if err != nil {
|
|
|
|
return lines, err
|
|
|
|
}
|
|
|
|
lines = bytes.Split(fileContent, []byte("\n"))
|
|
|
|
}
|
|
|
|
|
|
|
|
return lines, nil
|
|
|
|
}
|