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

fix: bad connection handling for in cluster dialer #1800

Merged
merged 4 commits into from
Jun 19, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
68 changes: 67 additions & 1 deletion pkg/k8s/dialer.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,12 @@ import (
"fmt"
"io"
"net"
"os"
"regexp"
"strconv"
"sync"
"sync/atomic"
"syscall"
"time"

coreV1 "k8s.io/api/core/v1"
Expand Down Expand Up @@ -78,7 +81,13 @@ func (c *contextDialer) DialContext(ctx context.Context, network string, addr st

err := c.exec(addr, ctrStdin, ctrStdout, ctrStderr)
if err != nil {
err = fmt.Errorf("failed to exec in pod: %w (stderr: %q)", err, stderrBuff.String())
stderrStr := stderrBuff.String()
socatErr := tryParseSocatError(network, addr, stderrStr)
if socatErr != nil {
err = fmt.Errorf("socat error: %w", socatErr)
} else {
err = fmt.Errorf("failed to exec in pod: %w (stderr: %q)", err, stderrStr)
}
}
_ = conn.closeWithError(err)
connectFailure <- err
Expand Down Expand Up @@ -110,6 +119,63 @@ func detectConnSuccess(connectSuccess chan struct{}) io.Writer {
return pw
}

var (
connectionRefusedErrorRE = regexp.MustCompile(`E connect\(\d+, AF=\d+ (?P<hostport>[\[\]0-9.:a-z]+), \d+\): Connection refused`)
nameResolutionErrorRE = regexp.MustCompile(`E getaddrinfo\("(?P<hostname>[a-zA-z-.0-9]+)",.*\): Name does not resolve`)
matejvasek marked this conversation as resolved.
Show resolved Hide resolved
)

// tries to detect common errors from `socat` stderr
func tryParseSocatError(network, address, stderr string) error {
groups := nameResolutionErrorRE.FindStringSubmatch(stderr)
if groups != nil {
var name string
if len(groups) > 1 {
name = groups[1]
}
return &net.OpError{
Op: "dial",
Net: network,
Source: nil,
Addr: nil,
Err: &net.DNSError{
Err: "no such host",
Name: name,
IsNotFound: true,
},
}
}
groups = connectionRefusedErrorRE.FindStringSubmatch(stderr)
if groups != nil {
var (
addr net.IP
port int
zone string
)
if len(groups) > 1 {
h, p, err := net.SplitHostPort(groups[1])
if err == nil {
addr = net.ParseIP(h)
p, _ := strconv.ParseInt(p, 10, 16)
port = int(p)
}
}
return &net.OpError{
Op: "dial",
Net: network,
Addr: &net.TCPAddr{
IP: addr,
Port: port,
Zone: zone,
},
Err: &os.SyscallError{
Syscall: "connect",
Err: syscall.ECONNREFUSED,
},
}
}
return nil
}

func (c *contextDialer) Close() error {
// closing the channel will cause stdin of the attached container to return EOF
// as a result the pod exits -- it transits to Completed state
Expand Down
15 changes: 12 additions & 3 deletions pkg/k8s/dialer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,13 +167,22 @@ func TestDialUnreachable(t *testing.T) {
t.Cleanup(func() {
dialer.Close()
})

_, err = dialer.DialContext(ctx, "tcp", "does-not.exists.svc:80")
if err == nil {
t.Error("error was expected but got nil")
return
}
if !strings.Contains(err.Error(), "not resolve") {
t.Errorf("error %q doesn't containe expected sub-string: ", err.Error())
if !strings.Contains(err.Error(), "no such host") {
t.Errorf("error %q doesn't containe expected substring: ", err.Error())
matejvasek marked this conversation as resolved.
Show resolved Hide resolved
}

_, err = dialer.DialContext(ctx, "tcp", "localhost:80")
if err == nil {
t.Error("error was expected but got nil")
return
}
if !strings.Contains(err.Error(), "connection refused") {
t.Errorf("error %q doesn't containe expected substring: ", err.Error())
matejvasek marked this conversation as resolved.
Show resolved Hide resolved
}
}