kubelet_client.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /*
  2. Copyright 2014 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 client
  14. import (
  15. "errors"
  16. "fmt"
  17. "net"
  18. "net/http"
  19. "strings"
  20. "time"
  21. "k8s.io/kubernetes/pkg/api"
  22. "k8s.io/kubernetes/pkg/api/validation"
  23. "k8s.io/kubernetes/pkg/client/restclient"
  24. "k8s.io/kubernetes/pkg/client/transport"
  25. utilnet "k8s.io/kubernetes/pkg/util/net"
  26. )
  27. type KubeletClientConfig struct {
  28. // Default port - used if no information about Kubelet port can be found in Node.NodeStatus.DaemonEndpoints.
  29. Port uint
  30. EnableHttps bool
  31. // TLSClientConfig contains settings to enable transport layer security
  32. restclient.TLSClientConfig
  33. // Server requires Bearer authentication
  34. BearerToken string
  35. // HTTPTimeout is used by the client to timeout http requests to Kubelet.
  36. HTTPTimeout time.Duration
  37. // Dial is a custom dialer used for the client
  38. Dial func(net, addr string) (net.Conn, error)
  39. }
  40. // KubeletClient is an interface for all kubelet functionality
  41. type KubeletClient interface {
  42. ConnectionInfoGetter
  43. }
  44. type ConnectionInfoGetter interface {
  45. GetConnectionInfo(ctx api.Context, nodeName string) (scheme string, port uint, transport http.RoundTripper, err error)
  46. }
  47. // HTTPKubeletClient is the default implementation of KubeletHealthchecker, accesses the kubelet over HTTP.
  48. type HTTPKubeletClient struct {
  49. Client *http.Client
  50. Config *KubeletClientConfig
  51. }
  52. func MakeTransport(config *KubeletClientConfig) (http.RoundTripper, error) {
  53. tlsConfig, err := transport.TLSConfigFor(config.transportConfig())
  54. if err != nil {
  55. return nil, err
  56. }
  57. rt := http.DefaultTransport
  58. if config.Dial != nil || tlsConfig != nil {
  59. rt = utilnet.SetOldTransportDefaults(&http.Transport{
  60. Dial: config.Dial,
  61. TLSClientConfig: tlsConfig,
  62. })
  63. }
  64. return transport.HTTPWrappersForConfig(config.transportConfig(), rt)
  65. }
  66. // TODO: this structure is questionable, it should be using client.Config and overriding defaults.
  67. func NewStaticKubeletClient(config *KubeletClientConfig) (KubeletClient, error) {
  68. transport, err := MakeTransport(config)
  69. if err != nil {
  70. return nil, err
  71. }
  72. c := &http.Client{
  73. Transport: transport,
  74. Timeout: config.HTTPTimeout,
  75. }
  76. return &HTTPKubeletClient{
  77. Client: c,
  78. Config: config,
  79. }, nil
  80. }
  81. // In default HTTPKubeletClient ctx is unused.
  82. func (c *HTTPKubeletClient) GetConnectionInfo(ctx api.Context, nodeName string) (string, uint, http.RoundTripper, error) {
  83. if errs := validation.ValidateNodeName(nodeName, false); len(errs) != 0 {
  84. return "", 0, nil, fmt.Errorf("invalid node name: %s", strings.Join(errs, ";"))
  85. }
  86. scheme := "http"
  87. if c.Config.EnableHttps {
  88. scheme = "https"
  89. }
  90. return scheme, c.Config.Port, c.Client.Transport, nil
  91. }
  92. // FakeKubeletClient is a fake implementation of KubeletClient which returns an error
  93. // when called. It is useful to pass to the master in a test configuration with
  94. // no kubelets.
  95. type FakeKubeletClient struct{}
  96. func (c FakeKubeletClient) GetConnectionInfo(ctx api.Context, nodeName string) (string, uint, http.RoundTripper, error) {
  97. return "", 0, nil, errors.New("Not Implemented")
  98. }
  99. // transportConfig converts a client config to an appropriate transport config.
  100. func (c *KubeletClientConfig) transportConfig() *transport.Config {
  101. cfg := &transport.Config{
  102. TLS: transport.TLSConfig{
  103. CAFile: c.CAFile,
  104. CAData: c.CAData,
  105. CertFile: c.CertFile,
  106. CertData: c.CertData,
  107. KeyFile: c.KeyFile,
  108. KeyData: c.KeyData,
  109. },
  110. BearerToken: c.BearerToken,
  111. }
  112. if c.EnableHttps && !cfg.HasCA() {
  113. cfg.TLS.Insecure = true
  114. }
  115. return cfg
  116. }