cache.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /*
  2. Copyright 2015 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package transport
  14. import (
  15. "fmt"
  16. "net"
  17. "net/http"
  18. "sync"
  19. "time"
  20. utilnet "k8s.io/apimachinery/pkg/util/net"
  21. )
  22. // TlsTransportCache caches TLS http.RoundTrippers different configurations. The
  23. // same RoundTripper will be returned for configs with identical TLS options If
  24. // the config has no custom TLS options, http.DefaultTransport is returned.
  25. type tlsTransportCache struct {
  26. mu sync.Mutex
  27. transports map[tlsCacheKey]*http.Transport
  28. }
  29. const idleConnsPerHost = 25
  30. var tlsCache = &tlsTransportCache{transports: make(map[tlsCacheKey]*http.Transport)}
  31. type tlsCacheKey struct {
  32. insecure bool
  33. caData string
  34. certData string
  35. keyData string
  36. getCert string
  37. serverName string
  38. dial string
  39. }
  40. func (t tlsCacheKey) String() string {
  41. keyText := "<none>"
  42. if len(t.keyData) > 0 {
  43. keyText = "<redacted>"
  44. }
  45. return fmt.Sprintf("insecure:%v, caData:%#v, certData:%#v, keyData:%s, getCert: %s, serverName:%s, dial:%s", t.insecure, t.caData, t.certData, keyText, t.getCert, t.serverName, t.dial)
  46. }
  47. func (c *tlsTransportCache) get(config *Config) (http.RoundTripper, error) {
  48. key, err := tlsConfigKey(config)
  49. if err != nil {
  50. return nil, err
  51. }
  52. // Ensure we only create a single transport for the given TLS options
  53. c.mu.Lock()
  54. defer c.mu.Unlock()
  55. // See if we already have a custom transport for this config
  56. if t, ok := c.transports[key]; ok {
  57. return t, nil
  58. }
  59. // Get the TLS options for this client config
  60. tlsConfig, err := TLSConfigFor(config)
  61. if err != nil {
  62. return nil, err
  63. }
  64. // The options didn't require a custom TLS config
  65. if tlsConfig == nil && config.Dial == nil {
  66. return http.DefaultTransport, nil
  67. }
  68. dial := config.Dial
  69. if dial == nil {
  70. dial = (&net.Dialer{
  71. Timeout: 30 * time.Second,
  72. KeepAlive: 30 * time.Second,
  73. }).DialContext
  74. }
  75. // Cache a single transport for these options
  76. c.transports[key] = utilnet.SetTransportDefaults(&http.Transport{
  77. Proxy: http.ProxyFromEnvironment,
  78. TLSHandshakeTimeout: 10 * time.Second,
  79. TLSClientConfig: tlsConfig,
  80. MaxIdleConnsPerHost: idleConnsPerHost,
  81. DialContext: dial,
  82. })
  83. return c.transports[key], nil
  84. }
  85. // tlsConfigKey returns a unique key for tls.Config objects returned from TLSConfigFor
  86. func tlsConfigKey(c *Config) (tlsCacheKey, error) {
  87. // Make sure ca/key/cert content is loaded
  88. if err := loadTLSFiles(c); err != nil {
  89. return tlsCacheKey{}, err
  90. }
  91. return tlsCacheKey{
  92. insecure: c.TLS.Insecure,
  93. caData: string(c.TLS.CAData),
  94. certData: string(c.TLS.CertData),
  95. keyData: string(c.TLS.KeyData),
  96. getCert: fmt.Sprintf("%p", c.TLS.GetCert),
  97. serverName: c.TLS.ServerName,
  98. dial: fmt.Sprintf("%p", c.Dial),
  99. }, nil
  100. }