Skip to content

Commit 2ca6bec

Browse files
author
Johannes Frey
committed
Register kubeconfig flag variable via RegisterFlags func
Signed-off-by: Johannes Frey <[email protected]>
1 parent c83076e commit 2ca6bec

File tree

2 files changed

+22
-3
lines changed

2 files changed

+22
-3
lines changed

alias.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,10 @@ type TypeMeta = metav1.TypeMeta
7070
type ObjectMeta = metav1.ObjectMeta
7171

7272
var (
73+
// RegisterFlags registers flag variables to the given FlagSet if not already registered.
74+
// It uses the default command line FlagSet, if none is provided. Currently, it only registers the kubeconfig flag.
75+
RegisterFlags = config.RegisterFlags
76+
7377
// GetConfigOrDie creates a *rest.Config for talking to a Kubernetes apiserver.
7478
// If --kubeconfig is set, will use the kubeconfig file at that location. Otherwise will assume running
7579
// in cluster and use the cluster provided kubeconfig.

pkg/client/config/config.go

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,30 @@ import (
2929
logf "sigs.k8s.io/controller-runtime/pkg/internal/log"
3030
)
3131

32+
// KubeconfigFlagName is the name of the kubeconfig flag
33+
const KubeconfigFlagName = "kubeconfig"
34+
3235
var (
3336
kubeconfig string
3437
log = logf.RuntimeLog.WithName("client").WithName("config")
3538
)
3639

40+
// init registers the "kubeconfig" flag to the default command line FlagSet.
41+
// TODO: This should be removed, as it potentially leads to redefined flag errors for users, if they already
42+
// have registered the "kubeconfig" flag to the command line FlagSet in other parts of their code.
3743
func init() {
38-
// TODO: Fix this to allow double vendoring this library but still register flags on behalf of users
39-
flag.StringVar(&kubeconfig, "kubeconfig", "",
40-
"Paths to a kubeconfig. Only required if out-of-cluster.")
44+
RegisterFlags(flag.CommandLine)
45+
}
46+
47+
// RegisterFlags registers flag variables to the given FlagSet if not already registered.
48+
// It uses the default command line FlagSet, if none is provided. Currently, it only registers the kubeconfig flag.
49+
func RegisterFlags(fs *flag.FlagSet) {
50+
if fs == nil {
51+
fs = flag.CommandLine
52+
}
53+
if fs.Lookup(KubeconfigFlagName) == nil {
54+
fs.StringVar(&kubeconfig, KubeconfigFlagName, "", "Paths to a kubeconfig. Only required if out-of-cluster.")
55+
}
4156
}
4257

4358
// GetConfig creates a *rest.Config for talking to a Kubernetes API server.

0 commit comments

Comments
 (0)