Skip to content

Commit 3cec909

Browse files
committed
fixes a bug that connection refused error cannot be recognized correctly
1 parent f17b608 commit 3cec909

File tree

5 files changed

+51
-11
lines changed

5 files changed

+51
-11
lines changed

staging/src/k8s.io/apimachinery/pkg/util/net/util.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,3 +54,20 @@ func IsConnectionReset(err error) bool {
5454
}
5555
return false
5656
}
57+
58+
// Returns if the given err is "connection refused" error
59+
func IsConnectionRefused(err error) bool {
60+
if urlErr, ok := err.(*url.Error); ok {
61+
err = urlErr.Err
62+
}
63+
if opErr, ok := err.(*net.OpError); ok {
64+
err = opErr.Err
65+
}
66+
if osErr, ok := err.(*os.SyscallError); ok {
67+
err = osErr.Err
68+
}
69+
if errno, ok := err.(syscall.Errno); ok && errno == syscall.ECONNREFUSED {
70+
return true
71+
}
72+
return false
73+
}

staging/src/k8s.io/apimachinery/pkg/util/net/util_test.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ package net
1818

1919
import (
2020
"net"
21+
"net/url"
22+
"os"
23+
"syscall"
2124
"testing"
2225
)
2326

@@ -66,3 +69,28 @@ func TestIPNetEqual(t *testing.T) {
6669
}
6770
}
6871
}
72+
73+
func TestIsConnectionRefused(t *testing.T) {
74+
testCases := []struct {
75+
err error
76+
expect bool
77+
}{
78+
{
79+
&url.Error{Err: &net.OpError{Err: syscall.ECONNRESET}},
80+
false,
81+
},
82+
{
83+
&url.Error{Err: &net.OpError{Err: syscall.ECONNREFUSED}},
84+
true,
85+
},
86+
{&url.Error{Err: &net.OpError{Err: &os.SyscallError{Err: syscall.ECONNREFUSED}}},
87+
true,
88+
},
89+
}
90+
91+
for _, tc := range testCases {
92+
if result := IsConnectionRefused(tc.err); result != tc.expect {
93+
t.Errorf("Expect to be %v, but actual is %v", tc.expect, result)
94+
}
95+
}
96+
}

staging/src/k8s.io/client-go/tools/cache/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ go_library(
7676
"//staging/src/k8s.io/apimachinery/pkg/util/clock:go_default_library",
7777
"//staging/src/k8s.io/apimachinery/pkg/util/diff:go_default_library",
7878
"//staging/src/k8s.io/apimachinery/pkg/util/naming:go_default_library",
79+
"//staging/src/k8s.io/apimachinery/pkg/util/net:go_default_library",
7980
"//staging/src/k8s.io/apimachinery/pkg/util/runtime:go_default_library",
8081
"//staging/src/k8s.io/apimachinery/pkg/util/sets:go_default_library",
8182
"//staging/src/k8s.io/apimachinery/pkg/util/wait:go_default_library",

staging/src/k8s.io/client-go/tools/cache/reflector.go

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,8 @@ import (
2222
"fmt"
2323
"io"
2424
"math/rand"
25-
"net"
26-
"net/url"
2725
"reflect"
2826
"sync"
29-
"syscall"
3027
"time"
3128

3229
apierrs "k8s.io/apimachinery/pkg/api/errors"
@@ -35,6 +32,7 @@ import (
3532
"k8s.io/apimachinery/pkg/runtime"
3633
"k8s.io/apimachinery/pkg/util/clock"
3734
"k8s.io/apimachinery/pkg/util/naming"
35+
utilnet "k8s.io/apimachinery/pkg/util/net"
3836
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
3937
"k8s.io/apimachinery/pkg/util/wait"
4038
"k8s.io/apimachinery/pkg/watch"
@@ -285,13 +283,9 @@ func (r *Reflector) ListAndWatch(stopCh <-chan struct{}) error {
285283
// It doesn't make sense to re-list all objects because most likely we will be able to restart
286284
// watch where we ended.
287285
// If that's the case wait and resend watch request.
288-
if urlError, ok := err.(*url.Error); ok {
289-
if opError, ok := urlError.Err.(*net.OpError); ok {
290-
if errno, ok := opError.Err.(syscall.Errno); ok && errno == syscall.ECONNREFUSED {
291-
time.Sleep(time.Second)
292-
continue
293-
}
294-
}
286+
if utilnet.IsConnectionRefused(err) {
287+
time.Sleep(time.Second)
288+
continue
295289
}
296290
return nil
297291
}

staging/src/k8s.io/client-go/tools/cache/reflector_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import (
2424
"testing"
2525
"time"
2626

27-
v1 "k8s.io/api/core/v1"
27+
"k8s.io/api/core/v1"
2828
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2929
"k8s.io/apimachinery/pkg/runtime"
3030
"k8s.io/apimachinery/pkg/util/wait"

0 commit comments

Comments
 (0)