2019-02-08 21:43:54 +02:00
|
|
|
/*
|
|
|
|
Copyright 2019 The Kubernetes Authors.
|
|
|
|
|
|
|
|
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 nfdworker
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/tls"
|
|
|
|
"crypto/x509"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"regexp"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"golang.org/x/net/context"
|
|
|
|
"google.golang.org/grpc"
|
|
|
|
"google.golang.org/grpc/credentials"
|
|
|
|
"k8s.io/apimachinery/pkg/util/validation"
|
|
|
|
pb "sigs.k8s.io/node-feature-discovery/pkg/labeler"
|
|
|
|
"sigs.k8s.io/node-feature-discovery/pkg/version"
|
|
|
|
"sigs.k8s.io/node-feature-discovery/source"
|
|
|
|
"sigs.k8s.io/node-feature-discovery/source/cpu"
|
2020-02-16 17:43:12 +02:00
|
|
|
"sigs.k8s.io/node-feature-discovery/source/custom"
|
2019-02-08 21:43:54 +02:00
|
|
|
"sigs.k8s.io/node-feature-discovery/source/fake"
|
|
|
|
"sigs.k8s.io/node-feature-discovery/source/iommu"
|
|
|
|
"sigs.k8s.io/node-feature-discovery/source/kernel"
|
|
|
|
"sigs.k8s.io/node-feature-discovery/source/local"
|
|
|
|
"sigs.k8s.io/node-feature-discovery/source/memory"
|
|
|
|
"sigs.k8s.io/node-feature-discovery/source/network"
|
|
|
|
"sigs.k8s.io/node-feature-discovery/source/panic_fake"
|
|
|
|
"sigs.k8s.io/node-feature-discovery/source/pci"
|
|
|
|
"sigs.k8s.io/node-feature-discovery/source/storage"
|
|
|
|
"sigs.k8s.io/node-feature-discovery/source/system"
|
usb: Add support for USB device discovery
This builds on the PCI support to enable the discovery of USB devices.
This is primarily intended to be used for the discovery of Edge-based
heterogeneous accelerators that are connected via USB, such as the Coral
USB Accelerator and the Intel NCS2 - our main motivation for adding this
capability to NFD, and as part of our work in the SODALITE H2020
project.
USB devices may define their base class at either the device or
interface levels. In the case where no device class is set, the
per-device interfaces are enumerated instead. USB devices may
furthermore have multiple interfaces, which may or may not use the
identical class across each interface. We therefore report device
existence for each unique class definition to enable more fine-grained
labelling and node selection.
The default labelling format includes the class, vendor and device
(product) IDs, as follows:
feature.node.kubernetes.io/usb-fe_1a6e_089a.present=true
As with PCI, a subset of device classes are whitelisted for matching.
By default, there are only a subset of device classes under which
accelerators tend to be mapped, which is used as the basis for
the whitelist. These are:
- Video
- Miscellaneous
- Application Specific
- Vendor Specific
For those interested in matching other classes, this may be extended
by using the UsbId rule provided through the custom source. A full
list of class codes is provided by the USB-IF at:
https://www.usb.org/defined-class-codes
For the moment, owing to a lack of a demonstrable use case, neither
the subclass nor the protocol information are exposed. If this
becomes necessary, support for these attributes can be trivially
added.
Signed-off-by: Paul Mundt <paul.mundt@adaptant.io>
2020-05-14 22:32:55 +02:00
|
|
|
"sigs.k8s.io/node-feature-discovery/source/usb"
|
2020-04-22 09:59:01 +03:00
|
|
|
"sigs.k8s.io/yaml"
|
2019-02-08 21:43:54 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// package loggers
|
|
|
|
var (
|
|
|
|
stdoutLogger = log.New(os.Stdout, "", log.LstdFlags)
|
|
|
|
stderrLogger = log.New(os.Stderr, "", log.LstdFlags)
|
|
|
|
nodeName = os.Getenv("NODE_NAME")
|
|
|
|
)
|
|
|
|
|
|
|
|
// Global config
|
|
|
|
type NFDConfig struct {
|
|
|
|
Sources struct {
|
2020-05-19 14:57:39 +03:00
|
|
|
CPU *cpu.NFDConfig `json:"cpu,omitempty"`
|
2019-02-08 21:43:54 +02:00
|
|
|
Kernel *kernel.NFDConfig `json:"kernel,omitempty"`
|
|
|
|
Pci *pci.NFDConfig `json:"pci,omitempty"`
|
usb: Add support for USB device discovery
This builds on the PCI support to enable the discovery of USB devices.
This is primarily intended to be used for the discovery of Edge-based
heterogeneous accelerators that are connected via USB, such as the Coral
USB Accelerator and the Intel NCS2 - our main motivation for adding this
capability to NFD, and as part of our work in the SODALITE H2020
project.
USB devices may define their base class at either the device or
interface levels. In the case where no device class is set, the
per-device interfaces are enumerated instead. USB devices may
furthermore have multiple interfaces, which may or may not use the
identical class across each interface. We therefore report device
existence for each unique class definition to enable more fine-grained
labelling and node selection.
The default labelling format includes the class, vendor and device
(product) IDs, as follows:
feature.node.kubernetes.io/usb-fe_1a6e_089a.present=true
As with PCI, a subset of device classes are whitelisted for matching.
By default, there are only a subset of device classes under which
accelerators tend to be mapped, which is used as the basis for
the whitelist. These are:
- Video
- Miscellaneous
- Application Specific
- Vendor Specific
For those interested in matching other classes, this may be extended
by using the UsbId rule provided through the custom source. A full
list of class codes is provided by the USB-IF at:
https://www.usb.org/defined-class-codes
For the moment, owing to a lack of a demonstrable use case, neither
the subclass nor the protocol information are exposed. If this
becomes necessary, support for these attributes can be trivially
added.
Signed-off-by: Paul Mundt <paul.mundt@adaptant.io>
2020-05-14 22:32:55 +02:00
|
|
|
Usb *usb.NFDConfig `json:"usb,omitempty"`
|
2020-02-16 17:43:12 +02:00
|
|
|
Custom *custom.NFDConfig `json:"custom,omitempty"`
|
2019-02-08 21:43:54 +02:00
|
|
|
} `json:"sources,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
var config = NFDConfig{}
|
|
|
|
|
|
|
|
// Labels are a Kubernetes representation of discovered features.
|
|
|
|
type Labels map[string]string
|
|
|
|
|
|
|
|
// Command line arguments
|
|
|
|
type Args struct {
|
|
|
|
LabelWhiteList string
|
|
|
|
CaFile string
|
|
|
|
CertFile string
|
|
|
|
KeyFile string
|
|
|
|
ConfigFile string
|
|
|
|
NoPublish bool
|
|
|
|
Options string
|
|
|
|
Oneshot bool
|
|
|
|
Server string
|
|
|
|
ServerNameOverride string
|
|
|
|
SleepInterval time.Duration
|
|
|
|
Sources []string
|
|
|
|
}
|
|
|
|
|
|
|
|
type NfdWorker interface {
|
|
|
|
Run() error
|
|
|
|
}
|
|
|
|
|
|
|
|
type nfdWorker struct {
|
2019-08-27 23:27:40 +03:00
|
|
|
args Args
|
|
|
|
clientConn *grpc.ClientConn
|
|
|
|
client pb.LabelerClient
|
2019-02-08 21:43:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create new NfdWorker instance.
|
2020-05-19 14:57:39 +03:00
|
|
|
func NewNfdWorker(args Args) (NfdWorker, error) {
|
2019-02-08 21:43:54 +02:00
|
|
|
nfd := &nfdWorker{args: args}
|
|
|
|
if args.SleepInterval > 0 && args.SleepInterval < time.Second {
|
|
|
|
stderrLogger.Printf("WARNING: too short sleep-intervall specified (%s), forcing to 1s", args.SleepInterval.String())
|
|
|
|
args.SleepInterval = time.Second
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check TLS related args
|
|
|
|
if args.CertFile != "" || args.KeyFile != "" || args.CaFile != "" {
|
|
|
|
if args.CertFile == "" {
|
|
|
|
return nfd, fmt.Errorf("--cert-file needs to be specified alongside --key-file and --ca-file")
|
|
|
|
}
|
|
|
|
if args.KeyFile == "" {
|
|
|
|
return nfd, fmt.Errorf("--key-file needs to be specified alongside --cert-file and --ca-file")
|
|
|
|
}
|
|
|
|
if args.CaFile == "" {
|
|
|
|
return nfd, fmt.Errorf("--ca-file needs to be specified alongside --cert-file and --key-file")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nfd, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Run NfdWorker client. Returns if a fatal error is encountered, or, after
|
|
|
|
// one request if OneShot is set to 'true' in the worker args.
|
|
|
|
func (w *nfdWorker) Run() error {
|
|
|
|
stdoutLogger.Printf("Node Feature Discovery Worker %s", version.Get())
|
|
|
|
stdoutLogger.Printf("NodeName: '%s'", nodeName)
|
|
|
|
|
|
|
|
// Parse config
|
|
|
|
err := configParse(w.args.ConfigFile, w.args.Options)
|
|
|
|
if err != nil {
|
|
|
|
stderrLogger.Print(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Configure the parameters for feature discovery.
|
|
|
|
enabledSources, labelWhiteList, err := configureParameters(w.args.Sources, w.args.LabelWhiteList)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error occurred while configuring parameters: %s", err.Error())
|
|
|
|
}
|
|
|
|
|
2019-08-27 23:27:40 +03:00
|
|
|
// Connect to NFD master
|
|
|
|
err = w.connect()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to connect: %v", err)
|
|
|
|
}
|
|
|
|
defer w.disconnect()
|
|
|
|
|
|
|
|
for {
|
|
|
|
// Get the set of feature labels.
|
|
|
|
labels := createFeatureLabels(enabledSources, labelWhiteList)
|
|
|
|
|
|
|
|
// Update the node with the feature labels.
|
|
|
|
if w.client != nil {
|
|
|
|
err := advertiseFeatureLabels(w.client, labels)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to advertise labels: %s", err.Error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if w.args.Oneshot {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
if w.args.SleepInterval > 0 {
|
|
|
|
time.Sleep(w.args.SleepInterval)
|
|
|
|
} else {
|
|
|
|
w.disconnect()
|
|
|
|
// Sleep forever
|
|
|
|
select {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// connect creates a client connection to the NFD master
|
|
|
|
func (w *nfdWorker) connect() error {
|
|
|
|
// Return a dummy connection in case of dry-run
|
|
|
|
if w.args.NoPublish {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check that if a connection already exists
|
|
|
|
if w.clientConn != nil {
|
|
|
|
return fmt.Errorf("client connection already exists")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Dial and create a client
|
2019-07-02 16:42:52 +03:00
|
|
|
dialOpts := []grpc.DialOption{grpc.WithBlock(), grpc.WithTimeout(60 * time.Second)}
|
2019-02-08 21:43:54 +02:00
|
|
|
if w.args.CaFile != "" || w.args.CertFile != "" || w.args.KeyFile != "" {
|
|
|
|
// Load client cert for client authentication
|
|
|
|
cert, err := tls.LoadX509KeyPair(w.args.CertFile, w.args.KeyFile)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to load client certificate: %v", err)
|
|
|
|
}
|
|
|
|
// Load CA cert for server cert verification
|
|
|
|
caCert, err := ioutil.ReadFile(w.args.CaFile)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to read root certificate file: %v", err)
|
|
|
|
}
|
|
|
|
caPool := x509.NewCertPool()
|
|
|
|
if ok := caPool.AppendCertsFromPEM(caCert); !ok {
|
|
|
|
return fmt.Errorf("failed to add certificate from '%s'", w.args.CaFile)
|
|
|
|
}
|
|
|
|
// Create TLS config
|
|
|
|
tlsConfig := &tls.Config{
|
|
|
|
Certificates: []tls.Certificate{cert},
|
|
|
|
RootCAs: caPool,
|
|
|
|
ServerName: w.args.ServerNameOverride,
|
|
|
|
}
|
|
|
|
dialOpts = append(dialOpts, grpc.WithTransportCredentials(credentials.NewTLS(tlsConfig)))
|
|
|
|
} else {
|
|
|
|
dialOpts = append(dialOpts, grpc.WithInsecure())
|
|
|
|
}
|
|
|
|
conn, err := grpc.Dial(w.args.Server, dialOpts...)
|
|
|
|
if err != nil {
|
2019-08-27 23:27:40 +03:00
|
|
|
return err
|
2019-02-08 21:43:54 +02:00
|
|
|
}
|
2019-08-27 23:27:40 +03:00
|
|
|
w.clientConn = conn
|
|
|
|
w.client = pb.NewLabelerClient(conn)
|
2019-02-08 21:43:54 +02:00
|
|
|
|
2019-08-27 23:27:40 +03:00
|
|
|
return nil
|
|
|
|
}
|
2019-02-08 21:43:54 +02:00
|
|
|
|
2019-08-27 23:27:40 +03:00
|
|
|
// disconnect closes the connection to NFD master
|
|
|
|
func (w *nfdWorker) disconnect() {
|
|
|
|
if w.clientConn != nil {
|
|
|
|
w.clientConn.Close()
|
2019-02-08 21:43:54 +02:00
|
|
|
}
|
2019-08-27 23:27:40 +03:00
|
|
|
w.clientConn = nil
|
|
|
|
w.client = nil
|
2019-02-08 21:43:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Parse configuration options
|
|
|
|
func configParse(filepath string, overrides string) error {
|
2020-05-19 14:57:39 +03:00
|
|
|
config.Sources.CPU = &cpu.Config
|
2019-02-08 21:43:54 +02:00
|
|
|
config.Sources.Kernel = &kernel.Config
|
|
|
|
config.Sources.Pci = &pci.Config
|
usb: Add support for USB device discovery
This builds on the PCI support to enable the discovery of USB devices.
This is primarily intended to be used for the discovery of Edge-based
heterogeneous accelerators that are connected via USB, such as the Coral
USB Accelerator and the Intel NCS2 - our main motivation for adding this
capability to NFD, and as part of our work in the SODALITE H2020
project.
USB devices may define their base class at either the device or
interface levels. In the case where no device class is set, the
per-device interfaces are enumerated instead. USB devices may
furthermore have multiple interfaces, which may or may not use the
identical class across each interface. We therefore report device
existence for each unique class definition to enable more fine-grained
labelling and node selection.
The default labelling format includes the class, vendor and device
(product) IDs, as follows:
feature.node.kubernetes.io/usb-fe_1a6e_089a.present=true
As with PCI, a subset of device classes are whitelisted for matching.
By default, there are only a subset of device classes under which
accelerators tend to be mapped, which is used as the basis for
the whitelist. These are:
- Video
- Miscellaneous
- Application Specific
- Vendor Specific
For those interested in matching other classes, this may be extended
by using the UsbId rule provided through the custom source. A full
list of class codes is provided by the USB-IF at:
https://www.usb.org/defined-class-codes
For the moment, owing to a lack of a demonstrable use case, neither
the subclass nor the protocol information are exposed. If this
becomes necessary, support for these attributes can be trivially
added.
Signed-off-by: Paul Mundt <paul.mundt@adaptant.io>
2020-05-14 22:32:55 +02:00
|
|
|
config.Sources.Usb = &usb.Config
|
2020-02-16 17:43:12 +02:00
|
|
|
config.Sources.Custom = &custom.Config
|
2019-02-08 21:43:54 +02:00
|
|
|
|
|
|
|
data, err := ioutil.ReadFile(filepath)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Failed to read config file: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read config file
|
|
|
|
err = yaml.Unmarshal(data, &config)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Failed to parse config file: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parse config overrides
|
|
|
|
err = yaml.Unmarshal([]byte(overrides), &config)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Failed to parse --options: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// configureParameters returns all the variables required to perform feature
|
|
|
|
// discovery based on command line arguments.
|
|
|
|
func configureParameters(sourcesWhiteList []string, labelWhiteListStr string) (enabledSources []source.FeatureSource, labelWhiteList *regexp.Regexp, err error) {
|
|
|
|
// A map for lookup
|
|
|
|
sourcesWhiteListMap := map[string]struct{}{}
|
|
|
|
for _, s := range sourcesWhiteList {
|
|
|
|
sourcesWhiteListMap[strings.TrimSpace(s)] = struct{}{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Configure feature sources.
|
|
|
|
allSources := []source.FeatureSource{
|
|
|
|
cpu.Source{},
|
|
|
|
fake.Source{},
|
|
|
|
iommu.Source{},
|
|
|
|
kernel.Source{},
|
|
|
|
memory.Source{},
|
|
|
|
network.Source{},
|
|
|
|
panic_fake.Source{},
|
|
|
|
pci.Source{},
|
|
|
|
storage.Source{},
|
|
|
|
system.Source{},
|
usb: Add support for USB device discovery
This builds on the PCI support to enable the discovery of USB devices.
This is primarily intended to be used for the discovery of Edge-based
heterogeneous accelerators that are connected via USB, such as the Coral
USB Accelerator and the Intel NCS2 - our main motivation for adding this
capability to NFD, and as part of our work in the SODALITE H2020
project.
USB devices may define their base class at either the device or
interface levels. In the case where no device class is set, the
per-device interfaces are enumerated instead. USB devices may
furthermore have multiple interfaces, which may or may not use the
identical class across each interface. We therefore report device
existence for each unique class definition to enable more fine-grained
labelling and node selection.
The default labelling format includes the class, vendor and device
(product) IDs, as follows:
feature.node.kubernetes.io/usb-fe_1a6e_089a.present=true
As with PCI, a subset of device classes are whitelisted for matching.
By default, there are only a subset of device classes under which
accelerators tend to be mapped, which is used as the basis for
the whitelist. These are:
- Video
- Miscellaneous
- Application Specific
- Vendor Specific
For those interested in matching other classes, this may be extended
by using the UsbId rule provided through the custom source. A full
list of class codes is provided by the USB-IF at:
https://www.usb.org/defined-class-codes
For the moment, owing to a lack of a demonstrable use case, neither
the subclass nor the protocol information are exposed. If this
becomes necessary, support for these attributes can be trivially
added.
Signed-off-by: Paul Mundt <paul.mundt@adaptant.io>
2020-05-14 22:32:55 +02:00
|
|
|
usb.Source{},
|
2020-02-16 17:43:12 +02:00
|
|
|
custom.Source{},
|
2019-02-08 21:43:54 +02:00
|
|
|
// local needs to be the last source so that it is able to override
|
|
|
|
// labels from other sources
|
|
|
|
local.Source{},
|
|
|
|
}
|
|
|
|
|
|
|
|
enabledSources = []source.FeatureSource{}
|
|
|
|
for _, s := range allSources {
|
|
|
|
if _, enabled := sourcesWhiteListMap[s.Name()]; enabled {
|
|
|
|
enabledSources = append(enabledSources, s)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Compile labelWhiteList regex
|
|
|
|
labelWhiteList, err = regexp.Compile(labelWhiteListStr)
|
|
|
|
if err != nil {
|
|
|
|
stderrLogger.Printf("error parsing whitelist regex (%s): %s", labelWhiteListStr, err)
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return enabledSources, labelWhiteList, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// createFeatureLabels returns the set of feature labels from the enabled
|
|
|
|
// sources and the whitelist argument.
|
|
|
|
func createFeatureLabels(sources []source.FeatureSource, labelWhiteList *regexp.Regexp) (labels Labels) {
|
|
|
|
labels = Labels{}
|
|
|
|
|
|
|
|
// Do feature discovery from all configured sources.
|
|
|
|
for _, source := range sources {
|
|
|
|
labelsFromSource, err := getFeatureLabels(source)
|
|
|
|
if err != nil {
|
|
|
|
stderrLogger.Printf("discovery failed for source [%s]: %s", source.Name(), err.Error())
|
|
|
|
stderrLogger.Printf("continuing ...")
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
for name, value := range labelsFromSource {
|
|
|
|
// Log discovered feature.
|
|
|
|
stdoutLogger.Printf("%s = %s", name, value)
|
|
|
|
// Skip if label doesn't match labelWhiteList
|
|
|
|
if !labelWhiteList.Match([]byte(name)) {
|
|
|
|
stderrLogger.Printf("%s does not match the whitelist (%s) and will not be published.", name, labelWhiteList.String())
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
labels[name] = value
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return labels
|
|
|
|
}
|
|
|
|
|
|
|
|
// getFeatureLabels returns node labels for features discovered by the
|
|
|
|
// supplied source.
|
|
|
|
func getFeatureLabels(source source.FeatureSource) (labels Labels, err error) {
|
|
|
|
defer func() {
|
|
|
|
if r := recover(); r != nil {
|
|
|
|
stderrLogger.Printf("panic occurred during discovery of source [%s]: %v", source.Name(), r)
|
|
|
|
err = fmt.Errorf("%v", r)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
labels = Labels{}
|
|
|
|
features, err := source.Discover()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
for k, v := range features {
|
|
|
|
// Validate label name
|
|
|
|
prefix := source.Name() + "-"
|
|
|
|
switch source.(type) {
|
|
|
|
case local.Source:
|
|
|
|
// Do not prefix labels from the hooks
|
|
|
|
prefix = ""
|
|
|
|
}
|
|
|
|
|
|
|
|
label := prefix + k
|
|
|
|
// Validate label name. Use dummy namespace 'ns' because there is no
|
|
|
|
// function to validate just the name part
|
2019-04-05 15:31:40 -07:00
|
|
|
labelName := "ns/" + label
|
|
|
|
// Do not use dummy namespace if there is already a namespace
|
|
|
|
if strings.Contains(label, "/") {
|
|
|
|
labelName = label
|
|
|
|
}
|
|
|
|
errs := validation.IsQualifiedName(labelName)
|
2019-02-08 21:43:54 +02:00
|
|
|
if len(errs) > 0 {
|
|
|
|
stderrLogger.Printf("Ignoring invalid feature name '%s': %s", label, errs)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
value := fmt.Sprintf("%v", v)
|
|
|
|
// Validate label value
|
|
|
|
errs = validation.IsValidLabelValue(value)
|
|
|
|
if len(errs) > 0 {
|
|
|
|
stderrLogger.Printf("Ignoring invalid feature value %s=%s: %s", label, value, errs)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
labels[label] = value
|
|
|
|
}
|
|
|
|
return labels, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// advertiseFeatureLabels advertises the feature labels to a Kubernetes node
|
|
|
|
// via the NFD server.
|
|
|
|
func advertiseFeatureLabels(client pb.LabelerClient, labels Labels) error {
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
|
|
|
defer cancel()
|
|
|
|
|
2019-09-03 14:22:16 +03:00
|
|
|
stdoutLogger.Printf("Sending labeling request to nfd-master")
|
2019-02-08 21:43:54 +02:00
|
|
|
|
|
|
|
labelReq := pb.SetLabelsRequest{Labels: labels,
|
|
|
|
NfdVersion: version.Get(),
|
|
|
|
NodeName: nodeName}
|
|
|
|
_, err := client.SetLabels(ctx, &labelReq)
|
|
|
|
if err != nil {
|
|
|
|
stderrLogger.Printf("failed to set node labels: %v", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|