Cleanup cAdvisor error responses with proper headers & response codes
This commit is contained in:
parent
d258736528
commit
3b3a855ba4
@ -85,7 +85,7 @@ func handleRequest(supportedApiVersions map[string]ApiVersion, m manager.Manager
|
|||||||
versions = append(versions, v)
|
versions = append(versions, v)
|
||||||
}
|
}
|
||||||
sort.Strings(versions)
|
sort.Strings(versions)
|
||||||
fmt.Fprintf(w, "Supported API versions: %s", strings.Join(versions, ","))
|
http.Error(w, fmt.Sprintf("Supported API versions: %s", strings.Join(versions, ",")), http.StatusBadRequest)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -109,7 +109,7 @@ func handleRequest(supportedApiVersions map[string]ApiVersion, m manager.Manager
|
|||||||
if requestType == "" {
|
if requestType == "" {
|
||||||
requestTypes := versionHandler.SupportedRequestTypes()
|
requestTypes := versionHandler.SupportedRequestTypes()
|
||||||
sort.Strings(requestTypes)
|
sort.Strings(requestTypes)
|
||||||
fmt.Fprintf(w, "Supported request types: %q", strings.Join(requestTypes, ","))
|
http.Error(w, fmt.Sprintf("Supported request types: %q", strings.Join(requestTypes, ",")), http.StatusBadRequest)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -44,7 +44,7 @@ func RegisterHandlers(mux httpmux.Mux, containerManager manager.Manager, httpAut
|
|||||||
mux.HandleFunc(validate.ValidatePage, func(w http.ResponseWriter, r *http.Request) {
|
mux.HandleFunc(validate.ValidatePage, func(w http.ResponseWriter, r *http.Request) {
|
||||||
err := validate.HandleRequest(w, containerManager)
|
err := validate.HandleRequest(w, containerManager)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Fprintf(w, "%s", err)
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
@ -104,15 +104,9 @@ func RegisterPrometheusHandler(mux httpmux.Mux, containerManager manager.Manager
|
|||||||
}
|
}
|
||||||
|
|
||||||
func staticHandlerNoAuth(w http.ResponseWriter, r *http.Request) {
|
func staticHandlerNoAuth(w http.ResponseWriter, r *http.Request) {
|
||||||
err := static.HandleRequest(w, r.URL)
|
static.HandleRequest(w, r.URL)
|
||||||
if err != nil {
|
|
||||||
fmt.Fprintf(w, "%s", err)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func staticHandler(w http.ResponseWriter, r *auth.AuthenticatedRequest) {
|
func staticHandler(w http.ResponseWriter, r *auth.AuthenticatedRequest) {
|
||||||
err := static.HandleRequest(w, r.URL)
|
static.HandleRequest(w, r.URL)
|
||||||
if err != nil {
|
|
||||||
fmt.Fprintf(w, "%s", err)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -163,7 +163,7 @@ func printUnit(bytes uint64) string {
|
|||||||
return ByteSize(bytes).Unit()
|
return ByteSize(bytes).Unit()
|
||||||
}
|
}
|
||||||
|
|
||||||
func serveContainersPage(m manager.Manager, w http.ResponseWriter, u *url.URL) error {
|
func serveContainersPage(m manager.Manager, w http.ResponseWriter, u *url.URL) {
|
||||||
start := time.Now()
|
start := time.Now()
|
||||||
|
|
||||||
// The container name is the path after the handler
|
// The container name is the path after the handler
|
||||||
@ -175,14 +175,16 @@ func serveContainersPage(m manager.Manager, w http.ResponseWriter, u *url.URL) e
|
|||||||
}
|
}
|
||||||
cont, err := m.GetContainerInfo(containerName, &reqParams)
|
cont, err := m.GetContainerInfo(containerName, &reqParams)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to get container %q with error: %v", containerName, err)
|
http.Error(w, fmt.Sprintf("failed to get container %q with error: %v", containerName, err), http.StatusNotFound)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
displayName := getContainerDisplayName(cont.ContainerReference)
|
displayName := getContainerDisplayName(cont.ContainerReference)
|
||||||
|
|
||||||
// Get the MachineInfo
|
// Get the MachineInfo
|
||||||
machineInfo, err := m.GetMachineInfo()
|
machineInfo, err := m.GetMachineInfo()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
http.Error(w, fmt.Sprintf("failed to get machine info: %v", err), http.StatusInternalServerError)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
rootDir := getRootDir(containerName)
|
rootDir := getRootDir(containerName)
|
||||||
@ -241,7 +243,6 @@ func serveContainersPage(m manager.Manager, w http.ResponseWriter, u *url.URL) e
|
|||||||
}
|
}
|
||||||
|
|
||||||
glog.V(5).Infof("Request took %s", time.Since(start))
|
glog.V(5).Infof("Request took %s", time.Since(start))
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Build a relative path to the root of the container page.
|
// Build a relative path to the root of the container page.
|
||||||
|
@ -51,7 +51,7 @@ func toStatusKV(status info.DockerStatus) ([]keyVal, []keyVal) {
|
|||||||
}, ds
|
}, ds
|
||||||
}
|
}
|
||||||
|
|
||||||
func serveDockerPage(m manager.Manager, w http.ResponseWriter, u *url.URL) error {
|
func serveDockerPage(m manager.Manager, w http.ResponseWriter, u *url.URL) {
|
||||||
start := time.Now()
|
start := time.Now()
|
||||||
|
|
||||||
// The container name is the path after the handler
|
// The container name is the path after the handler
|
||||||
@ -66,7 +66,8 @@ func serveDockerPage(m manager.Manager, w http.ResponseWriter, u *url.URL) error
|
|||||||
}
|
}
|
||||||
conts, err := m.AllDockerContainers(&reqParams)
|
conts, err := m.AllDockerContainers(&reqParams)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to get container %q with error: %v", containerName, err)
|
http.Error(w, fmt.Sprintf("failed to get container %q with error: %v", containerName, err), http.StatusNotFound)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
subcontainers := make([]link, 0, len(conts))
|
subcontainers := make([]link, 0, len(conts))
|
||||||
for _, cont := range conts {
|
for _, cont := range conts {
|
||||||
@ -79,14 +80,16 @@ func serveDockerPage(m manager.Manager, w http.ResponseWriter, u *url.URL) error
|
|||||||
// Get Docker status
|
// Get Docker status
|
||||||
status, err := m.DockerInfo()
|
status, err := m.DockerInfo()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
http.Error(w, fmt.Sprintf("failed to get docker info: %v", err), http.StatusInternalServerError)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
dockerStatus, driverStatus := toStatusKV(status)
|
dockerStatus, driverStatus := toStatusKV(status)
|
||||||
// Get Docker Images
|
// Get Docker Images
|
||||||
images, err := m.DockerImages()
|
images, err := m.DockerImages()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
http.Error(w, fmt.Sprintf("failed to get docker images: %v", err), http.StatusInternalServerError)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
dockerContainersText := "Docker Containers"
|
dockerContainersText := "Docker Containers"
|
||||||
@ -110,7 +113,8 @@ func serveDockerPage(m manager.Manager, w http.ResponseWriter, u *url.URL) error
|
|||||||
}
|
}
|
||||||
cont, err := m.DockerContainer(containerName[1:], &reqParams)
|
cont, err := m.DockerContainer(containerName[1:], &reqParams)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to get container %q with error: %v", containerName, err)
|
http.Error(w, fmt.Sprintf("failed to get container %q with error: %v", containerName, err), http.StatusNotFound)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
displayName := getContainerDisplayName(cont.ContainerReference)
|
displayName := getContainerDisplayName(cont.ContainerReference)
|
||||||
|
|
||||||
@ -128,7 +132,8 @@ func serveDockerPage(m manager.Manager, w http.ResponseWriter, u *url.URL) error
|
|||||||
// Get the MachineInfo
|
// Get the MachineInfo
|
||||||
machineInfo, err := m.GetMachineInfo()
|
machineInfo, err := m.GetMachineInfo()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
http.Error(w, fmt.Sprintf("failed to get machine info: %v", err), http.StatusInternalServerError)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
data = &pageData{
|
data = &pageData{
|
||||||
DisplayName: displayName,
|
DisplayName: displayName,
|
||||||
@ -153,5 +158,5 @@ func serveDockerPage(m manager.Manager, w http.ResponseWriter, u *url.URL) error
|
|||||||
}
|
}
|
||||||
|
|
||||||
glog.V(5).Infof("Request took %s", time.Since(start))
|
glog.V(5).Infof("Request took %s", time.Since(start))
|
||||||
return nil
|
return
|
||||||
}
|
}
|
||||||
|
@ -77,37 +77,25 @@ func init() {
|
|||||||
|
|
||||||
func containerHandlerNoAuth(containerManager manager.Manager) http.HandlerFunc {
|
func containerHandlerNoAuth(containerManager manager.Manager) http.HandlerFunc {
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
err := serveContainersPage(containerManager, w, r.URL)
|
serveContainersPage(containerManager, w, r.URL)
|
||||||
if err != nil {
|
|
||||||
fmt.Fprintf(w, "%s", err)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func containerHandler(containerManager manager.Manager) auth.AuthenticatedHandlerFunc {
|
func containerHandler(containerManager manager.Manager) auth.AuthenticatedHandlerFunc {
|
||||||
return func(w http.ResponseWriter, r *auth.AuthenticatedRequest) {
|
return func(w http.ResponseWriter, r *auth.AuthenticatedRequest) {
|
||||||
err := serveContainersPage(containerManager, w, r.URL)
|
serveContainersPage(containerManager, w, r.URL)
|
||||||
if err != nil {
|
|
||||||
fmt.Fprintf(w, "%s", err)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func dockerHandlerNoAuth(containerManager manager.Manager) http.HandlerFunc {
|
func dockerHandlerNoAuth(containerManager manager.Manager) http.HandlerFunc {
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
err := serveDockerPage(containerManager, w, r.URL)
|
serveDockerPage(containerManager, w, r.URL)
|
||||||
if err != nil {
|
|
||||||
fmt.Fprintf(w, "%s", err)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func dockerHandler(containerManager manager.Manager) auth.AuthenticatedHandlerFunc {
|
func dockerHandler(containerManager manager.Manager) auth.AuthenticatedHandlerFunc {
|
||||||
return func(w http.ResponseWriter, r *auth.AuthenticatedRequest) {
|
return func(w http.ResponseWriter, r *auth.AuthenticatedRequest) {
|
||||||
err := serveDockerPage(containerManager, w, r.URL)
|
serveDockerPage(containerManager, w, r.URL)
|
||||||
if err != nil {
|
|
||||||
fmt.Fprintf(w, "%s", err)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -22,6 +22,8 @@ import (
|
|||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
"path"
|
"path"
|
||||||
|
|
||||||
|
"github.com/golang/glog"
|
||||||
)
|
)
|
||||||
|
|
||||||
const StaticResource = "/static/"
|
const StaticResource = "/static/"
|
||||||
@ -47,16 +49,18 @@ var staticFiles = map[string][]byte{
|
|||||||
"jquery-1.10.2.min.js": jqueryJs,
|
"jquery-1.10.2.min.js": jqueryJs,
|
||||||
}
|
}
|
||||||
|
|
||||||
func HandleRequest(w http.ResponseWriter, u *url.URL) error {
|
func HandleRequest(w http.ResponseWriter, u *url.URL) {
|
||||||
if len(u.Path) <= len(StaticResource) {
|
if len(u.Path) <= len(StaticResource) {
|
||||||
return fmt.Errorf("unknown static resource %q", u.Path)
|
http.Error(w, fmt.Sprintf("unknown static resource %q", u.Path), http.StatusNotFound)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get the static content if it exists.
|
// Get the static content if it exists.
|
||||||
resource := u.Path[len(StaticResource):]
|
resource := u.Path[len(StaticResource):]
|
||||||
content, ok := staticFiles[resource]
|
content, ok := staticFiles[resource]
|
||||||
if !ok {
|
if !ok {
|
||||||
return fmt.Errorf("unknown static resource %q", resource)
|
http.Error(w, fmt.Sprintf("unknown static resource %q", u.Path), http.StatusNotFound)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set Content-Type if we were able to detect it.
|
// Set Content-Type if we were able to detect it.
|
||||||
@ -65,6 +69,7 @@ func HandleRequest(w http.ResponseWriter, u *url.URL) error {
|
|||||||
w.Header().Set("Content-Type", contentType)
|
w.Header().Set("Content-Type", contentType)
|
||||||
}
|
}
|
||||||
|
|
||||||
_, err := w.Write(content)
|
if _, err := w.Write(content); err != nil {
|
||||||
return err
|
glog.Errorf("Failed to write response: %v", err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user