mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-27 21:47:07 +00:00
Merge pull request #8224 from cjcullen/nodenames
Remove /Validate endpoint
This commit is contained in:
commit
7b9dbbf29f
@ -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)
|
||||
}
|
||||
}
|
||||
|
@ -41,8 +41,6 @@ import (
|
||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/auth/authorizer"
|
||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/auth/handlers"
|
||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/client"
|
||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/fields"
|
||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/labels"
|
||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/master/ports"
|
||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/registry/componentstatus"
|
||||
controlleretcd "github.com/GoogleCloudPlatform/kubernetes/pkg/registry/controller/etcd"
|
||||
@ -497,7 +495,7 @@ func (m *Master) init(c *Config) {
|
||||
"persistentVolumeClaims": persistentVolumeClaimStorage,
|
||||
"persistentVolumeClaims/status": persistentVolumeClaimStatusStorage,
|
||||
|
||||
"componentStatuses": componentstatus.NewStorage(func() map[string]apiserver.Server { return m.getServersToValidate(c, false) }),
|
||||
"componentStatuses": componentstatus.NewStorage(func() map[string]apiserver.Server { return m.getServersToValidate(c) }),
|
||||
}
|
||||
|
||||
apiVersions := []string{}
|
||||
@ -539,8 +537,6 @@ func (m *Master) init(c *Config) {
|
||||
m.mux.HandleFunc("/", apiserver.IndexHandler(m.handlerContainer, m.muxHelper))
|
||||
}
|
||||
|
||||
// TODO: This is now deprecated. Should be removed once client dependencies are gone.
|
||||
apiserver.InstallValidator(m.muxHelper, func() map[string]apiserver.Server { return m.getServersToValidate(c, true) })
|
||||
if c.EnableLogsSupport {
|
||||
apiserver.InstallLogsSupport(m.muxHelper)
|
||||
}
|
||||
@ -673,7 +669,7 @@ func (m *Master) InstallSwaggerAPI() {
|
||||
swagger.RegisterSwaggerService(swaggerConfig, m.handlerContainer)
|
||||
}
|
||||
|
||||
func (m *Master) getServersToValidate(c *Config, includeNodes bool) map[string]apiserver.Server {
|
||||
func (m *Master) getServersToValidate(c *Config) map[string]apiserver.Server {
|
||||
serversToValidate := map[string]apiserver.Server{
|
||||
"controller-manager": {Addr: "127.0.0.1", Port: ports.ControllerManagerPort, Path: "/healthz"},
|
||||
"scheduler": {Addr: "127.0.0.1", Port: ports.SchedulerPort, Path: "/healthz"},
|
||||
@ -700,15 +696,6 @@ func (m *Master) getServersToValidate(c *Config, includeNodes bool) map[string]a
|
||||
}
|
||||
serversToValidate[fmt.Sprintf("etcd-%d", ix)] = apiserver.Server{Addr: addr, Port: port, Path: "/v2/keys/"}
|
||||
}
|
||||
if includeNodes && m.nodeRegistry != nil {
|
||||
nodes, err := m.nodeRegistry.ListMinions(api.NewDefaultContext(), labels.Everything(), fields.Everything())
|
||||
if err != nil {
|
||||
glog.Errorf("Failed to list minions: %v", err)
|
||||
}
|
||||
for ix, node := range nodes.Items {
|
||||
serversToValidate[fmt.Sprintf("node-%d", ix)] = apiserver.Server{Addr: node.Name, Port: ports.KubeletPort, Path: "/healthz", EnableHTTPS: true}
|
||||
}
|
||||
}
|
||||
return serversToValidate
|
||||
}
|
||||
|
||||
|
@ -36,18 +36,7 @@ func TestGetServersToValidate(t *testing.T) {
|
||||
|
||||
master.nodeRegistry = registrytest.NewMinionRegistry([]string{"node1", "node2"}, api.NodeResources{})
|
||||
|
||||
servers := master.getServersToValidate(&config, true)
|
||||
|
||||
if len(servers) != 7 {
|
||||
t.Errorf("unexpected server list: %#v", servers)
|
||||
}
|
||||
for _, server := range []string{"scheduler", "controller-manager", "etcd-0", "etcd-1", "etcd-2", "node-0", "node-1"} {
|
||||
if _, ok := servers[server]; !ok {
|
||||
t.Errorf("server list missing: %s", server)
|
||||
}
|
||||
}
|
||||
|
||||
servers = master.getServersToValidate(&config, false)
|
||||
servers := master.getServersToValidate(&config)
|
||||
|
||||
if len(servers) != 5 {
|
||||
t.Errorf("unexpected server list: %#v", servers)
|
||||
|
Loading…
Reference in New Issue
Block a user