diff --git a/fission/function.go b/fission/function.go index 99e9bbca..cfe9ef20 100644 --- a/fission/function.go +++ b/fission/function.go @@ -754,8 +754,8 @@ func fnTest(c *cli.Context) error { routerURL := os.Getenv("FISSION_ROUTER") if len(routerURL) == 0 { // Portforward to the fission router - localRouterPort := util.SetupPortForward(util.GetKubeConfigPath(), - util.GetFissionNamespace(), "application=fission-router") + localRouterPort := util.SetupPortForward(util.GetFissionNamespace(), + "application=fission-router") routerURL = "127.0.0.1:" + localRouterPort } else { routerURL = strings.TrimPrefix(routerURL, "http://") diff --git a/fission/support/dump.go b/fission/support/dump.go index 85386b28..0e7d9320 100644 --- a/fission/support/dump.go +++ b/fission/support/dump.go @@ -60,7 +60,7 @@ func DumpInfo(c *cli.Context) error { } client := util.GetApiClient(util.GetServerUrl()) - _, k8sClient := util.GetKubernetesClient(util.GetKubeConfigPath()) + _, k8sClient := util.GetKubernetesClient() ress := map[string]resources.Resource{ // kubernetes info diff --git a/fission/util/portforward.go b/fission/util/portforward.go index 101f4a1b..826817fe 100644 --- a/fission/util/portforward.go +++ b/fission/util/portforward.go @@ -25,7 +25,7 @@ import ( "strings" "time" - "k8s.io/api/core/v1" + v1 "k8s.io/api/core/v1" meta_v1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/client-go/tools/portforward" "k8s.io/client-go/transport/spdy" @@ -39,9 +39,9 @@ import ( // is found by looking for a service in the same namespace and using // its targetPort. Once the port forward is started, wait for it to // start accepting connections before returning. -func SetupPortForward(kubeConfig, namespace, labelSelector string) string { - log.Verbose(2, "Setting up port forward to %s in namespace %s using the kubeconfig at %s", - labelSelector, namespace, kubeConfig) +func SetupPortForward(namespace, labelSelector string) string { + log.Verbose(2, "Setting up port forward to %s in namespace %s", + labelSelector, namespace) localPort, err := findFreePort() if err != nil { @@ -62,7 +62,7 @@ func SetupPortForward(kubeConfig, namespace, labelSelector string) string { log.Verbose(2, "Starting port forward from local port %v", localPort) go func() { - err := runPortForward(kubeConfig, labelSelector, localPort, namespace) + err := runPortForward(labelSelector, localPort, namespace) if err != nil { log.Fatal(fmt.Sprintf("Error forwarding to controller port: %s", err.Error())) } @@ -101,8 +101,8 @@ func findFreePort() (string, error) { } // runPortForward creates a local port forward to the specified pod -func runPortForward(kubeConfig string, labelSelector string, localPort string, ns string) error { - config, clientset := GetKubernetesClient(kubeConfig) +func runPortForward(labelSelector string, localPort string, ns string) error { + config, clientset := GetKubernetesClient() log.Verbose(2, "Connected to Kubernetes API") diff --git a/fission/util/util.go b/fission/util/util.go index ee58c127..06800f75 100644 --- a/fission/util/util.go +++ b/fission/util/util.go @@ -19,6 +19,7 @@ package util import ( "fmt" "os" + "os/user" "path/filepath" "regexp" "strings" @@ -53,20 +54,6 @@ func GetFissionNamespace() string { return fissionNamespace } -func GetKubeConfigPath() string { - kubeConfig := os.Getenv("KUBECONFIG") - if len(kubeConfig) == 0 { - home := os.Getenv("HOME") - kubeConfig = filepath.Join(home, ".kube", "config") - - if _, err := os.Stat(kubeConfig); os.IsNotExist(err) { - log.Fatal("Couldn't find kubeconfig file. " + - "Set the KUBECONFIG environment variable to your kubeconfig's path.") - } - } - return kubeConfig -} - func GetServerUrl() string { return GetApplicationUrl("application=fission-api") } @@ -77,8 +64,7 @@ func GetApplicationUrl(selector string) string { fissionUrl := os.Getenv("FISSION_URL") if len(fissionUrl) == 0 { fissionNamespace := GetFissionNamespace() - kubeConfig := GetKubeConfigPath() - localPort := SetupPortForward(kubeConfig, fissionNamespace, "application=fission-api") + localPort := SetupPortForward(fissionNamespace, "application=fission-api") serverUrl = "http://127.0.0.1:" + localPort } else { serverUrl = fissionUrl @@ -128,10 +114,35 @@ func KubifyName(old string) string { return newName } -func GetKubernetesClient(kubeConfig string) (*restclient.Config, *kubernetes.Clientset) { - config, err := clientcmd.BuildConfigFromFlags("", kubeConfig) +// GetKubernetesClient builds a new kubernetes client. If the KUBECONFIG +// environment variable is empty or doesn't exist, ~/.kube/config is used for +// the kube config path +func GetKubernetesClient() (*restclient.Config, *kubernetes.Clientset) { + loadingRules := clientcmd.NewDefaultClientConfigLoadingRules() + + kubeConfigPath := os.Getenv("KUBECONFIG") + if len(kubeConfigPath) == 0 { + usr, err := user.Current() + if err != nil { + log.Fatal(fmt.Sprintf("Could not get the current users directory: %s", err)) + } + + kubeConfigPath = filepath.Join(usr.HomeDir, ".kube", "config") + + if _, err := os.Stat(kubeConfigPath); os.IsNotExist(err) { + log.Fatal("Couldn't find kubeconfig file. " + + "Set the KUBECONFIG environment variable to your kubeconfig's path.") + } + loadingRules.ExplicitPath = kubeConfigPath + log.Verbose(2, "Using kubeconfig from %q", kubeConfigPath) + } else { + log.Verbose(2, "Using kubeconfig from environment %q", kubeConfigPath) + } + + config, err := clientcmd.NewNonInteractiveDeferredLoadingClientConfig( + loadingRules, &clientcmd.ConfigOverrides{}).ClientConfig() if err != nil { - log.Fatal(fmt.Sprintf("Failed to connect to Kubernetes: %s", err)) + log.Fatal(fmt.Sprintf("Failed to build Kubernetes config: %s", err)) } clientset, err := kubernetes.NewForConfig(config)