Skip to content

Commit 4a75b93

Browse files
ash2kk8s-publishing-bot
authored andcommitted
Use Dial with context
Kubernetes-commit: 5e8e570dbda6ed89af9bc2e0a05e3d94bfdfcb61
1 parent 4bb327e commit 4a75b93

File tree

7 files changed

+28
-50
lines changed

7 files changed

+28
-50
lines changed

discovery/discovery_client.go

-4
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,8 @@ const (
4444
defaultRetries = 2
4545
// protobuf mime type
4646
mimePb = "application/[email protected]+protobuf"
47-
)
48-
49-
var (
5047
// defaultTimeout is the maximum amount of time per request when no timeout has been set on a RESTClient.
5148
// Defaults to 32s in order to have a distinguishable length of time, relative to other timeouts that exist.
52-
// It's a variable to be able to change it in tests.
5349
defaultTimeout = 32 * time.Second
5450
)
5551

discovery/discovery_client_test.go

+6-27
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,11 @@ import (
2323
"net/http"
2424
"net/http/httptest"
2525
"reflect"
26-
"strings"
2726
"testing"
28-
"time"
2927

3028
"github.com/gogo/protobuf/proto"
3129
"github.com/googleapis/gnostic/OpenAPIv2"
30+
"github.com/stretchr/testify/assert"
3231

3332
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
3433
"k8s.io/apimachinery/pkg/runtime/schema"
@@ -131,31 +130,11 @@ func TestGetServerGroupsWithBrokenServer(t *testing.T) {
131130
}
132131
}
133132
}
134-
func TestGetServerGroupsWithTimeout(t *testing.T) {
135-
done := make(chan bool)
136-
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
137-
// first we need to write headers, otherwise http client will complain about
138-
// exceeding timeout awaiting headers, only after we can block the call
139-
w.Header().Set("Connection", "keep-alive")
140-
if wf, ok := w.(http.Flusher); ok {
141-
wf.Flush()
142-
}
143-
<-done
144-
}))
145-
defer server.Close()
146-
defer close(done)
147-
client := NewDiscoveryClientForConfigOrDie(&restclient.Config{Host: server.URL, Timeout: 2 * time.Second})
148-
_, err := client.ServerGroups()
149-
// the error we're getting here is wrapped in errors.errorString which makes
150-
// it impossible to unwrap and check it's attributes, so instead we're checking
151-
// the textual output which is presenting http.httpError with timeout set to true
152-
if err == nil {
153-
t.Fatal("missing error")
154-
}
155-
if !strings.Contains(err.Error(), "timeout:true") &&
156-
!strings.Contains(err.Error(), "context.deadlineExceededError") {
157-
t.Fatalf("unexpected error: %v", err)
158-
}
133+
134+
func TestTimeoutIsSet(t *testing.T) {
135+
cfg := &restclient.Config{}
136+
setDiscoveryDefaults(cfg)
137+
assert.Equal(t, defaultTimeout, cfg.Timeout)
159138
}
160139

161140
func TestGetServerResourcesWithV1Server(t *testing.T) {

rest/config.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ limitations under the License.
1717
package rest
1818

1919
import (
20+
"context"
2021
"fmt"
2122
"io/ioutil"
2223
"net"
@@ -110,7 +111,7 @@ type Config struct {
110111
Timeout time.Duration
111112

112113
// Dial specifies the dial function for creating unencrypted TCP connections.
113-
Dial func(network, addr string) (net.Conn, error)
114+
Dial func(ctx context.Context, network, address string) (net.Conn, error)
114115

115116
// Version forces a specific version to be used (if registered)
116117
// Do we need this?

rest/config_test.go

+12-13
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ limitations under the License.
1717
package rest
1818

1919
import (
20+
"context"
21+
"errors"
2022
"io"
2123
"net"
2224
"net/http"
@@ -25,8 +27,6 @@ import (
2527
"strings"
2628
"testing"
2729

28-
fuzz "github.com/google/gofuzz"
29-
3030
"k8s.io/api/core/v1"
3131
"k8s.io/apimachinery/pkg/runtime"
3232
"k8s.io/apimachinery/pkg/runtime/schema"
@@ -35,8 +35,7 @@ import (
3535
clientcmdapi "k8s.io/client-go/tools/clientcmd/api"
3636
"k8s.io/client-go/util/flowcontrol"
3737

38-
"errors"
39-
38+
fuzz "github.com/google/gofuzz"
4039
"github.com/stretchr/testify/assert"
4140
)
4241

@@ -208,7 +207,7 @@ func (n *fakeNegotiatedSerializer) DecoderToVersion(serializer runtime.Decoder,
208207
return &fakeCodec{}
209208
}
210209

211-
var fakeDialFunc = func(network, addr string) (net.Conn, error) {
210+
var fakeDialFunc = func(ctx context.Context, network, addr string) (net.Conn, error) {
212211
return nil, fakeDialerError
213212
}
214213
var fakeDialerError = errors.New("fakedialer")
@@ -253,7 +252,7 @@ func TestAnonymousConfig(t *testing.T) {
253252
r.Config = map[string]string{}
254253
},
255254
// Dial does not require fuzzer
256-
func(r *func(network, addr string) (net.Conn, error), f fuzz.Continue) {},
255+
func(r *func(ctx context.Context, network, addr string) (net.Conn, error), f fuzz.Continue) {},
257256
)
258257
for i := 0; i < 20; i++ {
259258
original := &Config{}
@@ -284,10 +283,10 @@ func TestAnonymousConfig(t *testing.T) {
284283
expected.WrapTransport = nil
285284
}
286285
if actual.Dial != nil {
287-
_, actualError := actual.Dial("", "")
288-
_, expectedError := actual.Dial("", "")
286+
_, actualError := actual.Dial(context.Background(), "", "")
287+
_, expectedError := expected.Dial(context.Background(), "", "")
289288
if !reflect.DeepEqual(expectedError, actualError) {
290-
t.Fatalf("CopyConfig dropped the Dial field")
289+
t.Fatalf("CopyConfig dropped the Dial field")
291290
}
292291
} else {
293292
actual.Dial = nil
@@ -329,7 +328,7 @@ func TestCopyConfig(t *testing.T) {
329328
func(r *AuthProviderConfigPersister, f fuzz.Continue) {
330329
*r = fakeAuthProviderConfigPersister{}
331330
},
332-
func(r *func(network, addr string) (net.Conn, error), f fuzz.Continue) {
331+
func(r *func(ctx context.Context, network, addr string) (net.Conn, error), f fuzz.Continue) {
333332
*r = fakeDialFunc
334333
},
335334
)
@@ -351,8 +350,8 @@ func TestCopyConfig(t *testing.T) {
351350
expected.WrapTransport = nil
352351
}
353352
if actual.Dial != nil {
354-
_, actualError := actual.Dial("", "")
355-
_, expectedError := actual.Dial("", "")
353+
_, actualError := actual.Dial(context.Background(), "", "")
354+
_, expectedError := expected.Dial(context.Background(), "", "")
356355
if !reflect.DeepEqual(expectedError, actualError) {
357356
t.Fatalf("CopyConfig dropped the Dial field")
358357
}
@@ -361,7 +360,7 @@ func TestCopyConfig(t *testing.T) {
361360
expected.Dial = nil
362361
if actual.AuthConfigPersister != nil {
363362
actualError := actual.AuthConfigPersister.Persist(nil)
364-
expectedError := actual.AuthConfigPersister.Persist(nil)
363+
expectedError := expected.AuthConfigPersister.Persist(nil)
365364
if !reflect.DeepEqual(expectedError, actualError) {
366365
t.Fatalf("CopyConfig dropped the Dial field")
367366
}

transport/cache.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -85,15 +85,15 @@ func (c *tlsTransportCache) get(config *Config) (http.RoundTripper, error) {
8585
dial = (&net.Dialer{
8686
Timeout: 30 * time.Second,
8787
KeepAlive: 30 * time.Second,
88-
}).Dial
88+
}).DialContext
8989
}
9090
// Cache a single transport for these options
9191
c.transports[key] = utilnet.SetTransportDefaults(&http.Transport{
9292
Proxy: http.ProxyFromEnvironment,
9393
TLSHandshakeTimeout: 10 * time.Second,
9494
TLSClientConfig: tlsConfig,
9595
MaxIdleConnsPerHost: idleConnsPerHost,
96-
Dial: dial,
96+
DialContext: dial,
9797
})
9898
return c.transports[key], nil
9999
}

transport/cache_test.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ limitations under the License.
1717
package transport
1818

1919
import (
20+
"context"
2021
"net"
2122
"net/http"
2223
"testing"
@@ -52,10 +53,11 @@ func TestTLSConfigKey(t *testing.T) {
5253
}
5354

5455
// Make sure config fields that affect the tls config affect the cache key
56+
dialer := net.Dialer{}
5557
uniqueConfigurations := map[string]*Config{
5658
"no tls": {},
57-
"dialer": {Dial: net.Dial},
58-
"dialer2": {Dial: func(network, address string) (net.Conn, error) { return nil, nil }},
59+
"dialer": {Dial: dialer.DialContext},
60+
"dialer2": {Dial: func(ctx context.Context, network, address string) (net.Conn, error) { return nil, nil }},
5961
"insecure": {TLS: TLSConfig{Insecure: true}},
6062
"cadata 1": {TLS: TLSConfig{CAData: []byte{1}}},
6163
"cadata 2": {TLS: TLSConfig{CAData: []byte{2}}},

transport/config.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ limitations under the License.
1717
package transport
1818

1919
import (
20+
"context"
2021
"net"
2122
"net/http"
2223
)
@@ -53,7 +54,7 @@ type Config struct {
5354
WrapTransport func(rt http.RoundTripper) http.RoundTripper
5455

5556
// Dial specifies the dial function for creating unencrypted TCP connections.
56-
Dial func(network, addr string) (net.Conn, error)
57+
Dial func(ctx context.Context, network, address string) (net.Conn, error)
5758
}
5859

5960
// ImpersonationConfig has all the available impersonation options

0 commit comments

Comments
 (0)