2014-06-06 23:40:48 +00:00
|
|
|
/*
|
2015-05-01 16:19:44 +00:00
|
|
|
Copyright 2014 The Kubernetes Authors All rights reserved.
|
2014-06-06 23:40:48 +00:00
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
2014-06-12 20:09:41 +00:00
|
|
|
|
2015-08-12 17:35:07 +00:00
|
|
|
package unversioned
|
2014-06-06 23:40:48 +00:00
|
|
|
|
|
|
|
import (
|
2014-07-18 14:54:43 +00:00
|
|
|
"errors"
|
2014-06-06 23:40:48 +00:00
|
|
|
"net/http"
|
2015-10-02 04:30:49 +00:00
|
|
|
|
|
|
|
"k8s.io/kubernetes/pkg/util"
|
2014-06-06 23:40:48 +00:00
|
|
|
)
|
|
|
|
|
2014-10-10 22:19:23 +00:00
|
|
|
// KubeletClient is an interface for all kubelet functionality
|
|
|
|
type KubeletClient interface {
|
2015-03-23 18:42:39 +00:00
|
|
|
ConnectionInfoGetter
|
2014-10-10 22:19:23 +00:00
|
|
|
}
|
|
|
|
|
2015-03-23 18:42:39 +00:00
|
|
|
type ConnectionInfoGetter interface {
|
2015-05-14 00:30:37 +00:00
|
|
|
GetConnectionInfo(host string) (scheme string, port uint, transport http.RoundTripper, err error)
|
2015-03-23 18:42:39 +00:00
|
|
|
}
|
|
|
|
|
2015-05-14 22:26:44 +00:00
|
|
|
// HTTPKubeletClient is the default implementation of KubeletHealthchecker, accesses the kubelet over HTTP.
|
2014-10-10 22:19:23 +00:00
|
|
|
type HTTPKubeletClient struct {
|
2015-06-25 17:53:41 +00:00
|
|
|
Client *http.Client
|
|
|
|
Config *KubeletConfig
|
2014-10-10 22:19:23 +00:00
|
|
|
}
|
|
|
|
|
2015-05-29 22:33:22 +00:00
|
|
|
func MakeTransport(config *KubeletConfig) (http.RoundTripper, error) {
|
2015-04-01 23:19:17 +00:00
|
|
|
cfg := &Config{TLSClientConfig: config.TLSClientConfig}
|
|
|
|
if config.EnableHttps {
|
|
|
|
hasCA := len(config.CAFile) > 0 || len(config.CAData) > 0
|
|
|
|
if !hasCA {
|
|
|
|
cfg.Insecure = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
tlsConfig, err := TLSConfigFor(cfg)
|
2015-01-29 22:43:09 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-06-02 16:52:35 +00:00
|
|
|
if config.Dial != nil || tlsConfig != nil {
|
2015-10-02 04:30:49 +00:00
|
|
|
return util.SetTransportDefaults(&http.Transport{
|
2015-06-02 16:52:35 +00:00
|
|
|
Dial: config.Dial,
|
2015-01-29 22:43:09 +00:00
|
|
|
TLSClientConfig: tlsConfig,
|
2015-10-02 04:30:49 +00:00
|
|
|
}), nil
|
2015-06-02 16:52:35 +00:00
|
|
|
} else {
|
2015-06-04 18:58:38 +00:00
|
|
|
return http.DefaultTransport, nil
|
2014-10-10 22:19:23 +00:00
|
|
|
}
|
2015-05-29 22:33:22 +00:00
|
|
|
}
|
2014-10-10 22:19:23 +00:00
|
|
|
|
2015-05-29 22:33:22 +00:00
|
|
|
// TODO: this structure is questionable, it should be using client.Config and overriding defaults.
|
|
|
|
func NewKubeletClient(config *KubeletConfig) (KubeletClient, error) {
|
|
|
|
transport, err := MakeTransport(config)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-01-29 22:43:09 +00:00
|
|
|
c := &http.Client{
|
|
|
|
Transport: transport,
|
2015-03-19 01:11:39 +00:00
|
|
|
Timeout: config.HTTPTimeout,
|
2015-01-29 22:43:09 +00:00
|
|
|
}
|
2014-10-10 22:19:23 +00:00
|
|
|
return &HTTPKubeletClient{
|
2015-06-25 17:53:41 +00:00
|
|
|
Client: c,
|
|
|
|
Config: config,
|
2014-10-10 22:19:23 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2015-03-23 18:42:39 +00:00
|
|
|
func (c *HTTPKubeletClient) GetConnectionInfo(host string) (string, uint, http.RoundTripper, error) {
|
|
|
|
scheme := "http"
|
2015-06-25 17:53:41 +00:00
|
|
|
if c.Config.EnableHttps {
|
2015-03-23 18:42:39 +00:00
|
|
|
scheme = "https"
|
|
|
|
}
|
2015-06-25 17:53:41 +00:00
|
|
|
return scheme, c.Config.Port, c.Client.Transport, nil
|
2015-03-23 18:42:39 +00:00
|
|
|
}
|
|
|
|
|
2014-11-03 22:50:41 +00:00
|
|
|
// FakeKubeletClient is a fake implementation of KubeletClient which returns an error
|
|
|
|
// when called. It is useful to pass to the master in a test configuration with
|
|
|
|
// no kubelets.
|
|
|
|
type FakeKubeletClient struct{}
|
2014-06-06 23:40:48 +00:00
|
|
|
|
2015-03-23 18:42:39 +00:00
|
|
|
func (c FakeKubeletClient) GetConnectionInfo(host string) (string, uint, http.RoundTripper, error) {
|
|
|
|
return "", 0, nil, errors.New("Not Implemented")
|
|
|
|
}
|