Skip to content

Commit

Permalink
fix: nomos default kubeconfig path (#1028)
Browse files Browse the repository at this point in the history
Work around a controller-runtime quirk that was causing nomos not
to have a default KUBECONFIG value.
  • Loading branch information
karlkfi authored Nov 15, 2023
1 parent 56da4b0 commit 04cf605
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 7 deletions.
16 changes: 16 additions & 0 deletions cmd/nomos/nomos.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ import (
"kpt.dev/configsync/cmd/nomos/version"
"kpt.dev/configsync/cmd/nomos/vet"
"kpt.dev/configsync/pkg/api/configmanagement"
"kpt.dev/configsync/pkg/client/restconfig"
pkgversion "kpt.dev/configsync/pkg/version"
"sigs.k8s.io/controller-runtime/pkg/client/config"
)

var (
Expand Down Expand Up @@ -57,6 +59,20 @@ func main() {
// Register klog flags
klog.InitFlags(fs)

// Work around the controller-runtime init registering a --kubeconfig flag
// with no default value. Use the same default as kubectl instead.
// https://github.com/kubernetes-sigs/controller-runtime/blob/v0.16.3/pkg/client/config/config.go#L44
if f := fs.Lookup(config.KubeconfigFlagName); f != nil {
// Change the default value & usage.
// This path should be expected because import inits load first.
defaultKubeConfigPath, err := restconfig.KubeConfigPath()
if err != nil {
klog.Fatal(err)
}
f.DefValue = defaultKubeConfigPath
f.Usage = "Path to the client config file."
}

// Cobra uses the pflag lib, instead of the go flag lib.
// So re-register all go flags as global (aka persistent) pflags.
rootCmd.PersistentFlags().AddGoFlagSet(fs)
Expand Down
12 changes: 6 additions & 6 deletions pkg/client/restconfig/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,15 @@ func defaultGetCurrentUser() (*user.User, error) {
return user.Current()
}

// newConfigPath returns the correct kubeconfig file path to use, depending on
// the current user settings and the runtime environment.
func newConfigPath() (string, error) {
// First try the KUBECONFIG variable.
// KubeConfigPath returns the path to the kubeconfig:
// 1. ${KUBECONFIG}, if non-empty
// 2. ${userCurrentTestHook.HomeDir}/.kube/config, if userCurrentTestHook is set
// 3. ${HOME}/.kube/config
func KubeConfigPath() (string, error) {
envPath := os.Getenv("KUBECONFIG")
if envPath != "" {
return envPath, nil
}
// Try the current user.
curentUser, err := userCurrentTestHook()
if err != nil {
return "", errors.Wrapf(err, "failed to get current user")
Expand All @@ -60,7 +60,7 @@ func newConfigPath() (string, error) {
// newRawConfigWithRules returns a clientcmdapi.Config from a configuration file whose path is
// provided by newConfigPath, and the clientcmd.ClientConfigLoadingRules associated with it
func newRawConfigWithRules() (*clientcmdapi.Config, *clientcmd.ClientConfigLoadingRules, error) {
configPath, err := newConfigPath()
configPath, err := KubeConfigPath()
if err != nil {
return nil, nil, errors.Wrap(err, "while getting config path")
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/client/restconfig/restconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ const DefaultTimeout = 15 * time.Second
func NewRestConfig(timeout time.Duration) (*rest.Config, error) {
var cfg *rest.Config
// Detect kubectl config file
path, err := newConfigPath()
path, err := KubeConfigPath()
if err != nil {
// Build from k8s downward API
cfg, err = NewFromInClusterConfig()
Expand Down

0 comments on commit 04cf605

Please sign in to comment.