From 458dd8dc5802a84446d215c584bbd81cb59c3455 Mon Sep 17 00:00:00 2001 From: Markus Lehtonen Date: Thu, 28 May 2020 17:43:43 +0300 Subject: [PATCH] nfd-master: add --kubeconfig flag Useful with --prune and for development purposes. --- cmd/nfd-master/main.go | 4 ++++ pkg/apihelper/k8shelpers.go | 12 +++++++++++- pkg/nfd-master/nfd-master.go | 3 ++- 3 files changed, 17 insertions(+), 2 deletions(-) diff --git a/cmd/nfd-master/main.go b/cmd/nfd-master/main.go index 43ca65568..caf1e03f0 100644 --- a/cmd/nfd-master/main.go +++ b/cmd/nfd-master/main.go @@ -66,6 +66,7 @@ func argsParse(argv []string) (master.Args, error) { %s [--prune] [--no-publish] [--label-whitelist=] [--port=] [--ca-file=] [--cert-file=] [--key-file=] [--verify-node-name] [--extra-label-ns=] [--resource-labels=] + [--kubeconfig=] %s -h | --help %s --version @@ -74,6 +75,8 @@ func argsParse(argv []string) (master.Args, error) { --version Output version and exit. --prune Prune all NFD related attributes from all nodes of the cluster and exit. + --kubeconfig= Kubeconfig to use [Default: ] + of the cluster and exit. --port= Port on which to listen for connections. [Default: 8080] --ca-file= Root certificate for verifying connections @@ -122,6 +125,7 @@ func argsParse(argv []string) (master.Args, error) { args.ExtraLabelNs = strings.Split(arguments["--extra-label-ns"].(string), ",") args.ResourceLabels = strings.Split(arguments["--resource-labels"].(string), ",") args.Prune = arguments["--prune"].(bool) + args.Kubeconfig = arguments["--kubeconfig"].(string) return args, nil } diff --git a/pkg/apihelper/k8shelpers.go b/pkg/apihelper/k8shelpers.go index 17e8f7e38..460994ece 100644 --- a/pkg/apihelper/k8shelpers.go +++ b/pkg/apihelper/k8shelpers.go @@ -24,18 +24,28 @@ import ( "k8s.io/apimachinery/pkg/types" k8sclient "k8s.io/client-go/kubernetes" restclient "k8s.io/client-go/rest" + "k8s.io/client-go/tools/clientcmd" ) // Implements APIHelpers type K8sHelpers struct { + Kubeconfig string } func (h K8sHelpers) GetClient() (*k8sclient.Clientset, error) { // Set up an in-cluster K8S client. - config, err := restclient.InClusterConfig() + var config *restclient.Config + var err error + + if h.Kubeconfig == "" { + config, err = restclient.InClusterConfig() + } else { + config, err = clientcmd.BuildConfigFromFlags("", h.Kubeconfig) + } if err != nil { return nil, err } + clientset, err := k8sclient.NewForConfig(config) if err != nil { return nil, err diff --git a/pkg/nfd-master/nfd-master.go b/pkg/nfd-master/nfd-master.go index 7ee456bbf..66ae9799b 100644 --- a/pkg/nfd-master/nfd-master.go +++ b/pkg/nfd-master/nfd-master.go @@ -70,6 +70,7 @@ type Args struct { CertFile string ExtraLabelNs []string KeyFile string + Kubeconfig string LabelWhiteList *regexp.Regexp NoPublish bool Port int @@ -124,7 +125,7 @@ func NewNfdMaster(args Args) (NfdMaster, error) { } // Initialize Kubernetes API helpers - nfd.apihelper = apihelper.K8sHelpers{} + nfd.apihelper = apihelper.K8sHelpers{Kubeconfig: args.Kubeconfig} return nfd, nil }