From 1a1ac49ce0eef0840f80c89478161e85a5c08a5e Mon Sep 17 00:00:00 2001 From: Ta-Ching Chen Date: Thu, 2 Aug 2018 17:20:53 +0800 Subject: [PATCH] Fix CLI failed to set up port-forwarding when multiple controller pods exist in the same namespace (#712) * Fix CLI failed to set up port-forwarding when multiple controller pods exist in the same namespace * Check pod healthy state before establishing the connection --- fission/portforward/portforward.go | 36 ++++++++++++++++++++++++------ 1 file changed, 29 insertions(+), 7 deletions(-) diff --git a/fission/portforward/portforward.go b/fission/portforward/portforward.go index 19c5e357..8b6d60fe 100644 --- a/fission/portforward/portforward.go +++ b/fission/portforward/portforward.go @@ -25,12 +25,14 @@ import ( "strings" "time" + "k8s.io/api/core/v1" meta_v1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/client-go/kubernetes" "k8s.io/client-go/tools/clientcmd" "k8s.io/client-go/tools/portforward" "k8s.io/client-go/transport/spdy" + "github.com/fission/fission" "github.com/fission/fission/fission/log" ) @@ -126,19 +128,39 @@ func runPortForward(kubeConfig string, labelSelector string, localPort string, n log.Fatal("Error getting controller pod for port-forwarding") } + nsList := make([]string, 0) + namespaces := make(map[string][]*v1.Pod) + // 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) + if _, ok := namespaces[p.Namespace]; !ok { + namespaces[p.Namespace] = []*v1.Pod{} + nsList = append(nsList, p.Namespace) + } + namespaces[p.Namespace] = append(namespaces[p.Namespace], &p) + } + if len(nsList) > 1 { + log.Fatal(fmt.Sprintf("Found %v fission installs, set FISSION_NAMESPACE to one of: %v", + len(namespaces), strings.Join(nsList, " "))) } - log.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 + pods, ok := namespaces[ns] + if !ok { + log.Fatal(fmt.Sprintf("Error finding fission install within the given namespace %v, please check FISSION_NAMESPACE is set properly", ns)) + } + + var podName, podNameSpace string + + // make sure we establish the connection to a healthy pod + for _, p := range pods { + if fission.IsReadyPod(p) { + podName = p.Name + podNameSpace = p.Namespace + break + } + } // get the service and the target port svcs, err := clientset.CoreV1().Services(podNameSpace).