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
This commit is contained in:
Djordje Lukic 2020-04-29 23:39:54 +02:00
parent 9ea91791b4
commit 8571cf5a04
6 changed files with 49 additions and 27 deletions

View File

@ -3,6 +3,7 @@ package backend
import ( import (
"context" "context"
"errors" "errors"
"fmt"
) )
var ( 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)
} }

View File

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

View File

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

View File

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

View File

@ -112,7 +112,11 @@ func New(opts ...StoreOpt) (Store, error) {
// Get returns the context with the given name // Get returns the context with the given name
func (s *store) Get(name string, getter func() interface{}) (*Metadata, error) { func (s *store) Get(name string, getter func() interface{}) (*Metadata, error) {
meta := filepath.Join(s.root, contextsDir, metadataDir, contextdirOf(name), metaFile) 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) { func read(meta string, getter func() interface{}) (*Metadata, error) {

View File

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