mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-24 20:24:09 +00:00
Fixes golint errors in pkg/apiserver
This commit is contained in:
parent
780c441d19
commit
5cfbed4453
@ -76,24 +76,24 @@ func MakeAsync(fn WorkFunc) <-chan interface{} {
|
||||
return channel
|
||||
}
|
||||
|
||||
// ApiServer is an HTTPHandler that delegates to RESTStorage objects.
|
||||
// APIServer is an HTTPHandler that delegates to RESTStorage objects.
|
||||
// It handles URLs of the form:
|
||||
// ${prefix}/${storage_key}[/${object_name}]
|
||||
// Where 'prefix' is an arbitrary string, and 'storage_key' points to a RESTStorage object stored in storage.
|
||||
//
|
||||
// TODO: consider migrating this to go-restful which is a more full-featured version of the same thing.
|
||||
type ApiServer struct {
|
||||
type APIServer struct {
|
||||
prefix string
|
||||
storage map[string]RESTStorage
|
||||
ops *Operations
|
||||
logserver http.Handler
|
||||
}
|
||||
|
||||
// New creates a new ApiServer object.
|
||||
// New creates a new APIServer object.
|
||||
// 'storage' contains a map of handlers.
|
||||
// 'prefix' is the hosting path prefix.
|
||||
func New(storage map[string]RESTStorage, prefix string) *ApiServer {
|
||||
return &ApiServer{
|
||||
func New(storage map[string]RESTStorage, prefix string) *APIServer {
|
||||
return &APIServer{
|
||||
storage: storage,
|
||||
prefix: prefix,
|
||||
ops: NewOperations(),
|
||||
@ -101,7 +101,7 @@ func New(storage map[string]RESTStorage, prefix string) *ApiServer {
|
||||
}
|
||||
}
|
||||
|
||||
func (server *ApiServer) handleIndex(w http.ResponseWriter) {
|
||||
func (server *APIServer) handleIndex(w http.ResponseWriter) {
|
||||
w.WriteHeader(http.StatusOK)
|
||||
// TODO: serve this out of a file?
|
||||
data := "<html><body>Welcome to Kubernetes</body></html>"
|
||||
@ -109,12 +109,12 @@ func (server *ApiServer) handleIndex(w http.ResponseWriter) {
|
||||
}
|
||||
|
||||
// HTTP Handler interface
|
||||
func (server *ApiServer) ServeHTTP(w http.ResponseWriter, req *http.Request) {
|
||||
func (server *APIServer) ServeHTTP(w http.ResponseWriter, req *http.Request) {
|
||||
defer func() {
|
||||
if x := recover(); x != nil {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
fmt.Fprint(w, "apiserver panic. Look in log for details.")
|
||||
glog.Infof("ApiServer panic'd on %v %v: %#v\n%s\n", req.Method, req.RequestURI, x, debug.Stack())
|
||||
glog.Infof("APIServer panic'd on %v %v: %#v\n%s\n", req.Method, req.RequestURI, x, debug.Stack())
|
||||
}
|
||||
}()
|
||||
defer MakeLogged(req, &w).StacktraceWhen(
|
||||
@ -155,17 +155,17 @@ func (server *ApiServer) ServeHTTP(w http.ResponseWriter, req *http.Request) {
|
||||
LogOf(w).Addf("'%v' has no storage object", requestParts[0])
|
||||
server.notFound(req, w)
|
||||
return
|
||||
} else {
|
||||
server.handleREST(requestParts, url, req, w, storage)
|
||||
}
|
||||
|
||||
server.handleREST(requestParts, url, req, w, storage)
|
||||
}
|
||||
|
||||
func (server *ApiServer) notFound(req *http.Request, w http.ResponseWriter) {
|
||||
func (server *APIServer) notFound(req *http.Request, w http.ResponseWriter) {
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
fmt.Fprintf(w, "Not Found: %#v", req)
|
||||
}
|
||||
|
||||
func (server *ApiServer) write(statusCode int, object interface{}, w http.ResponseWriter) {
|
||||
func (server *APIServer) write(statusCode int, object interface{}, w http.ResponseWriter) {
|
||||
w.WriteHeader(statusCode)
|
||||
output, err := api.Encode(object)
|
||||
if err != nil {
|
||||
@ -175,19 +175,19 @@ func (server *ApiServer) write(statusCode int, object interface{}, w http.Respon
|
||||
w.Write(output)
|
||||
}
|
||||
|
||||
func (server *ApiServer) error(err error, w http.ResponseWriter) {
|
||||
func (server *APIServer) error(err error, w http.ResponseWriter) {
|
||||
w.WriteHeader(500)
|
||||
fmt.Fprintf(w, "Internal Error: %#v", err)
|
||||
}
|
||||
|
||||
func (server *ApiServer) readBody(req *http.Request) ([]byte, error) {
|
||||
func (server *APIServer) readBody(req *http.Request) ([]byte, error) {
|
||||
defer req.Body.Close()
|
||||
return ioutil.ReadAll(req.Body)
|
||||
}
|
||||
|
||||
// finishReq finishes up a request, waiting until the operation finishes or, after a timeout, creating an
|
||||
// Operation to recieve the result and returning its ID down the writer.
|
||||
func (server *ApiServer) finishReq(out <-chan interface{}, sync bool, timeout time.Duration, w http.ResponseWriter) {
|
||||
func (server *APIServer) finishReq(out <-chan interface{}, sync bool, timeout time.Duration, w http.ResponseWriter) {
|
||||
op := server.ops.NewOperation(out)
|
||||
if sync {
|
||||
op.WaitFor(timeout)
|
||||
@ -236,14 +236,14 @@ func parseTimeout(str string) time.Duration {
|
||||
// sync=[false|true] Synchronous request (only applies to create, update, delete operations)
|
||||
// timeout=<duration> Timeout for synchronous requests, only applies if sync=true
|
||||
// labels=<label-selector> Used for filtering list operations
|
||||
func (server *ApiServer) handleREST(parts []string, requestUrl *url.URL, req *http.Request, w http.ResponseWriter, storage RESTStorage) {
|
||||
sync := requestUrl.Query().Get("sync") == "true"
|
||||
timeout := parseTimeout(requestUrl.Query().Get("timeout"))
|
||||
func (server *APIServer) handleREST(parts []string, requestURL *url.URL, req *http.Request, w http.ResponseWriter, storage RESTStorage) {
|
||||
sync := requestURL.Query().Get("sync") == "true"
|
||||
timeout := parseTimeout(requestURL.Query().Get("timeout"))
|
||||
switch req.Method {
|
||||
case "GET":
|
||||
switch len(parts) {
|
||||
case 1:
|
||||
selector, err := labels.ParseSelector(requestUrl.Query().Get("labels"))
|
||||
selector, err := labels.ParseSelector(requestURL.Query().Get("labels"))
|
||||
if err != nil {
|
||||
server.error(err, w)
|
||||
return
|
||||
@ -325,7 +325,7 @@ func (server *ApiServer) handleREST(parts []string, requestUrl *url.URL, req *ht
|
||||
}
|
||||
}
|
||||
|
||||
func (server *ApiServer) handleOperationRequest(parts []string, w http.ResponseWriter, req *http.Request) {
|
||||
func (server *APIServer) handleOperationRequest(parts []string, w http.ResponseWriter, req *http.Request) {
|
||||
if req.Method != "GET" {
|
||||
server.notFound(req, w)
|
||||
}
|
||||
|
@ -25,7 +25,7 @@ import (
|
||||
"github.com/golang/glog"
|
||||
)
|
||||
|
||||
// Return true if a stacktrace should be logged for this status
|
||||
// StacktracePred returns true if a stacktrace should be logged for this status
|
||||
type StacktracePred func(httpStatus int) (logStacktrace bool)
|
||||
|
||||
// Add a layer on top of ResponseWriter, so we can track latency and error
|
||||
@ -42,6 +42,7 @@ type respLogger struct {
|
||||
logStacktracePred StacktracePred
|
||||
}
|
||||
|
||||
// DefaultStacktracePred is the default implementation of StacktracePred.
|
||||
func DefaultStacktracePred(status int) bool {
|
||||
return status != http.StatusOK && status != http.StatusAccepted
|
||||
}
|
||||
|
@ -47,7 +47,7 @@ type Operations struct {
|
||||
ops map[string]*Operation
|
||||
}
|
||||
|
||||
// Returns a new Operations repository.
|
||||
// NewOperations returns a new Operations repository.
|
||||
func NewOperations() *Operations {
|
||||
ops := &Operations{
|
||||
ops: map[string]*Operation{},
|
||||
@ -56,7 +56,7 @@ func NewOperations() *Operations {
|
||||
return ops
|
||||
}
|
||||
|
||||
// Add a new operation. Lock-free.
|
||||
// NewOperation adds a new operation. It is lock-free.
|
||||
func (ops *Operations) NewOperation(from <-chan interface{}) *Operation {
|
||||
id := atomic.AddInt64(&ops.lastID, 1)
|
||||
op := &Operation{
|
||||
@ -93,7 +93,7 @@ func (ops *Operations) List() api.ServerOpList {
|
||||
return ol
|
||||
}
|
||||
|
||||
// Returns the operation with the given ID, or nil
|
||||
// Get returns the operation with the given ID, or nil
|
||||
func (ops *Operations) Get(id string) *Operation {
|
||||
ops.lock.Lock()
|
||||
defer ops.lock.Unlock()
|
||||
@ -131,7 +131,7 @@ func (op *Operation) wait() {
|
||||
op.notify <- true
|
||||
}
|
||||
|
||||
// Wait for the specified duration, or until the operation finishes,
|
||||
// WaitFor waits for the specified duration, or until the operation finishes,
|
||||
// whichever happens first.
|
||||
func (op *Operation) WaitFor(timeout time.Duration) {
|
||||
select {
|
||||
@ -153,7 +153,7 @@ func (op *Operation) expired(limitTime time.Time) bool {
|
||||
return op.finished.Before(limitTime)
|
||||
}
|
||||
|
||||
// Return status information or the result of the operation if it is complete,
|
||||
// StatusOrResult returns status information or the result of the operation if it is complete,
|
||||
// with a bool indicating true in the latter case.
|
||||
func (op *Operation) StatusOrResult() (description interface{}, finished bool) {
|
||||
op.lock.Lock()
|
||||
|
Loading…
Reference in New Issue
Block a user