mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-29 14:37:00 +00:00
Merge pull request #18058 from mqliang/httpProbe
Auto commit by PR queue bot
This commit is contained in:
commit
9b106c6e6d
@ -17,15 +17,16 @@ limitations under the License.
|
||||
package apiserver
|
||||
|
||||
import (
|
||||
"crypto/tls"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"net"
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
"k8s.io/kubernetes/pkg/probe"
|
||||
httpprober "k8s.io/kubernetes/pkg/probe/http"
|
||||
"k8s.io/kubernetes/pkg/util"
|
||||
"time"
|
||||
)
|
||||
|
||||
const (
|
||||
probeTimeOut = time.Minute
|
||||
)
|
||||
|
||||
// TODO: this basic interface is duplicated in N places. consolidate?
|
||||
@ -51,41 +52,25 @@ type ServerStatus struct {
|
||||
Err string `json:"err,omitempty"`
|
||||
}
|
||||
|
||||
// TODO: can this use pkg/probe/http
|
||||
func (server *Server) DoServerCheck(rt http.RoundTripper) (probe.Result, string, error) {
|
||||
var client *http.Client
|
||||
scheme := "http://"
|
||||
func (server *Server) DoServerCheck(prober httpprober.HTTPProber) (probe.Result, string, error) {
|
||||
scheme := "http"
|
||||
if server.EnableHTTPS {
|
||||
// TODO(roberthbailey): The servers that use HTTPS are currently the
|
||||
// kubelets, and we should be using a standard kubelet client library
|
||||
// to talk to them rather than a separate http client.
|
||||
transport := util.SetTransportDefaults(&http.Transport{
|
||||
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
|
||||
})
|
||||
|
||||
client = &http.Client{Transport: transport}
|
||||
scheme = "https://"
|
||||
} else {
|
||||
client = &http.Client{Transport: rt}
|
||||
scheme = "https"
|
||||
}
|
||||
url := util.FormatURL(scheme, server.Addr, server.Port, server.Path)
|
||||
|
||||
result, data, err := prober.Probe(url, probeTimeOut)
|
||||
|
||||
resp, err := client.Get(scheme + net.JoinHostPort(server.Addr, strconv.Itoa(server.Port)) + server.Path)
|
||||
if err != nil {
|
||||
return probe.Unknown, "", err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
data, err := ioutil.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return probe.Unknown, string(data), err
|
||||
}
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return probe.Failure, string(data),
|
||||
fmt.Errorf("unhealthy http status code: %d (%s)", resp.StatusCode, resp.Status)
|
||||
if result == probe.Failure {
|
||||
return probe.Failure, string(data), err
|
||||
}
|
||||
if server.Validate != nil {
|
||||
if err := server.Validate(data); err != nil {
|
||||
if err := server.Validate([]byte(data)); err != nil {
|
||||
return probe.Failure, string(data), err
|
||||
}
|
||||
}
|
||||
return probe.Success, string(data), nil
|
||||
return result, string(data), nil
|
||||
}
|
||||
|
@ -17,31 +17,29 @@ limitations under the License.
|
||||
package apiserver
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"testing"
|
||||
|
||||
"k8s.io/kubernetes/pkg/probe"
|
||||
"net/url"
|
||||
"time"
|
||||
)
|
||||
|
||||
type fakeRoundTripper struct {
|
||||
err error
|
||||
resp *http.Response
|
||||
url string
|
||||
type fakeHttpProber struct {
|
||||
result probe.Result
|
||||
body string
|
||||
err error
|
||||
}
|
||||
|
||||
func (f *fakeRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
|
||||
f.url = req.URL.String()
|
||||
return f.resp, f.err
|
||||
func (f *fakeHttpProber) Probe(*url.URL, time.Duration) (probe.Result, string, error) {
|
||||
return f.result, f.body, f.err
|
||||
}
|
||||
|
||||
func alwaysError([]byte) error { return errors.New("test error") }
|
||||
|
||||
func matchError(data []byte) error {
|
||||
if string(data) == "bar" {
|
||||
if string(data) != "bar" {
|
||||
return errors.New("match error")
|
||||
}
|
||||
return nil
|
||||
@ -49,47 +47,44 @@ func matchError(data []byte) error {
|
||||
|
||||
func TestValidate(t *testing.T) {
|
||||
tests := []struct {
|
||||
err error
|
||||
data string
|
||||
expectedStatus probe.Result
|
||||
code int
|
||||
expectErr bool
|
||||
validator ValidatorFn
|
||||
probeResult probe.Result
|
||||
probeData string
|
||||
probeErr error
|
||||
|
||||
expectResult probe.Result
|
||||
expectData string
|
||||
expectErr bool
|
||||
|
||||
validator ValidatorFn
|
||||
}{
|
||||
{fmt.Errorf("test error"), "", probe.Unknown, 500 /*ignored*/, true, nil},
|
||||
{nil, "foo", probe.Success, 200, false, nil},
|
||||
{nil, "foo", probe.Failure, 500, true, nil},
|
||||
{nil, "foo", probe.Failure, 200, true, alwaysError},
|
||||
{nil, "foo", probe.Success, 200, false, matchError},
|
||||
{probe.Unknown, "", fmt.Errorf("probe error"), probe.Unknown, "", true, nil},
|
||||
{probe.Failure, "", nil, probe.Failure, "", false, nil},
|
||||
{probe.Success, "foo", nil, probe.Failure, "foo", true, matchError},
|
||||
{probe.Success, "foo", nil, probe.Success, "foo", false, nil},
|
||||
}
|
||||
|
||||
s := Server{Addr: "foo.com", Port: 8080, Path: "/healthz"}
|
||||
|
||||
for _, test := range tests {
|
||||
fakeRT := &fakeRoundTripper{
|
||||
err: test.err,
|
||||
resp: &http.Response{
|
||||
Body: ioutil.NopCloser(bytes.NewBufferString(test.data)),
|
||||
StatusCode: test.code,
|
||||
},
|
||||
fakeProber := &fakeHttpProber{
|
||||
result: test.probeResult,
|
||||
body: test.probeData,
|
||||
err: test.probeErr,
|
||||
}
|
||||
|
||||
s.Validate = test.validator
|
||||
status, data, err := s.DoServerCheck(fakeRT)
|
||||
expect := fmt.Sprintf("http://%s:%d/healthz", s.Addr, s.Port)
|
||||
if fakeRT.url != expect {
|
||||
t.Errorf("expected %s, got %s", expect, fakeRT.url)
|
||||
}
|
||||
result, data, err := s.DoServerCheck(fakeProber)
|
||||
if test.expectErr && err == nil {
|
||||
t.Errorf("unexpected non-error")
|
||||
t.Error("unexpected non-error")
|
||||
}
|
||||
if !test.expectErr && err != nil {
|
||||
t.Errorf("unexpected error: %v", err)
|
||||
}
|
||||
if data != test.data {
|
||||
t.Errorf("expected empty string, got %s", status)
|
||||
if data != test.expectData {
|
||||
t.Errorf("expected %s, got %s", test.expectData, data)
|
||||
}
|
||||
if status != test.expectedStatus {
|
||||
t.Errorf("expected %s, got %s", test.expectedStatus, status)
|
||||
if result != test.expectResult {
|
||||
t.Errorf("expected %s, got %s", test.expectResult, result)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -18,25 +18,25 @@ package componentstatus
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
"k8s.io/kubernetes/pkg/api"
|
||||
"k8s.io/kubernetes/pkg/apiserver"
|
||||
"k8s.io/kubernetes/pkg/probe"
|
||||
httpprober "k8s.io/kubernetes/pkg/probe/http"
|
||||
"k8s.io/kubernetes/pkg/runtime"
|
||||
"sync"
|
||||
)
|
||||
|
||||
type REST struct {
|
||||
GetServersToValidate func() map[string]apiserver.Server
|
||||
rt http.RoundTripper
|
||||
prober httpprober.HTTPProber
|
||||
}
|
||||
|
||||
// NewStorage returns a new REST.
|
||||
func NewStorage(serverRetriever func() map[string]apiserver.Server) *REST {
|
||||
return &REST{
|
||||
GetServersToValidate: serverRetriever,
|
||||
rt: http.DefaultTransport,
|
||||
prober: httpprober.New(),
|
||||
}
|
||||
}
|
||||
|
||||
@ -95,8 +95,7 @@ func ToConditionStatus(s probe.Result) api.ConditionStatus {
|
||||
}
|
||||
|
||||
func (rs *REST) getComponentStatus(name string, server apiserver.Server) *api.ComponentStatus {
|
||||
transport := rs.rt
|
||||
status, msg, err := server.DoServerCheck(transport)
|
||||
status, msg, err := server.DoServerCheck(rs.prober)
|
||||
errorMsg := ""
|
||||
if err != nil {
|
||||
errorMsg = err.Error()
|
||||
|
@ -17,34 +17,33 @@ limitations under the License.
|
||||
package componentstatus
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"reflect"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"k8s.io/kubernetes/pkg/api"
|
||||
"k8s.io/kubernetes/pkg/apiserver"
|
||||
"k8s.io/kubernetes/pkg/probe"
|
||||
"k8s.io/kubernetes/pkg/util"
|
||||
"net/url"
|
||||
"time"
|
||||
)
|
||||
|
||||
type fakeRoundTripper struct {
|
||||
err error
|
||||
resp *http.Response
|
||||
url string
|
||||
type fakeHttpProber struct {
|
||||
result probe.Result
|
||||
body string
|
||||
err error
|
||||
}
|
||||
|
||||
func (f *fakeRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
|
||||
f.url = req.URL.String()
|
||||
return f.resp, f.err
|
||||
func (f *fakeHttpProber) Probe(*url.URL, time.Duration) (probe.Result, string, error) {
|
||||
return f.result, f.body, f.err
|
||||
}
|
||||
|
||||
type testResponse struct {
|
||||
code int
|
||||
data string
|
||||
err error
|
||||
result probe.Result
|
||||
data string
|
||||
err error
|
||||
}
|
||||
|
||||
func NewTestREST(resp testResponse) *REST {
|
||||
@ -54,12 +53,10 @@ func NewTestREST(resp testResponse) *REST {
|
||||
"test1": {Addr: "testserver1", Port: 8000, Path: "/healthz"},
|
||||
}
|
||||
},
|
||||
rt: &fakeRoundTripper{
|
||||
err: resp.err,
|
||||
resp: &http.Response{
|
||||
Body: ioutil.NopCloser(bytes.NewBufferString(resp.data)),
|
||||
StatusCode: resp.code,
|
||||
},
|
||||
prober: &fakeHttpProber{
|
||||
result: resp.result,
|
||||
body: resp.data,
|
||||
err: resp.err,
|
||||
},
|
||||
}
|
||||
}
|
||||
@ -75,7 +72,7 @@ func createTestStatus(name string, status api.ConditionStatus, msg string, err s
|
||||
}
|
||||
|
||||
func TestList_NoError(t *testing.T) {
|
||||
r := NewTestREST(testResponse{code: 200, data: "ok"})
|
||||
r := NewTestREST(testResponse{result: probe.Success, data: "ok"})
|
||||
got, err := r.List(api.NewContext(), nil)
|
||||
if err != nil {
|
||||
t.Fatalf("Unexpected error: %v", err)
|
||||
@ -89,14 +86,14 @@ func TestList_NoError(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestList_FailedCheck(t *testing.T) {
|
||||
r := NewTestREST(testResponse{code: 500, data: ""})
|
||||
r := NewTestREST(testResponse{result: probe.Failure, data: ""})
|
||||
got, err := r.List(api.NewContext(), nil)
|
||||
if err != nil {
|
||||
t.Fatalf("Unexpected error: %v", err)
|
||||
}
|
||||
expect := &api.ComponentStatusList{
|
||||
Items: []api.ComponentStatus{
|
||||
*(createTestStatus("test1", api.ConditionFalse, "", "unhealthy http status code: 500 ()"))},
|
||||
*(createTestStatus("test1", api.ConditionFalse, "", ""))},
|
||||
}
|
||||
if e, a := expect, got; !reflect.DeepEqual(e, a) {
|
||||
t.Errorf("Got unexpected object. Diff: %s", util.ObjectDiff(e, a))
|
||||
@ -104,14 +101,14 @@ func TestList_FailedCheck(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestList_UnknownError(t *testing.T) {
|
||||
r := NewTestREST(testResponse{code: 500, data: "", err: fmt.Errorf("fizzbuzz error")})
|
||||
r := NewTestREST(testResponse{result: probe.Unknown, data: "", err: fmt.Errorf("fizzbuzz error")})
|
||||
got, err := r.List(api.NewContext(), nil)
|
||||
if err != nil {
|
||||
t.Fatalf("Unexpected error: %v", err)
|
||||
}
|
||||
expect := &api.ComponentStatusList{
|
||||
Items: []api.ComponentStatus{
|
||||
*(createTestStatus("test1", api.ConditionUnknown, "", "Get http://testserver1:8000/healthz: fizzbuzz error"))},
|
||||
*(createTestStatus("test1", api.ConditionUnknown, "", "fizzbuzz error"))},
|
||||
}
|
||||
if e, a := expect, got; !reflect.DeepEqual(e, a) {
|
||||
t.Errorf("Got unexpected object. Diff: %s", util.ObjectDiff(e, a))
|
||||
@ -119,7 +116,7 @@ func TestList_UnknownError(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestGet_NoError(t *testing.T) {
|
||||
r := NewTestREST(testResponse{code: 200, data: "ok"})
|
||||
r := NewTestREST(testResponse{result: probe.Success, data: "ok"})
|
||||
got, err := r.Get(api.NewContext(), "test1")
|
||||
if err != nil {
|
||||
t.Fatalf("Unexpected error: %v", err)
|
||||
@ -131,7 +128,7 @@ func TestGet_NoError(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestGet_BadName(t *testing.T) {
|
||||
r := NewTestREST(testResponse{code: 200, data: "ok"})
|
||||
r := NewTestREST(testResponse{result: probe.Success, data: "ok"})
|
||||
_, err := r.Get(api.NewContext(), "invalidname")
|
||||
if err == nil {
|
||||
t.Fatalf("Expected error, but did not get one")
|
||||
|
@ -23,6 +23,7 @@ import (
|
||||
"net"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
@ -102,3 +103,11 @@ func TLSClientConfig(transport http.RoundTripper) (*tls.Config, error) {
|
||||
return nil, fmt.Errorf("unknown transport type: %v", transport)
|
||||
}
|
||||
}
|
||||
|
||||
func FormatURL(scheme string, host string, port int, path string) *url.URL {
|
||||
return &url.URL{
|
||||
Scheme: scheme,
|
||||
Host: net.JoinHostPort(host, strconv.Itoa(port)),
|
||||
Path: path,
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user