Browse code

Move context stuff to its own package

Signed-off-by: Doug Davis <dug@us.ibm.com>

Doug Davis authored on 2015/09/10 06:14:09
Showing 11 changed files
... ...
@@ -4,10 +4,9 @@ import (
4 4
 	"encoding/json"
5 5
 	"net/http"
6 6
 
7
-	"golang.org/x/net/context"
8
-
9 7
 	"github.com/docker/docker/api/types"
10 8
 	"github.com/docker/docker/cliconfig"
9
+	"github.com/docker/docker/context"
11 10
 )
12 11
 
13 12
 func (s *Server) postAuth(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
... ...
@@ -8,15 +8,14 @@ import (
8 8
 	"strings"
9 9
 	"time"
10 10
 
11
-	"golang.org/x/net/context"
12 11
 	"golang.org/x/net/websocket"
13 12
 
14 13
 	"github.com/Sirupsen/logrus"
15 14
 	"github.com/docker/docker/api/types"
15
+	"github.com/docker/docker/context"
16 16
 	"github.com/docker/docker/daemon"
17 17
 	"github.com/docker/docker/pkg/ioutils"
18 18
 	"github.com/docker/docker/pkg/signal"
19
-	"github.com/docker/docker/pkg/version"
20 19
 	"github.com/docker/docker/runconfig"
21 20
 )
22 21
 
... ...
@@ -251,7 +250,7 @@ func (s *Server) postContainersKill(ctx context.Context, w http.ResponseWriter,
251 251
 		// Return error that's not caused because the container is stopped.
252 252
 		// Return error if the container is not running and the api is >= 1.20
253 253
 		// to keep backwards compatibility.
254
-		version, _ := ctx.Value("api-version").(version.Version)
254
+		version := ctx.Version()
255 255
 		if version.GreaterThanOrEqualTo("1.20") || !isStopped {
256 256
 			return fmt.Errorf("Cannot kill container %s: %v", name, err)
257 257
 		}
... ...
@@ -392,7 +391,7 @@ func (s *Server) postContainersCreate(ctx context.Context, w http.ResponseWriter
392 392
 	if err != nil {
393 393
 		return err
394 394
 	}
395
-	version, _ := ctx.Value("api-version").(version.Version)
395
+	version := ctx.Version()
396 396
 	adjustCPUShares := version.LessThan("1.19")
397 397
 
398 398
 	container, warnings, err := s.daemon.ContainerCreate(name, config, hostConfig, adjustCPUShares)
... ...
@@ -9,9 +9,8 @@ import (
9 9
 	"os"
10 10
 	"strings"
11 11
 
12
-	"golang.org/x/net/context"
13
-
14 12
 	"github.com/docker/docker/api/types"
13
+	"github.com/docker/docker/context"
15 14
 )
16 15
 
17 16
 // postContainersCopy is deprecated in favor of getContainersArchive.
... ...
@@ -8,17 +8,15 @@ import (
8 8
 	"strings"
9 9
 	"time"
10 10
 
11
-	"golang.org/x/net/context"
12
-
13 11
 	"github.com/Sirupsen/logrus"
14 12
 	"github.com/docker/docker/api"
15 13
 	"github.com/docker/docker/api/types"
16 14
 	"github.com/docker/docker/autogen/dockerversion"
15
+	"github.com/docker/docker/context"
17 16
 	"github.com/docker/docker/pkg/ioutils"
18 17
 	"github.com/docker/docker/pkg/jsonmessage"
19 18
 	"github.com/docker/docker/pkg/parsers/filters"
20 19
 	"github.com/docker/docker/pkg/parsers/kernel"
21
-	"github.com/docker/docker/pkg/version"
22 20
 	"github.com/docker/docker/utils"
23 21
 )
24 22
 
... ...
@@ -33,7 +31,7 @@ func (s *Server) getVersion(ctx context.Context, w http.ResponseWriter, r *http.
33 33
 		BuildTime:  dockerversion.BUILDTIME,
34 34
 	}
35 35
 
36
-	version, _ := ctx.Value("api-version").(version.Version)
36
+	version := ctx.Version()
37 37
 
38 38
 	if version.GreaterThanOrEqualTo("1.19") {
39 39
 		v.Experimental = utils.ExperimentalBuild()
... ...
@@ -7,10 +7,9 @@ import (
7 7
 	"net/http"
8 8
 	"strconv"
9 9
 
10
-	"golang.org/x/net/context"
11
-
12 10
 	"github.com/Sirupsen/logrus"
13 11
 	"github.com/docker/docker/api/types"
12
+	"github.com/docker/docker/context"
14 13
 	"github.com/docker/docker/pkg/stdcopy"
15 14
 	"github.com/docker/docker/runconfig"
16 15
 )
... ...
@@ -8,18 +8,16 @@ import (
8 8
 	"net/http"
9 9
 	"strings"
10 10
 
11
-	"golang.org/x/net/context"
12
-
13 11
 	"github.com/Sirupsen/logrus"
14 12
 	"github.com/docker/docker/api/types"
15 13
 	"github.com/docker/docker/builder"
16 14
 	"github.com/docker/docker/cliconfig"
15
+	"github.com/docker/docker/context"
17 16
 	"github.com/docker/docker/graph"
18 17
 	"github.com/docker/docker/pkg/ioutils"
19 18
 	"github.com/docker/docker/pkg/parsers"
20 19
 	"github.com/docker/docker/pkg/streamformatter"
21 20
 	"github.com/docker/docker/pkg/ulimit"
22
-	"github.com/docker/docker/pkg/version"
23 21
 	"github.com/docker/docker/runconfig"
24 22
 	"github.com/docker/docker/utils"
25 23
 )
... ...
@@ -36,7 +34,7 @@ func (s *Server) postCommit(ctx context.Context, w http.ResponseWriter, r *http.
36 36
 	cname := r.Form.Get("container")
37 37
 
38 38
 	pause := boolValue(r, "pause")
39
-	version, _ := ctx.Value("api-version").(version.Version)
39
+	version := ctx.Version()
40 40
 	if r.FormValue("pause") == "" && version.GreaterThanOrEqualTo("1.13") {
41 41
 		pause = true
42 42
 	}
... ...
@@ -283,7 +281,7 @@ func (s *Server) postBuild(ctx context.Context, w http.ResponseWriter, r *http.R
283 283
 
284 284
 	w.Header().Set("Content-Type", "application/json")
285 285
 
286
-	version, _ := ctx.Value("api-version").(version.Version)
286
+	version := ctx.Version()
287 287
 	if boolValue(r, "forcerm") && version.GreaterThanOrEqualTo("1.12") {
288 288
 		buildConfig.Remove = true
289 289
 	} else if r.FormValue("rm") == "" && version.GreaterThanOrEqualTo("1.12") {
... ...
@@ -4,9 +4,7 @@ import (
4 4
 	"fmt"
5 5
 	"net/http"
6 6
 
7
-	"golang.org/x/net/context"
8
-
9
-	"github.com/docker/docker/pkg/version"
7
+	"github.com/docker/docker/context"
10 8
 )
11 9
 
12 10
 // getContainersByName inspects containers configuration and serializes it as json.
... ...
@@ -18,7 +16,7 @@ func (s *Server) getContainersByName(ctx context.Context, w http.ResponseWriter,
18 18
 	var json interface{}
19 19
 	var err error
20 20
 
21
-	version, _ := ctx.Value("api-version").(version.Version)
21
+	version := ctx.Version()
22 22
 
23 23
 	switch {
24 24
 	case version.LessThan("1.20"):
... ...
@@ -12,11 +12,11 @@ import (
12 12
 	"strings"
13 13
 
14 14
 	"github.com/gorilla/mux"
15
-	"golang.org/x/net/context"
16 15
 
17 16
 	"github.com/Sirupsen/logrus"
18 17
 	"github.com/docker/docker/api"
19 18
 	"github.com/docker/docker/autogen/dockerversion"
19
+	"github.com/docker/docker/context"
20 20
 	"github.com/docker/docker/daemon"
21 21
 	"github.com/docker/docker/pkg/sockets"
22 22
 	"github.com/docker/docker/pkg/stringid"
... ...
@@ -261,14 +261,14 @@ func makeHTTPHandler(logging bool, localMethod string, localRoute string, handle
261 261
 		// as 'args' on the function call.
262 262
 
263 263
 		reqID := stringid.TruncateID(stringid.GenerateNonCryptoID())
264
-		api_version := version.Version(mux.Vars(r)["version"])
265
-		if api_version == "" {
266
-			api_version = api.Version
264
+		apiVersion := version.Version(mux.Vars(r)["version"])
265
+		if apiVersion == "" {
266
+			apiVersion = api.Version
267 267
 		}
268 268
 
269 269
 		ctx := context.Background()
270
-		ctx = context.WithValue(ctx, "docker-request-id", reqID)
271
-		ctx = context.WithValue(ctx, "api-version", api_version)
270
+		ctx = context.WithValue(ctx, context.RequestID, reqID)
271
+		ctx = context.WithValue(ctx, context.APIVersion, apiVersion)
272 272
 
273 273
 		// log the request
274 274
 		logrus.Debugf("Calling %s %s", localMethod, localRoute)
... ...
@@ -294,11 +294,11 @@ func makeHTTPHandler(logging bool, localMethod string, localRoute string, handle
294 294
 			writeCorsHeaders(w, r, corsHeaders)
295 295
 		}
296 296
 
297
-		if api_version.GreaterThan(api.Version) {
298
-			http.Error(w, fmt.Errorf("client is newer than server (client API version: %s, server API version: %s)", api_version, api.Version).Error(), http.StatusBadRequest)
297
+		if apiVersion.GreaterThan(api.Version) {
298
+			http.Error(w, fmt.Errorf("client is newer than server (client API version: %s, server API version: %s)", apiVersion, api.Version).Error(), http.StatusBadRequest)
299 299
 			return
300 300
 		}
301
-		if api_version.LessThan(api.MinVersion) {
301
+		if apiVersion.LessThan(api.MinVersion) {
302 302
 			http.Error(w, fmt.Errorf("client is too old, minimum supported API version is %s, please upgrade your client to a newer version", api.MinVersion).Error(), http.StatusBadRequest)
303 303
 			return
304 304
 		}
... ...
@@ -4,9 +4,8 @@ import (
4 4
 	"encoding/json"
5 5
 	"net/http"
6 6
 
7
-	"golang.org/x/net/context"
8
-
9 7
 	"github.com/docker/docker/api/types"
8
+	"github.com/docker/docker/context"
10 9
 )
11 10
 
12 11
 func (s *Server) getVolumesList(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
13 12
new file mode 100644
... ...
@@ -0,0 +1,64 @@
0
+package context
1
+
2
+import (
3
+	"golang.org/x/net/context"
4
+
5
+	"github.com/docker/docker/pkg/version"
6
+)
7
+
8
+const (
9
+	// RequestID is the unique ID for each http request
10
+	RequestID = "request-id"
11
+
12
+	// APIVersion is the client's requested API version
13
+	APIVersion = "api-version"
14
+)
15
+
16
+// Context is just our own wrapper for the golang 'Context' - mainly
17
+// so we can add our over version of the funcs.
18
+type Context struct {
19
+	context.Context
20
+}
21
+
22
+// Background creates a new Context based on golang's default one.
23
+func Background() Context {
24
+	return Context{context.Background()}
25
+}
26
+
27
+// WithValue will return a Context that has this new key/value pair
28
+// associated with it. Just uses the golang version but then wraps it.
29
+func WithValue(ctx Context, key, value interface{}) Context {
30
+	return Context{context.WithValue(ctx, key, value)}
31
+}
32
+
33
+// RequestID is a utility func to make it easier to get the
34
+// request ID associated with this Context/request.
35
+func (ctx Context) RequestID() string {
36
+	val := ctx.Value(RequestID)
37
+	if val == nil {
38
+		return ""
39
+	}
40
+
41
+	id, ok := val.(string)
42
+	if !ok {
43
+		// Ideally we shouldn't panic but we also should never get here
44
+		panic("Context RequestID isn't a string")
45
+	}
46
+	return id
47
+}
48
+
49
+// Version is a utility func to make it easier to get the
50
+// API version string associated with this Context/request.
51
+func (ctx Context) Version() version.Version {
52
+	val := ctx.Value(APIVersion)
53
+	if val == nil {
54
+		return version.Version("")
55
+	}
56
+
57
+	ver, ok := val.(version.Version)
58
+	if !ok {
59
+		// Ideally we shouldn't panic but we also should never get here
60
+		panic("Context APIVersion isn't a version.Version")
61
+	}
62
+	return ver
63
+}
0 64
new file mode 100644
... ...
@@ -0,0 +1,35 @@
0
+package context
1
+
2
+import (
3
+	"testing"
4
+
5
+	"github.com/docker/docker/pkg/version"
6
+)
7
+
8
+func TestContext(t *testing.T) {
9
+	ctx := Background()
10
+
11
+	// First make sure getting non-existent values doesn't break
12
+	if id := ctx.RequestID(); id != "" {
13
+		t.Fatalf("RequestID() should have been '', was: %q", id)
14
+	}
15
+
16
+	if ver := ctx.Version(); ver != "" {
17
+		t.Fatalf("Version() should have been '', was: %q", ver)
18
+	}
19
+
20
+	// Test basic set/get
21
+	ctx = WithValue(ctx, RequestID, "123")
22
+	if ctx.RequestID() != "123" {
23
+		t.Fatalf("RequestID() should have been '123'")
24
+	}
25
+
26
+	// Now make sure after a 2nd set we can still get both
27
+	ctx = WithValue(ctx, APIVersion, version.Version("x.y"))
28
+	if id := ctx.RequestID(); id != "123" {
29
+		t.Fatalf("RequestID() should have been '123', was %q", id)
30
+	}
31
+	if ver := ctx.Version(); ver != "x.y" {
32
+		t.Fatalf("Version() should have been 'x.y', was %q", ver)
33
+	}
34
+}