浏览代码

Create a new client on each request

`docker serve` doesn't need a context any more, the server takes the
current context from the request metadata and creates a new client
Djordje Lukic 5 年之前
父节点
当前提交
8571cf5a04
共有 6 个文件被更改,包括 49 次插入27 次删除
  1. 2 1
      backend/backend.go
  2. 1 7
      cli/cmd/serve.go
  3. 6 6
      cli/main.go
  4. 15 12
      containers/proxy/proxy.go
  5. 5 1
      context/store/store.go
  6. 20 0
      server/server.go

+ 2 - 1
backend/backend.go

@@ -3,6 +3,7 @@ package backend
 import (
 	"context"
 	"errors"
+	"fmt"
 )
 
 var (
@@ -50,5 +51,5 @@ func Get(ctx context.Context, backendType string) (interface{}, error) {
 		}
 	}
 
-	return nil, errors.New("not found")
+	return nil, fmt.Errorf("backend not found for context %q", backendType)
 }

+ 1 - 7
cli/cmd/serve.go

@@ -5,7 +5,6 @@ import (
 	"net"
 
 	cliv1 "github.com/docker/api/cli/v1"
-	"github.com/docker/api/client"
 	"github.com/docker/api/containers/proxy"
 	containersv1 "github.com/docker/api/containers/v1"
 	"github.com/docker/api/context/store"
@@ -45,12 +44,7 @@ func runServe(ctx context.Context, opts serveOpts) error {
 	}
 	defer l.Close()
 
-	c, err := client.New(ctx)
-	if err != nil {
-		return err
-	}
-
-	p := proxy.NewContainerApi(c)
+	p := proxy.NewContainerApi()
 
 	containersv1.RegisterContainersServer(s, p)
 	cliv1.RegisterCliServer(s, &cliServer{

+ 6 - 6
cli/main.go

@@ -65,14 +65,14 @@ func init() {
 	}
 }
 
-func isContextCommand(cmd *cobra.Command) bool {
+func isOwnCommand(cmd *cobra.Command) bool {
 	if cmd == nil {
 		return false
 	}
-	if cmd.Name() == "context" {
+	if cmd.Name() == "context" || cmd.Name() == "serve" {
 		return true
 	}
-	return isContextCommand(cmd.Parent())
+	return isOwnCommand(cmd.Parent())
 }
 
 func main() {
@@ -82,7 +82,7 @@ func main() {
 		Long:          "docker for the 2020s",
 		SilenceErrors: true,
 		PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
-			if !isContextCommand(cmd) {
+			if !isOwnCommand(cmd) {
 				execMoby(cmd.Context())
 			}
 			return nil
@@ -94,7 +94,7 @@ func main() {
 
 	helpFunc := root.HelpFunc()
 	root.SetHelpFunc(func(cmd *cobra.Command, args []string) {
-		if !isContextCommand(cmd) {
+		if !isOwnCommand(cmd) {
 			execMoby(cmd.Context())
 		}
 		helpFunc(cmd, args)
@@ -131,7 +131,7 @@ func main() {
 		currentContext = "default"
 	}
 
-	ctx = apicontext.WithCurrentContext(ctx, opts.Context)
+	ctx = apicontext.WithCurrentContext(ctx, currentContext)
 	if err != nil {
 		logrus.Fatal(err)
 	}

+ 15 - 12
containers/proxy/proxy.go

@@ -5,27 +5,30 @@ import (
 
 	"github.com/docker/api/client"
 	v1 "github.com/docker/api/containers/v1"
-	apicontext "github.com/docker/api/context"
 	"github.com/golang/protobuf/ptypes/empty"
 )
 
-func NewContainerApi(client *client.Client) v1.ContainersServer {
-	return &proxyContainerApi{
-		client: client,
-	}
+type clientKey struct{}
+
+func WithClient(ctx context.Context, c *client.Client) (context.Context, error) {
+	return context.WithValue(ctx, clientKey{}, c), nil
+}
+
+func Client(ctx context.Context) *client.Client {
+	c, _ := ctx.Value(clientKey{}).(*client.Client)
+	return c
 }
 
-type proxyContainerApi struct {
-	client *client.Client
+func NewContainerApi() v1.ContainersServer {
+	return &proxyContainerApi{}
 }
 
+type proxyContainerApi struct{}
+
 func (p *proxyContainerApi) List(ctx context.Context, _ *v1.ListRequest) (*v1.ListResponse, error) {
-	currentContext := apicontext.CurrentContext(ctx)
-	if err := p.client.SetContext(ctx, currentContext); err != nil {
-		return &v1.ListResponse{}, nil
-	}
+	client := Client(ctx)
 
-	c, err := p.client.ContainerService().List(ctx)
+	c, err := client.ContainerService().List(ctx)
 	if err != nil {
 		return &v1.ListResponse{}, nil
 	}

+ 5 - 1
context/store/store.go

@@ -112,7 +112,11 @@ func New(opts ...StoreOpt) (Store, error) {
 // Get returns the context with the given name
 func (s *store) Get(name string, getter func() interface{}) (*Metadata, error) {
 	meta := filepath.Join(s.root, contextsDir, metadataDir, contextdirOf(name), metaFile)
-	return read(meta, getter)
+	m, err := read(meta, getter)
+	if os.IsNotExist(err) {
+		return nil, fmt.Errorf("unknown conetxt %q", name)
+	}
+	return m, nil
 }
 
 func read(meta string, getter func() interface{}) (*Metadata, error) {

+ 20 - 0
server/server.go

@@ -31,7 +31,10 @@ import (
 	"context"
 	"errors"
 
+	"github.com/docker/api/client"
+	"github.com/docker/api/containers/proxy"
 	apicontext "github.com/docker/api/context"
+	"github.com/docker/api/context/store"
 	grpc_prometheus "github.com/grpc-ecosystem/go-grpc-prometheus"
 	"google.golang.org/grpc"
 	"google.golang.org/grpc/health"
@@ -66,9 +69,26 @@ func unaryMeta(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo,
 	if !ok {
 		return nil, errors.New("missing metadata")
 	}
+
 	key := md[apicontext.KEY]
+
 	if len(key) == 1 {
+		s, err := store.New()
+		if err != nil {
+			return nil, err
+		}
+		ctx = store.WithContextStore(ctx, s)
+
 		ctx = apicontext.WithCurrentContext(ctx, key[0])
+
+		c, err := client.New(ctx)
+		if err != nil {
+			return nil, err
+		}
+		ctx, err = proxy.WithClient(ctx, c)
+		if err != nil {
+			return nil, err
+		}
 	}
 	m, err := handler(ctx, req)
 	return m, err