2014-09-12 16:16:37 +00:00
|
|
|
/*
|
|
|
|
Copyright 2014 Google Inc. All rights reserved.
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package apiserver
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"net"
|
|
|
|
"net/http"
|
|
|
|
"strconv"
|
|
|
|
|
2015-01-25 03:22:18 +00:00
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/probe"
|
2014-09-12 16:16:37 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// TODO: this basic interface is duplicated in N places. consolidate?
|
|
|
|
type httpGet interface {
|
|
|
|
Get(url string) (*http.Response, error)
|
|
|
|
}
|
|
|
|
|
2014-11-02 20:52:31 +00:00
|
|
|
type Server struct {
|
|
|
|
Addr string
|
|
|
|
Port int
|
|
|
|
Path string
|
2014-09-12 16:16:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// validator is responsible for validating the cluster and serving
|
|
|
|
type validator struct {
|
|
|
|
// a list of servers to health check
|
2014-12-11 06:02:45 +00:00
|
|
|
servers func() map[string]Server
|
2014-09-12 16:16:37 +00:00
|
|
|
client httpGet
|
|
|
|
}
|
|
|
|
|
2015-01-25 03:22:18 +00:00
|
|
|
// TODO: can this use pkg/probe/http
|
2015-01-31 01:03:57 +00:00
|
|
|
func (s *Server) check(client httpGet) (probe.Result, string, error) {
|
2014-11-02 20:52:31 +00:00
|
|
|
resp, err := client.Get("http://" + net.JoinHostPort(s.Addr, strconv.Itoa(s.Port)) + s.Path)
|
2014-09-12 16:16:37 +00:00
|
|
|
if err != nil {
|
2015-01-25 03:22:18 +00:00
|
|
|
return probe.Unknown, "", err
|
2014-09-12 16:16:37 +00:00
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
data, err := ioutil.ReadAll(resp.Body)
|
|
|
|
if err != nil {
|
2015-01-25 03:22:18 +00:00
|
|
|
return probe.Unknown, string(data), err
|
2014-09-12 16:16:37 +00:00
|
|
|
}
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
2015-01-27 18:22:53 +00:00
|
|
|
return probe.Failure, string(data),
|
2014-09-12 16:16:37 +00:00
|
|
|
fmt.Errorf("unhealthy http status code: %d (%s)", resp.StatusCode, resp.Status)
|
|
|
|
}
|
2015-01-27 18:22:53 +00:00
|
|
|
return probe.Success, string(data), nil
|
2014-09-12 16:16:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type ServerStatus struct {
|
2015-01-25 03:22:18 +00:00
|
|
|
Component string `json:"component,omitempty"`
|
|
|
|
Health string `json:"health,omitempty"`
|
2015-01-31 01:03:57 +00:00
|
|
|
HealthCode probe.Result `json:"healthCode,omitempty"`
|
2015-01-25 03:22:18 +00:00
|
|
|
Msg string `json:"msg,omitempty"`
|
|
|
|
Err string `json:"err,omitempty"`
|
2014-09-12 16:16:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (v *validator) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
|
|
reply := []ServerStatus{}
|
2014-12-11 06:02:45 +00:00
|
|
|
for name, server := range v.servers() {
|
2014-09-12 16:16:37 +00:00
|
|
|
status, msg, err := server.check(v.client)
|
|
|
|
var errorMsg string
|
|
|
|
if err != nil {
|
|
|
|
errorMsg = err.Error()
|
|
|
|
} else {
|
|
|
|
errorMsg = "nil"
|
|
|
|
}
|
|
|
|
reply = append(reply, ServerStatus{name, status.String(), status, msg, errorMsg})
|
|
|
|
}
|
2014-11-02 20:52:31 +00:00
|
|
|
data, err := json.MarshalIndent(reply, "", " ")
|
2014-09-12 16:16:37 +00:00
|
|
|
if err != nil {
|
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
|
|
w.Write([]byte(err.Error()))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
w.Write(data)
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewValidator creates a validator for a set of servers.
|
2014-12-11 06:02:45 +00:00
|
|
|
func NewValidator(servers func() map[string]Server) (http.Handler, error) {
|
2014-11-02 20:52:31 +00:00
|
|
|
return &validator{
|
|
|
|
servers: servers,
|
|
|
|
client: &http.Client{},
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func makeTestValidator(servers map[string]string, get httpGet) (http.Handler, error) {
|
|
|
|
result := map[string]Server{}
|
2014-09-12 16:16:37 +00:00
|
|
|
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)
|
|
|
|
}
|
2014-11-02 20:52:31 +00:00
|
|
|
result[name] = Server{Addr: host, Port: val, Path: "/healthz"}
|
2014-09-12 16:16:37 +00:00
|
|
|
}
|
|
|
|
|
2014-12-11 06:02:45 +00:00
|
|
|
v, e := NewValidator(func() map[string]Server { return result })
|
2014-09-12 16:16:37 +00:00
|
|
|
if e == nil {
|
|
|
|
v.(*validator).client = get
|
|
|
|
}
|
|
|
|
return v, e
|
|
|
|
}
|