2019-01-11 13:55:28 +00:00
/ *
2021-02-19 13:43:31 +00:00
Copyright 2019 - 2021 The Kubernetes Authors .
2019-01-11 13: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 .
* /
2016-07-01 18:46:05 +00:00
package main
import (
2021-02-19 13:43:31 +00:00
"flag"
2016-07-14 21:17:27 +00:00
"fmt"
2021-02-19 13:43:31 +00:00
"os"
2021-02-23 18:42:17 +00:00
"strings"
2016-07-01 18:46:05 +00:00
2021-02-23 08:05:13 +00:00
"k8s.io/klog/v2"
2019-02-08 19:43:54 +00:00
worker "sigs.k8s.io/node-feature-discovery/pkg/nfd-worker"
2021-02-19 13:43:31 +00:00
"sigs.k8s.io/node-feature-discovery/pkg/utils"
2019-01-10 16:02:53 +00:00
"sigs.k8s.io/node-feature-discovery/pkg/version"
2016-07-01 18:46:05 +00:00
)
2016-07-19 22:35:42 +00:00
const (
2019-01-11 13:55:28 +00:00
// ProgramName is the canonical name of this program
ProgramName = "nfd-worker"
2016-07-19 22:35:42 +00:00
)
2016-07-01 18:46:05 +00:00
func main ( ) {
2021-02-19 13:43:31 +00:00
flags := flag . NewFlagSet ( ProgramName , flag . ExitOnError )
printVersion := flags . Bool ( "version" , false , "Print version and exit." )
args := parseArgs ( flags , os . Args [ 1 : ] ... )
if * printVersion {
fmt . Println ( ProgramName , version . Get ( ) )
os . Exit ( 0 )
}
2016-07-14 21:17:27 +00:00
// Assert that the version is known
2020-03-20 05:21:43 +00:00
if version . Undefined ( ) {
2021-02-23 08:05:13 +00:00
klog . Warningf ( "version not set! Set -ldflags \"-X sigs.k8s.io/node-feature-discovery/pkg/version.version=`git describe --tags --dirty --always`\" during build or run." )
2016-07-01 18:46:05 +00:00
}
2019-02-08 19:43:54 +00:00
// Get new NfdWorker instance
instance , err := worker . NewNfdWorker ( args )
2018-07-06 11:11:07 +00:00
if err != nil {
2021-02-23 08:05:13 +00:00
klog . Fatalf ( "Failed to initialize NfdWorker instance: %v" , err )
2018-07-06 11:11:07 +00:00
}
2019-02-08 19:43:54 +00:00
if err = instance . Run ( ) ; err != nil {
2021-02-23 08:05:13 +00:00
klog . Fatal ( err )
2016-12-16 23:50:48 +00:00
}
}
2021-02-19 13:43:31 +00:00
func parseArgs ( flags * flag . FlagSet , osArgs ... string ) * worker . Args {
args , overrides := initFlags ( flags )
_ = flags . Parse ( osArgs )
if len ( flags . Args ( ) ) > 0 {
fmt . Printf ( "unknown command line argument: %s\n" , flags . Args ( ) [ 0 ] )
flags . Usage ( )
os . Exit ( 2 )
2020-11-27 08:19:31 +00:00
}
2021-02-19 13:43:31 +00:00
// Handle overrides
flags . Visit ( func ( f * flag . Flag ) {
switch f . Name {
case "no-publish" :
args . Overrides . NoPublish = overrides . NoPublish
case "label-whitelist" :
2021-02-23 08:05:13 +00:00
klog . Warningf ( "--label-whitelist is deprecated, use 'core.labelWhiteList' option in the config file, instead" )
2021-02-19 13:43:31 +00:00
args . Overrides . LabelWhiteList = overrides . LabelWhiteList
case "sleep-interval" :
2021-02-23 08:05:13 +00:00
klog . Warningf ( "--sleep-interval is deprecated, use 'core.sleepInterval' option in the config file, instead" )
2021-02-19 13:43:31 +00:00
args . Overrides . SleepInterval = overrides . SleepInterval
case "sources" :
2021-02-23 08:05:13 +00:00
klog . Warningf ( "--sources is deprecated, use 'core.sources' option in the config file, instead" )
2021-02-19 13:43:31 +00:00
args . Overrides . Sources = overrides . Sources
2020-12-01 12:27:47 +00:00
}
2021-02-19 13:43:31 +00:00
} )
return args
}
func initFlags ( flagset * flag . FlagSet ) ( * worker . Args , * worker . ConfigOverrideArgs ) {
args := & worker . Args { }
flagset . StringVar ( & args . CaFile , "ca-file" , "" ,
"Root certificate for verifying connections" )
flagset . StringVar ( & args . CertFile , "cert-file" , "" ,
"Certificate used for authenticating connections" )
flagset . StringVar ( & args . ConfigFile , "config" , "/etc/kubernetes/node-feature-discovery/nfd-worker.conf" ,
"Config file to use." )
flagset . StringVar ( & args . KeyFile , "key-file" , "" ,
"Private key matching -cert-file" )
flagset . BoolVar ( & args . Oneshot , "oneshot" , false ,
"Do not publish feature labels" )
flagset . StringVar ( & args . Options , "options" , "" ,
"Specify config options from command line. Config options are specified " +
"in the same format as in the config file (i.e. json or yaml). These options" )
flagset . StringVar ( & args . Server , "server" , "localhost:8080" ,
"NFD server address to connecto to." )
flagset . StringVar ( & args . ServerNameOverride , "server-name-override" , "" ,
"Hostname expected from server certificate, useful in testing" )
2021-02-23 18:42:17 +00:00
initKlogFlags ( flagset , args )
2021-02-19 13:43:31 +00:00
// Flags overlapping with config file options
overrides := & worker . ConfigOverrideArgs {
LabelWhiteList : & utils . RegexpVal { } ,
Sources : & utils . StringSliceVal { } ,
2020-12-01 12:27:47 +00:00
}
2021-02-19 13:43:31 +00:00
overrides . NoPublish = flagset . Bool ( "no-publish" , false ,
"Do not publish discovered features, disable connection to nfd-master." )
flagset . Var ( overrides . LabelWhiteList , "label-whitelist" ,
"Regular expression to filter label names to publish to the Kubernetes API server. " +
"NB: the label namespace is omitted i.e. the filter is only applied to the name part after '/'. " +
"DEPRECATED: This parameter should be set via the config file." )
overrides . SleepInterval = flagset . Duration ( "sleep-interval" , 0 ,
"Time to sleep between re-labeling. Non-positive value implies no re-labeling (i.e. infinite sleep). " +
"DEPRECATED: This parameter should be set via the config file" )
flagset . Var ( overrides . Sources , "sources" ,
"Comma separated list of feature sources. Special value 'all' enables all feature sources. " +
"DEPRECATED: This parameter should be set via the config file" )
return args , overrides
2016-12-16 23:50:48 +00:00
}
2021-02-23 18:42:17 +00:00
func initKlogFlags ( flagset * flag . FlagSet , args * worker . Args ) {
args . Klog = make ( map [ string ] * utils . KlogFlagVal )
flags := flag . NewFlagSet ( "klog flags" , flag . ContinueOnError )
//flags.SetOutput(ioutil.Discard)
klog . InitFlags ( flags )
flags . VisitAll ( func ( f * flag . Flag ) {
name := klogConfigOptName ( f . Name )
args . Klog [ name ] = utils . NewKlogFlagVal ( f )
flagset . Var ( args . Klog [ name ] , f . Name , f . Usage )
} )
}
func klogConfigOptName ( flagName string ) string {
split := strings . Split ( flagName , "_" )
for i , v := range split [ 1 : ] {
split [ i + 1 ] = strings . Title ( v )
}
return strings . Join ( split , "" )
}