Skip to content

Commit

Permalink
Merge pull request moby#14785 from brahmaroutu/lint_api_server
Browse files Browse the repository at this point in the history
fix golint errors/warnings
  • Loading branch information
tiborvass committed Jul 29, 2015
2 parents 8a8de53 + 351f6b8 commit 2cd058c
Show file tree
Hide file tree
Showing 9 changed files with 95 additions and 78 deletions.
2 changes: 1 addition & 1 deletion api/server/profiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"github.com/gorilla/mux"
)

func ProfilerSetup(mainRouter *mux.Router, path string) {
func profilerSetup(mainRouter *mux.Router, path string) {
var r = mainRouter.PathPrefix(path).Subrouter()
r.HandleFunc("/vars", expVars)
r.HandleFunc("/pprof/", pprof.Index)
Expand Down
82 changes: 47 additions & 35 deletions api/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ import (
"github.com/docker/docker/utils"
)

type ServerConfig struct {
// Config provides the configuration for the API server
type Config struct {
Logging bool
EnableCors bool
CorsHeaders string
Expand All @@ -49,15 +50,17 @@ type ServerConfig struct {
TLSConfig *tls.Config
}

// Server contains instance details for the server
type Server struct {
daemon *daemon.Daemon
cfg *ServerConfig
cfg *Config
router *mux.Router
start chan struct{}
servers []serverCloser
}

func New(cfg *ServerConfig) *Server {
// New returns a new instance of the server based on the specified configuration.
func New(cfg *Config) *Server {
srv := &Server{
cfg: cfg,
start: make(chan struct{}),
Expand All @@ -67,6 +70,7 @@ func New(cfg *ServerConfig) *Server {
return srv
}

// Close closes servers and thus stop receiving requests
func (s *Server) Close() {
for _, srv := range s.servers {
if err := srv.Close(); err != nil {
Expand All @@ -80,9 +84,9 @@ type serverCloser interface {
Close() error
}

// ServeApi loops through all of the protocols sent in to docker and spawns
// ServeAPI loops through all of the protocols sent in to docker and spawns
// off a go routine to setup a serving http.Server for each.
func (s *Server) ServeApi(protoAddrs []string) error {
func (s *Server) ServeAPI(protoAddrs []string) error {
var chErrors = make(chan error, len(protoAddrs))

for _, protoAddr := range protoAddrs {
Expand Down Expand Up @@ -117,19 +121,27 @@ func (s *Server) ServeApi(protoAddrs []string) error {
return nil
}

type HttpServer struct {
// HTTPServer contains an instance of http server and the listener.
// srv *http.Server, contains configuration to create a http server and a mux router with all api end points.
// l net.Listener, is a TCP or Socket listener that dispatches incoming request to the router.
type HTTPServer struct {
srv *http.Server
l net.Listener
}

func (s *HttpServer) Serve() error {
// Serve starts listening for inbound requests.
func (s *HTTPServer) Serve() error {
return s.srv.Serve(s.l)
}
func (s *HttpServer) Close() error {

// Close closes the HTTPServer from listening for the inbound requests.
func (s *HTTPServer) Close() error {
return s.l.Close()
}

type HttpApiFunc func(version version.Version, w http.ResponseWriter, r *http.Request, vars map[string]string) error
// HTTPAPIFunc is an adapter to allow the use of ordinary functions as Docker API endpoints.
// Any function that has the appropriate signature can be register as a API endpoint (e.g. getVersion).
type HTTPAPIFunc func(version version.Version, w http.ResponseWriter, r *http.Request, vars map[string]string) error

func hijackServer(w http.ResponseWriter) (io.ReadCloser, io.Writer, error) {
conn, _, err := w.(http.Hijacker).Hijack()
Expand All @@ -153,8 +165,8 @@ func closeStreams(streams ...interface{}) {
}
}

// Check to make sure request's Content-Type is application/json
func checkForJson(r *http.Request) error {
// checkForJSON makes sure that the request's Content-Type is application/json.
func checkForJSON(r *http.Request) error {
ct := r.Header.Get("Content-Type")

// No Content-Type header is ok as long as there's no Body
Expand Down Expand Up @@ -438,7 +450,7 @@ func (s *Server) getEvents(version version.Version, w http.ResponseWriter, r *ht
outStream.Write(nil) // make sure response is sent immediately
enc := json.NewEncoder(outStream)

getContainerId := func(cn string) string {
getContainerID := func(cn string) string {
c, err := d.Get(cn)
if err != nil {
return ""
Expand All @@ -449,7 +461,7 @@ func (s *Server) getEvents(version version.Version, w http.ResponseWriter, r *ht
sendEvent := func(ev *jsonmessage.JSONMessage) error {
//incoming container filter can be name,id or partial id, convert and replace as a full container id
for i, cn := range ef["container"] {
ef["container"][i] = getContainerId(cn)
ef["container"][i] = getContainerID(cn)
}

if isFiltered(ev.Status, ef["event"]) || (isFiltered(ev.ID, ef["image"]) &&
Expand Down Expand Up @@ -684,7 +696,7 @@ func (s *Server) postCommit(version version.Version, w http.ResponseWriter, r *h
return err
}

if err := checkForJson(r); err != nil {
if err := checkForJSON(r); err != nil {
return err
}

Expand Down Expand Up @@ -734,8 +746,8 @@ func (s *Server) postImagesCreate(version version.Version, w http.ResponseWriter
authEncoded := r.Header.Get("X-Registry-Auth")
authConfig := &cliconfig.AuthConfig{}
if authEncoded != "" {
authJson := base64.NewDecoder(base64.URLEncoding, strings.NewReader(authEncoded))
if err := json.NewDecoder(authJson).Decode(authConfig); err != nil {
authJSON := base64.NewDecoder(base64.URLEncoding, strings.NewReader(authEncoded))
if err := json.NewDecoder(authJSON).Decode(authConfig); err != nil {
// for a pull it is not an error if no auth was given
// to increase compatibility with the existing api it is defaulting to be empty
authConfig = &cliconfig.AuthConfig{}
Expand Down Expand Up @@ -814,8 +826,8 @@ func (s *Server) getImagesSearch(version version.Version, w http.ResponseWriter,
)

if authEncoded != "" {
authJson := base64.NewDecoder(base64.URLEncoding, strings.NewReader(authEncoded))
if err := json.NewDecoder(authJson).Decode(&config); err != nil {
authJSON := base64.NewDecoder(base64.URLEncoding, strings.NewReader(authEncoded))
if err := json.NewDecoder(authJSON).Decode(&config); err != nil {
// for a search it is not an error if no auth was given
// to increase compatibility with the existing api it is defaulting to be empty
config = &cliconfig.AuthConfig{}
Expand Down Expand Up @@ -852,8 +864,8 @@ func (s *Server) postImagesPush(version version.Version, w http.ResponseWriter,
authEncoded := r.Header.Get("X-Registry-Auth")
if authEncoded != "" {
// the new format is to handle the authConfig as a header
authJson := base64.NewDecoder(base64.URLEncoding, strings.NewReader(authEncoded))
if err := json.NewDecoder(authJson).Decode(authConfig); err != nil {
authJSON := base64.NewDecoder(base64.URLEncoding, strings.NewReader(authEncoded))
if err := json.NewDecoder(authJSON).Decode(authConfig); err != nil {
// to increase compatibility to existing api it is defaulting to be empty
authConfig = &cliconfig.AuthConfig{}
}
Expand Down Expand Up @@ -923,7 +935,7 @@ func (s *Server) postContainersCreate(version version.Version, w http.ResponseWr
if err := parseForm(r); err != nil {
return err
}
if err := checkForJson(r); err != nil {
if err := checkForJSON(r); err != nil {
return err
}
var (
Expand All @@ -935,15 +947,15 @@ func (s *Server) postContainersCreate(version version.Version, w http.ResponseWr
if err != nil {
return err
}
adjustCpuShares(version, hostConfig)
adjustCPUShares(version, hostConfig)

containerId, warnings, err := s.daemon.ContainerCreate(name, config, hostConfig)
containerID, warnings, err := s.daemon.ContainerCreate(name, config, hostConfig)
if err != nil {
return err
}

return writeJSON(w, http.StatusCreated, &types.ContainerCreateResponse{
ID: containerId,
ID: containerID,
Warnings: warnings,
})
}
Expand Down Expand Up @@ -1045,7 +1057,7 @@ func (s *Server) postContainersStart(version version.Version, w http.ResponseWri
// allow a nil body for backwards compatibility
var hostConfig *runconfig.HostConfig
if r.Body != nil && (r.ContentLength > 0 || r.ContentLength == -1) {
if err := checkForJson(r); err != nil {
if err := checkForJSON(r); err != nil {
return err
}

Expand Down Expand Up @@ -1292,9 +1304,9 @@ func (s *Server) postBuild(version version.Version, w http.ResponseWriter, r *ht
buildConfig.CgroupParent = r.FormValue("cgroupparent")

var buildUlimits = []*ulimit.Ulimit{}
ulimitsJson := r.FormValue("ulimits")
if ulimitsJson != "" {
if err := json.NewDecoder(strings.NewReader(ulimitsJson)).Decode(&buildUlimits); err != nil {
ulimitsJSON := r.FormValue("ulimits")
if ulimitsJSON != "" {
if err := json.NewDecoder(strings.NewReader(ulimitsJSON)).Decode(&buildUlimits); err != nil {
return err
}
buildConfig.Ulimits = buildUlimits
Expand Down Expand Up @@ -1332,7 +1344,7 @@ func (s *Server) postContainersCopy(version version.Version, w http.ResponseWrit
return fmt.Errorf("Missing parameter")
}

if err := checkForJson(r); err != nil {
if err := checkForJSON(r); err != nil {
return err
}

Expand Down Expand Up @@ -1431,7 +1443,7 @@ func (s *Server) postContainerExecCreate(version version.Version, w http.Respons
if err := parseForm(r); err != nil {
return err
}
if err := checkForJson(r); err != nil {
if err := checkForJSON(r); err != nil {
return err
}
name := vars["name"]
Expand Down Expand Up @@ -1547,7 +1559,7 @@ func (s *Server) ping(version version.Version, w http.ResponseWriter, r *http.Re
return err
}

func (s *Server) initTcpSocket(addr string) (l net.Listener, err error) {
func (s *Server) initTCPSocket(addr string) (l net.Listener, err error) {
if s.cfg.TLSConfig == nil || s.cfg.TLSConfig.ClientAuth != tls.RequireAndVerifyClientCert {
logrus.Warn("/!\\ DON'T BIND ON ANY IP ADDRESS WITHOUT setting -tlsverify IF YOU DON'T KNOW WHAT YOU'RE DOING /!\\")
}
Expand All @@ -1560,7 +1572,7 @@ func (s *Server) initTcpSocket(addr string) (l net.Listener, err error) {
return
}

func makeHttpHandler(logging bool, localMethod string, localRoute string, handlerFunc HttpApiFunc, corsHeaders string, dockerVersion version.Version) http.HandlerFunc {
func makeHTTPHandler(logging bool, localMethod string, localRoute string, handlerFunc HTTPAPIFunc, corsHeaders string, dockerVersion version.Version) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
// log the request
logrus.Debugf("Calling %s %s", localMethod, localRoute)
Expand Down Expand Up @@ -1612,9 +1624,9 @@ func makeHttpHandler(logging bool, localMethod string, localRoute string, handle
func createRouter(s *Server) *mux.Router {
r := mux.NewRouter()
if os.Getenv("DEBUG") != "" {
ProfilerSetup(r, "/debug/")
profilerSetup(r, "/debug/")
}
m := map[string]map[string]HttpApiFunc{
m := map[string]map[string]HTTPAPIFunc{
"HEAD": {
"/containers/{name:.*}/archive": s.headContainersArchive,
},
Expand Down Expand Up @@ -1693,7 +1705,7 @@ func createRouter(s *Server) *mux.Router {
localMethod := method

// build the handler function
f := makeHttpHandler(s.cfg.Logging, localMethod, localRoute, localFct, corsHeaders, version.Version(s.cfg.Version))
f := makeHTTPHandler(s.cfg.Logging, localMethod, localRoute, localFct, corsHeaders, version.Version(s.cfg.Version))

// add the new route
if localRoute == "" {
Expand Down
25 changes: 14 additions & 11 deletions api/server/server_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ import (

const (
// See http://git.kernel.org/cgit/linux/kernel/git/tip/tip.git/tree/kernel/sched/sched.h?id=8cd9234c64c584432f6992fe944ca9e46ca8ea76#n269
linuxMinCpuShares = 2
linuxMaxCpuShares = 262144
linuxMinCPUShares = 2
linuxMaxCPUShares = 262144
)

// newServer sets up the required serverClosers and does protocol specific checking.
Expand All @@ -40,7 +40,7 @@ func (s *Server) newServer(proto, addr string) ([]serverCloser, error) {
// won't be ready.
<-s.start
case "tcp":
l, err := s.initTcpSocket(addr)
l, err := s.initTCPSocket(addr)
if err != nil {
return nil, err
}
Expand All @@ -56,7 +56,7 @@ func (s *Server) newServer(proto, addr string) ([]serverCloser, error) {
}
var res []serverCloser
for _, l := range ls {
res = append(res, &HttpServer{
res = append(res, &HTTPServer{
&http.Server{
Addr: addr,
Handler: s.router,
Expand All @@ -67,6 +67,9 @@ func (s *Server) newServer(proto, addr string) ([]serverCloser, error) {
return res, nil
}

// AcceptConnections allows clients to connect to the API server.
// Referenced Daemon is notified about this server, and waits for the
// daemon acknowledgement before the incoming connections are accepted.
func (s *Server) AcceptConnections(d *daemon.Daemon) {
// Tell the init daemon we are accepting requests
s.daemon = d
Expand Down Expand Up @@ -107,16 +110,16 @@ func allocateDaemonPort(addr string) error {
return nil
}

func adjustCpuShares(version version.Version, hostConfig *runconfig.HostConfig) {
func adjustCPUShares(version version.Version, hostConfig *runconfig.HostConfig) {
if version.LessThan("1.19") {
if hostConfig != nil && hostConfig.CPUShares > 0 {
// Handle unsupported CpuShares
if hostConfig.CPUShares < linuxMinCpuShares {
logrus.Warnf("Changing requested CpuShares of %d to minimum allowed of %d", hostConfig.CPUShares, linuxMinCpuShares)
hostConfig.CPUShares = linuxMinCpuShares
} else if hostConfig.CPUShares > linuxMaxCpuShares {
logrus.Warnf("Changing requested CpuShares of %d to maximum allowed of %d", hostConfig.CPUShares, linuxMaxCpuShares)
hostConfig.CPUShares = linuxMaxCpuShares
if hostConfig.CPUShares < linuxMinCPUShares {
logrus.Warnf("Changing requested CpuShares of %d to minimum allowed of %d", hostConfig.CPUShares, linuxMinCPUShares)
hostConfig.CPUShares = linuxMinCPUShares
} else if hostConfig.CPUShares > linuxMaxCPUShares {
logrus.Warnf("Changing requested CpuShares of %d to maximum allowed of %d", hostConfig.CPUShares, linuxMaxCPUShares)
hostConfig.CPUShares = linuxMaxCPUShares
}
}
}
Expand Down
Loading

0 comments on commit 2cd058c

Please sign in to comment.