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.
This commit is contained in:
Soam Vasani
2018-03-01 12:16:50 -08:00
committed by GitHub
parent 7ddbea59a2
commit 714f32d290
2 changed files with 22 additions and 6 deletions
+4 -4
View File
@@ -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
}
+18 -2
View File
@@ -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