Skip to content

Commit

Permalink
🌱 Register kubeconfig flag variable via RegisterFlags func (#1999)
Browse files Browse the repository at this point in the history
* Register kubeconfig flag variable via RegisterFlags func

Signed-off-by: Johannes Frey <johannes.frey@mercedes-benz.com>

* Use kubeconfig value if flag has been provided

Signed-off-by: Johannes Frey <johannes.frey@mercedes-benz.com>

* Lint

Signed-off-by: Johannes Frey <johannes.frey@mercedes-benz.com>
  • Loading branch information
johannesfrey authored Oct 26, 2022
1 parent 7d16aec commit a0e1772
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
4 changes: 4 additions & 0 deletions alias.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
23 changes: 20 additions & 3 deletions pkg/client/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,32 @@ 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 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.")
}
}

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

0 comments on commit a0e1772

Please sign in to comment.