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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Picking up first running pod instead of first unknown status pod from list #199

Merged
merged 2 commits into from Feb 4, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
25 changes: 21 additions & 4 deletions pkg/forward/kubernetes/forwarder.go
Expand Up @@ -172,6 +172,10 @@ func (f *Forwarder) Stop(ctx context.Context) error {
return nil
}

func isPodRunning(pod *apiv1.Pod) bool {
return pod.Status.Phase == apiv1.PodRunning
}

func (f *Forwarder) forwardLocal(ctx context.Context, selector string) error {
pods, err := f.clientSet.CoreV1().Pods(f.namespace).List(
ctx,
Expand All @@ -185,9 +189,22 @@ func (f *Forwarder) forwardLocal(ctx context.Context, selector string) error {
return fmt.Errorf("No pod available for selector '%s': %v", selector, err)
}

pod := pods.Items[0]
var runningPod apiv1.Pod
foundRunningPod := false

for _, pod := range pods.Items {
if isPodRunning(&pod) {
runningPod = pod
foundRunningPod = true
break
}
}

request := f.restClient.Post().Resource("pods").Namespace(f.namespace).Name(pod.Name).SubResource("portforward")
if !foundRunningPod {
return fmt.Errorf("No runnning pod available for selector '%s'", selector)
}

request := f.restClient.Post().Resource("pods").Namespace(f.namespace).Name(runningPod.Name).SubResource("portforward")

url := url.URL{
Scheme: request.URL().Scheme,
Expand All @@ -203,8 +220,8 @@ func (f *Forwarder) forwardLocal(ctx context.Context, selector string) error {

dialer := spdy.NewDialer(upgrader, &http.Client{Transport: transport}, "POST", &url)

stdoutStream := log.NewStreamer(log.StdOut, pod.Name, f.view)
stderrStream := log.NewStreamer(log.StdErr, pod.Name, f.view)
stdoutStream := log.NewStreamer(log.StdOut, runningPod.Name, f.view)
stderrStream := log.NewStreamer(log.StdErr, runningPod.Name, f.view)

fw, err := portforward.New(dialer, f.ports, f.stopChannel, f.readyChannel, stdoutStream, stderrStream)
if err != nil {
Expand Down