mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-09-17 23:19:26 +00:00
Merge pull request #3695 from mikedanese/ready
refactor pkg/health into more reusable pkg/probe
This commit is contained in:
@@ -24,7 +24,7 @@ import (
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/health"
|
||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/probe"
|
||||
)
|
||||
|
||||
// TODO: this basic interface is duplicated in N places. consolidate?
|
||||
@@ -45,29 +45,30 @@ type validator struct {
|
||||
client httpGet
|
||||
}
|
||||
|
||||
func (s *Server) check(client httpGet) (health.Status, string, error) {
|
||||
// TODO: can this use pkg/probe/http
|
||||
func (s *Server) check(client httpGet) (probe.Status, string, error) {
|
||||
resp, err := client.Get("http://" + net.JoinHostPort(s.Addr, strconv.Itoa(s.Port)) + s.Path)
|
||||
if err != nil {
|
||||
return health.Unknown, "", err
|
||||
return probe.Unknown, "", err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
data, err := ioutil.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return health.Unknown, string(data), err
|
||||
return probe.Unknown, string(data), err
|
||||
}
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return health.Unhealthy, string(data),
|
||||
return probe.Failure, string(data),
|
||||
fmt.Errorf("unhealthy http status code: %d (%s)", resp.StatusCode, resp.Status)
|
||||
}
|
||||
return health.Healthy, string(data), nil
|
||||
return probe.Success, string(data), nil
|
||||
}
|
||||
|
||||
type ServerStatus struct {
|
||||
Component string `json:"component,omitempty"`
|
||||
Health string `json:"health,omitempty"`
|
||||
HealthCode health.Status `json:"healthCode,omitempty"`
|
||||
Msg string `json:"msg,omitempty"`
|
||||
Err string `json:"err,omitempty"`
|
||||
Component string `json:"component,omitempty"`
|
||||
Health string `json:"health,omitempty"`
|
||||
HealthCode probe.Status `json:"healthCode,omitempty"`
|
||||
Msg string `json:"msg,omitempty"`
|
||||
Err string `json:"err,omitempty"`
|
||||
}
|
||||
|
||||
func (v *validator) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
|
@@ -25,7 +25,7 @@ import (
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/health"
|
||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/probe"
|
||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/util"
|
||||
)
|
||||
|
||||
@@ -54,13 +54,13 @@ func TestValidate(t *testing.T) {
|
||||
tests := []struct {
|
||||
err error
|
||||
data string
|
||||
expectedStatus health.Status
|
||||
expectedStatus probe.Status
|
||||
code int
|
||||
expectErr bool
|
||||
}{
|
||||
{fmt.Errorf("test error"), "", health.Unknown, 500 /*ignored*/, true},
|
||||
{nil, "foo", health.Healthy, 200, false},
|
||||
{nil, "foo", health.Unhealthy, 500, true},
|
||||
{fmt.Errorf("test error"), "", probe.Unknown, 500 /*ignored*/, true},
|
||||
{nil, "foo", probe.Success, 200, false},
|
||||
{nil, "foo", probe.Failure, 500, true},
|
||||
}
|
||||
|
||||
s := Server{Addr: "foo.com", Port: 8080, Path: "/healthz"}
|
||||
|
Reference in New Issue
Block a user