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 .
* /
package main
import (
"fmt"
"log"
"regexp"
"strconv"
2019-04-05 22:31:40 +00:00
"strings"
2019-01-11 13:55:28 +00:00
"github.com/docopt/docopt-go"
2019-02-08 19:43:54 +00:00
master "sigs.k8s.io/node-feature-discovery/pkg/nfd-master"
2019-01-11 13:55:28 +00:00
"sigs.k8s.io/node-feature-discovery/pkg/version"
)
const (
// ProgramName is the canonical name of this program
ProgramName = "nfd-master"
)
func main ( ) {
// Assert that the version is known
2020-03-20 05:21:43 +00:00
if version . Undefined ( ) {
log . Print ( "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." )
2019-01-11 13:55:28 +00:00
}
// Parse command-line arguments.
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
}
2019-02-08 19:43:54 +00:00
// Get new NfdMaster instance
instance , err := master . NewNfdMaster ( args )
2019-01-11 13:55:28 +00:00
if err != nil {
2019-02-08 19:43:54 +00:00
log . Fatalf ( "Failed to initialize NfdMaster instance: %v" , err )
2019-01-11 13:55:28 +00:00
}
2019-01-29 14:21:48 +00:00
2019-02-08 19:43:54 +00:00
if err = instance . Run ( ) ; err != nil {
log . Fatalf ( "ERROR: %v" , err )
2019-01-29 14:21:48 +00:00
}
2019-01-11 13:55:28 +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 ) ( master . Args , error ) {
args := master . Args { }
2019-01-11 13:55:28 +00:00
usage := fmt . Sprintf ( ` % s .
Usage :
% s [ -- no - publish ] [ -- label - whitelist = < pattern > ] [ -- port = < port > ]
2019-01-30 12:54:51 +00:00
[ -- ca - file = < path > ] [ -- cert - file = < path > ] [ -- key - file = < path > ]
2020-03-05 14:40:55 +00:00
[ -- verify - node - name ] [ -- extra - label - ns = < list > ] [ -- resource - labels = < list > ]
2019-01-11 13:55:28 +00:00
% s - h | -- help
% s -- version
Options :
2019-04-05 22:31:40 +00:00
- h -- help Show this screen .
-- version Output version and exit .
-- port = < port > Port on which to listen for connections .
[ Default : 8080 ]
-- ca - file = < path > Root certificate for verifying connections
[ Default : ]
-- cert - file = < path > Certificate used for authenticating connections
[ Default : ]
-- key - file = < path > Private key matching -- cert - file
[ Default : ]
-- verify - node - name Verify worker node name against CN from the TLS
certificate . Only has effect when TLS authentication
has been enabled .
-- no - publish Do not publish feature labels
-- label - whitelist = < pattern > Regular expression to filter label names to
publish to the Kubernetes API server . [ Default : ]
-- extra - label - ns = < list > Comma separated list of allowed extra label namespaces
2020-03-05 14:40:55 +00:00
[ Default : ]
-- resource - labels = < list > Comma separated list of labels to be exposed as extended resources .
2019-04-05 22:31:40 +00:00
[ Default : ] ` ,
2019-01-11 13:55:28 +00:00
ProgramName ,
ProgramName ,
ProgramName ,
ProgramName ,
)
arguments , _ := docopt . Parse ( usage , argv , true ,
fmt . Sprintf ( "%s %s" , ProgramName , version . Get ( ) ) , false )
// Parse argument values as usable types.
var err error
2019-02-08 19:43:54 +00:00
args . CaFile = arguments [ "--ca-file" ] . ( string )
args . CertFile = arguments [ "--cert-file" ] . ( string )
args . KeyFile = arguments [ "--key-file" ] . ( string )
args . NoPublish = arguments [ "--no-publish" ] . ( bool )
args . Port , err = strconv . Atoi ( arguments [ "--port" ] . ( string ) )
2019-01-11 13:55:28 +00:00
if err != nil {
return args , fmt . Errorf ( "invalid --port defined: %s" , err )
}
2019-02-08 19:43:54 +00:00
args . LabelWhiteList , err = regexp . Compile ( arguments [ "--label-whitelist" ] . ( string ) )
2019-01-11 13:55:28 +00:00
if err != nil {
return args , fmt . Errorf ( "error parsing whitelist regex (%s): %s" , arguments [ "--label-whitelist" ] , err )
}
2019-02-08 19:43:54 +00:00
args . VerifyNodeName = arguments [ "--verify-node-name" ] . ( bool )
2019-04-05 22:31:40 +00:00
args . ExtraLabelNs = strings . Split ( arguments [ "--extra-label-ns" ] . ( string ) , "," )
2020-03-05 14:40:55 +00:00
args . ResourceLabels = strings . Split ( arguments [ "--resource-labels" ] . ( string ) , "," )
2019-01-11 13:55:28 +00:00
return args , nil
}