From 6abadc05e98571d551786fdb7622a93f2aba2a96 Mon Sep 17 00:00:00 2001 From: 6za <53096417+6za@users.noreply.github.com> Date: Fri, 9 Dec 2022 15:47:30 +0000 Subject: [PATCH] #845 - add retry logic Signed-off-by: 6za <53096417+6za@users.noreply.github.com> --- internal/k8s/portForward.go | 19 ++++++++++++++++++- internal/k8s/wrappers.go | 2 +- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/internal/k8s/portForward.go b/internal/k8s/portForward.go index 91de22077..c5ad1df87 100644 --- a/internal/k8s/portForward.go +++ b/internal/k8s/portForward.go @@ -10,6 +10,7 @@ import ( "os" "strings" "sync" + "time" v1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" @@ -57,6 +58,18 @@ type PortForwardAServiceRequest struct { ReadyCh chan struct{} } +func PortForwardPodWithRetry(clientset *kubernetes.Clientset, req PortForwardAPodRequest) error { + for i := 0; i < 10; i++ { + err := PortForwardPod(clientset, req) + if err == nil { + return nil + } + time.Sleep(20 * time.Second) + } + return fmt.Errorf("not able to open port-forward") + +} + // PortForwardPod receives a PortForwardAPodRequest, and enable port forward for the specified resource. If the provided // Pod name matches a running Pod, it will try to port forward for that Pod on the specified port. func PortForwardPod(clientset *kubernetes.Clientset, req PortForwardAPodRequest) error { @@ -75,7 +88,11 @@ func PortForwardPod(clientset *kubernetes.Clientset, req PortForwardAPodRequest) break } } - + if runningPod == nil { + return fmt.Errorf("error reading pod details") + } + log.Println("Namespace for PF", runningPod.Namespace) + log.Println("Name for PF", runningPod.Name) path := fmt.Sprintf("/api/v1/namespaces/%s/pods/%s/portforward", runningPod.Namespace, runningPod.Name) hostIP := strings.TrimLeft(req.RestConfig.Host, "htps:/") diff --git a/internal/k8s/wrappers.go b/internal/k8s/wrappers.go index 220e06c5a..29eb16607 100644 --- a/internal/k8s/wrappers.go +++ b/internal/k8s/wrappers.go @@ -151,7 +151,7 @@ func OpenPortForwardPodWrapper(podName string, namespace string, podPort int, po clientset, err := GetClientSet(false) go func() { - err = PortForwardPod(clientset, portForwardRequest) + err = PortForwardPodWithRetry(clientset, portForwardRequest) if err != nil { log.Println(err) }