Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better KUBECONFIG support #410

Merged
merged 2 commits into from
Mar 21, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 8 additions & 13 deletions cli/internal/k8s/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"errors"
"fmt"
"io"
"os"
"regexp"
"time"

Expand Down Expand Up @@ -150,24 +149,20 @@ func init() {
klog.SetLogger(generateLogShim())
}

// getRestConfig uses the K8s "client-go" library to get the currently active kube context, in the same way that
// "kubectl" gets it if no extra config flags like "--kubeconfig" are passed
func getRestConfig() *rest.Config {
message.Debug("k8s.getRestConfig()")

// use the KUBECONFIG context if it exists
configPath := os.Getenv("KUBECONFIG")
if configPath == "" {
// use the current context in the default kubeconfig in the home path of the user
homePath, err := os.UserHomeDir()
if err != nil {
message.Fatal(nil, "Unable to load the current user's home directory")
}
configPath = homePath + "/.kube/config"
}

config, err := clientcmd.BuildConfigFromFlags("", configPath)
// Build the config from the currently active kube context in the default way that the k8s client-go gets it, which
// is to look at the KUBECONFIG env var
config, err := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(
clientcmd.NewDefaultClientConfigLoadingRules(),
&clientcmd.ConfigOverrides{}).ClientConfig()
if err != nil {
message.Fatalf(err, "Unable to connect to the K8s cluster")
}

return config
}

Expand Down