@@ -9,12 +9,17 @@ import (
9
9
"context"
10
10
"errors"
11
11
"fmt"
12
+ "io"
12
13
"net"
13
14
"os/exec"
14
15
"strings"
15
16
"time"
16
17
)
17
18
19
+ const (
20
+ errorDialingBackend = "error: error upgrading connection: error dialing backend: EOF"
21
+ )
22
+
18
23
// ForwardPortOfPod establishes a TCP port forwarding to a Kubernetes pod
19
24
func ForwardPortOfPod (ctx context.Context , kubeconfig string , namespace , name , port string ) (readychan chan struct {}, errchan chan error ) {
20
25
return forwardPort (ctx , kubeconfig , namespace , "pod" , name , port )
@@ -48,17 +53,31 @@ func forwardPort(ctx context.Context, kubeconfig string, namespace, resourceType
48
53
command .Stderr = & serr
49
54
err := command .Start ()
50
55
if err != nil {
51
- errchan <- fmt .Errorf ("unexpected error string port-forward: %w" , errors .New (serr .String ()))
52
- if command .Process != nil {
53
- _ = command .Process .Kill ()
56
+ if strings .TrimSuffix (serr .String (), "\n " ) == errorDialingBackend {
57
+ errchan <- io .EOF
58
+ if command .Process != nil {
59
+ _ = command .Process .Kill ()
60
+ }
61
+ } else {
62
+ errchan <- fmt .Errorf ("unexpected error string port-forward: %w" , errors .New (serr .String ()))
63
+ if command .Process != nil {
64
+ _ = command .Process .Kill ()
65
+ }
54
66
}
55
67
}
56
68
57
69
err = command .Wait ()
58
70
if err != nil {
59
- errchan <- fmt .Errorf ("unexpected error string port-forward: %w" , errors .New (serr .String ()))
60
- if command .Process != nil {
61
- _ = command .Process .Kill ()
71
+ if strings .TrimSuffix (serr .String (), "\n " ) == errorDialingBackend {
72
+ errchan <- io .EOF
73
+ if command .Process != nil {
74
+ _ = command .Process .Kill ()
75
+ }
76
+ } else {
77
+ errchan <- fmt .Errorf ("unexpected error running port-forward: %w" , errors .New (serr .String ()))
78
+ if command .Process != nil {
79
+ _ = command .Process .Kill ()
80
+ }
62
81
}
63
82
}
64
83
}()
0 commit comments