From 2ca6bec9fe715d1524d91a07a75bdd4fe06cbe36 Mon Sep 17 00:00:00 2001 From: Johannes Frey Date: Tue, 6 Sep 2022 16:27:24 +0200 Subject: [PATCH 1/3] Register kubeconfig flag variable via RegisterFlags func Signed-off-by: Johannes Frey --- alias.go | 4 ++++ pkg/client/config/config.go | 21 ++++++++++++++++++--- 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/alias.go b/alias.go index 29f964dcbe..35cba30be5 100644 --- a/alias.go +++ b/alias.go @@ -70,6 +70,10 @@ type TypeMeta = metav1.TypeMeta type ObjectMeta = metav1.ObjectMeta var ( + // RegisterFlags registers flag variables to the given FlagSet if not already registered. + // It uses the default command line FlagSet, if none is provided. Currently, it only registers the kubeconfig flag. + RegisterFlags = config.RegisterFlags + // GetConfigOrDie creates a *rest.Config for talking to a Kubernetes apiserver. // If --kubeconfig is set, will use the kubeconfig file at that location. Otherwise will assume running // in cluster and use the cluster provided kubeconfig. diff --git a/pkg/client/config/config.go b/pkg/client/config/config.go index ff44a225fe..eebbe1bfd9 100644 --- a/pkg/client/config/config.go +++ b/pkg/client/config/config.go @@ -29,15 +29,30 @@ import ( logf "sigs.k8s.io/controller-runtime/pkg/internal/log" ) +// KubeconfigFlagName is the name of the kubeconfig flag +const KubeconfigFlagName = "kubeconfig" + var ( kubeconfig string log = logf.RuntimeLog.WithName("client").WithName("config") ) +// init registers the "kubeconfig" flag to the default command line FlagSet. +// TODO: This should be removed, as it potentially leads to redefined flag errors for users, if they already +// have registered the "kubeconfig" flag to the command line FlagSet in other parts of their code. func init() { - // TODO: Fix this to allow double vendoring this library but still register flags on behalf of users - flag.StringVar(&kubeconfig, "kubeconfig", "", - "Paths to a kubeconfig. Only required if out-of-cluster.") + RegisterFlags(flag.CommandLine) +} + +// RegisterFlags registers flag variables to the given FlagSet if not already registered. +// It uses the default command line FlagSet, if none is provided. Currently, it only registers the kubeconfig flag. +func RegisterFlags(fs *flag.FlagSet) { + if fs == nil { + fs = flag.CommandLine + } + if fs.Lookup(KubeconfigFlagName) == nil { + fs.StringVar(&kubeconfig, KubeconfigFlagName, "", "Paths to a kubeconfig. Only required if out-of-cluster.") + } } // GetConfig creates a *rest.Config for talking to a Kubernetes API server. From d960b3810e91970903083c3ab4cf85255ca7b5de Mon Sep 17 00:00:00 2001 From: Johannes Frey Date: Fri, 7 Oct 2022 08:32:07 +0200 Subject: [PATCH 2/3] Use kubeconfig value if flag has been provided Signed-off-by: Johannes Frey --- pkg/client/config/config.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/pkg/client/config/config.go b/pkg/client/config/config.go index eebbe1bfd9..19b3462ee5 100644 --- a/pkg/client/config/config.go +++ b/pkg/client/config/config.go @@ -50,7 +50,10 @@ func RegisterFlags(fs *flag.FlagSet) { if fs == nil { fs = flag.CommandLine } - if fs.Lookup(KubeconfigFlagName) == nil { + f := fs.Lookup(KubeconfigFlagName) + if f != nil { + kubeconfig = f.Value.String() + } else { fs.StringVar(&kubeconfig, KubeconfigFlagName, "", "Paths to a kubeconfig. Only required if out-of-cluster.") } } From 249c55df3fdb6fa18a365ee85d2a803cdf906cc8 Mon Sep 17 00:00:00 2001 From: Johannes Frey Date: Wed, 26 Oct 2022 10:56:25 +0200 Subject: [PATCH 3/3] Lint --- pkg/client/config/config.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/pkg/client/config/config.go b/pkg/client/config/config.go index 19b3462ee5..a81b1a878d 100644 --- a/pkg/client/config/config.go +++ b/pkg/client/config/config.go @@ -50,8 +50,7 @@ func RegisterFlags(fs *flag.FlagSet) { if fs == nil { fs = flag.CommandLine } - f := fs.Lookup(KubeconfigFlagName) - if f != nil { + if f := fs.Lookup(KubeconfigFlagName); f != nil { kubeconfig = f.Value.String() } else { fs.StringVar(&kubeconfig, KubeconfigFlagName, "", "Paths to a kubeconfig. Only required if out-of-cluster.")