mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-09-08 04:32:37 +00:00
kubelet: add GetNodeInfo implementation
Signed-off-by: Federico Simoncelli <fsimonce@redhat.com>
This commit is contained in:
@@ -29,6 +29,7 @@ import (
|
||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/latest"
|
||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/probe"
|
||||
httprobe "github.com/GoogleCloudPlatform/kubernetes/pkg/probe/http"
|
||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/runtime"
|
||||
)
|
||||
|
||||
// ErrPodInfoNotAvailable may be returned when the requested pod info is not available.
|
||||
@@ -38,6 +39,7 @@ var ErrPodInfoNotAvailable = errors.New("no pod info available")
|
||||
type KubeletClient interface {
|
||||
KubeletHealthChecker
|
||||
PodInfoGetter
|
||||
NodeInfoGetter
|
||||
}
|
||||
|
||||
// KubeletHealthchecker is an interface for healthchecking kubelets
|
||||
@@ -53,6 +55,10 @@ type PodInfoGetter interface {
|
||||
GetPodStatus(host, podNamespace, podID string) (api.PodStatusResult, error)
|
||||
}
|
||||
|
||||
type NodeInfoGetter interface {
|
||||
GetNodeInfo(host string) (api.NodeInfo, error)
|
||||
}
|
||||
|
||||
// HTTPKubeletClient is the default implementation of PodInfoGetter and KubeletHealthchecker, accesses the kubelet over HTTP.
|
||||
type HTTPKubeletClient struct {
|
||||
Client *http.Client
|
||||
@@ -102,33 +108,41 @@ func (c *HTTPKubeletClient) url(host, path, query string) string {
|
||||
|
||||
// GetPodInfo gets information about the specified pod.
|
||||
func (c *HTTPKubeletClient) GetPodStatus(host, podNamespace, podID string) (api.PodStatusResult, error) {
|
||||
query := url.Values{"podID": {podID}, "podNamespace": {podNamespace}}
|
||||
request, err := http.NewRequest("GET", c.url(host, "/api/v1beta1/podInfo", query.Encode()), nil)
|
||||
status := api.PodStatusResult{}
|
||||
if err != nil {
|
||||
return status, err
|
||||
}
|
||||
response, err := c.Client.Do(request)
|
||||
if err != nil {
|
||||
return status, err
|
||||
}
|
||||
defer response.Body.Close()
|
||||
query := url.Values{"podID": {podID}, "podNamespace": {podNamespace}}
|
||||
response, err := c.getEntity(host, "/api/v1beta1/podInfo", query.Encode(), &status)
|
||||
if response.StatusCode == http.StatusNotFound {
|
||||
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
|
||||
}
|
||||
|
||||
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)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
response, err := c.Client.Do(request)
|
||||
if err != nil {
|
||||
return response, err
|
||||
}
|
||||
defer response.Body.Close()
|
||||
if response.StatusCode >= 300 || response.StatusCode < 200 {
|
||||
return status, fmt.Errorf("kubelet %q server responded with HTTP error code %d for pod %s/%s", host, response.StatusCode, podNamespace, podID)
|
||||
return response, fmt.Errorf("kubelet %q server responded with HTTP error code %d", host, response.StatusCode)
|
||||
}
|
||||
body, err := ioutil.ReadAll(response.Body)
|
||||
if err != nil {
|
||||
return status, err
|
||||
return response, err
|
||||
}
|
||||
// Check that this data can be unmarshalled
|
||||
err = latest.Codec.DecodeInto(body, &status)
|
||||
if err != nil {
|
||||
return status, err
|
||||
}
|
||||
return status, nil
|
||||
err = latest.Codec.DecodeInto(body, entity)
|
||||
return response, err
|
||||
}
|
||||
|
||||
func (c *HTTPKubeletClient) HealthCheck(host string) (probe.Result, error) {
|
||||
@@ -145,6 +159,11 @@ func (c FakeKubeletClient) GetPodStatus(host, podNamespace string, podID string)
|
||||
return api.PodStatusResult{}, errors.New("Not Implemented")
|
||||
}
|
||||
|
||||
// GetNodeInfo is a fake implementation of PodInfoGetter.GetNodeInfo
|
||||
func (c FakeKubeletClient) GetNodeInfo(host string) (api.NodeInfo, error) {
|
||||
return api.NodeInfo{}, errors.New("Not Implemented")
|
||||
}
|
||||
|
||||
func (c FakeKubeletClient) HealthCheck(host string) (probe.Result, error) {
|
||||
return probe.Unknown, errors.New("Not Implemented")
|
||||
}
|
||||
|
Reference in New Issue
Block a user