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 (
|
|
|
|
"net/http"
|
|
|
|
|
2015-08-05 22:03:47 +00:00
|
|
|
"k8s.io/kubernetes/pkg/probe"
|
2015-11-28 02:40:28 +00:00
|
|
|
httpprober "k8s.io/kubernetes/pkg/probe/http"
|
2016-01-06 15:56:41 +00:00
|
|
|
utilnet "k8s.io/kubernetes/pkg/util/net"
|
2015-11-28 02:40:28 +00:00
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
probeTimeOut = time.Minute
|
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-11-28 02:40:28 +00:00
|
|
|
func (server *Server) DoServerCheck(prober httpprober.HTTPProber) (probe.Result, string, error) {
|
|
|
|
scheme := "http"
|
2015-04-15 19:23:02 +00:00
|
|
|
if server.EnableHTTPS {
|
2015-11-28 02:40:28 +00:00
|
|
|
scheme = "https"
|
2015-04-01 23:19:17 +00:00
|
|
|
}
|
2016-01-06 15:56:41 +00:00
|
|
|
url := utilnet.FormatURL(scheme, server.Addr, server.Port, server.Path)
|
2015-11-28 02:40:28 +00:00
|
|
|
|
2016-02-02 15:03:50 +00:00
|
|
|
result, data, err := prober.Probe(url, nil, probeTimeOut)
|
2015-04-15 19:23:02 +00:00
|
|
|
|
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
|
|
|
}
|
2015-11-28 02:40:28 +00:00
|
|
|
if result == probe.Failure {
|
|
|
|
return probe.Failure, string(data), err
|
2014-09-12 16:16:37 +00:00
|
|
|
}
|
2015-06-24 04:47:24 +00:00
|
|
|
if server.Validate != nil {
|
2015-11-28 02:40:28 +00:00
|
|
|
if err := server.Validate([]byte(data)); err != nil {
|
2015-06-24 04:47:24 +00:00
|
|
|
return probe.Failure, string(data), err
|
|
|
|
}
|
|
|
|
}
|
2015-11-28 02:40:28 +00:00
|
|
|
return result, string(data), nil
|
2014-09-12 16:16:37 +00:00
|
|
|
}
|