2014-06-06 23:40:48 +00:00
|
|
|
/*
|
|
|
|
Copyright 2014 Google Inc. All rights reserved.
|
|
|
|
|
|
|
|
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
|
|
|
|
2014-06-06 23:40:48 +00:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
2014-07-18 14:54:43 +00:00
|
|
|
"errors"
|
2014-06-06 23:40:48 +00:00
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
2014-07-01 02:46:10 +00:00
|
|
|
"net"
|
2014-06-06 23:40:48 +00:00
|
|
|
"net/http"
|
2015-03-02 09:39:12 +00:00
|
|
|
"net/url"
|
2014-07-01 02:46:10 +00:00
|
|
|
"strconv"
|
|
|
|
|
2014-07-01 22:35:56 +00:00
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
|
2014-12-15 17:29:04 +00:00
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/latest"
|
2015-01-25 03:22:18 +00:00
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/probe"
|
|
|
|
httprobe "github.com/GoogleCloudPlatform/kubernetes/pkg/probe/http"
|
2015-03-02 00:34:48 +00:00
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/runtime"
|
2014-06-06 23:40:48 +00:00
|
|
|
)
|
|
|
|
|
2014-09-02 10:00:28 +00:00
|
|
|
// ErrPodInfoNotAvailable may be returned when the requested pod info is not available.
|
2014-07-18 14:54:43 +00:00
|
|
|
var ErrPodInfoNotAvailable = errors.New("no pod info available")
|
|
|
|
|
2014-10-10 22:19:23 +00:00
|
|
|
// KubeletClient is an interface for all kubelet functionality
|
|
|
|
type KubeletClient interface {
|
|
|
|
KubeletHealthChecker
|
|
|
|
PodInfoGetter
|
2015-03-02 00:34:48 +00:00
|
|
|
NodeInfoGetter
|
2014-10-10 22:19:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// KubeletHealthchecker is an interface for healthchecking kubelets
|
|
|
|
type KubeletHealthChecker interface {
|
2015-01-31 01:03:57 +00:00
|
|
|
HealthCheck(host string) (probe.Result, error)
|
2014-10-10 22:19:23 +00:00
|
|
|
}
|
|
|
|
|
2014-07-01 22:35:56 +00:00
|
|
|
// PodInfoGetter is an interface for things that can get information about a pod's containers.
|
2014-06-16 05:34:16 +00:00
|
|
|
// Injectable for easy testing.
|
2014-07-01 22:35:56 +00:00
|
|
|
type PodInfoGetter interface {
|
2015-01-14 02:11:24 +00:00
|
|
|
// GetPodStatus returns information about all containers which are part
|
|
|
|
// Returns an api.PodStatus, or an error if one occurs.
|
|
|
|
GetPodStatus(host, podNamespace, podID string) (api.PodStatusResult, error)
|
2014-06-06 23:40:48 +00:00
|
|
|
}
|
|
|
|
|
2015-03-02 00:34:48 +00:00
|
|
|
type NodeInfoGetter interface {
|
|
|
|
GetNodeInfo(host string) (api.NodeInfo, error)
|
|
|
|
}
|
|
|
|
|
2014-10-10 22:19:23 +00:00
|
|
|
// HTTPKubeletClient is the default implementation of PodInfoGetter and KubeletHealthchecker, accesses the kubelet over HTTP.
|
|
|
|
type HTTPKubeletClient struct {
|
|
|
|
Client *http.Client
|
|
|
|
Port uint
|
|
|
|
EnableHttps bool
|
|
|
|
}
|
|
|
|
|
2015-01-29 22:43:09 +00:00
|
|
|
// TODO: this structure is questionable, it should be using client.Config and overriding defaults.
|
2014-10-10 22:19:23 +00:00
|
|
|
func NewKubeletClient(config *KubeletConfig) (KubeletClient, error) {
|
|
|
|
transport := http.DefaultTransport
|
2015-01-29 22:43:09 +00:00
|
|
|
|
|
|
|
tlsConfig, err := TLSConfigFor(&Config{
|
|
|
|
TLSClientConfig: config.TLSClientConfig,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if tlsConfig != nil {
|
|
|
|
transport = &http.Transport{
|
|
|
|
TLSClientConfig: tlsConfig,
|
2015-01-12 21:38:48 +00:00
|
|
|
}
|
2014-10-10 22:19:23 +00:00
|
|
|
}
|
|
|
|
|
2015-01-29 22:43:09 +00:00
|
|
|
c := &http.Client{
|
|
|
|
Transport: transport,
|
|
|
|
}
|
2014-10-10 22:19:23 +00:00
|
|
|
return &HTTPKubeletClient{
|
|
|
|
Client: c,
|
|
|
|
Port: config.Port,
|
|
|
|
EnableHttps: config.EnableHttps,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2015-03-02 09:39:12 +00:00
|
|
|
func (c *HTTPKubeletClient) url(host, path, query string) string {
|
|
|
|
scheme := "http"
|
2014-10-10 22:19:23 +00:00
|
|
|
if c.EnableHttps {
|
2015-03-02 09:39:12 +00:00
|
|
|
scheme = "https"
|
2014-10-10 22:19:23 +00:00
|
|
|
}
|
|
|
|
|
2015-03-02 09:39:12 +00:00
|
|
|
return (&url.URL{
|
|
|
|
Scheme: scheme,
|
|
|
|
Host: net.JoinHostPort(host, strconv.FormatUint(uint64(c.Port), 10)),
|
|
|
|
Path: path,
|
|
|
|
RawQuery: query,
|
|
|
|
}).String()
|
2014-06-06 23:40:48 +00:00
|
|
|
}
|
|
|
|
|
2014-07-08 07:15:41 +00:00
|
|
|
// GetPodInfo gets information about the specified pod.
|
2015-01-14 02:11:24 +00:00
|
|
|
func (c *HTTPKubeletClient) GetPodStatus(host, podNamespace, podID string) (api.PodStatusResult, error) {
|
|
|
|
status := api.PodStatusResult{}
|
2015-03-02 00:34:48 +00:00
|
|
|
query := url.Values{"podID": {podID}, "podNamespace": {podNamespace}}
|
|
|
|
response, err := c.getEntity(host, "/api/v1beta1/podInfo", query.Encode(), &status)
|
2015-03-11 16:48:01 +00:00
|
|
|
if response != nil && response.StatusCode == http.StatusNotFound {
|
2015-03-02 00:34:48 +00:00
|
|
|
return status, ErrPodInfoNotAvailable
|
|
|
|
}
|
|
|
|
return status, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetNodeInfo gets information about the specified node.
|
|
|
|
func (c *HTTPKubeletClient) GetNodeInfo(host string) (api.NodeInfo, error) {
|
|
|
|
info := api.NodeInfo{}
|
|
|
|
_, err := c.getEntity(host, "/api/v1beta1/nodeInfo", "", &info)
|
|
|
|
return info, err
|
|
|
|
}
|
|
|
|
|
2015-03-11 16:48:01 +00:00
|
|
|
// getEntity might return a nil response.
|
2015-03-02 00:34:48 +00:00
|
|
|
func (c *HTTPKubeletClient) getEntity(host, path, query string, entity runtime.Object) (*http.Response, error) {
|
|
|
|
request, err := http.NewRequest("GET", c.url(host, path, query), nil)
|
2014-06-06 23:40:48 +00:00
|
|
|
if err != nil {
|
2015-03-02 00:34:48 +00:00
|
|
|
return nil, err
|
2014-06-06 23:40:48 +00:00
|
|
|
}
|
|
|
|
response, err := c.Client.Do(request)
|
|
|
|
if err != nil {
|
2015-03-02 00:34:48 +00:00
|
|
|
return response, err
|
2014-06-06 23:40:48 +00:00
|
|
|
}
|
|
|
|
defer response.Body.Close()
|
2015-01-30 00:05:41 +00:00
|
|
|
if response.StatusCode >= 300 || response.StatusCode < 200 {
|
2015-03-02 00:34:48 +00:00
|
|
|
return response, fmt.Errorf("kubelet %q server responded with HTTP error code %d", host, response.StatusCode)
|
2015-01-30 00:05:41 +00:00
|
|
|
}
|
2014-06-06 23:40:48 +00:00
|
|
|
body, err := ioutil.ReadAll(response.Body)
|
|
|
|
if err != nil {
|
2015-03-02 00:34:48 +00:00
|
|
|
return response, err
|
2014-06-06 23:40:48 +00:00
|
|
|
}
|
2015-03-02 00:34:48 +00:00
|
|
|
err = latest.Codec.DecodeInto(body, entity)
|
|
|
|
return response, err
|
2014-06-06 23:40:48 +00:00
|
|
|
}
|
|
|
|
|
2015-01-31 01:03:57 +00:00
|
|
|
func (c *HTTPKubeletClient) HealthCheck(host string) (probe.Result, error) {
|
2015-03-02 09:39:12 +00:00
|
|
|
return httprobe.DoHTTPProbe(c.url(host, "/healthz", ""), c.Client)
|
2014-10-10 22:19:23 +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
|
|
|
|
2014-07-08 07:15:41 +00:00
|
|
|
// GetPodInfo is a fake implementation of PodInfoGetter.GetPodInfo.
|
2015-01-14 02:11:24 +00:00
|
|
|
func (c FakeKubeletClient) GetPodStatus(host, podNamespace string, podID string) (api.PodStatusResult, error) {
|
|
|
|
return api.PodStatusResult{}, errors.New("Not Implemented")
|
2014-11-03 22:50:41 +00:00
|
|
|
}
|
|
|
|
|
2015-03-02 00:34:48 +00:00
|
|
|
// GetNodeInfo is a fake implementation of PodInfoGetter.GetNodeInfo
|
|
|
|
func (c FakeKubeletClient) GetNodeInfo(host string) (api.NodeInfo, error) {
|
|
|
|
return api.NodeInfo{}, errors.New("Not Implemented")
|
|
|
|
}
|
|
|
|
|
2015-01-31 01:03:57 +00:00
|
|
|
func (c FakeKubeletClient) HealthCheck(host string) (probe.Result, error) {
|
2015-01-25 03:22:18 +00:00
|
|
|
return probe.Unknown, errors.New("Not Implemented")
|
2014-06-06 23:40:48 +00:00
|
|
|
}
|