mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-29 14:37:00 +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
|
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:
|
// It handles URLs of the form:
|
||||||
// ${prefix}/${storage_key}[/${object_name}]
|
// ${prefix}/${storage_key}[/${object_name}]
|
||||||
// Where 'prefix' is an arbitrary string, and 'storage_key' points to a RESTStorage object stored in storage.
|
// 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.
|
// 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
|
prefix string
|
||||||
storage map[string]RESTStorage
|
storage map[string]RESTStorage
|
||||||
ops *Operations
|
ops *Operations
|
||||||
logserver http.Handler
|
logserver http.Handler
|
||||||
}
|
}
|
||||||
|
|
||||||
// New creates a new ApiServer object.
|
// New creates a new APIServer object.
|
||||||
// 'storage' contains a map of handlers.
|
// 'storage' contains a map of handlers.
|
||||||
// 'prefix' is the hosting path prefix.
|
// 'prefix' is the hosting path prefix.
|
||||||
func New(storage map[string]RESTStorage, prefix string) *ApiServer {
|
func New(storage map[string]RESTStorage, prefix string) *APIServer {
|
||||||
return &ApiServer{
|
return &APIServer{
|
||||||
storage: storage,
|
storage: storage,
|
||||||
prefix: prefix,
|
prefix: prefix,
|
||||||
ops: NewOperations(),
|
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)
|
w.WriteHeader(http.StatusOK)
|
||||||
// TODO: serve this out of a file?
|
// TODO: serve this out of a file?
|
||||||
data := "<html><body>Welcome to Kubernetes</body></html>"
|
data := "<html><body>Welcome to Kubernetes</body></html>"
|
||||||
@ -109,12 +109,12 @@ func (server *ApiServer) handleIndex(w http.ResponseWriter) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// HTTP Handler interface
|
// 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() {
|
defer func() {
|
||||||
if x := recover(); x != nil {
|
if x := recover(); x != nil {
|
||||||
w.WriteHeader(http.StatusInternalServerError)
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
fmt.Fprint(w, "apiserver panic. Look in log for details.")
|
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(
|
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])
|
LogOf(w).Addf("'%v' has no storage object", requestParts[0])
|
||||||
server.notFound(req, w)
|
server.notFound(req, w)
|
||||||
return
|
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)
|
w.WriteHeader(http.StatusNotFound)
|
||||||
fmt.Fprintf(w, "Not Found: %#v", req)
|
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)
|
w.WriteHeader(statusCode)
|
||||||
output, err := api.Encode(object)
|
output, err := api.Encode(object)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -175,19 +175,19 @@ func (server *ApiServer) write(statusCode int, object interface{}, w http.Respon
|
|||||||
w.Write(output)
|
w.Write(output)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (server *ApiServer) error(err error, w http.ResponseWriter) {
|
func (server *APIServer) error(err error, w http.ResponseWriter) {
|
||||||
w.WriteHeader(500)
|
w.WriteHeader(500)
|
||||||
fmt.Fprintf(w, "Internal Error: %#v", err)
|
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()
|
defer req.Body.Close()
|
||||||
return ioutil.ReadAll(req.Body)
|
return ioutil.ReadAll(req.Body)
|
||||||
}
|
}
|
||||||
|
|
||||||
// finishReq finishes up a request, waiting until the operation finishes or, after a timeout, creating an
|
// 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.
|
// 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)
|
op := server.ops.NewOperation(out)
|
||||||
if sync {
|
if sync {
|
||||||
op.WaitFor(timeout)
|
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)
|
// sync=[false|true] Synchronous request (only applies to create, update, delete operations)
|
||||||
// timeout=<duration> Timeout for synchronous requests, only applies if sync=true
|
// timeout=<duration> Timeout for synchronous requests, only applies if sync=true
|
||||||
// labels=<label-selector> Used for filtering list operations
|
// 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) {
|
func (server *APIServer) handleREST(parts []string, requestURL *url.URL, req *http.Request, w http.ResponseWriter, storage RESTStorage) {
|
||||||
sync := requestUrl.Query().Get("sync") == "true"
|
sync := requestURL.Query().Get("sync") == "true"
|
||||||
timeout := parseTimeout(requestUrl.Query().Get("timeout"))
|
timeout := parseTimeout(requestURL.Query().Get("timeout"))
|
||||||
switch req.Method {
|
switch req.Method {
|
||||||
case "GET":
|
case "GET":
|
||||||
switch len(parts) {
|
switch len(parts) {
|
||||||
case 1:
|
case 1:
|
||||||
selector, err := labels.ParseSelector(requestUrl.Query().Get("labels"))
|
selector, err := labels.ParseSelector(requestURL.Query().Get("labels"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
server.error(err, w)
|
server.error(err, w)
|
||||||
return
|
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" {
|
if req.Method != "GET" {
|
||||||
server.notFound(req, w)
|
server.notFound(req, w)
|
||||||
}
|
}
|
||||||
|
@ -25,7 +25,7 @@ import (
|
|||||||
"github.com/golang/glog"
|
"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)
|
type StacktracePred func(httpStatus int) (logStacktrace bool)
|
||||||
|
|
||||||
// Add a layer on top of ResponseWriter, so we can track latency and error
|
// Add a layer on top of ResponseWriter, so we can track latency and error
|
||||||
@ -42,6 +42,7 @@ type respLogger struct {
|
|||||||
logStacktracePred StacktracePred
|
logStacktracePred StacktracePred
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// DefaultStacktracePred is the default implementation of StacktracePred.
|
||||||
func DefaultStacktracePred(status int) bool {
|
func DefaultStacktracePred(status int) bool {
|
||||||
return status != http.StatusOK && status != http.StatusAccepted
|
return status != http.StatusOK && status != http.StatusAccepted
|
||||||
}
|
}
|
||||||
|
@ -47,7 +47,7 @@ type Operations struct {
|
|||||||
ops map[string]*Operation
|
ops map[string]*Operation
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns a new Operations repository.
|
// NewOperations returns a new Operations repository.
|
||||||
func NewOperations() *Operations {
|
func NewOperations() *Operations {
|
||||||
ops := &Operations{
|
ops := &Operations{
|
||||||
ops: map[string]*Operation{},
|
ops: map[string]*Operation{},
|
||||||
@ -56,7 +56,7 @@ func NewOperations() *Operations {
|
|||||||
return ops
|
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 {
|
func (ops *Operations) NewOperation(from <-chan interface{}) *Operation {
|
||||||
id := atomic.AddInt64(&ops.lastID, 1)
|
id := atomic.AddInt64(&ops.lastID, 1)
|
||||||
op := &Operation{
|
op := &Operation{
|
||||||
@ -93,7 +93,7 @@ func (ops *Operations) List() api.ServerOpList {
|
|||||||
return ol
|
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 {
|
func (ops *Operations) Get(id string) *Operation {
|
||||||
ops.lock.Lock()
|
ops.lock.Lock()
|
||||||
defer ops.lock.Unlock()
|
defer ops.lock.Unlock()
|
||||||
@ -131,7 +131,7 @@ func (op *Operation) wait() {
|
|||||||
op.notify <- true
|
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.
|
// whichever happens first.
|
||||||
func (op *Operation) WaitFor(timeout time.Duration) {
|
func (op *Operation) WaitFor(timeout time.Duration) {
|
||||||
select {
|
select {
|
||||||
@ -153,7 +153,7 @@ func (op *Operation) expired(limitTime time.Time) bool {
|
|||||||
return op.finished.Before(limitTime)
|
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.
|
// with a bool indicating true in the latter case.
|
||||||
func (op *Operation) StatusOrResult() (description interface{}, finished bool) {
|
func (op *Operation) StatusOrResult() (description interface{}, finished bool) {
|
||||||
op.lock.Lock()
|
op.lock.Lock()
|
||||||
|
Loading…
Reference in New Issue
Block a user