2019-02-08 21:43:54 +02:00
|
|
|
/*
|
2021-02-19 15:43:31 +02:00
|
|
|
Copyright 2019-2021 The Kubernetes Authors.
|
2019-02-08 21:43:54 +02: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.
|
|
|
|
*/
|
|
|
|
|
2021-07-08 11:02:39 +03:00
|
|
|
package worker
|
2019-02-08 21:43:54 +02:00
|
|
|
|
|
|
|
import (
|
2020-04-21 22:03:37 +03:00
|
|
|
"encoding/json"
|
2019-02-08 21:43:54 +02:00
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
2020-11-27 08:26:22 +02:00
|
|
|
"path/filepath"
|
2019-02-08 21:43:54 +02:00
|
|
|
"regexp"
|
2021-03-01 09:02:22 +02:00
|
|
|
"sort"
|
2019-02-08 21:43:54 +02:00
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"golang.org/x/net/context"
|
|
|
|
"k8s.io/apimachinery/pkg/util/validation"
|
2021-02-23 10:05:13 +02:00
|
|
|
"k8s.io/klog/v2"
|
2021-02-19 15:43:31 +02:00
|
|
|
"sigs.k8s.io/yaml"
|
|
|
|
|
2021-04-23 08:39:54 +03:00
|
|
|
"sigs.k8s.io/node-feature-discovery/pkg/api/feature"
|
2019-02-08 21:43:54 +02:00
|
|
|
pb "sigs.k8s.io/node-feature-discovery/pkg/labeler"
|
2021-07-08 11:02:39 +03:00
|
|
|
nfdclient "sigs.k8s.io/node-feature-discovery/pkg/nfd-client"
|
2021-02-19 15:43:31 +02:00
|
|
|
"sigs.k8s.io/node-feature-discovery/pkg/utils"
|
2019-02-08 21:43:54 +02:00
|
|
|
"sigs.k8s.io/node-feature-discovery/pkg/version"
|
|
|
|
"sigs.k8s.io/node-feature-discovery/source"
|
2021-03-01 09:02:22 +02:00
|
|
|
|
|
|
|
// Register all source packages
|
|
|
|
_ "sigs.k8s.io/node-feature-discovery/source/cpu"
|
|
|
|
_ "sigs.k8s.io/node-feature-discovery/source/custom"
|
|
|
|
_ "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/pci"
|
|
|
|
_ "sigs.k8s.io/node-feature-discovery/source/storage"
|
|
|
|
_ "sigs.k8s.io/node-feature-discovery/source/system"
|
|
|
|
_ "sigs.k8s.io/node-feature-discovery/source/usb"
|
2019-02-08 21:43:54 +02:00
|
|
|
)
|
|
|
|
|
2022-01-19 12:42:06 +05:30
|
|
|
// NFDConfig contains the configuration settings of NfdWorker.
|
2019-02-08 21:43:54 +02:00
|
|
|
type NFDConfig struct {
|
2020-11-27 10:19:31 +02:00
|
|
|
Core coreConfig
|
2020-04-21 22:03:37 +03:00
|
|
|
Sources sourcesConfig
|
2019-02-08 21:43:54 +02:00
|
|
|
}
|
|
|
|
|
2020-11-27 10:19:31 +02:00
|
|
|
type coreConfig struct {
|
2021-02-23 20:42:17 +02:00
|
|
|
Klog map[string]string
|
2021-02-19 15:43:31 +02:00
|
|
|
LabelWhiteList utils.RegexpVal
|
2020-12-01 14:54:59 +02:00
|
|
|
NoPublish bool
|
2021-08-27 12:52:24 +03:00
|
|
|
FeatureSources []string
|
2021-11-25 11:24:09 +02:00
|
|
|
Sources *[]string
|
2021-11-25 10:58:08 +02:00
|
|
|
LabelSources []string
|
2020-12-01 14:54:59 +02:00
|
|
|
SleepInterval duration
|
2020-11-27 10:19:31 +02:00
|
|
|
}
|
|
|
|
|
2020-04-21 22:03:37 +03:00
|
|
|
type sourcesConfig map[string]source.Config
|
2019-02-08 21:43:54 +02:00
|
|
|
|
|
|
|
// Labels are a Kubernetes representation of discovered features.
|
|
|
|
type Labels map[string]string
|
|
|
|
|
2022-01-19 12:42:06 +05:30
|
|
|
// Args are the command line arguments of NfdWorker.
|
2019-02-08 21:43:54 +02:00
|
|
|
type Args struct {
|
2021-07-08 11:02:39 +03:00
|
|
|
nfdclient.Args
|
|
|
|
|
|
|
|
ConfigFile string
|
|
|
|
Oneshot bool
|
|
|
|
Options string
|
2021-02-19 15:43:31 +02:00
|
|
|
|
2021-02-23 20:42:17 +02:00
|
|
|
Klog map[string]*utils.KlogFlagVal
|
2021-02-19 15:43:31 +02:00
|
|
|
Overrides ConfigOverrideArgs
|
|
|
|
}
|
|
|
|
|
|
|
|
// ConfigOverrideArgs are args that override config file options
|
|
|
|
type ConfigOverrideArgs struct {
|
|
|
|
NoPublish *bool
|
|
|
|
|
|
|
|
// Deprecated
|
|
|
|
LabelWhiteList *utils.RegexpVal
|
2020-12-01 14:54:59 +02:00
|
|
|
SleepInterval *time.Duration
|
2021-12-03 09:22:43 +02:00
|
|
|
FeatureSources *utils.StringSliceVal
|
2021-11-25 10:58:08 +02:00
|
|
|
LabelSources *utils.StringSliceVal
|
2019-02-08 21:43:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type nfdWorker struct {
|
2021-07-08 11:02:39 +03:00
|
|
|
nfdclient.NfdBaseClient
|
|
|
|
|
2020-04-21 19:40:01 +03:00
|
|
|
args Args
|
2021-02-18 14:18:30 +02:00
|
|
|
certWatch *utils.FsWatcher
|
2020-04-21 19:40:01 +03:00
|
|
|
client pb.LabelerClient
|
2020-11-27 10:19:31 +02:00
|
|
|
configFilePath string
|
|
|
|
config *NFDConfig
|
2021-02-11 21:38:13 +02:00
|
|
|
stop chan struct{} // channel for signaling stop
|
2021-08-27 12:52:24 +03:00
|
|
|
featureSources []source.FeatureSource
|
2021-08-27 12:21:16 +03:00
|
|
|
labelSources []source.LabelSource
|
2020-12-01 14:54:59 +02:00
|
|
|
}
|
|
|
|
|
2020-12-01 14:27:47 +02:00
|
|
|
type duration struct {
|
|
|
|
time.Duration
|
|
|
|
}
|
|
|
|
|
2022-01-19 12:42:06 +05:30
|
|
|
// NewNfdWorker creates new NfdWorker instance.
|
2021-07-08 11:02:39 +03:00
|
|
|
func NewNfdWorker(args *Args) (nfdclient.NfdClient, error) {
|
|
|
|
base, err := nfdclient.NewNfdBaseClient(&args.Args)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-04-21 19:40:01 +03:00
|
|
|
nfd := &nfdWorker{
|
2021-07-08 11:02:39 +03:00
|
|
|
NfdBaseClient: base,
|
|
|
|
|
2021-02-19 15:43:31 +02:00
|
|
|
args: *args,
|
2020-12-01 15:53:04 +02:00
|
|
|
config: &NFDConfig{},
|
2021-03-01 09:02:22 +02:00
|
|
|
stop: make(chan struct{}, 1),
|
2020-04-21 19:40:01 +03:00
|
|
|
}
|
|
|
|
|
2020-11-27 10:19:31 +02:00
|
|
|
if args.ConfigFile != "" {
|
|
|
|
nfd.configFilePath = filepath.Clean(args.ConfigFile)
|
|
|
|
}
|
|
|
|
|
2019-02-08 21:43:54 +02:00
|
|
|
return nfd, nil
|
|
|
|
}
|
|
|
|
|
2020-11-27 10:19:31 +02:00
|
|
|
func newDefaultConfig() *NFDConfig {
|
|
|
|
return &NFDConfig{
|
2020-12-01 14:27:47 +02:00
|
|
|
Core: coreConfig{
|
2021-02-19 15:43:31 +02:00
|
|
|
LabelWhiteList: utils.RegexpVal{Regexp: *regexp.MustCompile("")},
|
2020-12-01 14:54:59 +02:00
|
|
|
SleepInterval: duration{60 * time.Second},
|
2021-08-27 12:52:24 +03:00
|
|
|
FeatureSources: []string{"all"},
|
2021-11-25 10:58:08 +02:00
|
|
|
LabelSources: []string{"all"},
|
2021-02-23 20:42:17 +02:00
|
|
|
Klog: make(map[string]string),
|
2020-12-01 14:27:47 +02:00
|
|
|
},
|
2020-11-27 10:19:31 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-08 21:43:54 +02:00
|
|
|
// 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 {
|
2021-02-23 10:05:13 +02:00
|
|
|
klog.Infof("Node Feature Discovery Worker %s", version.Get())
|
2021-07-08 11:02:39 +03:00
|
|
|
klog.Infof("NodeName: '%s'", nfdclient.NodeName())
|
2019-02-08 21:43:54 +02:00
|
|
|
|
2020-11-27 10:19:31 +02:00
|
|
|
// Create watcher for config file and read initial configuration
|
2021-02-16 21:14:22 +02:00
|
|
|
configWatch, err := utils.CreateFsWatcher(time.Second, w.configFilePath)
|
2019-08-27 23:27:40 +03:00
|
|
|
if err != nil {
|
2020-11-27 10:19:31 +02:00
|
|
|
return err
|
2019-08-27 23:27:40 +03:00
|
|
|
}
|
2020-11-30 17:23:28 +02:00
|
|
|
if err := w.configure(w.configFilePath, w.args.Options); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-08-27 23:27:40 +03:00
|
|
|
|
2021-02-18 14:18:30 +02:00
|
|
|
// Create watcher for TLS certificates
|
|
|
|
w.certWatch, err = utils.CreateFsWatcher(time.Second, w.args.CaFile, w.args.CertFile, w.args.KeyFile)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-11-27 10:19:31 +02:00
|
|
|
// Connect to NFD master
|
2021-07-08 11:02:39 +03:00
|
|
|
err = w.Connect()
|
2020-11-27 08:26:22 +02:00
|
|
|
if err != nil {
|
2020-11-27 10:19:31 +02:00
|
|
|
return fmt.Errorf("failed to connect: %v", err)
|
2020-11-27 08:26:22 +02:00
|
|
|
}
|
2021-07-08 11:02:39 +03:00
|
|
|
defer w.Disconnect()
|
2020-11-27 08:26:22 +02:00
|
|
|
|
|
|
|
labelTrigger := time.After(0)
|
2019-08-27 23:27:40 +03:00
|
|
|
for {
|
2020-11-26 14:15:39 +02:00
|
|
|
select {
|
2020-11-27 08:26:22 +02:00
|
|
|
case <-labelTrigger:
|
2021-03-01 18:39:49 +02:00
|
|
|
// Run feature discovery
|
2021-08-27 12:52:24 +03:00
|
|
|
for _, s := range w.featureSources {
|
|
|
|
klog.V(2).Infof("running discovery for %q source", s.Name())
|
2021-03-01 18:39:49 +02:00
|
|
|
if err := s.Discover(); err != nil {
|
2021-08-27 12:52:24 +03:00
|
|
|
klog.Errorf("feature discovery of %q source failed: %v", s.Name(), err)
|
2021-03-01 18:39:49 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-26 14:15:39 +02:00
|
|
|
// Get the set of feature labels.
|
2021-08-27 12:21:16 +03:00
|
|
|
labels := createFeatureLabels(w.labelSources, w.config.Core.LabelWhiteList.Regexp)
|
2020-11-26 14:15:39 +02:00
|
|
|
|
|
|
|
// Update the node with the feature labels.
|
|
|
|
if w.client != nil {
|
2021-08-27 12:52:24 +03:00
|
|
|
err := w.advertiseFeatureLabels(labels)
|
2020-11-26 14:15:39 +02:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to advertise labels: %s", err.Error())
|
|
|
|
}
|
2019-08-27 23:27:40 +03:00
|
|
|
}
|
|
|
|
|
2020-11-26 14:15:39 +02:00
|
|
|
if w.args.Oneshot {
|
|
|
|
return nil
|
|
|
|
}
|
2019-08-27 23:27:40 +03:00
|
|
|
|
2020-12-01 14:27:47 +02:00
|
|
|
if w.config.Core.SleepInterval.Duration > 0 {
|
|
|
|
labelTrigger = time.After(w.config.Core.SleepInterval.Duration)
|
2020-11-26 14:15:39 +02:00
|
|
|
}
|
2020-11-27 08:26:22 +02:00
|
|
|
|
2021-02-16 21:14:22 +02:00
|
|
|
case <-configWatch.Events:
|
|
|
|
klog.Infof("reloading configuration")
|
2020-11-30 17:23:28 +02:00
|
|
|
if err := w.configure(w.configFilePath, w.args.Options); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-11-27 10:19:31 +02:00
|
|
|
// Manage connection to master
|
|
|
|
if w.config.Core.NoPublish {
|
2021-07-08 11:02:39 +03:00
|
|
|
w.Disconnect()
|
|
|
|
} else if w.ClientConn() == nil {
|
|
|
|
if err := w.Connect(); err != nil {
|
2020-11-27 10:19:31 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2020-11-27 18:02:45 +02:00
|
|
|
// Always re-label after a re-config event. This way the new config
|
|
|
|
// comes into effect even if the sleep interval is long (or infinite)
|
|
|
|
labelTrigger = time.After(0)
|
2021-02-11 21:38:13 +02:00
|
|
|
|
2021-02-18 14:18:30 +02:00
|
|
|
case <-w.certWatch.Events:
|
|
|
|
klog.Infof("TLS certificate update, renewing connection to nfd-master")
|
2021-07-08 11:02:39 +03:00
|
|
|
w.Disconnect()
|
|
|
|
if err := w.Connect(); err != nil {
|
2021-02-18 14:18:30 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-02-11 21:38:13 +02:00
|
|
|
case <-w.stop:
|
2021-02-23 10:05:13 +02:00
|
|
|
klog.Infof("shutting down nfd-worker")
|
2021-02-11 21:38:13 +02:00
|
|
|
configWatch.Close()
|
2021-02-18 14:18:30 +02:00
|
|
|
w.certWatch.Close()
|
2021-02-11 21:38:13 +02:00
|
|
|
return nil
|
2019-08-27 23:27:40 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-11 21:38:13 +02:00
|
|
|
// Stop NfdWorker
|
|
|
|
func (w *nfdWorker) Stop() {
|
|
|
|
select {
|
|
|
|
case w.stop <- struct{}{}:
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-08 11:02:39 +03:00
|
|
|
// Connect creates a client connection to the NFD master
|
|
|
|
func (w *nfdWorker) Connect() error {
|
2019-08-27 23:27:40 +03:00
|
|
|
// Return a dummy connection in case of dry-run
|
2020-11-27 10:19:31 +02:00
|
|
|
if w.config.Core.NoPublish {
|
2019-08-27 23:27:40 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-07-08 11:02:39 +03:00
|
|
|
if err := w.NfdBaseClient.Connect(); err != nil {
|
2019-08-27 23:27:40 +03:00
|
|
|
return err
|
2019-02-08 21:43:54 +02:00
|
|
|
}
|
2021-07-08 11:02:39 +03:00
|
|
|
|
|
|
|
w.client = pb.NewLabelerClient(w.ClientConn())
|
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
|
|
|
|
2021-07-08 11:02:39 +03:00
|
|
|
// Disconnect closes the connection to NFD master
|
|
|
|
func (w *nfdWorker) Disconnect() {
|
|
|
|
w.NfdBaseClient.Disconnect()
|
2019-08-27 23:27:40 +03:00
|
|
|
w.client = nil
|
2019-02-08 21:43:54 +02:00
|
|
|
}
|
2020-12-01 14:27:47 +02:00
|
|
|
func (c *coreConfig) sanitize() {
|
|
|
|
if c.SleepInterval.Duration > 0 && c.SleepInterval.Duration < time.Second {
|
2021-02-23 10:05:13 +02:00
|
|
|
klog.Warningf("too short sleep-intervall specified (%s), forcing to 1s",
|
2020-12-01 14:27:47 +02:00
|
|
|
c.SleepInterval.Duration.String())
|
|
|
|
c.SleepInterval = duration{time.Second}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-23 20:42:17 +02:00
|
|
|
func (w *nfdWorker) configureCore(c coreConfig) error {
|
|
|
|
// Handle klog
|
|
|
|
for k, a := range w.args.Klog {
|
|
|
|
if !a.IsSetFromCmdline() {
|
|
|
|
v, ok := c.Klog[k]
|
|
|
|
if !ok {
|
|
|
|
v = a.DefValue()
|
|
|
|
}
|
|
|
|
if err := a.SetFromConfig(v); err != nil {
|
2021-03-02 18:44:28 +02:00
|
|
|
return fmt.Errorf("failed to set logger option klog.%s = %v: %v", k, v, err)
|
2021-02-23 20:42:17 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for k := range c.Klog {
|
|
|
|
if _, ok := w.args.Klog[k]; !ok {
|
|
|
|
klog.Warningf("unknown logger option in config: %q", k)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-27 12:52:24 +03:00
|
|
|
// Determine enabled feature sources
|
|
|
|
featureSources := make(map[string]source.FeatureSource)
|
|
|
|
for _, name := range c.FeatureSources {
|
|
|
|
if name == "all" {
|
|
|
|
for n, s := range source.GetAllFeatureSources() {
|
2021-11-26 11:22:39 +02:00
|
|
|
if ts, ok := s.(source.SupplementalSource); !ok || !ts.DisableByDefault() {
|
2021-08-27 12:52:24 +03:00
|
|
|
featureSources[n] = s
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
disable := false
|
|
|
|
strippedName := name
|
|
|
|
if strings.HasPrefix(name, "-") {
|
|
|
|
strippedName = name[1:]
|
|
|
|
disable = true
|
|
|
|
}
|
|
|
|
if s := source.GetFeatureSource(strippedName); s != nil {
|
|
|
|
if !disable {
|
|
|
|
featureSources[name] = s
|
|
|
|
} else {
|
|
|
|
delete(featureSources, strippedName)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
klog.Warningf("skipping unknown feature source %q specified in core.featureSources", name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
w.featureSources = make([]source.FeatureSource, 0, len(featureSources))
|
|
|
|
for _, s := range featureSources {
|
|
|
|
w.featureSources = append(w.featureSources, s)
|
|
|
|
}
|
|
|
|
|
|
|
|
sort.Slice(w.featureSources, func(i, j int) bool { return w.featureSources[i].Name() < w.featureSources[j].Name() })
|
|
|
|
|
2021-08-27 12:21:16 +03:00
|
|
|
// Determine enabled label sources
|
|
|
|
labelSources := make(map[string]source.LabelSource)
|
2021-11-25 10:58:08 +02:00
|
|
|
for _, name := range c.LabelSources {
|
2021-03-01 09:02:22 +02:00
|
|
|
if name == "all" {
|
|
|
|
for n, s := range source.GetAllLabelSources() {
|
2021-11-26 11:22:39 +02:00
|
|
|
if ts, ok := s.(source.SupplementalSource); !ok || !ts.DisableByDefault() {
|
2021-08-27 12:21:16 +03:00
|
|
|
labelSources[n] = s
|
2021-03-01 09:02:22 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2021-11-25 10:22:20 +02:00
|
|
|
disable := false
|
|
|
|
strippedName := name
|
|
|
|
if strings.HasPrefix(name, "-") {
|
|
|
|
strippedName = name[1:]
|
|
|
|
disable = true
|
|
|
|
}
|
|
|
|
if s := source.GetLabelSource(strippedName); s != nil {
|
|
|
|
if !disable {
|
2021-08-27 12:21:16 +03:00
|
|
|
labelSources[name] = s
|
2021-11-25 10:22:20 +02:00
|
|
|
} else {
|
2021-08-27 12:21:16 +03:00
|
|
|
delete(labelSources, strippedName)
|
2021-11-25 10:22:20 +02:00
|
|
|
}
|
2021-03-01 09:02:22 +02:00
|
|
|
} else {
|
2021-11-25 12:14:19 +02:00
|
|
|
klog.Warningf("skipping unknown source %q specified in core.sources (or -sources)", name)
|
2021-03-01 09:02:22 +02:00
|
|
|
}
|
2020-12-01 15:53:04 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-27 12:21:16 +03:00
|
|
|
w.labelSources = make([]source.LabelSource, 0, len(labelSources))
|
|
|
|
for _, s := range labelSources {
|
|
|
|
w.labelSources = append(w.labelSources, s)
|
2020-12-01 15:53:04 +02:00
|
|
|
}
|
2021-03-01 09:02:22 +02:00
|
|
|
|
2021-08-27 12:21:16 +03:00
|
|
|
sort.Slice(w.labelSources, func(i, j int) bool {
|
|
|
|
iP, jP := w.labelSources[i].Priority(), w.labelSources[j].Priority()
|
2021-03-01 09:02:22 +02:00
|
|
|
if iP != jP {
|
|
|
|
return iP < jP
|
2020-12-01 15:53:04 +02:00
|
|
|
}
|
2021-08-27 12:21:16 +03:00
|
|
|
return w.labelSources[i].Name() < w.labelSources[j].Name()
|
2021-03-01 09:02:22 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
if klog.V(1).Enabled() {
|
2021-08-27 12:52:24 +03:00
|
|
|
n := make([]string, len(w.featureSources))
|
|
|
|
for i, s := range w.featureSources {
|
|
|
|
n[i] = s.Name()
|
|
|
|
}
|
|
|
|
klog.Infof("enabled feature sources: %s", strings.Join(n, ", "))
|
|
|
|
|
|
|
|
n = make([]string, len(w.labelSources))
|
2021-08-27 12:21:16 +03:00
|
|
|
for i, s := range w.labelSources {
|
2021-03-01 09:02:22 +02:00
|
|
|
n[i] = s.Name()
|
2020-12-01 15:53:04 +02:00
|
|
|
}
|
2021-03-01 09:02:22 +02:00
|
|
|
klog.Infof("enabled label sources: %s", strings.Join(n, ", "))
|
2020-12-01 15:53:04 +02:00
|
|
|
}
|
2021-03-01 09:02:22 +02:00
|
|
|
|
2021-02-23 20:42:17 +02:00
|
|
|
return nil
|
2020-12-01 15:53:04 +02:00
|
|
|
}
|
|
|
|
|
2019-02-08 21:43:54 +02:00
|
|
|
// Parse configuration options
|
2020-11-30 17:23:28 +02:00
|
|
|
func (w *nfdWorker) configure(filepath string, overrides string) error {
|
2020-04-21 22:03:37 +03:00
|
|
|
// Create a new default config
|
2020-11-27 10:19:31 +02:00
|
|
|
c := newDefaultConfig()
|
2021-03-01 09:02:22 +02:00
|
|
|
confSources := source.GetAllConfigurableSources()
|
|
|
|
c.Sources = make(map[string]source.Config, len(confSources))
|
|
|
|
for _, s := range confSources {
|
2020-04-21 22:03:37 +03:00
|
|
|
c.Sources[s.Name()] = s.NewConfig()
|
2019-02-08 21:43:54 +02:00
|
|
|
}
|
|
|
|
|
2020-04-21 22:03:37 +03:00
|
|
|
// Try to read and parse config file
|
2020-11-30 17:23:28 +02:00
|
|
|
if filepath != "" {
|
|
|
|
data, err := ioutil.ReadFile(filepath)
|
2020-04-21 22:03:37 +03:00
|
|
|
if err != nil {
|
2020-11-30 17:23:28 +02:00
|
|
|
if os.IsNotExist(err) {
|
2021-02-23 10:05:13 +02:00
|
|
|
klog.Infof("config file %q not found, using defaults", filepath)
|
2020-11-30 17:23:28 +02:00
|
|
|
} else {
|
|
|
|
return fmt.Errorf("error reading config file: %s", err)
|
|
|
|
}
|
2020-04-21 22:03:37 +03:00
|
|
|
} else {
|
2020-11-30 17:23:28 +02:00
|
|
|
err = yaml.Unmarshal(data, c)
|
|
|
|
if err != nil {
|
2021-02-25 12:12:06 -05:00
|
|
|
return fmt.Errorf("failed to parse config file: %s", err)
|
2020-11-30 17:23:28 +02:00
|
|
|
}
|
2021-11-25 11:24:09 +02:00
|
|
|
|
|
|
|
if c.Core.Sources != nil {
|
|
|
|
klog.Warningf("found deprecated 'core.sources' config file option, please use 'core.labelSources' instead")
|
|
|
|
c.Core.LabelSources = *c.Core.Sources
|
|
|
|
}
|
|
|
|
|
2021-03-02 18:44:28 +02:00
|
|
|
klog.Infof("configuration file %q parsed", filepath)
|
2020-04-21 22:03:37 +03:00
|
|
|
}
|
2019-02-08 21:43:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Parse config overrides
|
2020-11-30 17:23:28 +02:00
|
|
|
if err := yaml.Unmarshal([]byte(overrides), c); err != nil {
|
2021-11-25 12:14:19 +02:00
|
|
|
return fmt.Errorf("failed to parse -options: %s", err)
|
2019-02-08 21:43:54 +02:00
|
|
|
}
|
|
|
|
|
2021-02-19 15:43:31 +02:00
|
|
|
if w.args.Overrides.LabelWhiteList != nil {
|
|
|
|
c.Core.LabelWhiteList = *w.args.Overrides.LabelWhiteList
|
2020-12-01 14:54:59 +02:00
|
|
|
}
|
2021-02-19 15:43:31 +02:00
|
|
|
if w.args.Overrides.NoPublish != nil {
|
|
|
|
c.Core.NoPublish = *w.args.Overrides.NoPublish
|
2020-11-27 10:19:31 +02:00
|
|
|
}
|
2021-02-19 15:43:31 +02:00
|
|
|
if w.args.Overrides.SleepInterval != nil {
|
|
|
|
c.Core.SleepInterval = duration{*w.args.Overrides.SleepInterval}
|
2020-12-01 14:27:47 +02:00
|
|
|
}
|
2021-12-03 09:22:43 +02:00
|
|
|
if w.args.Overrides.FeatureSources != nil {
|
|
|
|
c.Core.FeatureSources = *w.args.Overrides.FeatureSources
|
|
|
|
}
|
2021-11-25 10:58:08 +02:00
|
|
|
if w.args.Overrides.LabelSources != nil {
|
|
|
|
c.Core.LabelSources = *w.args.Overrides.LabelSources
|
2020-12-01 15:53:04 +02:00
|
|
|
}
|
2020-12-01 14:27:47 +02:00
|
|
|
|
|
|
|
c.Core.sanitize()
|
2020-11-27 10:19:31 +02:00
|
|
|
|
2020-04-21 22:03:37 +03:00
|
|
|
w.config = c
|
|
|
|
|
2021-02-23 20:42:17 +02:00
|
|
|
if err := w.configureCore(c.Core); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-12-01 15:53:04 +02:00
|
|
|
|
2021-03-01 09:02:22 +02:00
|
|
|
// (Re-)configure sources
|
|
|
|
for _, s := range confSources {
|
2020-04-21 22:03:37 +03:00
|
|
|
s.SetConfig(c.Sources[s.Name()])
|
|
|
|
}
|
2020-11-30 17:23:28 +02:00
|
|
|
|
2021-03-02 18:44:28 +02:00
|
|
|
klog.Infof("worker (re-)configuration successfully completed")
|
|
|
|
|
2020-11-30 17:23:28 +02:00
|
|
|
return nil
|
2019-02-08 21:43:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// createFeatureLabels returns the set of feature labels from the enabled
|
|
|
|
// sources and the whitelist argument.
|
2021-03-01 07:45:32 +02:00
|
|
|
func createFeatureLabels(sources []source.LabelSource, labelWhiteList regexp.Regexp) (labels Labels) {
|
2019-02-08 21:43:54 +02:00
|
|
|
labels = Labels{}
|
|
|
|
|
2021-03-01 18:39:49 +02:00
|
|
|
// Get labels from all enabled label sources
|
2021-03-10 21:09:47 +02:00
|
|
|
klog.Info("starting feature discovery...")
|
2019-02-08 21:43:54 +02:00
|
|
|
for _, source := range sources {
|
nfd-worker: fix --label-whitelist
Unify handling of --label-whitelist in nfd-worker and nfd-master. That is,
in nfd-worker, apply the regexp filter on non-namespaced part of the
label name.
Brief history:
1. Originally the whitelist regexp was applied on the full namespaced
label name (that would be e.g.
'feature.node.kubernetes.io/cpu-cpuid.AVX' in the current nfd version)
2. Commit 81752b2d changed the behavior so that the regexp was applied
on the non-namespaced part (that would be `cpu-cpuid.AVX`)
3. Commit 40918827 added support for custom label namespaces. With this
change, the label whitelist handling diverged between nfd-worker and
nfd-master. In nfd-master the whitelist regexp is always applied on
the non-namespaced label name. However, in nfd-worker the whitelist
handling is two-fold (and inconsistent): for labels in the standard
nfd namespace regexp is applied on the non-namespaced part (e.g.
`cpu-cpuid.AVX`, but, for labels in custom namespaces the regexp is
applied on the full name (e.g. `example.com/my-feature`).
This patch changes nfd-worker to behave similarly to nfd-master. The
namespace part is now always omitted, which should be easier for the
users to comprehend.
Also, fixes a bug in the label name prefixing so that the name of the
feature source is not prefixed into labels with custom label namespace
(effectively mangling the intended namespace). For example, previously a
'example.com/feature' label from the 'custom' feature source would be
prefixed with the source name, mangling it to
'custom-example.com/feature'.
2020-04-28 10:38:38 +03:00
|
|
|
labelsFromSource, err := getFeatureLabels(source, labelWhiteList)
|
2019-02-08 21:43:54 +02:00
|
|
|
if err != nil {
|
2021-02-23 10:05:13 +02:00
|
|
|
klog.Errorf("discovery failed for source %q: %v", source.Name(), err)
|
2019-02-08 21:43:54 +02:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
for name, value := range labelsFromSource {
|
|
|
|
labels[name] = value
|
|
|
|
}
|
|
|
|
}
|
2021-03-10 21:09:47 +02:00
|
|
|
klog.Info("feature discovery completed")
|
|
|
|
utils.KlogDump(1, "labels discovered by feature sources:", " ", labels)
|
2019-02-08 21:43:54 +02:00
|
|
|
return labels
|
|
|
|
}
|
|
|
|
|
|
|
|
// getFeatureLabels returns node labels for features discovered by the
|
|
|
|
// supplied source.
|
2021-03-01 07:45:32 +02:00
|
|
|
func getFeatureLabels(source source.LabelSource, labelWhiteList regexp.Regexp) (labels Labels, err error) {
|
2019-02-08 21:43:54 +02:00
|
|
|
labels = Labels{}
|
2021-03-01 18:39:49 +02:00
|
|
|
features, err := source.GetLabels()
|
2019-02-08 21:43:54 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
nfd-worker: fix --label-whitelist
Unify handling of --label-whitelist in nfd-worker and nfd-master. That is,
in nfd-worker, apply the regexp filter on non-namespaced part of the
label name.
Brief history:
1. Originally the whitelist regexp was applied on the full namespaced
label name (that would be e.g.
'feature.node.kubernetes.io/cpu-cpuid.AVX' in the current nfd version)
2. Commit 81752b2d changed the behavior so that the regexp was applied
on the non-namespaced part (that would be `cpu-cpuid.AVX`)
3. Commit 40918827 added support for custom label namespaces. With this
change, the label whitelist handling diverged between nfd-worker and
nfd-master. In nfd-master the whitelist regexp is always applied on
the non-namespaced label name. However, in nfd-worker the whitelist
handling is two-fold (and inconsistent): for labels in the standard
nfd namespace regexp is applied on the non-namespaced part (e.g.
`cpu-cpuid.AVX`, but, for labels in custom namespaces the regexp is
applied on the full name (e.g. `example.com/my-feature`).
This patch changes nfd-worker to behave similarly to nfd-master. The
namespace part is now always omitted, which should be easier for the
users to comprehend.
Also, fixes a bug in the label name prefixing so that the name of the
feature source is not prefixed into labels with custom label namespace
(effectively mangling the intended namespace). For example, previously a
'example.com/feature' label from the 'custom' feature source would be
prefixed with the source name, mangling it to
'custom-example.com/feature'.
2020-04-28 10:38:38 +03:00
|
|
|
|
|
|
|
// Prefix for labels in the default namespace
|
|
|
|
prefix := source.Name() + "-"
|
2021-03-01 09:02:22 +02:00
|
|
|
switch source.Name() {
|
2021-12-09 21:25:28 +02:00
|
|
|
case "local", "custom":
|
|
|
|
// Do not prefix labels from the custom rules, hooks or feature files
|
nfd-worker: fix --label-whitelist
Unify handling of --label-whitelist in nfd-worker and nfd-master. That is,
in nfd-worker, apply the regexp filter on non-namespaced part of the
label name.
Brief history:
1. Originally the whitelist regexp was applied on the full namespaced
label name (that would be e.g.
'feature.node.kubernetes.io/cpu-cpuid.AVX' in the current nfd version)
2. Commit 81752b2d changed the behavior so that the regexp was applied
on the non-namespaced part (that would be `cpu-cpuid.AVX`)
3. Commit 40918827 added support for custom label namespaces. With this
change, the label whitelist handling diverged between nfd-worker and
nfd-master. In nfd-master the whitelist regexp is always applied on
the non-namespaced label name. However, in nfd-worker the whitelist
handling is two-fold (and inconsistent): for labels in the standard
nfd namespace regexp is applied on the non-namespaced part (e.g.
`cpu-cpuid.AVX`, but, for labels in custom namespaces the regexp is
applied on the full name (e.g. `example.com/my-feature`).
This patch changes nfd-worker to behave similarly to nfd-master. The
namespace part is now always omitted, which should be easier for the
users to comprehend.
Also, fixes a bug in the label name prefixing so that the name of the
feature source is not prefixed into labels with custom label namespace
(effectively mangling the intended namespace). For example, previously a
'example.com/feature' label from the 'custom' feature source would be
prefixed with the source name, mangling it to
'custom-example.com/feature'.
2020-04-28 10:38:38 +03:00
|
|
|
prefix = ""
|
|
|
|
}
|
|
|
|
|
2019-02-08 21:43:54 +02:00
|
|
|
for k, v := range features {
|
nfd-worker: fix --label-whitelist
Unify handling of --label-whitelist in nfd-worker and nfd-master. That is,
in nfd-worker, apply the regexp filter on non-namespaced part of the
label name.
Brief history:
1. Originally the whitelist regexp was applied on the full namespaced
label name (that would be e.g.
'feature.node.kubernetes.io/cpu-cpuid.AVX' in the current nfd version)
2. Commit 81752b2d changed the behavior so that the regexp was applied
on the non-namespaced part (that would be `cpu-cpuid.AVX`)
3. Commit 40918827 added support for custom label namespaces. With this
change, the label whitelist handling diverged between nfd-worker and
nfd-master. In nfd-master the whitelist regexp is always applied on
the non-namespaced label name. However, in nfd-worker the whitelist
handling is two-fold (and inconsistent): for labels in the standard
nfd namespace regexp is applied on the non-namespaced part (e.g.
`cpu-cpuid.AVX`, but, for labels in custom namespaces the regexp is
applied on the full name (e.g. `example.com/my-feature`).
This patch changes nfd-worker to behave similarly to nfd-master. The
namespace part is now always omitted, which should be easier for the
users to comprehend.
Also, fixes a bug in the label name prefixing so that the name of the
feature source is not prefixed into labels with custom label namespace
(effectively mangling the intended namespace). For example, previously a
'example.com/feature' label from the 'custom' feature source would be
prefixed with the source name, mangling it to
'custom-example.com/feature'.
2020-04-28 10:38:38 +03:00
|
|
|
// Split label name into namespace and name compoents. Use dummy 'ns'
|
|
|
|
// default namespace because there is no function to validate just
|
|
|
|
// the name part
|
|
|
|
split := strings.SplitN(k, "/", 2)
|
|
|
|
|
|
|
|
label := prefix + split[0]
|
|
|
|
nameForValidation := "ns/" + label
|
|
|
|
nameForWhiteListing := label
|
|
|
|
|
|
|
|
if len(split) == 2 {
|
|
|
|
label = k
|
|
|
|
nameForValidation = label
|
|
|
|
nameForWhiteListing = split[1]
|
2019-02-08 21:43:54 +02:00
|
|
|
}
|
|
|
|
|
nfd-worker: fix --label-whitelist
Unify handling of --label-whitelist in nfd-worker and nfd-master. That is,
in nfd-worker, apply the regexp filter on non-namespaced part of the
label name.
Brief history:
1. Originally the whitelist regexp was applied on the full namespaced
label name (that would be e.g.
'feature.node.kubernetes.io/cpu-cpuid.AVX' in the current nfd version)
2. Commit 81752b2d changed the behavior so that the regexp was applied
on the non-namespaced part (that would be `cpu-cpuid.AVX`)
3. Commit 40918827 added support for custom label namespaces. With this
change, the label whitelist handling diverged between nfd-worker and
nfd-master. In nfd-master the whitelist regexp is always applied on
the non-namespaced label name. However, in nfd-worker the whitelist
handling is two-fold (and inconsistent): for labels in the standard
nfd namespace regexp is applied on the non-namespaced part (e.g.
`cpu-cpuid.AVX`, but, for labels in custom namespaces the regexp is
applied on the full name (e.g. `example.com/my-feature`).
This patch changes nfd-worker to behave similarly to nfd-master. The
namespace part is now always omitted, which should be easier for the
users to comprehend.
Also, fixes a bug in the label name prefixing so that the name of the
feature source is not prefixed into labels with custom label namespace
(effectively mangling the intended namespace). For example, previously a
'example.com/feature' label from the 'custom' feature source would be
prefixed with the source name, mangling it to
'custom-example.com/feature'.
2020-04-28 10:38:38 +03:00
|
|
|
// Validate label name.
|
|
|
|
errs := validation.IsQualifiedName(nameForValidation)
|
2019-02-08 21:43:54 +02:00
|
|
|
if len(errs) > 0 {
|
2021-02-25 12:12:06 -05:00
|
|
|
klog.Warningf("ignoring invalid feature name '%s': %s", label, errs)
|
2019-02-08 21:43:54 +02:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
value := fmt.Sprintf("%v", v)
|
|
|
|
// Validate label value
|
|
|
|
errs = validation.IsValidLabelValue(value)
|
|
|
|
if len(errs) > 0 {
|
2021-02-25 12:12:06 -05:00
|
|
|
klog.Warningf("ignoring invalid feature value %s=%s: %s", label, value, errs)
|
2019-02-08 21:43:54 +02:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
nfd-worker: fix --label-whitelist
Unify handling of --label-whitelist in nfd-worker and nfd-master. That is,
in nfd-worker, apply the regexp filter on non-namespaced part of the
label name.
Brief history:
1. Originally the whitelist regexp was applied on the full namespaced
label name (that would be e.g.
'feature.node.kubernetes.io/cpu-cpuid.AVX' in the current nfd version)
2. Commit 81752b2d changed the behavior so that the regexp was applied
on the non-namespaced part (that would be `cpu-cpuid.AVX`)
3. Commit 40918827 added support for custom label namespaces. With this
change, the label whitelist handling diverged between nfd-worker and
nfd-master. In nfd-master the whitelist regexp is always applied on
the non-namespaced label name. However, in nfd-worker the whitelist
handling is two-fold (and inconsistent): for labels in the standard
nfd namespace regexp is applied on the non-namespaced part (e.g.
`cpu-cpuid.AVX`, but, for labels in custom namespaces the regexp is
applied on the full name (e.g. `example.com/my-feature`).
This patch changes nfd-worker to behave similarly to nfd-master. The
namespace part is now always omitted, which should be easier for the
users to comprehend.
Also, fixes a bug in the label name prefixing so that the name of the
feature source is not prefixed into labels with custom label namespace
(effectively mangling the intended namespace). For example, previously a
'example.com/feature' label from the 'custom' feature source would be
prefixed with the source name, mangling it to
'custom-example.com/feature'.
2020-04-28 10:38:38 +03:00
|
|
|
// Skip if label doesn't match labelWhiteList
|
|
|
|
if !labelWhiteList.MatchString(nameForWhiteListing) {
|
2021-02-23 10:05:13 +02:00
|
|
|
klog.Infof("%q does not match the whitelist (%s) and will not be published.", nameForWhiteListing, labelWhiteList.String())
|
nfd-worker: fix --label-whitelist
Unify handling of --label-whitelist in nfd-worker and nfd-master. That is,
in nfd-worker, apply the regexp filter on non-namespaced part of the
label name.
Brief history:
1. Originally the whitelist regexp was applied on the full namespaced
label name (that would be e.g.
'feature.node.kubernetes.io/cpu-cpuid.AVX' in the current nfd version)
2. Commit 81752b2d changed the behavior so that the regexp was applied
on the non-namespaced part (that would be `cpu-cpuid.AVX`)
3. Commit 40918827 added support for custom label namespaces. With this
change, the label whitelist handling diverged between nfd-worker and
nfd-master. In nfd-master the whitelist regexp is always applied on
the non-namespaced label name. However, in nfd-worker the whitelist
handling is two-fold (and inconsistent): for labels in the standard
nfd namespace regexp is applied on the non-namespaced part (e.g.
`cpu-cpuid.AVX`, but, for labels in custom namespaces the regexp is
applied on the full name (e.g. `example.com/my-feature`).
This patch changes nfd-worker to behave similarly to nfd-master. The
namespace part is now always omitted, which should be easier for the
users to comprehend.
Also, fixes a bug in the label name prefixing so that the name of the
feature source is not prefixed into labels with custom label namespace
(effectively mangling the intended namespace). For example, previously a
'example.com/feature' label from the 'custom' feature source would be
prefixed with the source name, mangling it to
'custom-example.com/feature'.
2020-04-28 10:38:38 +03:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2019-02-08 21:43:54 +02:00
|
|
|
labels[label] = value
|
|
|
|
}
|
|
|
|
return labels, nil
|
|
|
|
}
|
|
|
|
|
2021-04-23 08:39:54 +03:00
|
|
|
// getFeatures returns raw features from all feature sources
|
|
|
|
func getFeatures() map[string]*feature.DomainFeatures {
|
|
|
|
features := make(map[string]*feature.DomainFeatures)
|
|
|
|
|
|
|
|
for name, src := range source.GetAllFeatureSources() {
|
|
|
|
features[name] = src.GetFeatures()
|
|
|
|
}
|
|
|
|
|
|
|
|
return features
|
|
|
|
}
|
|
|
|
|
2019-02-08 21:43:54 +02:00
|
|
|
// advertiseFeatureLabels advertises the feature labels to a Kubernetes node
|
|
|
|
// via the NFD server.
|
2021-08-27 12:52:24 +03:00
|
|
|
func (w *nfdWorker) advertiseFeatureLabels(labels Labels) error {
|
2019-02-08 21:43:54 +02:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
|
|
|
defer cancel()
|
|
|
|
|
2021-02-25 12:12:06 -05:00
|
|
|
klog.Infof("sending labeling request to nfd-master")
|
2019-02-08 21:43:54 +02:00
|
|
|
|
|
|
|
labelReq := pb.SetLabelsRequest{Labels: labels,
|
2021-04-23 08:39:54 +03:00
|
|
|
Features: getFeatures(),
|
2019-02-08 21:43:54 +02:00
|
|
|
NfdVersion: version.Get(),
|
2021-07-08 11:02:39 +03:00
|
|
|
NodeName: nfdclient.NodeName()}
|
2021-08-27 12:52:24 +03:00
|
|
|
_, err := w.client.SetLabels(ctx, &labelReq)
|
2019-02-08 21:43:54 +02:00
|
|
|
if err != nil {
|
2021-02-23 10:05:13 +02:00
|
|
|
klog.Errorf("failed to set node labels: %v", err)
|
2019-02-08 21:43:54 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2020-04-21 22:03:37 +03:00
|
|
|
|
2020-12-01 14:27:47 +02:00
|
|
|
// UnmarshalJSON implements the Unmarshaler interface from "encoding/json"
|
|
|
|
func (d *duration) UnmarshalJSON(data []byte) error {
|
|
|
|
var v interface{}
|
|
|
|
if err := json.Unmarshal(data, &v); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
switch val := v.(type) {
|
|
|
|
case float64:
|
|
|
|
d.Duration = time.Duration(val)
|
|
|
|
case string:
|
|
|
|
var err error
|
|
|
|
d.Duration, err = time.ParseDuration(val)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
return fmt.Errorf("invalid duration %s", data)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-04-21 22:03:37 +03:00
|
|
|
// UnmarshalJSON implements the Unmarshaler interface from "encoding/json"
|
|
|
|
func (c *sourcesConfig) UnmarshalJSON(data []byte) error {
|
|
|
|
// First do a raw parse to get the per-source data
|
|
|
|
raw := map[string]json.RawMessage{}
|
|
|
|
err := yaml.Unmarshal(data, &raw)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Then parse each source-specific data structure
|
|
|
|
// NOTE: we expect 'c' to be pre-populated with correct per-source data
|
|
|
|
// types. Non-pre-populated keys are ignored.
|
|
|
|
for k, rawv := range raw {
|
|
|
|
if v, ok := (*c)[k]; ok {
|
|
|
|
err := yaml.Unmarshal(rawv, &v)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to parse %q source config: %v", k, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|