2019-01-11 15:55:28 +02:00
/ *
2021-02-19 15:43:31 +02:00
Copyright 2019 - 2021 The Kubernetes Authors .
2019-01-11 15:55:28 +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 .
* /
2016-07-01 11:46:05 -07:00
package main
import (
2021-02-19 15:43:31 +02:00
"flag"
2016-07-14 14:17:27 -07:00
"fmt"
2021-02-19 15:43:31 +02:00
"os"
2016-07-01 11:46:05 -07:00
2021-02-23 10:05:13 +02:00
"k8s.io/klog/v2"
2024-03-14 19:23:07 +01:00
"sigs.k8s.io/node-feature-discovery/pkg/features"
2022-12-23 10:45:07 +02:00
worker "sigs.k8s.io/node-feature-discovery/pkg/nfd-worker"
2021-02-19 15:43:31 +02:00
"sigs.k8s.io/node-feature-discovery/pkg/utils"
2024-03-14 19:23:07 +01:00
klogutils "sigs.k8s.io/node-feature-discovery/pkg/utils/klog"
2019-01-10 18:02:53 +02:00
"sigs.k8s.io/node-feature-discovery/pkg/version"
2016-07-01 11:46:05 -07:00
)
2016-07-19 15:35:42 -07:00
const (
2019-01-11 15:55:28 +02:00
// ProgramName is the canonical name of this program
2024-09-23 10:37:56 +02:00
ProgramName = "nfd-worker"
2016-07-19 15:35:42 -07:00
)
2016-07-01 11:46:05 -07:00
func main ( ) {
2021-02-19 15:43:31 +02:00
flags := flag . NewFlagSet ( ProgramName , flag . ExitOnError )
printVersion := flags . Bool ( "version" , false , "Print version and exit." )
2024-03-14 19:23:07 +01:00
// Add FeatureGates flag
if err := features . NFDMutableFeatureGate . Add ( features . DefaultNFDFeatureGates ) ; err != nil {
klog . ErrorS ( err , "failed to add default feature gates" )
os . Exit ( 1 )
}
features . NFDMutableFeatureGate . AddFlag ( flags )
2021-02-19 15:43:31 +02:00
args := parseArgs ( flags , os . Args [ 1 : ] ... )
if * printVersion {
fmt . Println ( ProgramName , version . Get ( ) )
os . Exit ( 0 )
}
2016-07-14 14:17:27 -07:00
// Assert that the version is known
2020-03-20 07:21:43 +02:00
if version . Undefined ( ) {
2024-12-12 22:14:50 +02:00
klog . InfoS ( "version not set! Set -ldflags \"-X sigs.k8s.io/node-feature-discovery/pkg/version.version=`git describe --tags --dirty --always --match 'v*'`\" during build or run." )
2016-07-01 11:46:05 -07:00
}
2021-03-05 14:44:44 +02:00
// Plug klog into grpc logging infrastructure
utils . ConfigureGrpcKlog ( )
2019-02-08 21:43:54 +02:00
// Get new NfdWorker instance
2024-07-05 15:22:58 +02:00
instance , err := worker . NewNfdWorker ( worker . WithArgs ( args ) )
2018-07-06 14:11:07 +03:00
if err != nil {
2023-05-03 11:04:08 +03:00
klog . ErrorS ( err , "failed to initialize NfdWorker instance" )
os . Exit ( 1 )
2018-07-06 14:11:07 +03:00
}
2019-02-08 21:43:54 +02:00
if err = instance . Run ( ) ; err != nil {
2023-05-03 11:04:08 +03:00
klog . ErrorS ( err , "error while running" )
os . Exit ( 1 )
2016-12-16 15:50:48 -08:00
}
}
2021-02-19 15:43:31 +02:00
func parseArgs ( flags * flag . FlagSet , osArgs ... string ) * worker . Args {
args , overrides := initFlags ( flags )
_ = flags . Parse ( osArgs )
if len ( flags . Args ( ) ) > 0 {
2021-02-25 12:12:06 -05:00
fmt . Fprintf ( flags . Output ( ) , "unknown command line argument: %s\n" , flags . Args ( ) [ 0 ] )
2021-02-19 15:43:31 +02:00
flags . Usage ( )
os . Exit ( 2 )
2020-11-27 10:19:31 +02:00
}
2021-02-19 15:43:31 +02:00
// Handle overrides
flags . Visit ( func ( f * flag . Flag ) {
switch f . Name {
case "no-publish" :
args . Overrides . NoPublish = overrides . NoPublish
2021-12-03 09:22:43 +02:00
case "feature-sources" :
args . Overrides . FeatureSources = overrides . FeatureSources
2021-11-25 11:56:40 +02:00
case "label-sources" :
args . Overrides . LabelSources = overrides . LabelSources
2020-12-01 14:27:47 +02:00
}
2021-02-19 15:43:31 +02:00
} )
return args
}
func initFlags ( flagset * flag . FlagSet ) ( * worker . Args , * worker . ConfigOverrideArgs ) {
args := & worker . Args { }
flagset . StringVar ( & args . ConfigFile , "config" , "/etc/kubernetes/node-feature-discovery/nfd-worker.conf" ,
"Config file to use." )
2022-08-12 13:10:48 +03:00
flagset . StringVar ( & args . Kubeconfig , "kubeconfig" , "" ,
"Kubeconfig to use" )
2021-02-19 15:43:31 +02:00
flagset . BoolVar ( & args . Oneshot , "oneshot" , false ,
"Do not publish feature labels" )
2023-06-06 16:39:02 +02:00
flagset . IntVar ( & args . MetricsPort , "metrics" , 8081 ,
"Port on which to expose metrics." )
2024-09-23 10:37:56 +02:00
flagset . IntVar ( & args . GrpcHealthPort , "grpc-health" , 8082 ,
"Port on which to expose the grpc health endpoint." )
2021-02-19 15:43:31 +02:00
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" )
2023-09-07 10:54:59 +01:00
args . Klog = klogutils . InitKlogFlags ( flagset )
2021-02-23 20:42:17 +02:00
2021-02-19 15:43:31 +02:00
// Flags overlapping with config file options
overrides := & worker . ConfigOverrideArgs {
2021-12-03 09:22:43 +02:00
FeatureSources : & utils . StringSliceVal { } ,
2021-11-25 10:58:08 +02:00
LabelSources : & utils . StringSliceVal { } ,
2020-12-01 14:27:47 +02:00
}
2021-02-19 15:43:31 +02:00
overrides . NoPublish = flagset . Bool ( "no-publish" , false ,
2022-08-12 13:10:48 +03:00
"Do not publish discovered features, disable connection to nfd-master and don't create NodeFeature object." )
2021-12-03 09:22:43 +02:00
flagset . Var ( overrides . FeatureSources , "feature-sources" ,
"Comma separated list of feature sources. Special value 'all' enables all sources. " +
"Prefix the source name with '-' to disable it." )
2021-11-25 11:56:40 +02:00
flagset . Var ( overrides . LabelSources , "label-sources" ,
2021-12-03 09:22:43 +02:00
"Comma separated list of label sources. Special value 'all' enables all sources. " +
2021-11-25 10:22:20 +02:00
"Prefix the source name with '-' to disable it." )
2021-02-19 15:43:31 +02:00
return args , overrides
2016-12-16 15:50:48 -08:00
}