Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#845 - add retry logic #846

Merged
merged 1 commit into from
Dec 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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