diff --git a/.golangci.yml b/.golangci.yml index e4138482bb..68a0e7039b 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -7,13 +7,14 @@ run: linters: default: none enable: - - misspell + - govet - ineffassign + - misspell - modernize + - noctx - revive - unconvert - unused - - govet settings: govet: enable: diff --git a/agent/testutils/fakes.go b/agent/testutils/fakes.go index d6710326dd..feb09e1b83 100644 --- a/agent/testutils/fakes.go +++ b/agent/testutils/fakes.go @@ -237,13 +237,13 @@ func NewMockDispatcher(t *testing.T, secConfig *ca.SecurityConfig, local bool) ( tempDir, err := os.MkdirTemp("", "local-dispatcher-socket") require.NoError(t, err) addr = filepath.Join(tempDir, "socket") - l, err = net.Listen("unix", addr) + l, err = (&net.ListenConfig{}).Listen(t.Context(), "unix", addr) require.NoError(t, err) cleanup = func() { os.RemoveAll(tempDir) } } else { - l, err = net.Listen("tcp", "127.0.0.1:0") + l, err = (&net.ListenConfig{}).Listen(t.Context(), "tcp", "127.0.0.1:0") require.NoError(t, err) addr = l.Addr().String() } diff --git a/ca/testutils/cautils.go b/ca/testutils/cautils.go index 9bd491f03d..686d7de0ed 100644 --- a/ca/testutils/cautils.go +++ b/ca/testutils/cautils.go @@ -154,12 +154,19 @@ func newTestCA(t *testing.T, tempBaseDir string, apiRootCA api.RootCA, krwGenera } var ( + ctx context.Context externalSigningServer *ExternalSigningServer externalCAs []*api.ExternalCA err error rootCA ca.RootCA ) + if t != nil { + ctx = t.Context() + } else { + ctx = context.Background() + } + if apiRootCA.RootRotation != nil { rootCA, err = ca.NewRootCA( apiRootCA.CACert, apiRootCA.RootRotation.CACert, apiRootCA.RootRotation.CAKey, ca.DefaultNodeCertExpiration, apiRootCA.RootRotation.CrossSignedCACert) @@ -230,7 +237,7 @@ func newTestCA(t *testing.T, tempBaseDir string, apiRootCA api.RootCA, krwGenera assert.NoError(t, err) } - l, err := net.Listen("tcp", "127.0.0.1:0") + l, err := (&net.ListenConfig{}).Listen(ctx, "tcp", "127.0.0.1:0") if t != nil { assert.NoError(t, err) } diff --git a/ca/transport.go b/ca/transport.go index 7f95ab986e..0f7a932a1f 100644 --- a/ca/transport.go +++ b/ca/transport.go @@ -87,7 +87,7 @@ func (c *MutableTLSCreds) ClientHandshake(ctx context.Context, addr string, rawC var err error errChannel := make(chan error, 1) go func() { - errChannel <- conn.Handshake() + errChannel <- conn.HandshakeContext(context.Background()) }() select { case err = <-errChannel: @@ -106,7 +106,7 @@ func (c *MutableTLSCreds) ServerHandshake(rawConn net.Conn) (net.Conn, credentia c.Lock() conn := tls.Server(rawConn, c.config) c.Unlock() - if err := conn.Handshake(); err != nil { + if err := conn.HandshakeContext(context.Background()); err != nil { rawConn.Close() return nil, nil, err } diff --git a/cmd/swarm-bench/collector.go b/cmd/swarm-bench/collector.go index 15c23c7535..dae2527218 100644 --- a/cmd/swarm-bench/collector.go +++ b/cmd/swarm-bench/collector.go @@ -22,7 +22,7 @@ type Collector struct { // once they come online. func (c *Collector) Listen(port int) error { var err error - c.ln, err = net.Listen("tcp", ":"+strconv.Itoa(port)) + c.ln, err = (&net.ListenConfig{}).Listen(context.Background(), "tcp", ":"+strconv.Itoa(port)) return err } diff --git a/connectionbroker/broker.go b/connectionbroker/broker.go index 93344d2380..2ac1e9d98d 100644 --- a/connectionbroker/broker.go +++ b/connectionbroker/broker.go @@ -4,6 +4,7 @@ package connectionbroker import ( + "context" "net" "sync" "time" @@ -69,7 +70,7 @@ func (b *Broker) SelectRemote(dialOpts ...grpc.DialOption) (*Conn, error) { grpc.WithUnaryInterceptor(grpc_prometheus.UnaryClientInterceptor), grpc.WithStreamInterceptor(grpc_prometheus.StreamClientInterceptor), grpc.WithDialer(func(addr string, timeout time.Duration) (net.Conn, error) { - return net.DialTimeout("tcp", addr, timeout) + return (&net.Dialer{Timeout: timeout}).DialContext(context.Background(), "tcp", addr) })) cc, err := grpc.Dial(peer.Addr, dialOpts...) diff --git a/manager/controlapi/ca_rotation.go b/manager/controlapi/ca_rotation.go index 0847126c0a..20f3bb2310 100644 --- a/manager/controlapi/ca_rotation.go +++ b/manager/controlapi/ca_rotation.go @@ -117,7 +117,7 @@ func validateExternalCAURL(dialer *net.Dialer, tlsOpts *tls.Config, caURL string port = "443" } - conn, err := tls.DialWithDialer(dialer, "tcp", net.JoinHostPort(host, port), tlsOpts) + conn, err := (&tls.Dialer{NetDialer: dialer, Config: tlsOpts}).DialContext(context.Background(), "tcp", net.JoinHostPort(host, port)) if conn != nil { conn.Close() } diff --git a/manager/manager.go b/manager/manager.go index 8b14005641..0f75a4bb46 100644 --- a/manager/manager.go +++ b/manager/manager.go @@ -401,7 +401,7 @@ func (m *Manager) BindRemote(ctx context.Context, addrs RemoteAddrs) error { advertiseAddr = net.JoinHostPort("0.0.0.0", advertiseAddrPort) } - l, err := net.Listen("tcp", addrs.ListenAddr) + l, err := (&net.ListenConfig{}).Listen(context.Background(), "tcp", addrs.ListenAddr) if err != nil { return errors.Wrap(err, "failed to listen on remote API address") } diff --git a/manager/state/raft/testutils/testutils.go b/manager/state/raft/testutils/testutils.go index 8612af9c26..84ddc41ef5 100644 --- a/manager/state/raft/testutils/testutils.go +++ b/manager/state/raft/testutils/testutils.go @@ -290,7 +290,7 @@ var _ raft.EncryptionKeyRotator = NewSimpleKeyRotator(raft.EncryptionKeys{}) // NewNode creates a new raft node to use for tests func NewNode(t *testing.T, clockSource *fakeclock.FakeClock, tc *cautils.TestCA, opts ...raft.NodeOptions) *TestNode { - l, err := net.Listen("tcp", "127.0.0.1:0") + l, err := (&net.ListenConfig{}).Listen(t.Context(), "tcp", "127.0.0.1:0") require.NoError(t, err, "can't bind to raft service port") wrappedListener := NewWrappedListener(l) diff --git a/manager/state/raft/transport/transport.go b/manager/state/raft/transport/transport.go index 549326bdab..ebf7c9a7b4 100644 --- a/manager/state/raft/transport/transport.go +++ b/manager/state/raft/transport/transport.go @@ -352,7 +352,7 @@ func (t *Transport) dial(addr string) (*grpc.ClientConn, error) { // TODO(anshul) Add an option to configure this. grpcOptions = append(grpcOptions, grpc.WithDialer(func(addr string, timeout time.Duration) (net.Conn, error) { - return net.DialTimeout("tcp", addr, timeout) + return (&net.Dialer{Timeout: timeout}).DialContext(context.Background(), "tcp", addr) })) // TODO(dperny): this changes the max received message size for outgoing diff --git a/manager/state/raft/util.go b/manager/state/raft/util.go index 905e0043a3..099decfa97 100644 --- a/manager/state/raft/util.go +++ b/manager/state/raft/util.go @@ -22,7 +22,7 @@ func dial(addr string, _ string, creds credentials.TransportCredentials, timeout grpc.WithUnaryInterceptor(grpc_prometheus.UnaryClientInterceptor), grpc.WithStreamInterceptor(grpc_prometheus.StreamClientInterceptor), grpc.WithDialer(func(addr string, timeout time.Duration) (net.Conn, error) { - return net.DialTimeout("tcp", addr, timeout) + return (&net.Dialer{Timeout: timeout}).DialContext(context.Background(), "tcp", addr) }), } diff --git a/xnet/xnet_unix.go b/xnet/xnet_unix.go index d1096e2313..d4571f396d 100644 --- a/xnet/xnet_unix.go +++ b/xnet/xnet_unix.go @@ -3,6 +3,7 @@ package xnet import ( + "context" "net" "time" ) @@ -10,11 +11,11 @@ import ( // ListenLocal opens a local socket for control communication func ListenLocal(socket string) (net.Listener, error) { // on unix it's just a unix socket - return net.Listen("unix", socket) + return (&net.ListenConfig{}).Listen(context.Background(), "unix", socket) } // DialTimeoutLocal is a DialTimeout function for local sockets func DialTimeoutLocal(socket string, timeout time.Duration) (net.Conn, error) { // on unix, we dial a unix socket - return net.DialTimeout("unix", socket, timeout) + return (&net.Dialer{Timeout: timeout}).DialContext(context.Background(), "unix", socket) }