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 <tibor@docker.com>
This commit is contained in:
Tibor Vass 2015-09-29 17:32:07 -04:00
parent b08f071e18
commit 94e3b0f428
14 changed files with 41 additions and 163 deletions

View File

@ -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 {

View File

@ -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)

View File

@ -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.

View File

@ -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()

View File

@ -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 {

View File

@ -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") {

View File

@ -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"):

View File

@ -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)
}

View File

@ -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)
}
}

View File

@ -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

View File

@ -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
}

View File

@ -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 {

View File

@ -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
}

View File

@ -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)
}
}