Skip to content

Commit a0e1772

Browse files
authored
🌱 Register kubeconfig flag variable via RegisterFlags func (#1999)
* Register kubeconfig flag variable via RegisterFlags func Signed-off-by: Johannes Frey <[email protected]> * Use kubeconfig value if flag has been provided Signed-off-by: Johannes Frey <[email protected]> * Lint Signed-off-by: Johannes Frey <[email protected]>
1 parent 7d16aec commit a0e1772

File tree

2 files changed

+24
-3
lines changed

2 files changed

+24
-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: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,32 @@ 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 f := fs.Lookup(KubeconfigFlagName); f != nil {
54+
kubeconfig = f.Value.String()
55+
} else {
56+
fs.StringVar(&kubeconfig, KubeconfigFlagName, "", "Paths to a kubeconfig. Only required if out-of-cluster.")
57+
}
4158
}
4259

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

0 commit comments

Comments
 (0)