Skip to content

Commit

Permalink
#845 - add retry logic (#846)
Browse files Browse the repository at this point in the history
Signed-off-by: 6za <53096417+6za@users.noreply.github.com>

Signed-off-by: 6za <53096417+6za@users.noreply.github.com>
  • Loading branch information
6za authored Dec 9, 2022
1 parent 9f9e03c commit 8ebb10d
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
19 changes: 18 additions & 1 deletion internal/k8s/portForward.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"os"
"strings"
"sync"
"time"

v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -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 {
Expand All @@ -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:/")

Expand Down
2 changes: 1 addition & 1 deletion internal/k8s/wrappers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down

0 comments on commit 8ebb10d

Please sign in to comment.