1
0
Fork 0
mirror of https://github.com/kubernetes-sigs/node-feature-discovery.git synced 2025-03-28 02:37:11 +00:00

source/local: support non-binary labels

Make the feature detector hooks, run by the 'local' feature source,
support non-binary label values. Hooks can advertise non-binary value by
using <name>=<value> format.

For example, /etc/kubernetes/node-feature-discovery/source.d/myhook
having the following stdout:
LABEL_1
LABEL_2=foobar

Would translate into the following labels:
feature.node.kubernetes.io/myhook-LABEL_1 = true
feature.node.kubernetes.io/myhook-LABEL_2 = foobar
This commit is contained in:
Markus Lehtonen 2018-09-21 15:16:41 +03:00
parent 9474ef0815
commit 4b066ed815
2 changed files with 22 additions and 12 deletions

View file

@ -124,7 +124,7 @@ the only label value published for features is the string `"true"`._
"feature.node.kubernetes.io/nfd-rdt-<feature-name>": "true",
"feature.node.kubernetes.io/nfd-selinux-<feature-name>": "true",
"feature.node.kubernetes.io/nfd-storage-<feature-name>": "true",
"feature.node.kubernetes.io/<hook name>-<feature name>": "true"
"feature.node.kubernetes.io/<hook name>-<feature name>": "<feature value>"
}
```
@ -193,11 +193,15 @@ available inside the Docker image so Volumes and VolumeMounts must be used if
standard NFD images are used.
The hook files must be executable. When executed, the hooks are supposed to
print all discovered features in `stdout`, one feature per line. The output in
stdout is used in the node label as is. Unlike other feature sources, the
source name (i.e. `local`) is not used as a prefix in the label. The full name
of node label name will conform to the following convention:
print all discovered features in `stdout`, one feature per line. Hook can
advertise both binary and non-binary labels, using either `<feature name>` or
`<feature name>=<feature value>` output format. Unlike other feature sources,
the source name (i.e. `local`) is not used as a prefix. The hook name is used
as the prefix, instead. The full name of node label name will conform to the
following convention:
`feature.node.kubernetes.io/<hook name>-<feature name>`.
The value of the label is either `true` (for binary labels) or `<feature name>`
(for non-binary labels).
`stderr` output of the hooks is propagated to NFD log so it can be used for
debugging and logging.
@ -207,12 +211,12 @@ User has a shell script
following `stdout` output:
```
MY_FEATURE_1
MY_FEATURE_2
MY_FEATURE_2=myvalue
```
which, in turn, will translate into the following node labels:
```
feature.node.kubernetes.io/my-source-MY_FEATURE_1=true
feature.node.kubernetes.io/my-source-MY_FEATURE_2=true
feature.node.kubernetes.io/my-source-MY_FEATURE_2=myvalue
```
**NOTE!** NFD will blindly run any executables placed/mounted in the hooks

View file

@ -24,6 +24,7 @@ import (
"os"
"os/exec"
"path/filepath"
"strings"
"github.com/kubernetes-incubator/node-feature-discovery/source"
)
@ -58,8 +59,8 @@ func (s Source) Discover() (source.Features, error) {
log.Printf("ERROR: source hook '%v' failed: %v", hook, err)
continue
}
for _, feature := range hookFeatures {
features[hook+"-"+feature] = true
for feature, value := range hookFeatures {
features[hook+"-"+feature] = value
}
}
@ -67,8 +68,8 @@ func (s Source) Discover() (source.Features, error) {
}
// Run one hook
func runHook(file string) ([]string, error) {
var features []string
func runHook(file string) (map[string]string, error) {
features := map[string]string{}
path := filepath.Join(hookDir, file)
filestat, err := os.Stat(path)
@ -106,7 +107,12 @@ func runHook(file string) ([]string, error) {
lines = bytes.Split(stdout.Bytes(), []byte("\n"))
for _, line := range lines {
if len(line) > 0 {
features = append(features, string(line))
lineSplit := strings.SplitN(string(line), "=", 2)
if len(lineSplit) == 1 {
features[lineSplit[0]] = "true"
} else {
features[lineSplit[0]] = lineSplit[1]
}
}
}
}