2019-01-11 13:55:28 +00: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 .
* /
2016-07-01 18:46:05 +00:00
package main
import (
2016-07-14 21:17:27 +00:00
"fmt"
2016-07-01 18:46:05 +00:00
"log"
2016-07-19 22:35:42 +00:00
"strings"
2018-04-11 16:33:06 +00:00
"time"
2016-07-01 18:46:05 +00:00
2018-03-01 18:49:57 +00:00
"github.com/docopt/docopt-go"
2019-02-08 19:43:54 +00:00
worker "sigs.k8s.io/node-feature-discovery/pkg/nfd-worker"
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 ( ) {
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 ( ) {
log . Printf ( "WARNING: 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
}
2016-12-16 23:50:48 +00:00
// Parse command-line arguments.
2019-01-11 13:55:28 +00:00
args , err := argsParse ( nil )
if err != nil {
2019-02-08 19:43:54 +00:00
log . Fatalf ( "failed to parse command line: %v" , err )
2019-01-11 13:55:28 +00:00
}
2016-12-16 23:50:48 +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 {
2019-02-08 19:43:54 +00:00
log . 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 {
log . Fatalf ( "ERROR: %v" , err )
2016-12-16 23:50:48 +00:00
}
}
// argsParse parses the command line arguments passed to the program.
// The argument argv is passed only for testing purposes.
2019-02-08 19:43:54 +00:00
func argsParse ( argv [ ] string ) ( worker . Args , error ) {
args := worker . Args { }
2016-07-19 22:35:42 +00:00
usage := fmt . Sprintf ( ` % s .
Usage :
2018-04-11 16:33:06 +00:00
% s [ -- no - publish ] [ -- sources = < sources > ] [ -- label - whitelist = < pattern > ]
2018-07-06 11:11:07 +00:00
[ -- oneshot | -- sleep - interval = < seconds > ] [ -- config = < path > ]
2019-01-30 13:02:50 +00:00
[ -- options = < config > ] [ -- server = < server > ] [ -- server - name - override = < name > ]
2019-01-30 12:54:51 +00:00
[ -- ca - file = < path > ] [ -- cert - file = < path > ] [ -- key - file = < path > ]
2016-07-19 22:35:42 +00:00
% s - h | -- help
% s -- version
Options :
2016-10-17 17:24:37 +00:00
- h -- help Show this screen .
-- version Output version and exit .
2018-07-06 11:11:07 +00:00
-- config = < path > Config file to use .
2019-01-11 13:55:28 +00:00
[ Default : / etc / kubernetes / node - feature - discovery / nfd - worker . conf ]
2018-09-21 08:26:57 +00:00
-- options = < config > 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
will override settings read from the config file .
[ Default : ]
2019-01-29 14:21:48 +00:00
-- ca - file = < path > Root certificate for verifying connections
[ Default : ]
2019-01-30 12:54:51 +00:00
-- cert - file = < path > Certificate used for authenticating connections
[ Default : ]
-- key - file = < path > Private key matching -- cert - file
[ Default : ]
2019-01-11 13:55:28 +00:00
-- server = < server > NFD server address to connecto to .
[ Default : localhost : 8080 ]
2019-01-30 13:02:50 +00:00
-- server - name - override = < name > Name ( CN ) expect from server certificate , useful
in testing
[ Default : ]
2020-09-09 08:24:29 +00:00
-- sources = < sources > Comma separated list of feature sources . Special
value ' all ' enables all feature sources .
[ Default : all ]
2016-10-26 00:05:55 +00:00
-- no - publish Do not publish discovered features to the
cluster - local Kubernetes API server .
-- label - whitelist = < pattern > Regular expression to filter label names to
2020-05-20 20:19:09 +00:00
publish to the Kubernetes API server .
NB : the label namespace is omitted i . e . the filter
is only applied to the name part after '/' .
[ Default : ]
2018-04-11 16:33:06 +00:00
-- oneshot Label once and exit .
-- sleep - interval = < seconds > Time to sleep between re - labeling . Non - positive
value implies no re - labeling ( i . e . infinite
sleep ) . [ Default : 60 s ] ` ,
2016-07-19 22:35:42 +00:00
ProgramName ,
ProgramName ,
ProgramName ,
ProgramName ,
)
2020-05-19 12:21:21 +00:00
arguments , _ := docopt . ParseArgs ( usage , argv ,
fmt . Sprintf ( "%s %s" , ProgramName , version . Get ( ) ) )
2016-07-19 22:35:42 +00:00
// Parse argument values as usable types.
2018-04-11 16:33:06 +00:00
var err error
2019-02-08 19:43:54 +00:00
args . CaFile = arguments [ "--ca-file" ] . ( string )
args . CertFile = arguments [ "--cert-file" ] . ( string )
args . ConfigFile = arguments [ "--config" ] . ( string )
args . KeyFile = arguments [ "--key-file" ] . ( string )
args . Options = arguments [ "--options" ] . ( string )
args . Server = arguments [ "--server" ] . ( string )
args . ServerNameOverride = arguments [ "--server-name-override" ] . ( string )
args . Sources = strings . Split ( arguments [ "--sources" ] . ( string ) , "," )
args . LabelWhiteList = arguments [ "--label-whitelist" ] . ( string )
args . Oneshot = arguments [ "--oneshot" ] . ( bool )
args . SleepInterval , err = time . ParseDuration ( arguments [ "--sleep-interval" ] . ( string ) )
2018-04-11 16:33:06 +00:00
if err != nil {
2019-01-11 13:55:28 +00:00
return args , fmt . Errorf ( "invalid --sleep-interval specified: %s" , err . Error ( ) )
2018-04-11 16:33:06 +00:00
}
2020-11-27 08:19:31 +00:00
// Parse deprecated/override args
if arguments [ "--no-publish" ] . ( bool ) {
b := true
args . NoPublish = & b
}
2019-01-11 13:55:28 +00:00
return args , nil
2016-12-16 23:50:48 +00:00
}