From 714f32d2906a43f7aeebfe1fece1d290c7f62d56 Mon Sep 17 00:00:00 2001 From: Soam Vasani Date: Thu, 1 Mar 2018 12:16:50 -0800 Subject: [PATCH] Detect fission namespace in cli (#519) Remove the hard coded default "fission" namespace that was added in the last PR, replacing it with some logic to figure out where fission is installed and connecting to it if there's exactly one installation. If there is more than one fission installation, error out with a useful message. Also improve the error message for a missing kubeconfig file. --- fission/main.go | 8 ++++---- fission/portforward.go | 20 ++++++++++++++++++-- 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/fission/main.go b/fission/main.go index 4b22b98c..2f53539d 100644 --- a/fission/main.go +++ b/fission/main.go @@ -25,10 +25,6 @@ import ( func getFissionNamespace() string { fissionNamespace := os.Getenv("FISSION_NAMESPACE") - if len(fissionNamespace) == 0 { - // TODO make this smarter, perhaps based on helm releases - fissionNamespace = "fission" - } return fissionNamespace } @@ -37,6 +33,10 @@ func getKubeConfigPath() string { if len(kubeConfig) == 0 { home := os.Getenv("HOME") kubeConfig = filepath.Join(home, ".kube", "config") + + if _, err := os.Stat(kubeConfig); os.IsNotExist(err) { + fatal("Couldn't find kubeconfig file. Set the KUBECONFIG environment variable to your kubeconfig's path.") + } } return kubeConfig } diff --git a/fission/portforward.go b/fission/portforward.go index 94ff1aac..402fbf26 100644 --- a/fission/portforward.go +++ b/fission/portforward.go @@ -5,10 +5,10 @@ import ( "net" "os" "strconv" + "strings" "time" meta_v1 "k8s.io/apimachinery/pkg/apis/meta/v1" - // "k8s.io/client-go/rest" "k8s.io/client-go/kubernetes" "k8s.io/client-go/tools/clientcmd" "k8s.io/client-go/tools/portforward" @@ -52,13 +52,29 @@ func runPortForward(kubeConfig string, labelSelector string, localPort string, f fatal(fmt.Sprintf("Failed to connect to Kubernetes: %s", err)) } - // get the pod; if there is more than one, always port-forward to the first. + // if fission namespace is unset, try to find a fission pod in any namespace + if len(fissionNamespace) == 0 { + fissionNamespace = meta_v1.NamespaceAll + } + + // get the pod; if there is more than one, ask the user to disambiguate podList, err := clientset.CoreV1().Pods(fissionNamespace). List(meta_v1.ListOptions{LabelSelector: labelSelector}) if err != nil || len(podList.Items) == 0 { fatal("Error getting controller pod for port-forwarding") } + // make a useful error message if there is more than one install + if len(podList.Items) > 1 { + namespaces := make([]string, 0) + for _, p := range podList.Items { + namespaces = append(namespaces, p.Namespace) + } + fatal(fmt.Sprintf("Found %v fission installs, set FISSION_NAMESPACE to one of: %v", + len(podList.Items), strings.Join(namespaces, " "))) + } + + // pick the first pod podName := podList.Items[0].Name podNameSpace := podList.Items[0].Namespace