From 9f99767b6c771f0c3192d64aa5f2a4520fa410c2 Mon Sep 17 00:00:00 2001 From: Brendan Burns Date: Fri, 11 Jul 2014 16:45:09 -0700 Subject: [PATCH 1/2] Add a /healthz handler to the kubelet server, so that the master can validate kubelet health. --- pkg/kubelet/kubelet_server.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/pkg/kubelet/kubelet_server.go b/pkg/kubelet/kubelet_server.go index 0e6d7cc8fb..8b62ed4b69 100644 --- a/pkg/kubelet/kubelet_server.go +++ b/pkg/kubelet/kubelet_server.go @@ -58,6 +58,10 @@ func (s *KubeletServer) ServeHTTP(w http.ResponseWriter, req *http.Request) { return } switch { + case u.Path == "/healthz": + w.WriteHeader(http.StatusOK) + w.Write([]byte("ok")) + return case u.Path == "/container" || u.Path == "/containers": defer req.Body.Close() data, err := ioutil.ReadAll(req.Body) From ae179121c689550313beb5ee12dca676d6313196 Mon Sep 17 00:00:00 2001 From: Brendan Burns Date: Mon, 14 Jul 2014 17:00:23 -0700 Subject: [PATCH 2/2] Address comments. --- pkg/healthz/doc.go | 20 ++++++++++++++++++++ pkg/healthz/healthz.go | 31 +++++++++++++++++++++++++++++++ pkg/kubelet/kubelet.go | 6 ++++-- pkg/kubelet/kubelet_server.go | 11 ++++------- 4 files changed, 59 insertions(+), 9 deletions(-) create mode 100644 pkg/healthz/doc.go create mode 100644 pkg/healthz/healthz.go diff --git a/pkg/healthz/doc.go b/pkg/healthz/doc.go new file mode 100644 index 0000000000..16414f578d --- /dev/null +++ b/pkg/healthz/doc.go @@ -0,0 +1,20 @@ +/* +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 healthz implements basic http server health checking. +// Usage: +// import _ "healthz" registers a handler on the path '/healthz', that serves 200s +package healthz diff --git a/pkg/healthz/healthz.go b/pkg/healthz/healthz.go new file mode 100644 index 0000000000..8ae6b97f73 --- /dev/null +++ b/pkg/healthz/healthz.go @@ -0,0 +1,31 @@ +/* +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 healthz + +import ( + "net/http" +) + +func init() { + http.HandleFunc("/healthz", handleHealthz) +} + +func handleHealthz(w http.ResponseWriter, r *http.Request) { + // TODO Support user supplied health functions too. + w.WriteHeader(http.StatusOK) + w.Write([]byte("ok")) +} diff --git a/pkg/kubelet/kubelet.go b/pkg/kubelet/kubelet.go index 263f6c89f8..5bf45fcd0c 100644 --- a/pkg/kubelet/kubelet.go +++ b/pkg/kubelet/kubelet.go @@ -34,6 +34,7 @@ import ( "time" "github.com/GoogleCloudPlatform/kubernetes/pkg/api" + _ "github.com/GoogleCloudPlatform/kubernetes/pkg/healthz" "github.com/GoogleCloudPlatform/kubernetes/pkg/tools" "github.com/GoogleCloudPlatform/kubernetes/pkg/util" "github.com/coreos/go-etcd/etcd" @@ -145,8 +146,9 @@ func (kl *Kubelet) RunKubelet(dockerEndpoint, configPath, manifestURL, etcdServe if address != "" { glog.Infof("Starting to listen on %s:%d", address, port) handler := KubeletServer{ - Kubelet: kl, - UpdateChannel: updateChannel, + Kubelet: kl, + UpdateChannel: updateChannel, + DelegateHandler: http.DefaultServeMux, } s := &http.Server{ Addr: net.JoinHostPort(address, strconv.FormatUint(uint64(port), 10)), diff --git a/pkg/kubelet/kubelet_server.go b/pkg/kubelet/kubelet_server.go index 8b62ed4b69..adfc13e283 100644 --- a/pkg/kubelet/kubelet_server.go +++ b/pkg/kubelet/kubelet_server.go @@ -33,8 +33,9 @@ import ( // KubeletServer is a http.Handler which exposes kubelet functionality over HTTP. type KubeletServer struct { - Kubelet kubeletInterface - UpdateChannel chan<- manifestUpdate + Kubelet kubeletInterface + UpdateChannel chan<- manifestUpdate + DelegateHandler http.Handler } // kubeletInterface contains all the kubelet methods required by the server. @@ -58,10 +59,6 @@ func (s *KubeletServer) ServeHTTP(w http.ResponseWriter, req *http.Request) { return } switch { - case u.Path == "/healthz": - w.WriteHeader(http.StatusOK) - w.Write([]byte("ok")) - return case u.Path == "/container" || u.Path == "/containers": defer req.Body.Close() data, err := ioutil.ReadAll(req.Body) @@ -109,7 +106,7 @@ func (s *KubeletServer) ServeHTTP(w http.ResponseWriter, req *http.Request) { case strings.HasPrefix(u.Path, "/stats"): s.serveStats(w, req) default: - http.Error(w, "Not found.", http.StatusNotFound) + s.DelegateHandler.ServeHTTP(w, req) } }