From 94e3b0f4288cdff767817b751e9a318e665ea7ac Mon Sep 17 00:00:00 2001 From: Tibor Vass Date: Tue, 29 Sep 2015 17:32:07 -0400 Subject: [PATCH] Use golang.org/x/net/context in api/server/ This patch removes the internal context package and uses golang's package instead. Signed-off-by: Tibor Vass --- api/server/auth.go | 2 +- api/server/container.go | 13 +++---- api/server/copy.go | 2 +- api/server/daemon.go | 4 +-- api/server/exec.go | 2 +- api/server/image.go | 6 ++-- api/server/inspect.go | 4 +-- api/server/middleware.go | 34 ++++++++++--------- api/server/middleware_test.go | 24 ++----------- api/server/server.go | 5 ++- api/server/server_test.go | 7 ++-- api/server/volume.go | 2 +- context/context.go | 64 ----------------------------------- context/context_test.go | 35 ------------------- 14 files changed, 41 insertions(+), 163 deletions(-) delete mode 100644 context/context.go delete mode 100644 context/context_test.go diff --git a/api/server/auth.go b/api/server/auth.go index 0373a9d2e1..d4d8c3ebad 100644 --- a/api/server/auth.go +++ b/api/server/auth.go @@ -6,7 +6,7 @@ import ( "github.com/docker/docker/api/types" "github.com/docker/docker/cliconfig" - "github.com/docker/docker/context" + "golang.org/x/net/context" ) func (s *Server) postAuth(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error { diff --git a/api/server/container.go b/api/server/container.go index 2799e2a0e8..2c83d01792 100644 --- a/api/server/container.go +++ b/api/server/container.go @@ -9,19 +9,17 @@ import ( "syscall" "time" - "golang.org/x/net/websocket" - "github.com/Sirupsen/logrus" "github.com/docker/distribution/registry/api/errcode" "github.com/docker/docker/api/types" - "github.com/docker/docker/context" "github.com/docker/docker/daemon" derr "github.com/docker/docker/errors" "github.com/docker/docker/pkg/ioutils" "github.com/docker/docker/pkg/signal" - "github.com/docker/docker/pkg/version" "github.com/docker/docker/runconfig" "github.com/docker/docker/utils" + "golang.org/x/net/context" + "golang.org/x/net/websocket" ) func (s *Server) getContainersJSON(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error { @@ -75,12 +73,11 @@ func (s *Server) getContainersStats(ctx context.Context, w http.ResponseWriter, closeNotifier = notifier.CloseNotify() } - version, _ := ctx.Value("api-version").(version.Version) config := &daemon.ContainerStatsConfig{ Stream: stream, OutStream: out, Stop: closeNotifier, - Version: version, + Version: versionFromContext(ctx), } return s.daemon.ContainerStats(vars["name"], config) @@ -234,7 +231,7 @@ func (s *Server) postContainersKill(ctx context.Context, w http.ResponseWriter, // Return error that's not caused because the container is stopped. // Return error if the container is not running and the api is >= 1.20 // to keep backwards compatibility. - version := ctx.Version() + version := versionFromContext(ctx) if version.GreaterThanOrEqualTo("1.20") || !isStopped { return fmt.Errorf("Cannot kill container %s: %v", name, err) } @@ -373,7 +370,7 @@ func (s *Server) postContainersCreate(ctx context.Context, w http.ResponseWriter if err != nil { return err } - version := ctx.Version() + version := versionFromContext(ctx) adjustCPUShares := version.LessThan("1.19") ccr, err := s.daemon.ContainerCreate(name, config, hostConfig, adjustCPUShares) diff --git a/api/server/copy.go b/api/server/copy.go index b3d8ed320e..8b9fcba4c9 100644 --- a/api/server/copy.go +++ b/api/server/copy.go @@ -10,7 +10,7 @@ import ( "strings" "github.com/docker/docker/api/types" - "github.com/docker/docker/context" + "golang.org/x/net/context" ) // postContainersCopy is deprecated in favor of getContainersArchive. diff --git a/api/server/daemon.go b/api/server/daemon.go index b8ee0fa46e..8582f56852 100644 --- a/api/server/daemon.go +++ b/api/server/daemon.go @@ -12,12 +12,12 @@ import ( "github.com/docker/docker/api" "github.com/docker/docker/api/types" "github.com/docker/docker/autogen/dockerversion" - "github.com/docker/docker/context" "github.com/docker/docker/pkg/ioutils" "github.com/docker/docker/pkg/jsonmessage" "github.com/docker/docker/pkg/parsers/filters" "github.com/docker/docker/pkg/parsers/kernel" "github.com/docker/docker/utils" + "golang.org/x/net/context" ) func (s *Server) getVersion(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error { @@ -31,7 +31,7 @@ func (s *Server) getVersion(ctx context.Context, w http.ResponseWriter, r *http. BuildTime: dockerversion.BUILDTIME, } - version := ctx.Version() + version := versionFromContext(ctx) if version.GreaterThanOrEqualTo("1.19") { v.Experimental = utils.ExperimentalBuild() diff --git a/api/server/exec.go b/api/server/exec.go index 46d46c58b7..87c16a0f5e 100644 --- a/api/server/exec.go +++ b/api/server/exec.go @@ -9,9 +9,9 @@ import ( "github.com/Sirupsen/logrus" "github.com/docker/docker/api/types" - "github.com/docker/docker/context" "github.com/docker/docker/pkg/stdcopy" "github.com/docker/docker/runconfig" + "golang.org/x/net/context" ) func (s *Server) getExecByID(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error { diff --git a/api/server/image.go b/api/server/image.go index 9a5718b07f..ad369d34ef 100644 --- a/api/server/image.go +++ b/api/server/image.go @@ -13,7 +13,6 @@ import ( "github.com/docker/docker/api/types" "github.com/docker/docker/builder" "github.com/docker/docker/cliconfig" - "github.com/docker/docker/context" "github.com/docker/docker/graph" "github.com/docker/docker/pkg/ioutils" "github.com/docker/docker/pkg/parsers" @@ -21,6 +20,7 @@ import ( "github.com/docker/docker/pkg/ulimit" "github.com/docker/docker/runconfig" "github.com/docker/docker/utils" + "golang.org/x/net/context" ) func (s *Server) postCommit(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error { @@ -35,7 +35,7 @@ func (s *Server) postCommit(ctx context.Context, w http.ResponseWriter, r *http. cname := r.Form.Get("container") pause := boolValue(r, "pause") - version := ctx.Version() + version := versionFromContext(ctx) if r.FormValue("pause") == "" && version.GreaterThanOrEqualTo("1.13") { pause = true } @@ -282,7 +282,7 @@ func (s *Server) postBuild(ctx context.Context, w http.ResponseWriter, r *http.R w.Header().Set("Content-Type", "application/json") - version := ctx.Version() + version := versionFromContext(ctx) if boolValue(r, "forcerm") && version.GreaterThanOrEqualTo("1.12") { buildConfig.Remove = true } else if r.FormValue("rm") == "" && version.GreaterThanOrEqualTo("1.12") { diff --git a/api/server/inspect.go b/api/server/inspect.go index 782ae4865c..3d7dc85d74 100644 --- a/api/server/inspect.go +++ b/api/server/inspect.go @@ -4,7 +4,7 @@ import ( "fmt" "net/http" - "github.com/docker/docker/context" + "golang.org/x/net/context" ) // getContainersByName inspects containers configuration and serializes it as json. @@ -16,7 +16,7 @@ func (s *Server) getContainersByName(ctx context.Context, w http.ResponseWriter, var json interface{} var err error - version := ctx.Version() + version := versionFromContext(ctx) switch { case version.LessThan("1.20"): diff --git a/api/server/middleware.go b/api/server/middleware.go index 10683f39aa..014bc9daf9 100644 --- a/api/server/middleware.go +++ b/api/server/middleware.go @@ -8,12 +8,14 @@ import ( "github.com/Sirupsen/logrus" "github.com/docker/docker/api" "github.com/docker/docker/autogen/dockerversion" - "github.com/docker/docker/context" "github.com/docker/docker/errors" - "github.com/docker/docker/pkg/stringid" "github.com/docker/docker/pkg/version" + "golang.org/x/net/context" ) +// apiVersionKey is the client's requested API version. +const apiVersionKey = "api-version" + // middleware is an adapter to allow the use of ordinary functions as Docker API filters. // Any function that has the appropriate signature can be register as a middleware. type middleware func(handler HTTPAPIFunc) HTTPAPIFunc @@ -28,16 +30,6 @@ func (s *Server) loggingMiddleware(handler HTTPAPIFunc) HTTPAPIFunc { } } -// requestIDMiddleware generates a uniq ID for each request. -// This ID travels inside the context for tracing purposes. -func requestIDMiddleware(handler HTTPAPIFunc) HTTPAPIFunc { - return func(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error { - reqID := stringid.TruncateID(stringid.GenerateNonCryptoID()) - ctx = context.WithValue(ctx, context.RequestID, reqID) - return handler(ctx, w, r, vars) - } -} - // userAgentMiddleware checks the User-Agent header looking for a valid docker client spec. func (s *Server) userAgentMiddleware(handler HTTPAPIFunc) HTTPAPIFunc { return func(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error { @@ -93,7 +85,7 @@ func versionMiddleware(handler HTTPAPIFunc) HTTPAPIFunc { } w.Header().Set("Server", "Docker/"+dockerversion.VERSION+" ("+runtime.GOOS+")") - ctx = context.WithValue(ctx, context.APIVersion, apiVersion) + ctx = context.WithValue(ctx, apiVersionKey, apiVersion) return handler(ctx, w, r, vars) } } @@ -104,7 +96,6 @@ func versionMiddleware(handler HTTPAPIFunc) HTTPAPIFunc { // // Example: handleWithGlobalMiddlewares(s.getContainersName) // -// requestIDMiddlware( // s.loggingMiddleware( // s.userAgentMiddleware( // s.corsMiddleware( @@ -112,14 +103,12 @@ func versionMiddleware(handler HTTPAPIFunc) HTTPAPIFunc { // ) // ) // ) -// ) func (s *Server) handleWithGlobalMiddlewares(handler HTTPAPIFunc) HTTPAPIFunc { middlewares := []middleware{ versionMiddleware, s.corsMiddleware, s.userAgentMiddleware, s.loggingMiddleware, - requestIDMiddleware, } h := handler @@ -128,3 +117,16 @@ func (s *Server) handleWithGlobalMiddlewares(handler HTTPAPIFunc) HTTPAPIFunc { } return h } + +// versionFromContext returns an API version from the context using apiVersionKey. +// It panics if the context value does not have version.Version type. +func versionFromContext(ctx context.Context) (ver version.Version) { + if ctx == nil { + return + } + val := ctx.Value(apiVersionKey) + if val == nil { + return + } + return val.(version.Version) +} diff --git a/api/server/middleware_test.go b/api/server/middleware_test.go index eda4588b9e..7d6741f947 100644 --- a/api/server/middleware_test.go +++ b/api/server/middleware_test.go @@ -6,13 +6,13 @@ import ( "testing" "github.com/docker/distribution/registry/api/errcode" - "github.com/docker/docker/context" "github.com/docker/docker/errors" + "golang.org/x/net/context" ) func TestVersionMiddleware(t *testing.T) { handler := func(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error { - if ctx.Version() == "" { + if versionFromContext(ctx) == "" { t.Fatalf("Expected version, got empty string") } return nil @@ -30,7 +30,7 @@ func TestVersionMiddleware(t *testing.T) { func TestVersionMiddlewareWithErrors(t *testing.T) { handler := func(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error { - if ctx.Version() == "" { + if versionFromContext(ctx) == "" { t.Fatalf("Expected version, got empty string") } return nil @@ -54,21 +54,3 @@ func TestVersionMiddlewareWithErrors(t *testing.T) { t.Fatalf("Expected ErrorCodeNewerClientVersion, got %v", err) } } - -func TestRequestIDMiddleware(t *testing.T) { - handler := func(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error { - if ctx.RequestID() == "" { - t.Fatalf("Expected request-id, got empty string") - } - return nil - } - - h := requestIDMiddleware(handler) - - req, _ := http.NewRequest("GET", "/containers/json", nil) - resp := httptest.NewRecorder() - ctx := context.Background() - if err := h(ctx, resp, req, map[string]string{}); err != nil { - t.Fatal(err) - } -} diff --git a/api/server/server.go b/api/server/server.go index a2d927ceff..84dfff65c5 100644 --- a/api/server/server.go +++ b/api/server/server.go @@ -10,15 +10,14 @@ import ( "os" "strings" - "github.com/gorilla/mux" - "github.com/Sirupsen/logrus" "github.com/docker/distribution/registry/api/errcode" "github.com/docker/docker/api" - "github.com/docker/docker/context" "github.com/docker/docker/daemon" "github.com/docker/docker/pkg/sockets" "github.com/docker/docker/utils" + "github.com/gorilla/mux" + "golang.org/x/net/context" ) // Config provides the configuration for the API server diff --git a/api/server/server_test.go b/api/server/server_test.go index 3d1a613976..3dcd81f7aa 100644 --- a/api/server/server_test.go +++ b/api/server/server_test.go @@ -5,7 +5,7 @@ import ( "net/http/httptest" "testing" - "github.com/docker/docker/context" + "golang.org/x/net/context" ) func TestMiddlewares(t *testing.T) { @@ -19,12 +19,9 @@ func TestMiddlewares(t *testing.T) { ctx := context.Background() localHandler := func(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error { - if ctx.Version() == "" { + if versionFromContext(ctx) == "" { t.Fatalf("Expected version, got empty string") } - if ctx.RequestID() == "" { - t.Fatalf("Expected request-id, got empty string") - } return nil } diff --git a/api/server/volume.go b/api/server/volume.go index cec3b820c4..19e7d54abf 100644 --- a/api/server/volume.go +++ b/api/server/volume.go @@ -5,7 +5,7 @@ import ( "net/http" "github.com/docker/docker/api/types" - "github.com/docker/docker/context" + "golang.org/x/net/context" ) func (s *Server) getVolumesList(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error { diff --git a/context/context.go b/context/context.go deleted file mode 100644 index 3532507c3d..0000000000 --- a/context/context.go +++ /dev/null @@ -1,64 +0,0 @@ -package context - -import ( - "golang.org/x/net/context" - - "github.com/docker/docker/pkg/version" -) - -const ( - // RequestID is the unique ID for each http request - RequestID = "request-id" - - // APIVersion is the client's requested API version - APIVersion = "api-version" -) - -// Context is just our own wrapper for the golang 'Context' - mainly -// so we can add our over version of the funcs. -type Context struct { - context.Context -} - -// Background creates a new Context based on golang's default one. -func Background() Context { - return Context{context.Background()} -} - -// WithValue will return a Context that has this new key/value pair -// associated with it. Just uses the golang version but then wraps it. -func WithValue(ctx Context, key, value interface{}) Context { - return Context{context.WithValue(ctx, key, value)} -} - -// RequestID is a utility func to make it easier to get the -// request ID associated with this Context/request. -func (ctx Context) RequestID() string { - val := ctx.Value(RequestID) - if val == nil { - return "" - } - - id, ok := val.(string) - if !ok { - // Ideally we shouldn't panic but we also should never get here - panic("Context RequestID isn't a string") - } - return id -} - -// Version is a utility func to make it easier to get the -// API version string associated with this Context/request. -func (ctx Context) Version() version.Version { - val := ctx.Value(APIVersion) - if val == nil { - return version.Version("") - } - - ver, ok := val.(version.Version) - if !ok { - // Ideally we shouldn't panic but we also should never get here - panic("Context APIVersion isn't a version.Version") - } - return ver -} diff --git a/context/context_test.go b/context/context_test.go deleted file mode 100644 index e1ec47435b..0000000000 --- a/context/context_test.go +++ /dev/null @@ -1,35 +0,0 @@ -package context - -import ( - "testing" - - "github.com/docker/docker/pkg/version" -) - -func TestContext(t *testing.T) { - ctx := Background() - - // First make sure getting non-existent values doesn't break - if id := ctx.RequestID(); id != "" { - t.Fatalf("RequestID() should have been '', was: %q", id) - } - - if ver := ctx.Version(); ver != "" { - t.Fatalf("Version() should have been '', was: %q", ver) - } - - // Test basic set/get - ctx = WithValue(ctx, RequestID, "123") - if ctx.RequestID() != "123" { - t.Fatalf("RequestID() should have been '123'") - } - - // Now make sure after a 2nd set we can still get both - ctx = WithValue(ctx, APIVersion, version.Version("x.y")) - if id := ctx.RequestID(); id != "123" { - t.Fatalf("RequestID() should have been '123', was %q", id) - } - if ver := ctx.Version(); ver != "x.y" { - t.Fatalf("Version() should have been 'x.y', was %q", ver) - } -}