2014-09-12 16:16:37 +00:00
|
|
|
/*
|
2015-05-01 16:19:44 +00:00
|
|
|
Copyright 2014 The Kubernetes Authors All rights reserved.
|
2014-09-12 16:16:37 +00:00
|
|
|
|
|
|
|
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 (
|
2015-04-01 23:19:17 +00:00
|
|
|
"crypto/tls"
|
2014-09-12 16:16:37 +00:00
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"net"
|
|
|
|
"net/http"
|
|
|
|
"strconv"
|
2015-02-10 23:19:49 +00:00
|
|
|
"time"
|
2014-09-12 16:16:37 +00:00
|
|
|
|
2015-08-05 22:03:47 +00:00
|
|
|
"k8s.io/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)
|
|
|
|
}
|
|
|
|
|
2015-06-24 04:47:24 +00:00
|
|
|
type ValidatorFn func([]byte) error
|
|
|
|
|
2014-11-02 20:52:31 +00:00
|
|
|
type Server struct {
|
2015-04-01 23:19:17 +00:00
|
|
|
Addr string
|
|
|
|
Port int
|
|
|
|
Path string
|
|
|
|
EnableHTTPS bool
|
2015-06-24 04:47:24 +00:00
|
|
|
Validate ValidatorFn
|
2014-09-12 16:16:37 +00:00
|
|
|
}
|
|
|
|
|
2015-04-15 19:23:02 +00:00
|
|
|
type ServerStatus struct {
|
|
|
|
Component string `json:"component,omitempty"`
|
|
|
|
Health string `json:"health,omitempty"`
|
|
|
|
HealthCode probe.Result `json:"healthCode,omitempty"`
|
|
|
|
Msg string `json:"msg,omitempty"`
|
|
|
|
Err string `json:"err,omitempty"`
|
|
|
|
}
|
|
|
|
|
2015-01-25 03:22:18 +00:00
|
|
|
// TODO: can this use pkg/probe/http
|
2015-04-15 19:23:02 +00:00
|
|
|
func (server *Server) DoServerCheck(rt http.RoundTripper) (probe.Result, string, error) {
|
|
|
|
var client *http.Client
|
2015-04-01 23:19:17 +00:00
|
|
|
scheme := "http://"
|
2015-04-15 19:23:02 +00:00
|
|
|
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 := &http.Transport{
|
|
|
|
Proxy: http.ProxyFromEnvironment,
|
|
|
|
Dial: (&net.Dialer{
|
|
|
|
Timeout: 30 * time.Second,
|
|
|
|
KeepAlive: 30 * time.Second,
|
|
|
|
}).Dial,
|
|
|
|
TLSHandshakeTimeout: 10 * time.Second,
|
|
|
|
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
|
|
|
|
}
|
|
|
|
|
|
|
|
client = &http.Client{Transport: transport}
|
2015-04-01 23:19:17 +00:00
|
|
|
scheme = "https://"
|
2015-04-15 19:23:02 +00:00
|
|
|
} else {
|
|
|
|
client = &http.Client{Transport: rt}
|
2015-04-01 23:19:17 +00:00
|
|
|
}
|
2015-04-15 19:23:02 +00:00
|
|
|
|
|
|
|
resp, err := client.Get(scheme + net.JoinHostPort(server.Addr, strconv.Itoa(server.Port)) + server.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-06-24 04:47:24 +00:00
|
|
|
if server.Validate != nil {
|
|
|
|
if err := server.Validate(data); err != nil {
|
|
|
|
return probe.Failure, string(data), err
|
|
|
|
}
|
|
|
|
}
|
2015-01-27 18:22:53 +00:00
|
|
|
return probe.Success, string(data), nil
|
2014-09-12 16:16:37 +00:00
|
|
|
}
|