2014-06-06 23:40:48 +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.
|
|
|
|
*/
|
2014-06-08 23:10:29 +00:00
|
|
|
|
2014-06-06 23:40:48 +00:00
|
|
|
package kubelet
|
|
|
|
|
|
|
|
import (
|
2014-06-19 01:26:23 +00:00
|
|
|
"encoding/json"
|
2014-07-01 21:05:10 +00:00
|
|
|
"errors"
|
2014-06-06 23:40:48 +00:00
|
|
|
"fmt"
|
2014-07-14 21:48:51 +00:00
|
|
|
"io"
|
2014-06-06 23:40:48 +00:00
|
|
|
"io/ioutil"
|
2014-07-15 20:24:41 +00:00
|
|
|
"net"
|
2014-06-06 23:40:48 +00:00
|
|
|
"net/http"
|
|
|
|
"net/url"
|
2014-07-01 21:05:10 +00:00
|
|
|
"path"
|
2014-07-15 20:24:41 +00:00
|
|
|
"strconv"
|
2014-07-01 21:05:10 +00:00
|
|
|
"strings"
|
2014-07-15 20:24:41 +00:00
|
|
|
"time"
|
2014-06-06 23:40:48 +00:00
|
|
|
|
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
|
2014-07-15 22:38:56 +00:00
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/httplog"
|
2014-07-15 20:24:41 +00:00
|
|
|
"github.com/golang/glog"
|
2014-07-16 09:38:45 +00:00
|
|
|
"github.com/google/cadvisor/info"
|
2014-06-06 23:40:48 +00:00
|
|
|
"gopkg.in/v1/yaml"
|
|
|
|
)
|
|
|
|
|
2014-07-15 13:54:23 +00:00
|
|
|
// Server is a http.Handler which exposes kubelet functionality over HTTP.
|
|
|
|
type Server struct {
|
2014-07-15 20:24:41 +00:00
|
|
|
host HostInterface
|
|
|
|
updates chan<- interface{}
|
|
|
|
handler http.Handler
|
2014-06-06 23:40:48 +00:00
|
|
|
}
|
|
|
|
|
2014-07-15 20:24:41 +00:00
|
|
|
func ListenAndServeKubeletServer(host HostInterface, updates chan<- interface{}, delegate http.Handler, address string, port uint) {
|
|
|
|
glog.Infof("Starting to listen on %s:%d", address, port)
|
|
|
|
handler := Server{
|
|
|
|
host: host,
|
|
|
|
updates: updates,
|
|
|
|
handler: delegate,
|
|
|
|
}
|
|
|
|
s := &http.Server{
|
|
|
|
Addr: net.JoinHostPort(address, strconv.FormatUint(uint64(port), 10)),
|
|
|
|
Handler: &handler,
|
|
|
|
ReadTimeout: 10 * time.Second,
|
|
|
|
WriteTimeout: 10 * time.Second,
|
|
|
|
MaxHeaderBytes: 1 << 20,
|
|
|
|
}
|
|
|
|
s.ListenAndServe()
|
|
|
|
}
|
|
|
|
|
|
|
|
// HostInterface contains all the kubelet methods required by the server.
|
2014-06-08 23:10:29 +00:00
|
|
|
// For testablitiy.
|
2014-07-15 20:24:41 +00:00
|
|
|
type HostInterface interface {
|
|
|
|
GetContainerInfo(podFullName, containerName string, req *info.ContainerInfoRequest) (*info.ContainerInfo, error)
|
2014-07-15 22:40:02 +00:00
|
|
|
GetRootInfo(req *info.ContainerInfoRequest) (*info.ContainerInfo, error)
|
|
|
|
GetMachineInfo() (*info.MachineInfo, error)
|
2014-07-01 22:35:56 +00:00
|
|
|
GetPodInfo(name string) (api.PodInfo, error)
|
2014-07-15 07:04:30 +00:00
|
|
|
ServeLogs(w http.ResponseWriter, req *http.Request)
|
2014-06-08 23:10:29 +00:00
|
|
|
}
|
|
|
|
|
2014-07-15 13:54:23 +00:00
|
|
|
func (s *Server) error(w http.ResponseWriter, err error) {
|
2014-07-12 17:44:29 +00:00
|
|
|
http.Error(w, fmt.Sprintf("Internal Error: %v", err), http.StatusInternalServerError)
|
2014-06-06 23:40:48 +00:00
|
|
|
}
|
|
|
|
|
2014-07-15 13:54:23 +00:00
|
|
|
func (s *Server) ServeHTTP(w http.ResponseWriter, req *http.Request) {
|
2014-07-18 14:54:43 +00:00
|
|
|
defer httplog.MakeLogged(req, &w).StacktraceWhen(
|
|
|
|
httplog.StatusIsNot(
|
|
|
|
http.StatusOK,
|
|
|
|
http.StatusNotFound,
|
|
|
|
),
|
|
|
|
).Log()
|
2014-07-02 00:24:17 +00:00
|
|
|
|
2014-06-06 23:40:48 +00:00
|
|
|
u, err := url.ParseRequestURI(req.RequestURI)
|
|
|
|
if err != nil {
|
|
|
|
s.error(w, err)
|
|
|
|
return
|
|
|
|
}
|
2014-07-15 22:38:56 +00:00
|
|
|
// TODO: use an http.ServeMux instead of a switch.
|
2014-06-06 23:40:48 +00:00
|
|
|
switch {
|
2014-06-20 16:31:18 +00:00
|
|
|
case u.Path == "/container" || u.Path == "/containers":
|
2014-06-06 23:40:48 +00:00
|
|
|
defer req.Body.Close()
|
|
|
|
data, err := ioutil.ReadAll(req.Body)
|
|
|
|
if err != nil {
|
|
|
|
s.error(w, err)
|
|
|
|
return
|
|
|
|
}
|
2014-06-20 16:31:18 +00:00
|
|
|
if u.Path == "/container" {
|
|
|
|
// This is to provide backward compatibility. It only supports a single manifest
|
2014-07-15 20:24:41 +00:00
|
|
|
var pod Pod
|
|
|
|
err = yaml.Unmarshal(data, &pod.Manifest)
|
2014-06-20 16:31:18 +00:00
|
|
|
if err != nil {
|
|
|
|
s.error(w, err)
|
|
|
|
return
|
|
|
|
}
|
2014-07-15 20:24:41 +00:00
|
|
|
//TODO: sha1 of manifest?
|
|
|
|
pod.Name = "1"
|
|
|
|
s.updates <- PodUpdate{[]Pod{pod}, SET}
|
2014-06-20 16:31:18 +00:00
|
|
|
} else if u.Path == "/containers" {
|
|
|
|
var manifests []api.ContainerManifest
|
|
|
|
err = yaml.Unmarshal(data, &manifests)
|
|
|
|
if err != nil {
|
|
|
|
s.error(w, err)
|
|
|
|
return
|
|
|
|
}
|
2014-07-15 20:24:41 +00:00
|
|
|
pods := make([]Pod, len(manifests))
|
|
|
|
for i := range manifests {
|
|
|
|
pods[i].Name = fmt.Sprintf("%d", i+1)
|
|
|
|
pods[i].Manifest = manifests[i]
|
|
|
|
}
|
|
|
|
s.updates <- PodUpdate{pods, SET}
|
2014-06-06 23:40:48 +00:00
|
|
|
}
|
2014-07-01 22:35:56 +00:00
|
|
|
case u.Path == "/podInfo":
|
|
|
|
podID := u.Query().Get("podID")
|
|
|
|
if len(podID) == 0 {
|
2014-07-15 20:24:41 +00:00
|
|
|
w.WriteHeader(http.StatusBadRequest)
|
2014-07-12 17:44:29 +00:00
|
|
|
http.Error(w, "Missing 'podID=' query entry.", http.StatusBadRequest)
|
2014-06-06 23:40:48 +00:00
|
|
|
return
|
|
|
|
}
|
2014-07-15 20:24:41 +00:00
|
|
|
// TODO: backwards compatibility with existing API, needs API change
|
|
|
|
podFullName := GetPodFullName(&Pod{Name: podID, Namespace: "etcd"})
|
|
|
|
info, err := s.host.GetPodInfo(podFullName)
|
2014-07-18 14:54:43 +00:00
|
|
|
if err == ErrNoContainersInPod {
|
|
|
|
http.Error(w, "Pod does not exist", http.StatusNotFound)
|
|
|
|
return
|
|
|
|
}
|
2014-07-01 02:46:10 +00:00
|
|
|
if err != nil {
|
2014-07-12 17:44:29 +00:00
|
|
|
s.error(w, err)
|
2014-07-01 02:46:10 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
data, err := json.Marshal(info)
|
2014-06-06 23:40:48 +00:00
|
|
|
if err != nil {
|
2014-07-12 17:44:29 +00:00
|
|
|
s.error(w, err)
|
2014-06-06 23:40:48 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
w.WriteHeader(http.StatusOK)
|
2014-06-25 23:24:20 +00:00
|
|
|
w.Header().Add("Content-type", "application/json")
|
2014-07-01 02:46:10 +00:00
|
|
|
w.Write(data)
|
2014-07-01 21:05:10 +00:00
|
|
|
case strings.HasPrefix(u.Path, "/stats"):
|
|
|
|
s.serveStats(w, req)
|
2014-07-15 22:40:02 +00:00
|
|
|
case strings.HasPrefix(u.Path, "/spec"):
|
2014-07-15 20:24:41 +00:00
|
|
|
info, err := s.host.GetMachineInfo()
|
2014-07-15 22:40:02 +00:00
|
|
|
if err != nil {
|
|
|
|
s.error(w, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
data, err := json.Marshal(info)
|
|
|
|
if err != nil {
|
|
|
|
s.error(w, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
w.Header().Add("Content-type", "application/json")
|
|
|
|
w.Write(data)
|
2014-07-15 07:04:30 +00:00
|
|
|
case strings.HasPrefix(u.Path, "/logs/"):
|
2014-07-15 20:24:41 +00:00
|
|
|
s.host.ServeLogs(w, req)
|
2014-06-06 23:40:48 +00:00
|
|
|
default:
|
2014-07-15 20:24:41 +00:00
|
|
|
if s.handler != nil {
|
|
|
|
s.handler.ServeHTTP(w, req)
|
|
|
|
}
|
2014-06-06 23:40:48 +00:00
|
|
|
}
|
|
|
|
}
|
2014-07-01 21:05:10 +00:00
|
|
|
|
2014-07-15 13:54:23 +00:00
|
|
|
func (s *Server) serveStats(w http.ResponseWriter, req *http.Request) {
|
2014-07-15 20:24:41 +00:00
|
|
|
// /stats/<podfullname>/<containerName>
|
2014-07-01 21:05:10 +00:00
|
|
|
components := strings.Split(strings.TrimPrefix(path.Clean(req.URL.Path), "/"), "/")
|
2014-07-14 21:48:51 +00:00
|
|
|
var stats *info.ContainerInfo
|
2014-07-01 21:05:10 +00:00
|
|
|
var err error
|
2014-07-14 21:48:51 +00:00
|
|
|
var query info.ContainerInfoRequest
|
2014-07-15 22:40:02 +00:00
|
|
|
err = json.NewDecoder(req.Body).Decode(&query)
|
2014-07-14 21:48:51 +00:00
|
|
|
if err != nil && err != io.EOF {
|
|
|
|
s.error(w, err)
|
|
|
|
return
|
|
|
|
}
|
2014-07-01 21:05:10 +00:00
|
|
|
switch len(components) {
|
|
|
|
case 1:
|
|
|
|
// Machine stats
|
2014-07-15 20:24:41 +00:00
|
|
|
stats, err = s.host.GetRootInfo(&query)
|
2014-07-01 21:05:10 +00:00
|
|
|
case 2:
|
|
|
|
// pod stats
|
|
|
|
// TODO(monnand) Implement this
|
|
|
|
errors.New("pod level status currently unimplemented")
|
|
|
|
case 3:
|
2014-07-15 20:24:41 +00:00
|
|
|
stats, err = s.host.GetContainerInfo(components[1], components[2], &query)
|
2014-07-01 21:05:10 +00:00
|
|
|
default:
|
2014-07-12 17:44:29 +00:00
|
|
|
http.Error(w, "unknown resource.", http.StatusNotFound)
|
2014-07-01 21:05:10 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if err != nil {
|
2014-07-12 17:44:29 +00:00
|
|
|
s.error(w, err)
|
2014-07-01 21:05:10 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if stats == nil {
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
fmt.Fprint(w, "{}")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
data, err := json.Marshal(stats)
|
|
|
|
if err != nil {
|
2014-07-12 17:44:29 +00:00
|
|
|
s.error(w, err)
|
2014-07-01 21:05:10 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
w.Header().Add("Content-type", "application/json")
|
|
|
|
w.Write(data)
|
|
|
|
return
|
|
|
|
}
|