mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-09-15 14:14:39 +00:00
Remove /Validate endpoint
This commit is contained in:
@@ -157,12 +157,6 @@ func (g *APIGroupVersion) InstallREST(container *restful.Container) error {
|
||||
return errors.NewAggregate(registrationErrors)
|
||||
}
|
||||
|
||||
// TODO: This endpoint is deprecated and should be removed at some point.
|
||||
// Use "componentstatus" API instead.
|
||||
func InstallValidator(mux Mux, servers func() map[string]Server) {
|
||||
mux.Handle("/validate", NewValidator(servers))
|
||||
}
|
||||
|
||||
// TODO: document all handlers
|
||||
// InstallSupport registers the APIServer support functions
|
||||
func InstallSupport(mux Mux, ws *restful.WebService) {
|
||||
|
@@ -18,7 +18,6 @@ package apiserver
|
||||
|
||||
import (
|
||||
"crypto/tls"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"net"
|
||||
@@ -27,7 +26,6 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/probe"
|
||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/util"
|
||||
)
|
||||
|
||||
// TODO: this basic interface is duplicated in N places. consolidate?
|
||||
@@ -42,13 +40,6 @@ type Server struct {
|
||||
EnableHTTPS bool
|
||||
}
|
||||
|
||||
// validator is responsible for validating the cluster and serving
|
||||
type validator struct {
|
||||
// a list of servers to health check
|
||||
servers func() map[string]Server
|
||||
rt http.RoundTripper
|
||||
}
|
||||
|
||||
type ServerStatus struct {
|
||||
Component string `json:"component,omitempty"`
|
||||
Health string `json:"health,omitempty"`
|
||||
@@ -96,39 +87,3 @@ func (server *Server) DoServerCheck(rt http.RoundTripper) (probe.Result, string,
|
||||
}
|
||||
return probe.Success, string(data), nil
|
||||
}
|
||||
|
||||
func (v *validator) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
verb := "get"
|
||||
apiResource := ""
|
||||
var httpCode int
|
||||
reqStart := time.Now()
|
||||
defer monitor(&verb, &apiResource, util.GetClient(r), &httpCode, reqStart)
|
||||
|
||||
reply := []ServerStatus{}
|
||||
for name, server := range v.servers() {
|
||||
transport := v.rt
|
||||
status, msg, err := server.DoServerCheck(transport)
|
||||
var errorMsg string
|
||||
if err != nil {
|
||||
errorMsg = err.Error()
|
||||
} else {
|
||||
errorMsg = "nil"
|
||||
}
|
||||
reply = append(reply, ServerStatus{name, status.String(), status, msg, errorMsg})
|
||||
}
|
||||
data, err := json.MarshalIndent(reply, "", " ")
|
||||
if err != nil {
|
||||
httpCode = http.StatusInternalServerError
|
||||
w.WriteHeader(httpCode)
|
||||
w.Write([]byte(err.Error()))
|
||||
return
|
||||
}
|
||||
httpCode = http.StatusOK
|
||||
w.WriteHeader(httpCode)
|
||||
w.Write(data)
|
||||
}
|
||||
|
||||
// NewValidator creates a validator for a set of servers.
|
||||
func NewValidator(servers func() map[string]Server) http.Handler {
|
||||
return &validator{servers: servers, rt: http.DefaultTransport}
|
||||
}
|
||||
|
@@ -18,17 +18,12 @@ package apiserver
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"net"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strconv"
|
||||
"testing"
|
||||
|
||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/probe"
|
||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/util"
|
||||
)
|
||||
|
||||
type fakeRoundTripper struct {
|
||||
@@ -84,66 +79,3 @@ func TestValidate(t *testing.T) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func makeTestValidator(servers map[string]string, rt http.RoundTripper) (http.Handler, error) {
|
||||
result := map[string]Server{}
|
||||
for name, value := range servers {
|
||||
host, port, err := net.SplitHostPort(value)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("invalid server spec: %s (%v)", value, err)
|
||||
}
|
||||
val, err := strconv.Atoi(port)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("invalid server spec: %s (%v)", port, err)
|
||||
}
|
||||
result[name] = Server{Addr: host, Port: val, Path: "/healthz"}
|
||||
}
|
||||
|
||||
return &validator{servers: func() map[string]Server { return result }, rt: rt}, nil
|
||||
}
|
||||
|
||||
func TestValidator(t *testing.T) {
|
||||
fake := &fakeRoundTripper{
|
||||
resp: &http.Response{
|
||||
Body: ioutil.NopCloser(bytes.NewBufferString("foo")),
|
||||
StatusCode: 200,
|
||||
},
|
||||
}
|
||||
validator, err := makeTestValidator(map[string]string{
|
||||
"foo": "foo.com:80",
|
||||
"bar": "bar.com:8080",
|
||||
}, fake)
|
||||
if err != nil {
|
||||
t.Errorf("unexpected error: %v", err)
|
||||
}
|
||||
|
||||
testServer := httptest.NewServer(validator)
|
||||
defer testServer.Close()
|
||||
|
||||
resp, err := http.Get(testServer.URL + "/validatez")
|
||||
if err != nil {
|
||||
t.Errorf("unexpected error: %v", err)
|
||||
}
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
t.Errorf("unexpected response: %v", resp.StatusCode)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
data, err := ioutil.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
t.Errorf("unexpected error: %v", err)
|
||||
}
|
||||
var status []ServerStatus
|
||||
if err := json.Unmarshal(data, &status); err != nil {
|
||||
t.Errorf("unexpected error: %v", err)
|
||||
}
|
||||
components := util.StringSet{}
|
||||
for _, s := range status {
|
||||
if s.Err != "nil" {
|
||||
t.Errorf("Component %v is unhealthy: %v", s.Component, s.Err)
|
||||
}
|
||||
components.Insert(s.Component)
|
||||
}
|
||||
if len(status) != 2 || !components.Has("foo") || !components.Has("bar") {
|
||||
t.Errorf("unexpected status: %#v", status)
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user