mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-29 14:37:00 +00:00
Merge pull request #14967 from liggitt/set_transport_defaults
Add util to set transport defaults
This commit is contained in:
commit
66cbacc9c1
@ -23,9 +23,9 @@ import (
|
|||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strconv"
|
"strconv"
|
||||||
"time"
|
|
||||||
|
|
||||||
"k8s.io/kubernetes/pkg/probe"
|
"k8s.io/kubernetes/pkg/probe"
|
||||||
|
"k8s.io/kubernetes/pkg/util"
|
||||||
)
|
)
|
||||||
|
|
||||||
// TODO: this basic interface is duplicated in N places. consolidate?
|
// TODO: this basic interface is duplicated in N places. consolidate?
|
||||||
@ -59,15 +59,9 @@ func (server *Server) DoServerCheck(rt http.RoundTripper) (probe.Result, string,
|
|||||||
// TODO(roberthbailey): The servers that use HTTPS are currently the
|
// TODO(roberthbailey): The servers that use HTTPS are currently the
|
||||||
// kubelets, and we should be using a standard kubelet client library
|
// kubelets, and we should be using a standard kubelet client library
|
||||||
// to talk to them rather than a separate http client.
|
// to talk to them rather than a separate http client.
|
||||||
transport := &http.Transport{
|
transport := util.SetTransportDefaults(&http.Transport{
|
||||||
Proxy: http.ProxyFromEnvironment,
|
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
|
||||||
Dial: (&net.Dialer{
|
})
|
||||||
Timeout: 30 * time.Second,
|
|
||||||
KeepAlive: 30 * time.Second,
|
|
||||||
}).Dial,
|
|
||||||
TLSHandshakeTimeout: 10 * time.Second,
|
|
||||||
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
|
|
||||||
}
|
|
||||||
|
|
||||||
client = &http.Client{Transport: transport}
|
client = &http.Client{Transport: transport}
|
||||||
scheme = "https://"
|
scheme = "https://"
|
||||||
|
@ -380,15 +380,9 @@ func tlsTransportFor(config *Config) (http.RoundTripper, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Cache a single transport for these options
|
// Cache a single transport for these options
|
||||||
tlsTransports[key] = &http.Transport{
|
tlsTransports[key] = util.SetTransportDefaults(&http.Transport{
|
||||||
TLSClientConfig: tlsConfig,
|
TLSClientConfig: tlsConfig,
|
||||||
Proxy: http.ProxyFromEnvironment,
|
})
|
||||||
Dial: (&net.Dialer{
|
|
||||||
Timeout: 30 * time.Second,
|
|
||||||
KeepAlive: 30 * time.Second,
|
|
||||||
}).Dial,
|
|
||||||
TLSHandshakeTimeout: 10 * time.Second,
|
|
||||||
}
|
|
||||||
return tlsTransports[key], nil
|
return tlsTransports[key], nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -19,6 +19,8 @@ package unversioned
|
|||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
|
"k8s.io/kubernetes/pkg/util"
|
||||||
)
|
)
|
||||||
|
|
||||||
// KubeletClient is an interface for all kubelet functionality
|
// KubeletClient is an interface for all kubelet functionality
|
||||||
@ -49,10 +51,10 @@ func MakeTransport(config *KubeletConfig) (http.RoundTripper, error) {
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
if config.Dial != nil || tlsConfig != nil {
|
if config.Dial != nil || tlsConfig != nil {
|
||||||
return &http.Transport{
|
return util.SetTransportDefaults(&http.Transport{
|
||||||
Dial: config.Dial,
|
Dial: config.Dial,
|
||||||
TLSClientConfig: tlsConfig,
|
TLSClientConfig: tlsConfig,
|
||||||
}, nil
|
}), nil
|
||||||
} else {
|
} else {
|
||||||
return http.DefaultTransport, nil
|
return http.DefaultTransport, nil
|
||||||
}
|
}
|
||||||
|
@ -18,6 +18,7 @@ package util
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"io"
|
"io"
|
||||||
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
@ -44,3 +45,20 @@ func IsProbableEOF(err error) bool {
|
|||||||
}
|
}
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var defaultTransport = http.DefaultTransport.(*http.Transport)
|
||||||
|
|
||||||
|
// SetTransportDefaults applies the defaults from http.DefaultTransport
|
||||||
|
// for the Proxy, Dial, and TLSHandshakeTimeout fields if unset
|
||||||
|
func SetTransportDefaults(t *http.Transport) *http.Transport {
|
||||||
|
if t.Proxy == nil {
|
||||||
|
t.Proxy = defaultTransport.Proxy
|
||||||
|
}
|
||||||
|
if t.Dial == nil {
|
||||||
|
t.Dial = defaultTransport.Dial
|
||||||
|
}
|
||||||
|
if t.TLSHandshakeTimeout == 0 {
|
||||||
|
t.TLSHandshakeTimeout = defaultTransport.TLSHandshakeTimeout
|
||||||
|
}
|
||||||
|
return t
|
||||||
|
}
|
||||||
|
@ -21,7 +21,6 @@ import (
|
|||||||
"crypto/tls"
|
"crypto/tls"
|
||||||
"crypto/x509"
|
"crypto/x509"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
"time"
|
"time"
|
||||||
@ -72,17 +71,11 @@ func New(issuerURL, clientID, caFile, usernameClaim string) (*OIDCAuthenticator,
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Copied from http.DefaultTransport.
|
// Copied from http.DefaultTransport.
|
||||||
tr := &http.Transport{
|
tr := util.SetTransportDefaults(&http.Transport{
|
||||||
// According to golang's doc, if RootCAs is nil,
|
// According to golang's doc, if RootCAs is nil,
|
||||||
// TLS uses the host's root CA set.
|
// TLS uses the host's root CA set.
|
||||||
TLSClientConfig: &tls.Config{RootCAs: roots},
|
TLSClientConfig: &tls.Config{RootCAs: roots},
|
||||||
Proxy: http.ProxyFromEnvironment,
|
})
|
||||||
Dial: (&net.Dialer{
|
|
||||||
Timeout: 30 * time.Second,
|
|
||||||
KeepAlive: 30 * time.Second,
|
|
||||||
}).Dial,
|
|
||||||
TLSHandshakeTimeout: 10 * time.Second,
|
|
||||||
}
|
|
||||||
|
|
||||||
hc := &http.Client{}
|
hc := &http.Client{}
|
||||||
hc.Transport = tr
|
hc.Transport = tr
|
||||||
|
Loading…
Reference in New Issue
Block a user