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 (
"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)
}

View File

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

View File

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

View File

@ -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
}
type proxyContainerApi struct {
client *client.Client
func Client(ctx context.Context) *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) {
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
}

View File

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

View File

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