Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set correct applicatoin/json mime type for some kubelet endpoints #22966

Merged
merged 1 commit into from
Mar 16, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 17 additions & 3 deletions pkg/kubelet/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -495,7 +495,7 @@ func (s *Server) getPods(request *restful.Request, response *restful.Response) {
response.WriteError(http.StatusInternalServerError, err)
return
}
response.Write(data)
writeJsonResponse(response, data)
}

// getRunningPods returns a list of pods running on Kubelet. The list is
Expand All @@ -512,7 +512,7 @@ func (s *Server) getRunningPods(request *restful.Request, response *restful.Resp
response.WriteError(http.StatusInternalServerError, err)
return
}
response.Write(data)
writeJsonResponse(response, data)
}

// getLogs handles logs requests against the Kubelet.
Expand Down Expand Up @@ -585,7 +585,7 @@ func (s *Server) getRun(request *restful.Request, response *restful.Response) {
response.WriteError(http.StatusInternalServerError, err)
return
}
response.Write(data)
writeJsonResponse(response, data)
}

// getExec handles requests to run a command inside a container.
Expand Down Expand Up @@ -778,6 +778,20 @@ func getPodCoordinates(request *restful.Request) (namespace, pod string, uid typ
return
}

// Derived from go-restful writeJSON.
func writeJsonResponse(response *restful.Response, data []byte) {
if data == nil {
response.WriteHeader(http.StatusOK)
// do not write a nil representation
return
}
response.Header().Set(restful.HEADER_ContentType, restful.MIME_JSON)
response.WriteHeader(http.StatusOK)
if _, err := response.Write(data); err != nil {
glog.Errorf("Error writing response: %v", err)
}
}

// PortForwarder knows how to forward content from a data stream to/from a port
// in a pod.
type PortForwarder interface {
Expand Down
10 changes: 3 additions & 7 deletions pkg/kubelet/server/stats/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,12 +220,8 @@ func (h *handler) handlePodContainer(request *restful.Request, response *restful
}

func writeResponse(response *restful.Response, stats interface{}) {
if stats == nil {
return
}
err := response.WriteAsJson(stats)
if err != nil {
handleError(response, err)
if err := response.WriteAsJson(stats); err != nil {
glog.Errorf("Error writing response: %v", err)
}
}

Expand All @@ -236,7 +232,7 @@ func handleError(response *restful.Response, err error) {
response.WriteError(http.StatusNotFound, err)
default:
msg := fmt.Sprintf("Internal Error: %v", err)
glog.Infof("HTTP InternalServerError: %s", msg)
glog.Errorf("HTTP InternalServerError: %s", msg)
response.WriteErrorString(http.StatusInternalServerError, msg)
}
}